-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·102 lines (85 loc) · 3.08 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
#!/usr/bin/env node
const { build } = require('esbuild');
const fs = require('fs/promises');
const path = require('path');
const zipDir = require('zip-dir');
(async () => {
await build({
entryPoints: ['src/content/index.ts', 'src/background/timechimp.ts'],
outdir: 'build',
// We don't minify, to (hopefully) improve the Chrome store review times.
minify: false,
bundle: true,
sourcemap: true,
});
// Create the browser builds.
await buildChrome('build_chrome');
await buildFirefox('build_firefox');
// Create zip artifacts from the browser builds.
await fs.mkdir('artifacts', { recursive: true });
zipDir('build_chrome', { saveTo: path.resolve('artifacts', 'chrome.zip') });
zipDir('build_firefox', {
saveTo: path.resolve('artifacts', 'firefox.zip'),
});
// Create a zip archive with the sources.
const projectPath = path.resolve('.');
zipDir('.', {
saveTo: path.resolve('artifacts', 'sources.zip'),
filter: (p, stat) => {
const relPath = path.relative(projectPath, p);
// Exclude build dirs.
if (stat.isDirectory() && relPath.startsWith('build')) {
return false;
}
return !['.git', '.idea', 'artifacts', 'node_modules'].includes(
relPath,
);
},
});
})().catch((e) => {
console.error(e);
process.exit(1);
});
async function buildChrome(dir) {
await fs.mkdir(dir, { recursive: true });
await copyDir('build', path.resolve(dir, 'build'));
await fs.copyFile('icon.png', path.join(dir, 'icon.png'));
const manifestJson = JSON.parse(
(await fs.readFile('manifest.json')).toString(),
);
// Chrome doesn't use `browser_specific_settings`, so remove it.
manifestJson.browser_specific_settings = undefined;
await fs.writeFile(
path.join(dir, 'manifest.json'),
JSON.stringify(manifestJson, null, 2),
);
}
async function buildFirefox(dir) {
await fs.mkdir(dir, { recursive: true });
await copyDir('build', path.resolve(dir, 'build'));
await fs.copyFile('icon.png', path.join(dir, 'icon.png'));
const manifestJson = JSON.parse(
(await fs.readFile('manifest.json')).toString(),
);
// Firefox doesn't use `background.service_worker`,
// but `background.script` to define the background worker.
manifestJson.background.scripts = [manifestJson.background.service_worker];
manifestJson.background.service_worker = undefined;
await fs.writeFile(
path.join(dir, 'manifest.json'),
JSON.stringify(manifestJson, null, 2),
);
}
async function copyDir(src, dest) {
await fs.mkdir(dest, { recursive: true });
const entries = await fs.readdir(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
await copyDir(srcPath, destPath);
} else {
await fs.copyFile(srcPath, destPath);
}
}
}