-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mjs
78 lines (64 loc) · 2.19 KB
/
build.mjs
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
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { zip } from "zip-a-folder";
import manifest from "./assets/extension/manifest.mjs";
import manifestFirefox from "./assets/extension/manifest-firefox-extended.mjs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const distPath = path.join(__dirname, "dist");
const extensionPath = path.join(__dirname, "assets", "extension");
const extensionBuildPath = path.join(__dirname, "assets", "extension", "build");
const copyIcons = () => {
const iconsPath = path.join(extensionPath, "icons");
const distIconsPath = path.join(distPath, "icons");
const icons = fs.readdirSync(iconsPath);
fs.mkdirSync(distIconsPath, { recursive: true });
for (const icon of icons) {
const src = path.join(iconsPath, icon);
const dest = path.join(distIconsPath, icon);
fs.copyFileSync(src, dest);
}
};
const createManifest = (browser) => {
let finalManifest = { ...manifest };
if (browser === "firefox") {
finalManifest = { ...manifest, ...manifestFirefox };
}
fs.writeFileSync(
path.join(distPath, "manifest.json"),
JSON.stringify(finalManifest, null, 2)
);
};
const createZip = async (browser) => {
const zipName = browser === "chrome" ? "chrome.zip" : "firefox.zip";
const zipPath = path.join(extensionBuildPath, zipName);
fs.mkdirSync(extensionBuildPath, { recursive: true });
await zip(distPath, zipPath);
};
(async () => {
const command = process.argv[2];
switch (command) {
case "build-chrome":
copyIcons();
createManifest("chrome");
console.log(`Manifest file created in folder ${distPath}`);
break;
case "build-firefox":
copyIcons();
createManifest("firefox");
console.log(`Manifest file created in folder ${distPath}`);
break;
case "build-and-zip-all":
copyIcons();
createManifest("chrome");
createManifest("firefox");
await createZip("chrome");
await createZip("firefox");
console.log(
`Production zip files created in folder ${extensionBuildPath}`
);
break;
default:
throw new Error(`Unknown command: ${command || "NA"}`);
}
})();