-
Notifications
You must be signed in to change notification settings - Fork 1
/
parcel.ts
200 lines (179 loc) · 5.51 KB
/
parcel.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* eslint no-console: "off" */
import * as chalk from 'chalk';
import * as Bundler from 'parcel-bundler';
import * as fs from 'fs';
import {spawn, execSync, ChildProcess} from "child_process";
const ls = (dir: string): string[] => {
return fs.readdirSync(dir).map(x => `${dir}/${x}`);
};
if (fs.existsSync("./dist")) {
for (const file of ls("./dist")) {
fs.unlinkSync(file);
}
}
const entryFiles = [
"./templates/birkat_hamazon_page.html",
"./templates/daf_yomi_redirector.html",
"./templates/draw.html",
"./templates/homepage.html",
"./templates/last_redirecter.html",
"./templates/mishna.html",
"./templates/notes_redirecter.html",
"./templates/service_worker.html",
"./templates/siddur_page.html",
"./templates/talmud_page.html",
"./templates/tanakh.html",
];
const isProd = (() => {
switch (process.argv[2]) {
case "prod":
if (!fs.existsSync("sendgrid_api_key")) {
throw new Error("Could not find sendgrid_api_key file, which is necessary to deploy.");
}
return true;
case "dev":
return false;
case undefined:
default:
throw new Error(`No configuration specified. Usage: \`node ${process.argv[1]} <prod|dev>\``);
}
})();
const bundler = new Bundler(entryFiles, {
watch: !isProd,
minify: isProd,
hmr: false,
// @ts-ignore
autoInstall: false,
contentHash: isProd,
// TODO: examine how to re-enable this safely.
scopeHoist: false,
throwErrors: false,
});
bundler.on("bundled", () => {
const distFiles = new Set(fs.readdirSync("./dist"));
for (const template of fs.readdirSync("./templates")) {
if (!distFiles.has(template)) {
fs.copyFileSync(`./templates/${template}`, `./dist/${template}`);
}
}
});
bundler.bundle();
const IGNORED_PREFIXES = [
".#",
".eslintrc.js",
".eslintrc.dev.js",
".git/",
"cached_outputs/",
"dist/",
"test_data/",
"venv/",
];
const ignoreFile = (file: string) => (
IGNORED_PREFIXES.some(x => file.startsWith(x)) || file.includes("#")
);
const jsFiles = (dir = "."): string[] => {
const files: string[] = [];
for (const x of fs.readdirSync(dir, {withFileTypes: true})) {
const name = dir === "." ? x.name : `${dir}/${x.name}`;
if (ignoreFile(name) || name === "node_modules") {
continue;
}
if (x.isDirectory()) {
files.push(...jsFiles(name));
} else if (name.endsWith(".js")
|| name.endsWith(".ts")
|| name.endsWith(".jsx")
|| name.endsWith(".tsx")) {
files.push(name);
}
}
return files;
};
const FOUND_ERRORS_RE = (
/\[.*\d?\d:\d{2}:\d{2} [AP]M.*] (Found (\d+) errors?. Watching for file changes.)$/
);
function processTscOutput(output: string) {
if (output.includes("File change detected")
|| output.includes("Starting compilation in watch mode")
// File not found
|| output.includes("TS6053")) {
return;
}
const match = output.match(FOUND_ERRORS_RE);
if (match) {
if (match[2] === "0") {
console.log(chalk.blue("[ clean ] tsc"));
}
} else {
console.log(output);
}
}
if (!isProd) {
const tscProcess = spawn("npx", ["tsc", "--watch", "--preserveWatchOutput"]);
tscProcess.stdout!.on("data", data => processTscOutput(data.toString().trim()));
tscProcess.stderr!.on("data", data => processTscOutput(data.toString().trim()));
let eslint: ChildProcess | undefined;
const startEslint = () => {
const allJsFiles = jsFiles(".");
const needToSave = allJsFiles.filter(x => x.includes(".#"));
if (needToSave.length > 0) {
console.log(chalk.bgMagenta.bold(`Unsaved: ${needToSave}`));
}
const filesToLint = allJsFiles.filter(x => !x.includes(".#"));
eslint?.kill();
eslint = spawn("pre-commit/check_eslint.sh", filesToLint.concat(["--dev"]));
eslint.stdout!.on("data", data => console.log(data.toString()));
eslint.stderr!.on("data", data => console.error(chalk.bgRed(data.toString())));
eslint.on("close", code => {
if (code === 0) {
console.log(chalk.blue("[ clean ] eslint"));
}
});
};
startEslint();
fs.watch(".", {recursive: true}, (changeType, file) => {
if (!ignoreFile(file) && /.*\.[jt]sx?$/.test(file)) {
startEslint();
}
});
let serverProcess: ChildProcess | undefined;
const killServer = () => serverProcess?.kill();
const startServer = () => {
killServer();
const otherProcesses = execSync(
"ps ax | grep 'ts-node express_main.ts'").toString().split("\n");
for (const toKill of otherProcesses) {
if (toKill.includes("node_modules/.bin/ts-node")) {
const process = toKill.match(/^(\d+) .*/)![1];
execSync(`kill ${process}`);
}
}
serverProcess = spawn("npx", [
"nodemon express_main.ts",
"--config nodemon-express.json",
"--ignore parcel.ts",
"--ignore cached_outputs",
"--ignore test_data",
"--ignore js",
].flatMap(x => x.split(" ")), {
env: {
...process.env,
PORT: "5001",
FORCE_COLOR: "true",
},
});
serverProcess.stdout!.on("data", data => console.log(data.toString().trim()));
serverProcess.stderr!.on("data", data => console.log(data.toString().trim()));
process.stdin.pipe(serverProcess.stdin!);
};
let distFiles = new Set();
bundler.on('bundled', () => {
const newDistFiles = fs.readdirSync("./dist");
if (distFiles.size !== newDistFiles.length || !newDistFiles.every(x => distFiles.has(x))) {
distFiles = new Set(newDistFiles);
startServer();
}
});
bundler.on("buildError", () => killServer());
process.on("exit", () => killServer());
}