-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-phi-extension.ts
More file actions
53 lines (43 loc) · 1.64 KB
/
Copy pathprepare-phi-extension.ts
File metadata and controls
53 lines (43 loc) · 1.64 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
46
47
48
49
50
51
52
53
#!/usr/bin/env bun
import { mkdir, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
/**
* Prepares dist/phi-extension/ so it can be symlinked into phi's extensions dir.
*
* Goals:
* - no POSIX shell dependencies (mkdir -p / ln -sf / echo >)
* - idempotent + fails build on errors
*/
async function main() {
const pkgRoot = process.cwd();
const distDir = join(pkgRoot, "dist");
const extDir = join(distDir, "phi-extension");
const pkgJsonPath = join(extDir, "package.json");
const indexJsPath = join(extDir, "index.js");
await mkdir(extDir, { recursive: true });
// Ensure we never leave a dangling symlink behind from previous builds.
// (On Windows, symlink creation may require elevated privileges.)
await rm(indexJsPath, { force: true });
// Minimal package.json: ensure ESM semantics for index.js.
await writeFile(pkgJsonPath, '{"type":"module"}\n', "utf8");
// Avoid symlink entirely for portability: create a tiny re-export shim.
// This replaces the previous `ln -sf ../phi.js dist/phi-extension/index.js`.
// NOTE: We resolve realpath to ensure symlinked extension dirs can find dist/phi.js.
await writeFile(
indexJsPath,
`import { realpath } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const here = dirname(fileURLToPath(import.meta.url));
const realHere = await realpath(here);
const targetUrl = pathToFileURL(join(realHere, "..", "phi.js")).href;
const mod = await import(targetUrl);
export default mod.default;
`,
"utf8",
);
}
main().catch((err) => {
console.error(err);
process.exitCode = 1;
});