-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
79 lines (64 loc) · 2.5 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
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
const browsers = ['chrome', 'firefox'];
const srcDir = path.join(__dirname, 'src');
const manifestDir = path.join(__dirname, 'manifest');
const buildDir = path.join(__dirname, 'build');
function archiveAddon(browser) {
if (browser === 'firefox') {
const browserBuildDir = path.join(buildDir, browser);
const archiveFilename = `${browser}.zip`;
const zipArchivePath = path.join(buildDir, archiveFilename);
const outputZip = fs.createWriteStream(zipArchivePath);
const archiveZip = archiver('zip', { zlib: { level: 9 } });
outputZip.on('close', () => {
console.log(`${archiveFilename} has been created.`);
});
archiveZip.on('error', (err) => {
throw err;
});
archiveZip.pipe(outputZip);
archiveZip.directory(browserBuildDir, false);
archiveZip.finalize();
} else if (browser === 'chrome') {
const browserBuildDir = path.join(buildDir, browser);
const archiveFilename = `${browser}.zip`;
const zipArchivePath = path.join(buildDir, archiveFilename);
const outputZip = fs.createWriteStream(zipArchivePath);
const archiveZip = archiver('zip', { zlib: { level: 9 } });
outputZip.on('close', () => {
console.log(`${archiveFilename} has been created.`);
});
archiveZip.on('error', (err) => {
throw err;
});
archiveZip.pipe(outputZip);
archiveZip.directory(browserBuildDir, false);
archiveZip.finalize();
}
}
function buildExtension(browser) {
const manifestFile = `manifest-${browser}.json`;
const manifestSrcPath = path.join(manifestDir, manifestFile);
const manifestDestPath = path.join(buildDir, browser, 'manifest.json');
if (!fs.existsSync(manifestSrcPath)) {
console.error(`Manifest file for ${browser} not found: ${manifestSrcPath}`);
return;
}
const browserBuildDir = path.join(buildDir, browser);
if (!fs.existsSync(browserBuildDir)) {
fs.mkdirSync(browserBuildDir, {recursive: true});
}
fs.cpSync(srcDir, browserBuildDir, {recursive: true});
fs.copyFileSync(manifestSrcPath, manifestDestPath);
console.log(`Built extension for ${browser} in ${browserBuildDir}`);
archiveAddon(browser);
}
function main() {
if (!fs.existsSync(buildDir)) {
fs.mkdirSync(buildDir);
}
browsers.forEach(buildExtension);
}
main();