Repository navigation
Expand file tree
/
Copy pathlayerfs-source.mjs
More file actions
61 lines (57 loc) · 3.72 KB
/
Copy pathlayerfs-source.mjs
File metadata and controls
61 lines (57 loc) · 3.72 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
54
55
56
57
58
59
60
61
// Points .layerfs at the LayerFS checkout this repository builds on, and
// checks it against the pin. The checkout is LAYERFS_SOURCE, else layerfs.json's
// `source` (../layerfs beside this repository); a relative path resolves from
// this repository's root. Everything of LayerFS (the packages the Host links,
// @layerfs/agent, @layerfs/backend and @layerfs/runtime from api/ (agent,
// backend, runtime), the layerfs command in api/cli/, the agent skill, the Linux build) is read
// through .layerfs. @layerfs/backend
// and @layerfs/runtime import @layerfs/agent from api/'s own node_modules,
// which `npm install` in api/ links (it downloads nothing).
// node tools/layerfs-source.mjs [link] point .layerfs (pnpm install and pnpm test run this first;
// pnpm skips preinstall when nothing else changed)
// node tools/layerfs-source.mjs check also fail unless the checkout is at the pinned commit
import { execFileSync } from "node:child_process";
import { existsSync, lstatSync, readFileSync, readlinkSync, rmSync, symlinkSync } from "node:fs";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const ROOT = fileURLToPath(new URL("..", import.meta.url));
const LINK = join(ROOT, ".layerfs");
const pin = JSON.parse(readFileSync(join(ROOT, "layerfs.json"), "utf8"));
const mode = process.argv[2] ?? "link";
if (mode !== "link" && mode !== "check") {
console.error("usage: node tools/layerfs-source.mjs [link|check]");
process.exit(2);
}
const source = resolve(ROOT, process.env.LAYERFS_SOURCE || pin.source);
const hasV1 = existsSync(join(source, "api/agent/package.json"));
const hasV2 = existsSync(join(source, "api/node/package.json"));
if (!hasV1 && !hasV2) {
console.error(`layerfs-source: ${source} is not a LayerFS checkout with the #428 packages (no api/agent); set LAYERFS_SOURCE to one at ${pin.commit.slice(0, 10)}`);
process.exit(1);
}
if (!hasV2 && !existsSync(join(source, "api/node_modules/@layerfs/agent/package.json"))) {
console.error(`layerfs-source: ${source}/api has no node_modules/@layerfs/agent; run \`npm install\` in that api/ once (it only links its workspaces)`);
process.exit(1);
}
// the layerfs command agents run (DSH_LAYERFS_CLI) imports @layerfs/agent from api/cli/'s own node_modules
if (!hasV2 && !existsSync(join(source, "api/cli/src/layerfs.js"))) {
console.error(`layerfs-source: ${source} has no api/cli/src/layerfs.js, the layerfs command; set LAYERFS_SOURCE to a checkout at ${pin.commit.slice(0, 10)}`);
process.exit(1);
}
if (!hasV2 && !existsSync(join(source, "api/cli/node_modules/@layerfs/agent/package.json"))) {
console.error(`layerfs-source: ${source}/api/cli has no node_modules/@layerfs/agent; run \`npm --prefix "${join(source, "api/cli")}" install\` once (it only links the SDK)`);
process.exit(1);
}
let current = null;
try { if (lstatSync(LINK).isSymbolicLink()) current = resolve(ROOT, readlinkSync(LINK)); else throw new Error(`${LINK} exists and is not a link; remove it`); }
catch (error) { if (error.code !== "ENOENT") throw error; }
if (current !== source) {
if (current !== null) rmSync(LINK);
// a junction on Windows needs no extra privilege
symlinkSync(source, LINK, process.platform === "win32" ? "junction" : "dir");
}
let head = null;
try { head = execFileSync("git", ["-C", source, "rev-parse", "HEAD"], { encoding: "utf8", timeout: 10_000, stdio: ["ignore", "pipe", "ignore"] }).trim(); } catch {}
const at = head === pin.commit ? "at the pinned commit" : head ? `at ${head.slice(0, 10)}, not the pinned ${pin.commit.slice(0, 10)}` : `not a git checkout (pinned ${pin.commit.slice(0, 10)})`;
console.log(`layerfs-source: .layerfs -> ${source}, ${at}`);
if (mode === "check" && head !== pin.commit) process.exit(1);