-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
152 lines (126 loc) · 3.85 KB
/
build.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// library functions
const rimraf = require(`rimraf`)
const mkdirp = require(`mkdirp`)
const copyfiles = require(`copyfiles`)
const merge = require(`merge`)
const fs = require(`fs`)
const browserify = require(`browserify`)
// paths and more
const p_src = `./src`
const p_resrc = `${p_src}/resources`
const p_extensions = `./extensions`
const browsers = {
chrome : {
name: "chrome",
targetPath: `${p_extensions}/chrome`,
targetResourcesPath: `${p_extensions}/chrome/${process.env.npm_package_config_resources}`
},
firefox : {
name: "firefox",
targetPath: `${p_extensions}/firefox`,
targetResourcesPath: `${p_extensions}/firefox/${process.env.npm_package_config_resources}`
}
}
// execute specified build target(s)
// extract the command line arguments specifying what to build
// (first two arguments are the Node executable and the script file
// https://nodejs.org/docs/latest/api/process.html#process_process_argv )
if (process.argv.length == 2)
all()
else
process.argv
.slice(2)
.forEach(target => buildTarget(target))
function buildTarget(target) {
var targetAsFunction = {
clean : clean,
c : clean,
chrome : chrome,
ch : chrome,
firefox : firefox,
ff : firefox
}[target]
if (targetAsFunction)
targetAsFunction()
else
console.log(`Target "${target}" does not exist.`)
}
// target definitions
// - aggregates
function all() {
clean()
buildBrowser(browsers.chrome)
buildBrowser(browsers.firefox)
}
function chrome() {
clean()
buildBrowser(browsers.chrome)
}
function firefox() {
clean()
buildBrowser(browsers.firefox)
}
// - atoms
function clean() {
console.log(`deleting "${p_extensions}"`)
rimraf.sync(p_extensions, { }, abortBuildIfError)
console.log()
}
function buildBrowser(browser) {
console.log(`building ${browser.name} extension`)
createFolders(browser);
createManifest(browser);
bundleSources(browser);
copyResources(browser)
console.log()
}
// helpers
function createFolders(browser) {
console.log(`creating target folder "${browser.targetPath}"`)
mkdirp.sync(browser.targetPath, abortBuildIfError)
mkdirp.sync(browser.targetResourcesPath, abortBuildIfError)
}
function createManifest(browser) {
console.log(`creating manifest`)
fs.writeFile(`${browser.targetPath}/manifest.json`, createManifestStringFor(browser), abortBuildIfError)
}
function createManifestStringFor(browser) {
return replaceConfigVariables(createBrowserSpecificManifest(browser));
}
function createBrowserSpecificManifest(browser) {
var source = require(`${p_src}/manifest.json`)
// extract the shared part of the manifest and merge it with the browser-specific part
return merge(true, source["shared"], source[browser.name]);
}
function replaceConfigVariables(merged) {
return JSON.stringify(merged, null, `\t`).replace(
// variables in the manifest have the form "$config_foo",
// where package.json defines the config parameter "foo"
new RegExp(/\$config_(\w+)/, "g"),
// replace the match with the package.json entry
(match, parameterName) => process.env[`npm_package_config_${parameterName}`]
)
}
function bundleSources(browser) {
console.log(`bundling sources and dependencies into "${process.env.npm_package_main}"`)
bundleSourceFromTo(`${p_src}/${process.env.npm_package_main}`, `${browser.targetPath}/${process.env.npm_package_main}`)
}
function bundleSourceFromTo(entries, target) {
browserify(entries).bundle().pipe(fs.createWriteStream(target))
}
function copyResources(browser) {
console.log(`copying resources into "${browser.targetResourcesPath}"`)
copyFlat(`${p_resrc}/icon.png`, browser.targetResourcesPath)
}
// low level
function copyFlat() {
// copyfiles requires an array so create one from the arguments
copyfiles(Array.prototype.slice.call(arguments), true, abortBuildIfError)
}
function abortBuildIfError(error) {
if (error) {
console.log(`Error: ${error}`)
console.log(`Aborting build...`)
process.exit(1)
}
}