Repository navigation
Expand file tree
/
Copy pathbuild.ts
More file actions
63 lines (58 loc) · 2.19 KB
/
Copy pathbuild.ts
File metadata and controls
63 lines (58 loc) · 2.19 KB
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
/** biome-ignore-all lint/correctness/noNodejsModules: This code only runs in bun. */
import { createWriteStream, promises as fs } from "node:fs";
import path from "node:path";
import archiver from "archiver";
const srcDir = "src";
const distDir = "dist";
const firefoxDir = "dist/firefox";
const chromeDir = "dist/chrome";
const firefoxZip = "dist/firefox.zip";
const chromeZip = "dist/chrome.zip";
if (await fs.exists(distDir)) {
await fs.rm(distDir, { recursive: true, force: true });
}
await fs.mkdir(distDir);
copyDir(srcDir, firefoxDir).then(async () => {
const manifestFile = path.join(firefoxDir, "manifest.json");
const manifest = JSON.parse(await fs.readFile(manifestFile, "utf-8"));
manifest.background.service_worker = undefined;
manifest.key = undefined;
await fs.writeFile(manifestFile, JSON.stringify(manifest, null, 2));
await zipDir(firefoxDir, firefoxZip);
});
copyDir(srcDir, chromeDir).then(async () => {
const manifestFile = path.join(chromeDir, "manifest.json");
const manifest = JSON.parse(await fs.readFile(manifestFile, "utf-8"));
manifest.background.scripts = undefined;
await fs.writeFile(manifestFile, JSON.stringify(manifest, null, 2));
await zipDir(chromeDir, chromeZip);
});
async function copyDir(src: string, dest: string): Promise<void> {
const files = await fs.readdir(src, {
recursive: true,
withFileTypes: true,
});
const promises = files
.filter((file) => file.isFile())
.map(async (file) => {
const fileSrc = path.join(file.parentPath, file.name);
const fileRelative = path.relative(src, file.parentPath);
const fileDestDir = path.join(dest, fileRelative);
const fileDest = path.join(fileDestDir, file.name);
await fs.mkdir(fileDestDir, { recursive: true });
await fs.copyFile(fileSrc, fileDest);
});
await Promise.all(promises);
}
function zipDir(src: string, destZip: string): Promise<void> {
const archive = archiver("zip", { zlib: { level: 9 } });
const stream = createWriteStream(destZip);
return new Promise((resolve, reject) => {
archive
.directory(src, false)
.on("error", (err) => reject(err))
.pipe(stream);
stream.on("close", resolve);
archive.finalize();
});
}