forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.bex-packager.js
More file actions
70 lines (59 loc) · 2.06 KB
/
plugin.bex-packager.js
File metadata and controls
70 lines (59 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const path = require('path')
const fse = require('fs')
const archiver = require('archiver')
const mkdirp = require('mkdirp')
function findAndReplaceInSection (sectionArray, find, replace) {
const index = sectionArray.indexOf(find)
sectionArray[index] = replace
}
class BexPackager {
constructor (options) {
this.options = options
this.chromeDir = path.join(options.dest, 'chrome')
this.firefoxDir = path.join(options.dest, 'firefox')
}
apply (compiler) {
compiler.hooks.done.tap('done-compiling', stats => {
if (stats.hasErrors() === false) {
this.setupDirectories()
this.fixManifest()
this.bundleChrome()
this.bundleFirefox()
}
})
}
/**
* This will fix some of the paths in the manifest file which are different in the build version vs dev version.
*/
fixManifest () {
const manifestFilePath = path.join(this.options.src, 'manifest.json')
if (fse.existsSync(manifestFilePath) === true) {
const manifestFileData = fse.readFileSync(manifestFilePath)
let manifestData = JSON.parse(manifestFileData.toString())
findAndReplaceInSection(manifestData.background.scripts, 'www/bex-background.js', 'www/js/bex-background.js')
findAndReplaceInSection(manifestData.content_scripts[0].js, 'www/bex-content-script.js', 'www/js/bex-content-script.js')
const newValue = JSON.stringify(manifestData)
fse.writeFileSync(manifestFilePath, newValue, 'utf-8')
}
}
setupDirectories () {
mkdirp.sync(this.chromeDir)
mkdirp.sync(this.firefoxDir)
}
bundleChrome () {
this.outputToZip(this.options.src, this.chromeDir, this.options.name)
}
bundleFirefox () {
this.outputToZip(this.options.src, this.firefoxDir, this.options.name)
}
outputToZip (src, dest, fileName) {
let output = fse.createWriteStream(path.join(dest, fileName + '.zip'))
let archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
})
archive.pipe(output)
archive.directory(src, false)
archive.finalize()
}
}
module.exports = BexPackager