-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
152 lines (133 loc) · 4.01 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
const chokidar = require("chokidar");
const process = require("process");
const { spawn } = require("child_process");
const fs = require("fs");
const fsp = fs.promises;
const path = require("path");
function getFiles(atPath) {
let current = null;
let paths = [atPath];
async function next() {
let statPath = paths.shift();
if (!statPath) {
current = null;
return false;
}
let stat = await fsp.stat(statPath);
if (stat.isFile()) {
current = statPath;
return true;
} else if (stat.isDirectory()) {
const subpaths = (await fsp.readdir(statPath)).map((p) =>
path.join(statPath, p)
);
Array.prototype.push.apply(paths, subpaths);
return next();
}
throw `Not a directory or a file: ${statPath}`;
}
return { current: () => current, next: next };
}
async function copyToPublish() {
const cid = process.env.CLIENT_ID;
if (!cid) {
throw "Could not find CLIENT_ID environment variable";
}
if (!fs.existsSync("./publish")) {
fs.mkdirSync("./publish");
}
const efile = await fsp.open("./publish/environment.js", "w+");
await efile.writeFile(`window.CLIENT_ID = "${cid}";`);
efile.close();
await fsp.copyFile(
"./node_modules/dexie/dist/dexie.js",
"./publish/dexie.js"
);
await fsp.copyFile(
"./node_modules/webextension-polyfill/dist/browser-polyfill.js",
"./publish/browser-polyfill.js"
);
await fsp.copyFile("./empty_table.xlsx", "./publish/empty_table.xlsx");
await fsp.copyFile("./popup.html", "./publish/popup.html");
await fsp.copyFile("./manifest.json", "./publish/manifest.json");
await fsp.copyFile("./logo_48px.png", "./publish/logo_48px.png");
await fsp.copyFile(
"./logo_48px_disabled.png",
"./publish/logo_48px_disabled.png"
);
await fsp.copyFile("./logo_128px.png", "./publish/logo_128px.png");
}
// Copy */types/webextension.d.ts files to @types/*/webextension.d.ts to satisfy TypeScript
async function fixTscTypes() {
const files = getFiles("./node_modules");
while (await files.next()) {
const file = files.current();
const dtsMatch = file.match(
/node_modules[\\/](.*)[\\/]types[\\/]index\.d\.ts$/
);
if (dtsMatch && dtsMatch.length > 0) {
const package = dtsMatch[1];
if (package === "chokidar") continue;
const packagePath = `./node_modules/@types/${package}`;
console.log(`Adding ${packagePath}`);
try {
await fsp.stat(packagePath);
} catch {
await fsp.mkdir(packagePath);
}
await fsp.copyFile(file, path.join(packagePath, "index.d.ts"));
}
}
}
async function runTsc() {
const tsc = spawn("tsc", [], { shell: true, stdio: "inherit" });
return new Promise((resolve) => {
tsc.on("close", (code) => resolve(code));
});
}
async function build() {
process.stdout.write(`${new Date().toISOString()} Building.. \n`);
await copyToPublish();
await fixTscTypes();
let code = await runTsc();
if (code === 0) process.stdout.write(`${new Date().toISOString()} Done.`);
}
function watch() {
const tsc = spawn("tsc", ["-w"], { shell: true, stdio: "inherit" });
let changes = [];
chokidar
.watch(".", {
depth: 0,
awaitWriteFinish: true,
ignored: /(.*).js$|(.*).ts$/,
})
.on("change", async (event, path) => {
changes.push(new Date().toISOString());
if (changes.length > 1) return;
let last = changes.pop();
while (last) {
process.stdout.write(
`${new Date().toISOString()} Copying file changes for ${event}.\n`
);
await copyToPublish();
process.stdout.write("Done.");
const latest = changes.pop();
if (latest && latest > last) last = latest;
else break;
}
});
}
async function main(args) {
if (args.length === 0) {
await build();
} else {
if (args[0] === "build") {
await build();
} else if (args[0] === "watch") {
watch();
} else {
console.warn(`Unrecognized command line argument: ${args[0]}`);
}
}
}
main(process.argv.slice(2));