-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
45 lines (40 loc) · 1.39 KB
/
Copy pathbuild.mjs
File metadata and controls
45 lines (40 loc) · 1.39 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
import { spawn } from "node:child_process";
import { readdir, rename, rm, writeFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
const distDir = "dist";
const tscPath = fileURLToPath(
new URL("../node_modules/typescript/bin/tsc", import.meta.url),
);
const run = (command, args) =>
new Promise((resolve, reject) => {
const child = spawn(command, args, { stdio: "inherit" });
child.once("error", reject);
child.once("exit", (code) => {
if (code === 0) {
resolve();
return;
}
reject(new Error(`${command} exited with code ${code}`));
});
});
const renameJavaScriptFiles = async (directory) => {
for (const entry of await readdir(directory, { withFileTypes: true })) {
const entryPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
await renameJavaScriptFiles(entryPath);
continue;
}
if (path.extname(entry.name) === ".js") {
await rename(entryPath, `${entryPath.slice(0, -3)}.mjs`);
}
}
};
await rm(distDir, { recursive: true, force: true });
await run(process.execPath, [tscPath, "-p", "tsconfig.build.json"]);
await renameJavaScriptFiles(path.join(distDir, "esm"));
await Promise.all([
writeFile("dist/api.mjs", 'export * from "./esm/api.mjs";\n'),
writeFile("dist/cli.mjs", 'export * from "./esm/cli/index.mjs";\n'),
writeFile("dist/lock.mjs", 'export * from "./esm/cache/lock.mjs";\n'),
]);