Skip to content

Commit 1e88c0d

Browse files
committed
q
1 parent 4678d6a commit 1e88c0d

4 files changed

Lines changed: 77 additions & 2 deletions

File tree

transpile.ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ tsc.ignore.ts
44
node_modules
55
bundle.ts
66
transpile.ts
7+
transpile_pipe.ts
78
es.ts
89
bash/tee.ts
910
vitest.config.ts

transpile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ find . -type d \( \
3333
f -name "*.ts" \
3434
-print \
3535
| node gitignore.js "${IGNORE_FILE}" \
36-
| /bin/bash ts.sh transpile.ts "$@"
36+
| /bin/bash ts.sh transpile.ts "$@" | node transpile_pipe.ts

transpile.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*/
44
import * as esbuild from "esbuild";
55
import readline from "readline";
6+
import path from "path";
7+
import fs from "fs";
68

79
const log = (...args: any) => console.log("transpile.ts:", ...args);
810

@@ -46,10 +48,35 @@ const options: esbuild.BuildOptions = {
4648
platform: "node",
4749
format: "esm",
4850
target: "node20",
49-
logLevel: "info",
51+
logLevel: "warning",
5052
logOverride: {
5153
"direct-eval": "silent",
5254
},
55+
plugins: [
56+
{
57+
name: "watch-reporter",
58+
setup(build) {
59+
const mtimes = new Map<string, number>();
60+
let isInitialBuild = true;
61+
build.onEnd((result) => {
62+
if (result.errors.length > 0) return;
63+
const changed: string[] = [];
64+
for (const ep of entryPoints) {
65+
try {
66+
const stats = fs.statSync(ep);
67+
const lastMtime = mtimes.get(ep);
68+
if (lastMtime !== undefined && stats.mtimeMs > lastMtime) {
69+
changed.push(ep);
70+
}
71+
mtimes.set(ep, stats.mtimeMs);
72+
} catch (e) {}
73+
}
74+
changed.forEach((file) => console.log(`transpiled ${file}`));
75+
isInitialBuild = false;
76+
});
77+
},
78+
},
79+
],
5380
};
5481

5582
if (watch) {

transpile_pipe.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import readline from "readline";
2+
import { exec } from "child_process";
3+
import fs from "fs";
4+
5+
if (process.stdin.isTTY) {
6+
console.log(`
7+
Usage:
8+
node transpile.ts --watch | node transpile_pipe.ts
9+
10+
Description:
11+
Listen for "transpiled [file].ts" on stdin.
12+
Run prettier on "[file].js".
13+
Prefix stdin with "stdin: ".
14+
Print "frmtd: [file].js" on success.
15+
`);
16+
process.exit(0);
17+
}
18+
19+
const rl = readline.createInterface({
20+
input: process.stdin,
21+
terminal: false,
22+
});
23+
24+
rl.on("line", (line) => {
25+
console.log(`stdin: ${line}`);
26+
27+
const match = line.match(/^transpiled (.*\.ts)$/);
28+
if (match) {
29+
const tsFile = match[1];
30+
const jsFile = tsFile.replace(/\.ts$/, ".js");
31+
32+
if (fs.existsSync(jsFile)) {
33+
const cmd = `/bin/bash node_modules/.bin/prettier --config prettier.config.cjs --write ${jsFile}`;
34+
exec(cmd, (error, stdout, stderr) => {
35+
if (error) {
36+
console.error(`error: ${error.message}`);
37+
return;
38+
}
39+
if (stderr) {
40+
console.error(`stderr: ${stderr}`);
41+
return;
42+
}
43+
console.log(`frmtd: ${jsFile}`);
44+
});
45+
}
46+
}
47+
});

0 commit comments

Comments
 (0)