-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
320 lines (309 loc) · 10.1 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const fs = require('fs');
const JSZip = require('jszip');
const uglify = require('uglify-es');
const cleancss = require('clean-css');
const FirefoxExt = require('sign-addon').default;
const deepCopy = require('./deepCopy.js');
const createCrx = require('./createCrx.js');
const config = require('./config.json');
const uglifyOptions = require('./uglify_config.json');
const CleanCSSOptions = require('./clean_css_config.json');
const rootDir = __dirname.replace(/\\/g, '/') + '/';
const tempDir = rootDir + 'temp/';
const updateXml = "<?xml version='1.0' encoding='UTF-8'?><gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'><app appid='{EXT_ID}'><updatecheck codebase='{EXT_URL}' version='{EXT_VERSION}' /></app></gupdate>";
const buildExt = process.argv[2];
if (!buildExt || typeof(config[buildExt]) === 'undefined') {
console.log('Error: ' + buildExt + ' not found!');
process.exit(0);
}
const extConfig = config[buildExt];
const extIgnores = extConfig.basic.ignores;
const extDir = extConfig.basic.dir;
const outputDir = extConfig.basic.output.replace('{EXT_DIR}', extDir) + '/';
const BaseOutput = tempDir + buildExt + '.zip';
const FirefoxOutput = outputDir + 'firefox/';
const ChromeOutput = outputDir + 'chrome/';
const FirefoxManifest = typeof(extConfig.ext.gecko.manifest) === 'undefined' ? {} : require(extConfig.ext.gecko.manifest.replace('{EXT_DIR}', extDir));
const ChromeManifest = typeof(extConfig.ext.crx.manifest) === 'undefined' ? {} : require(extConfig.ext.crx.manifest.replace('{EXT_DIR}', extDir));
// Custom preprocess
let extCustom = null;
if (extConfig.basic.custom) {
extCustom = require(extConfig.basic.custom.replace('{EXT_DIR}', extDir));
}
// Check dirs
if (extConfig.basic.version.firefox || extConfig.basic.version.amo) {
if (!fs.existsSync(FirefoxOutput)) {
fs.mkdirSync(FirefoxOutput);
}
}
if (extConfig.basic.version.chrome || extConfig.basic.version.webstore) {
if (!fs.existsSync(ChromeOutput)) {
fs.mkdirSync(ChromeOutput);
}
}
function getFileExt(name) {
return name.includes('.') ? name.substr(name.lastIndexOf('.') + 1) : '';
}
function readDir(dir) {
return new Promise((resolve) => {
let readCount = 0;
let fileList = [];
fs.readdir(dir, (err, files) => {
if (err) {
console.log(err);
return;
}
files.forEach((filename) => {
if (extIgnores.includes(filename)) {
return;
}
if (fs.statSync(dir + '/' + filename).isFile()) {
let prefix = dir.substr(extDir.length + 1) + '/';
fileList.push({
"name": filename,
"path": (prefix === '/' ? '' : prefix) + filename,
"fullpath": dir + '/' + filename
});
} else {
readCount++;
readDir(dir + '/' + filename).then((subFileList) => {
readCount--;
fileList = fileList.concat(subFileList);
checkReadFinish();
});
}
});
checkReadFinish();
});
function checkReadFinish() {
if (isReadFinish()) {
resolve(fileList);
}
}
function isReadFinish() {
return readCount === 0;
}
});
}
function createZip(output, fileList) {
return new Promise((resolve) => {
let archive = new JSZip();
fileList.forEach((f) => {
let contentBuffer = null;
if (extCustom) {
contentBuffer = extCustom(f);
}
if (!f.fullpath.includes('.min.js') && getFileExt(f.fullpath) === 'js') {
const rs = uglify.minify(
contentBuffer ? contentBuffer : fs.readFileSync(f.fullpath, 'utf-8'),
uglifyOptions
);
if (rs.error) {
console.error(rs.error.message);
process.exit();
}
archive.file(
f.path,
Buffer.from(rs.code)
);
} else if (!f.fullpath.includes('.min.css') && getFileExt(f.fullpath) === 'css') {
archive.file(
f.path,
Buffer.from(
new cleancss(CleanCSSOptions).minify(contentBuffer ? contentBuffer : fs.readFileSync(f.fullpath, 'utf-8')).styles
)
);
} else {
archive.file(f.path, contentBuffer ? contentBuffer : fs.readFileSync(f.fullpath));
}
console.log('Added ' + f.path);
});
archive
.generateAsync({
type: "nodebuffer",
compression: "DEFLATE",
compressionOptions: {
level: 9
}
})
.then(r => {
fs.writeFileSync(output, r)
})
.then(resolve);
});
}
readDir(extDir).then((fileList) => {
console.log('Scanned all files');
createZip(BaseOutput, fileList).then(() => {
console.log('Created base zip file');
// Build chrome extension
if (extConfig.basic.version.chrome) {
let zip_out = ChromeOutput + extConfig.ext.filename.replace(/\{VERSION\}/g, extConfig.ext.version) + '.zip';
let crx_out = ChromeOutput + extConfig.ext.filename.replace(/\{VERSION\}/g, extConfig.ext.version) + '.crx';
let zip = new JSZip();
let manifest = deepCopy(ChromeManifest);
manifest.version = extConfig.ext.version;
manifest.update_url = extConfig.ext.crx.update;
zip.loadAsync(fs.readFileSync(BaseOutput)).then(() => {
zip.file('manifest.json', Buffer.from(JSON.stringify(manifest)));
})
.then(() => {
return zip.generateAsync({
type: "nodebuffer",
compression: "DEFLATE",
compressionOptions: {
level: 9
}
})
})
.then((r) => {
fs.writeFileSync(zip_out, r)
})
.then(() => {
return createCrx(fs.readFileSync(zip_out), fs.readFileSync(extConfig.ext.crx.key));
})
.then((crxBuffer) => {
fs.writeFileSync(crx_out, crxBuffer);
// If require update.json, generate it
if (extConfig.ext.crx.update_local) {
const update_file_path = extConfig.ext.crx.update_local.replace('{EXT_DIR}', extDir);
let update_file_xml = updateXml;
update_file_xml = update_file_xml.replace(/\{EXT_ID\}/g, extConfig.ext.crx.id);
update_file_xml = update_file_xml.replace(/\{EXT_VERSION\}/g, extConfig.ext.version);
update_file_xml = update_file_xml.replace(/\{EXT_URL\}/g, extConfig.ext.crx.download_url.replace(/\{VERSION\}/g, extConfig.ext.version));
fs.writeFileSync(update_file_path, Buffer.from(update_file_xml));
console.log('Updated update.xml');
}
console.log('Build chrome crx version finished');
});
}
// Build chrome webstore format
if (extConfig.basic.version.webstore) {
let zip_out = ChromeOutput + extConfig.ext.filename.replace(/\{VERSION\}/g, extConfig.ext.version) + '-webstore.zip';
let zip = new JSZip();
let manifest = deepCopy(ChromeManifest);
manifest.version = extConfig.ext.version;
zip.loadAsync(fs.readFileSync(BaseOutput)).then(() => {
zip.file('manifest.json', Buffer.from(JSON.stringify(manifest)));
})
.then(() => {
return zip.generateAsync({
type: "nodebuffer",
compression: "DEFLATE",
compressionOptions: {
level: 9
}
})
})
.then((r) => {
fs.writeFileSync(zip_out, r);
console.log('Build chrome webstore version finished');
});
}
// Build default firefox extension
if (extConfig.basic.version.firefox) {
let zip_out = FirefoxOutput + extConfig.ext.filename.replace(/\{VERSION\}/g, extConfig.ext.version) + '.zip';
let xpi_out = FirefoxOutput + extConfig.ext.filename.replace(/\{VERSION\}/g, extConfig.ext.version) + '.xpi';
let app_id = extConfig.ext.gecko.default;
let zip = new JSZip();
let manifest = deepCopy(FirefoxManifest);
manifest.version = extConfig.ext.version;
manifest.applications.gecko.id = app_id;
manifest.applications.gecko.update_url = extConfig.ext.gecko.update;
zip.loadAsync(fs.readFileSync(BaseOutput)).then(() => {
zip.file('manifest.json', Buffer.from(JSON.stringify(manifest)));
})
.then(() => {
return zip.generateAsync({
type: "nodebuffer",
compression: "DEFLATE",
compressionOptions: {
level: 9
}
})
})
.then((r) => {
fs.writeFileSync(zip_out, r);
})
.then(() => {
// sign
FirefoxExt({
xpiPath: zip_out,
version: extConfig.ext.version,
apiKey: extConfig.amo.user,
apiSecret: extConfig.amo.secret,
id: app_id,
downloadDir: FirefoxOutput
})
.then(function(result) {
if (result.success) {
console.log("Downloaded signed addon");
// Move download file to output dir
if (result.downloadedFiles[0] !== xpi_out) {
fs.renameSync(result.downloadedFiles[0], xpi_out);
}
// If require update.json, generate it
if (extConfig.ext.gecko.update_local) {
const update_file_path = extConfig.ext.gecko.update_local.replace('{EXT_DIR}', extDir);
let update_file_json = require(update_file_path);
while (update_file_json.addons[app_id].updates.length > 2) {
update_file_json.addons[app_id].updates.splice(0, 1);
}
update_file_json.addons[app_id].updates.push({
"version": extConfig.ext.version,
"update_link": extConfig.ext.gecko.download_url.replace(/\{VERSION\}/g, extConfig.ext.version),
"update_hash": result.raw_data.files[0].hash
});
fs.writeFileSync(update_file_path, Buffer.from(JSON.stringify(update_file_json)));
console.log('Updated update.json');
}
}
console.log('SIGN ' + (result.success ? "SUCCESS" : "FAIL"));
})
.catch(function(error) {
console.error("Signing error:", error);
});
});
}
// Build amo firefox extension
if (extConfig.basic.version.amo) {
let zip_out = FirefoxOutput + extConfig.ext.filename.replace(/\{VERSION\}/g, extConfig.ext.version) + '-amo.zip';
let app_id = extConfig.ext.gecko.amo;
let zip = new JSZip();
let manifest = deepCopy(FirefoxManifest);
manifest.version = extConfig.ext.version;
manifest.applications.gecko.id = app_id;
zip.loadAsync(fs.readFileSync(BaseOutput)).then(() => {
zip.file('manifest.json', Buffer.from(JSON.stringify(manifest)));
})
.then(() => {
return zip.generateAsync({
type: "nodebuffer",
compression: "DEFLATE",
compressionOptions: {
level: 9
}
})
})
.then((r) => {
fs.writeFileSync(zip_out, r);
})
.then(() => {
// sign
FirefoxExt({
xpiPath: zip_out,
version: extConfig.ext.version,
apiKey: extConfig.amo.user,
apiSecret: extConfig.amo.secret,
id: app_id
})
.then(function(result) {
console.log('SIGN ' + (result.success ? "SUCCESS" : "FAIL"));
})
.catch(function(error) {
console.error("Signing error:", error);
});
});
}
});
});