-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-shebang.ts
More file actions
executable file
·37 lines (30 loc) · 1.03 KB
/
update-shebang.ts
File metadata and controls
executable file
·37 lines (30 loc) · 1.03 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
#!/usr/bin/env tsx
import { spawnSync } from "node:child_process";
import { readFile, writeFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
const ROOT = dirname(__dirname);
const tsShebang = "#!/usr/bin/env tsx";
const jsShebang = "#!/usr/bin/env node";
async function updateShebang (path: string) {
const originalContent = await readFile(path, { encoding: "utf8" });
const updatedContent = originalContent.replace(tsShebang, jsShebang);
await writeFile(path, updatedContent, { encoding: "utf8" });
}
async function main () {
const npmResult = spawnSync("npm", ["show", `file://${ROOT}`, "bin", "--json"], {
cwd: ROOT,
shell: true,
encoding: "utf8",
});
if (npmResult.stdout) {
const binEntries = JSON.parse(npmResult.stdout) as Record<string, string>;
const binPaths = Object.values(binEntries);
for (const binPath of binPaths) {
await updateShebang(resolve(ROOT, binPath));
}
}
}
main().catch((err: Error) => {
process.exitCode = 1;
console.error(err.stack);
});