@antelopejs/dms 0.18.0 → 0.18.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +49 -0
- package/README.md +3 -3
- package/bin/control/backend-records.test.mjs +276 -0
- package/bin/control/backend.mjs +212 -0
- package/bin/control/backend.test.mjs +325 -0
- package/bin/control/base.mjs +3 -7
- package/bin/control/browser-client.mjs +6 -4
- package/bin/control/browser.mjs +173 -36
- package/bin/control/build-cache.mjs +394 -0
- package/bin/control/build-cache.test.mjs +270 -0
- package/bin/control/build-inputs.mjs +172 -0
- package/bin/control/build-key-refusals.test.mjs +138 -0
- package/bin/control/build-key.mjs +479 -0
- package/bin/control/build-key.test.mjs +392 -0
- package/bin/control/build-layout.test.mjs +314 -0
- package/bin/control/commands-stack.mjs +46 -6
- package/bin/control/commands-verify.mjs +14 -25
- package/bin/control/doctor.mjs +87 -16
- package/bin/control/forwarder.mjs +86 -0
- package/bin/control/forwarder.test.mjs +141 -0
- package/bin/control/frontend-check.mjs +54 -0
- package/bin/control/frontend-workspace.mjs +150 -0
- package/bin/control/frontend-workspace.test.mjs +133 -0
- package/bin/control/helpers/env.mjs +21 -0
- package/bin/control/helpers/evidence.mjs +13 -0
- package/bin/control/helpers/fake-run.mjs +290 -0
- package/bin/control/helpers/key-fixture.mjs +82 -0
- package/bin/control/helpers/pids.mjs +27 -0
- package/bin/control/helpers/repo.mjs +13 -0
- package/bin/control/helpers/tree.mjs +40 -0
- package/bin/control/identity.mjs +238 -0
- package/bin/control/identity.test.mjs +166 -0
- package/bin/control/journey.mjs +64 -16
- package/bin/control/journey.test.mjs +60 -2
- package/bin/control/namespaces.test.mjs +211 -0
- package/bin/control/options.mjs +2 -0
- package/bin/control/pid-guard.test.mjs +202 -0
- package/bin/control/prepared-workspace.mjs +20 -0
- package/bin/control/processes.mjs +122 -33
- package/bin/control/processes.test.mjs +255 -0
- package/bin/control/renderer.test.mjs +2 -1
- package/bin/control/run-copy.mjs +77 -0
- package/bin/control/run-state.mjs +142 -16
- package/bin/control/stack.mjs +259 -210
- package/bin/control/tab.mjs +13 -6
- package/bin/control/tab.test.mjs +444 -0
- package/bin/control/timing.mjs +244 -0
- package/bin/control/timing.test.mjs +197 -0
- package/bin/control/verify/decode.mjs +34 -36
- package/bin/control/verify/decode.test.mjs +40 -14
- package/bin/control/verify/doors.test.mjs +2 -0
- package/bin/control/verify/map.mjs +57 -7
- package/bin/control/verify/map.test.mjs +264 -0
- package/bin/control/verify/skill.mjs +12 -3
- package/bin/control/verify/types.mjs +2 -2
- package/bin/control/verify/verdict.mjs +34 -37
- package/bin/dms-control.mjs +13 -6
- package/frontend-vue/layers/dms-layout/app/components/layout/DashboardSearch.vue +24 -1
- package/package.json +1 -1
- package/skills/dms-project/SKILL.md +6 -3
- package/bin/control/verify/publish.mjs +0 -186
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { createConnection, createServer } from "node:net";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
const MAX_PORT = 65_535;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The file that names the port the run's API forwarder sends new
|
|
9
|
+
* connections to.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} dir The run folder
|
|
12
|
+
* @returns {string} The file
|
|
13
|
+
*/
|
|
14
|
+
export function apiTargetFile(dir) {
|
|
15
|
+
return join(dir, "api-target");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The port a target file names, or `null` when it is missing or names none.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} file The target file
|
|
22
|
+
* @returns {number | null} The port
|
|
23
|
+
*/
|
|
24
|
+
export function readTarget(file) {
|
|
25
|
+
let text;
|
|
26
|
+
try {
|
|
27
|
+
text = readFileSync(file, "utf8");
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const port = Number(text.trim());
|
|
32
|
+
return Number.isInteger(port) && port > 0 && port <= MAX_PORT ? port : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Points the forwarder's new connections at `port`. The file is replaced
|
|
37
|
+
* whole, so a connection reads the old port or the new one, never a part.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} file The target file
|
|
40
|
+
* @param {number} port The loopback port
|
|
41
|
+
*/
|
|
42
|
+
export function writeTarget(file, port) {
|
|
43
|
+
const next = `${file}.${process.pid}`;
|
|
44
|
+
writeFileSync(next, `${port}\n`);
|
|
45
|
+
renameSync(next, file);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @param {import("node:net").Socket} client
|
|
50
|
+
* @param {number | null} target
|
|
51
|
+
*/
|
|
52
|
+
function forward(client, target) {
|
|
53
|
+
if (target === null) {
|
|
54
|
+
client.resetAndDestroy();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const upstream = createConnection({
|
|
58
|
+
host: "127.0.0.1",
|
|
59
|
+
port: target,
|
|
60
|
+
allowHalfOpen: true,
|
|
61
|
+
});
|
|
62
|
+
upstream.on("error", () => client.resetAndDestroy());
|
|
63
|
+
client.on("error", () => upstream.destroy());
|
|
64
|
+
client.pipe(upstream);
|
|
65
|
+
upstream.pipe(client);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Serves a TCP forwarder on a loopback port. Each connection goes to the
|
|
70
|
+
* port `file` names when it arrives, and keeps it until it closes. One that
|
|
71
|
+
* arrives while the file names no port, or names a port nothing listens on,
|
|
72
|
+
* is reset at once.
|
|
73
|
+
*
|
|
74
|
+
* @param {number} port The loopback port to listen on
|
|
75
|
+
* @param {string} file The target file
|
|
76
|
+
* @returns {Promise<import("node:net").Server>} The server, once it listens
|
|
77
|
+
*/
|
|
78
|
+
export function serveForwarder(port, file) {
|
|
79
|
+
const server = createServer({ allowHalfOpen: true }, (client) =>
|
|
80
|
+
forward(client, readTarget(file)),
|
|
81
|
+
);
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
server.once("error", reject);
|
|
84
|
+
server.listen(port, "127.0.0.1", () => resolve(server));
|
|
85
|
+
});
|
|
86
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
3
|
+
import { createConnection, createServer } from "node:net";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import test from "node:test";
|
|
7
|
+
import { serveForwarder, writeTarget } from "./forwarder.mjs";
|
|
8
|
+
import { freePort } from "./processes.mjs";
|
|
9
|
+
|
|
10
|
+
const TIMEOUT_MS = 3_000;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A backend stand-in that answers `<name>:<what it received>` once the
|
|
14
|
+
* client finishes sending, and can drop every connection it holds.
|
|
15
|
+
*
|
|
16
|
+
* @param {import("node:test").TestContext} t The test
|
|
17
|
+
* @param {string} name What it answers with
|
|
18
|
+
* @returns {Promise<{ port: number, dropAll: () => void, received: () => string }>} Its port, a way to drop its connections, and what it received
|
|
19
|
+
*/
|
|
20
|
+
async function backend(t, name) {
|
|
21
|
+
const port = await freePort();
|
|
22
|
+
/** @type {Set<import("node:net").Socket>} */
|
|
23
|
+
const sockets = new Set();
|
|
24
|
+
let total = "";
|
|
25
|
+
const server = createServer({ allowHalfOpen: true }, (socket) => {
|
|
26
|
+
sockets.add(socket);
|
|
27
|
+
socket.on("close", () => sockets.delete(socket));
|
|
28
|
+
let received = "";
|
|
29
|
+
socket.on("data", (chunk) => {
|
|
30
|
+
received += String(chunk);
|
|
31
|
+
total += String(chunk);
|
|
32
|
+
});
|
|
33
|
+
socket.on("end", () => socket.end(`${name}:${received}`));
|
|
34
|
+
});
|
|
35
|
+
const dropAll = () => {
|
|
36
|
+
for (const socket of sockets) socket.destroy();
|
|
37
|
+
};
|
|
38
|
+
await new Promise((resolve) => server.listen(port, "127.0.0.1", resolve));
|
|
39
|
+
t.after(() => {
|
|
40
|
+
dropAll();
|
|
41
|
+
server.close();
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
port,
|
|
45
|
+
dropAll,
|
|
46
|
+
received: () => total,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A forwarder on a free port, with its target file.
|
|
52
|
+
*
|
|
53
|
+
* @param {import("node:test").TestContext} t The test
|
|
54
|
+
* @returns {Promise<{ port: number, file: string }>} Its port and target file
|
|
55
|
+
*/
|
|
56
|
+
async function forwarder(t) {
|
|
57
|
+
const dir = mkdtempSync(join(tmpdir(), "dms-control-forwarder-test-"));
|
|
58
|
+
const file = join(dir, "api-target");
|
|
59
|
+
const port = await freePort();
|
|
60
|
+
const server = await serveForwarder(port, file);
|
|
61
|
+
t.after(() => {
|
|
62
|
+
server.close();
|
|
63
|
+
rmSync(dir, { recursive: true, force: true });
|
|
64
|
+
});
|
|
65
|
+
return { port, file };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Opens a connection to `port`, sends `message` and finishes sending.
|
|
70
|
+
*
|
|
71
|
+
* @param {number} port The port
|
|
72
|
+
* @param {string} message What to send
|
|
73
|
+
* @returns {Promise<string>} What came back before the connection ended
|
|
74
|
+
* @throws When the connection is reset, or nothing ends it in time
|
|
75
|
+
*/
|
|
76
|
+
function exchange(port, message) {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
const socket = createConnection({ host: "127.0.0.1", port });
|
|
79
|
+
let answer = "";
|
|
80
|
+
socket.setTimeout(TIMEOUT_MS, () => {
|
|
81
|
+
socket.destroy();
|
|
82
|
+
reject(new Error("the connection hung"));
|
|
83
|
+
});
|
|
84
|
+
socket.on("data", (chunk) => {
|
|
85
|
+
answer += String(chunk);
|
|
86
|
+
});
|
|
87
|
+
socket.on("end", () => resolve(answer));
|
|
88
|
+
socket.on("error", reject);
|
|
89
|
+
socket.end(message);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
await test("each connection reaches the port the target names when it arrives", async (t) => {
|
|
94
|
+
const first = await backend(t, "first");
|
|
95
|
+
const second = await backend(t, "second");
|
|
96
|
+
const api = await forwarder(t);
|
|
97
|
+
writeTarget(api.file, first.port);
|
|
98
|
+
assert.equal(await exchange(api.port, "x"), "first:x");
|
|
99
|
+
writeTarget(api.file, second.port);
|
|
100
|
+
assert.equal(await exchange(api.port, "y"), "second:y");
|
|
101
|
+
writeTarget(api.file, first.port);
|
|
102
|
+
assert.equal(await exchange(api.port, "z"), "first:z");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
await test("a client that finishes sending still receives the whole answer", async (t) => {
|
|
106
|
+
const only = await backend(t, "only");
|
|
107
|
+
const api = await forwarder(t);
|
|
108
|
+
writeTarget(api.file, only.port);
|
|
109
|
+
const message = "m".repeat(256 * 1024);
|
|
110
|
+
assert.equal(await exchange(api.port, message), `only:${message}`);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
await test("a connection whose backend goes away is closed, not left open", async (t) => {
|
|
114
|
+
const dying = await backend(t, "dying");
|
|
115
|
+
const api = await forwarder(t);
|
|
116
|
+
writeTarget(api.file, dying.port);
|
|
117
|
+
const socket = createConnection({ host: "127.0.0.1", port: api.port });
|
|
118
|
+
t.after(() => socket.destroy());
|
|
119
|
+
socket.on("error", () => undefined);
|
|
120
|
+
const closed = new Promise((resolve, reject) => {
|
|
121
|
+
socket.on("close", resolve);
|
|
122
|
+
setTimeout(() => reject(new Error("still open")), TIMEOUT_MS).unref();
|
|
123
|
+
});
|
|
124
|
+
socket.write("partial");
|
|
125
|
+
while (dying.received() !== "partial") {
|
|
126
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
127
|
+
}
|
|
128
|
+
dying.dropAll();
|
|
129
|
+
await closed;
|
|
130
|
+
assert.equal(socket.destroyed, true);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
await test("with no listener or no port to reach, a connection is reset at once and the next one is served", async (t) => {
|
|
134
|
+
const alive = await backend(t, "alive");
|
|
135
|
+
const api = await forwarder(t);
|
|
136
|
+
await assert.rejects(exchange(api.port, "x"), { code: "ECONNRESET" });
|
|
137
|
+
writeTarget(api.file, await freePort());
|
|
138
|
+
await assert.rejects(exchange(api.port, "x"), { code: "ECONNRESET" });
|
|
139
|
+
writeTarget(api.file, alive.port);
|
|
140
|
+
assert.equal(await exchange(api.port, "y"), "alive:y");
|
|
141
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { rmSync, symlinkSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
buildInputs,
|
|
6
|
+
buildKey,
|
|
7
|
+
sourceNames,
|
|
8
|
+
WORKSPACE_FOLDER,
|
|
9
|
+
} from "./build-key.mjs";
|
|
10
|
+
import { layOutCopy } from "./run-copy.mjs";
|
|
11
|
+
import { frontendPackage, workspaceSetup } from "./stack.mjs";
|
|
12
|
+
|
|
13
|
+
const SUFFIX_BYTES = 4;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The key of the run's frontend build inputs as a fresh `up` would read
|
|
17
|
+
* them now: a new copy of the playground (see `layOutCopy`) with the run's
|
|
18
|
+
* installed project modules, whose workspace is set up without installing,
|
|
19
|
+
* keyed with the versions the run's own workspace installed. The copy is
|
|
20
|
+
* removed.
|
|
21
|
+
*
|
|
22
|
+
* @param {import("./run-state.mjs").RunState} state The run, after `up`
|
|
23
|
+
* @returns {Promise<string>} The key
|
|
24
|
+
*/
|
|
25
|
+
export async function currentFrontendBuildKey(state) {
|
|
26
|
+
const scratch = join(
|
|
27
|
+
state.dir,
|
|
28
|
+
`frontend-check-${randomBytes(SUFFIX_BYTES).toString("hex")}`,
|
|
29
|
+
);
|
|
30
|
+
try {
|
|
31
|
+
const project = layOutCopy(
|
|
32
|
+
state.playground,
|
|
33
|
+
state.repo ?? state.moduleRoot,
|
|
34
|
+
scratch,
|
|
35
|
+
state.renderer,
|
|
36
|
+
);
|
|
37
|
+
symlinkSync(
|
|
38
|
+
join(state.runPlayground ?? "", ".antelope"),
|
|
39
|
+
join(project, ".antelope"),
|
|
40
|
+
);
|
|
41
|
+
const workspace = join(project, WORKSPACE_FOLDER);
|
|
42
|
+
await workspaceSetup(state, "frontend-check", project, false)(workspace);
|
|
43
|
+
return buildKey(
|
|
44
|
+
buildInputs(state, frontendPackage(state.playground, state.renderer), {
|
|
45
|
+
project,
|
|
46
|
+
workspace,
|
|
47
|
+
names: sourceNames(workspace),
|
|
48
|
+
installed: join(state.runPlayground ?? "", WORKSPACE_FOLDER),
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
} finally {
|
|
52
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { describeFailure } from "./failure.mjs";
|
|
5
|
+
import { isRecord } from "./json.mjs";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {{ force: boolean, mode: "build", workspaceDir: string, project: { projectDir: string, environment: string }, beforeInstall?: () => Promise<void> }} SetupOptions The options `ajs dms build` passes the dms-frontend's `setupWorkspace`, with the hook it calls before it installs
|
|
9
|
+
* @typedef {(options: { workspaceDir: string }) => Promise<{ workspaceDir: string, layers: unknown[] }>} WorkspaceStep The step of `ajs dms build` that sets its workspace up
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const INSTALL_MARK = ".deps-hash";
|
|
13
|
+
const WORKSPACE_STEP = "./dist/commands/workspace-task.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The exit code of an `ajs dms build` that cannot build in its prepared
|
|
17
|
+
* workspace, before it built anything: its workspace step could not be
|
|
18
|
+
* replaced, or the workspace was not prepared.
|
|
19
|
+
*/
|
|
20
|
+
export const PREPARED_WORKSPACE_REFUSED = 75;
|
|
21
|
+
|
|
22
|
+
class StoppedBeforeInstall extends Error {}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {unknown} value
|
|
26
|
+
* @returns {value is (options: SetupOptions) => Promise<unknown>}
|
|
27
|
+
*/
|
|
28
|
+
function isSetup(value) {
|
|
29
|
+
return typeof value === "function";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {unknown} value
|
|
34
|
+
* @returns {value is () => unknown}
|
|
35
|
+
*/
|
|
36
|
+
function isEnvLoader(value) {
|
|
37
|
+
return typeof value === "function";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {string} frontendPackage
|
|
42
|
+
* @param {string} file
|
|
43
|
+
* @returns {Record<string, unknown>}
|
|
44
|
+
*/
|
|
45
|
+
function loadModule(frontendPackage, file) {
|
|
46
|
+
/** @type {unknown} */
|
|
47
|
+
const loaded = createRequire(join(frontendPackage, "package.json"))(file);
|
|
48
|
+
if (!isRecord(loaded)) throw new Error(`${file} exports nothing`);
|
|
49
|
+
return loaded;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @template T
|
|
54
|
+
* @param {string} frontendPackage
|
|
55
|
+
* @param {string} file
|
|
56
|
+
* @param {string} name
|
|
57
|
+
* @param {(value: unknown) => value is T} guard
|
|
58
|
+
* @returns {T}
|
|
59
|
+
*/
|
|
60
|
+
function exported(frontendPackage, file, name, guard) {
|
|
61
|
+
const loaded = loadModule(frontendPackage, file);
|
|
62
|
+
const value = Object.hasOwn(loaded, name) ? loaded[name] : undefined;
|
|
63
|
+
if (!guard(value)) throw new Error(`${file} exports no ${name}`);
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Makes the workspace step of the `ajs dms build` running in this process
|
|
69
|
+
* return the workspace it is given as it is, which `prepare-frontend` must
|
|
70
|
+
* have set up and installed, instead of setting it up again. A workspace
|
|
71
|
+
* that was not prepared ends the process with
|
|
72
|
+
* {@link PREPARED_WORKSPACE_REFUSED}.
|
|
73
|
+
*
|
|
74
|
+
* @param {string} frontendPackage The `@antelopejs/dms-frontend` folder that builds
|
|
75
|
+
* @throws When this dms-frontend has no such step, or exports it in a shape that cannot be replaced (an accessor, as a re-export compiles to, or a module namespace)
|
|
76
|
+
*/
|
|
77
|
+
export function buildInPreparedWorkspace(frontendPackage) {
|
|
78
|
+
const step = loadModule(frontendPackage, WORKSPACE_STEP);
|
|
79
|
+
exported(frontendPackage, WORKSPACE_STEP, "setUpWorkspace", isSetup);
|
|
80
|
+
/** @type {WorkspaceStep} */
|
|
81
|
+
const prepared = (options) => {
|
|
82
|
+
const workspaceDir = resolve(options.workspaceDir);
|
|
83
|
+
if (!existsSync(join(workspaceDir, INSTALL_MARK))) {
|
|
84
|
+
process.stderr.write(`dms-control: ${workspaceDir} is not prepared\n`);
|
|
85
|
+
process.exit(PREPARED_WORKSPACE_REFUSED);
|
|
86
|
+
}
|
|
87
|
+
return Promise.resolve({ workspaceDir, layers: [] });
|
|
88
|
+
};
|
|
89
|
+
try {
|
|
90
|
+
step.setUpWorkspace = prepared;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`${WORKSPACE_STEP} exports a setUpWorkspace that cannot be replaced (${describeFailure(error)})`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Sets up a frontend build workspace for the project in the current folder
|
|
100
|
+
* as `ajs dms build` does before its first build step: it loads the
|
|
101
|
+
* project's `.env.local` and `.env`, then runs the dms-frontend's own
|
|
102
|
+
* `setupWorkspace`, which copies the templates and each module's frontend,
|
|
103
|
+
* generates the registries and installs. Without `install`, it stops before
|
|
104
|
+
* the install. It first replaces this process's workspace step as the
|
|
105
|
+
* build's preload does (see {@link buildInPreparedWorkspace}), so a
|
|
106
|
+
* dms-frontend whose step cannot be replaced fails the setup, not the build.
|
|
107
|
+
*
|
|
108
|
+
* @param {string} frontendPackage The `@antelopejs/dms-frontend` folder that builds
|
|
109
|
+
* @param {string} workspace The workspace folder
|
|
110
|
+
* @param {string} environment The AntelopeJS environment
|
|
111
|
+
* @param {boolean} install Whether to install the workspace's dependencies
|
|
112
|
+
* @returns {Promise<void>} Resolves once it is set up
|
|
113
|
+
* @throws When this dms-frontend cannot set a workspace up, or build in one set up beforehand
|
|
114
|
+
*/
|
|
115
|
+
export async function setUpFrontendWorkspace(
|
|
116
|
+
frontendPackage,
|
|
117
|
+
workspace,
|
|
118
|
+
environment,
|
|
119
|
+
install,
|
|
120
|
+
) {
|
|
121
|
+
buildInPreparedWorkspace(frontendPackage);
|
|
122
|
+
const loadProjectEnv = exported(
|
|
123
|
+
frontendPackage,
|
|
124
|
+
"./dist/env-file.js",
|
|
125
|
+
"loadProjectEnv",
|
|
126
|
+
isEnvLoader,
|
|
127
|
+
);
|
|
128
|
+
const setupWorkspace = exported(
|
|
129
|
+
frontendPackage,
|
|
130
|
+
"./dist/workspace-setup.js",
|
|
131
|
+
"setupWorkspace",
|
|
132
|
+
isSetup,
|
|
133
|
+
);
|
|
134
|
+
loadProjectEnv();
|
|
135
|
+
/** @type {SetupOptions} */
|
|
136
|
+
const options = {
|
|
137
|
+
force: false,
|
|
138
|
+
mode: "build",
|
|
139
|
+
workspaceDir: workspace,
|
|
140
|
+
project: { projectDir: ".", environment },
|
|
141
|
+
};
|
|
142
|
+
if (!install) {
|
|
143
|
+
options.beforeInstall = () => Promise.reject(new StoppedBeforeInstall());
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
await setupWorkspace(options);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (!(error instanceof StoppedBeforeInstall)) throw error;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import test from "node:test";
|
|
6
|
+
import {
|
|
7
|
+
PREPARED_WORKSPACE_REFUSED,
|
|
8
|
+
setUpFrontendWorkspace,
|
|
9
|
+
} from "./frontend-workspace.mjs";
|
|
10
|
+
import { write } from "./helpers/repo.mjs";
|
|
11
|
+
import { treeOf } from "./helpers/tree.mjs";
|
|
12
|
+
import { ExitError, runToEnd } from "./processes.mjs";
|
|
13
|
+
|
|
14
|
+
const PRELOAD = new URL("prepared-workspace.mjs", import.meta.url).href;
|
|
15
|
+
const OWN_SETUP =
|
|
16
|
+
"async () => ({ workspaceDir: 'set up again', layers: ['copied'] })";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A dms-frontend whose CLI runs its workspace step on the folder it is
|
|
20
|
+
* given and prints what the step returned, as `ajs dms build` reads it,
|
|
21
|
+
* through the module's exports when it runs. Its step is a plain export, or
|
|
22
|
+
* the accessor TypeScript emits for a re-export.
|
|
23
|
+
*
|
|
24
|
+
* @param {import("node:test").TestContext} t The test
|
|
25
|
+
* @param {"export" | "re-export"} stepExport How the step is exported
|
|
26
|
+
*/
|
|
27
|
+
function frontendPackage(t, stepExport) {
|
|
28
|
+
const dir = mkdtempSync(join(tmpdir(), "dms-control-frontend-workspace-"));
|
|
29
|
+
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
30
|
+
const frontend = join(dir, "dms-frontend");
|
|
31
|
+
write(join(frontend, "package.json"), '{"version":"0.10.0"}');
|
|
32
|
+
write(
|
|
33
|
+
join(frontend, "dist", "commands", "workspace-task.js"),
|
|
34
|
+
stepExport === "export"
|
|
35
|
+
? `exports.setUpWorkspace = ${OWN_SETUP};\n`
|
|
36
|
+
: `Object.defineProperty(exports, "setUpWorkspace", { enumerable: true, get: () => ${OWN_SETUP} });\n`,
|
|
37
|
+
);
|
|
38
|
+
write(
|
|
39
|
+
join(frontend, "dist", "index.js"),
|
|
40
|
+
`require("./commands/workspace-task.js").setUpWorkspace({ workspaceDir: process.argv[2] }).then((result) => process.stdout.write(JSON.stringify(result)));\n`,
|
|
41
|
+
);
|
|
42
|
+
write(
|
|
43
|
+
join(frontend, "dist", "env-file.js"),
|
|
44
|
+
"exports.loadProjectEnv = () => {};\n",
|
|
45
|
+
);
|
|
46
|
+
write(
|
|
47
|
+
join(frontend, "dist", "workspace-setup.js"),
|
|
48
|
+
`exports.setupWorkspace = async (options) => require("node:fs").mkdirSync(options.workspaceDir, { recursive: true });\n`,
|
|
49
|
+
);
|
|
50
|
+
const workspace = join(dir, "project", ".dms-frontend");
|
|
51
|
+
const logs = join(dir, "logs");
|
|
52
|
+
mkdirSync(logs);
|
|
53
|
+
/**
|
|
54
|
+
* Runs the build as `up` does, and reads what it printed.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} folder The workspace it builds in
|
|
57
|
+
*/
|
|
58
|
+
const build = async (folder) => {
|
|
59
|
+
const run = runToEnd({
|
|
60
|
+
name: "frontend-build",
|
|
61
|
+
program: process.execPath,
|
|
62
|
+
args: ["--import", PRELOAD, join(frontend, "dist", "index.js"), folder],
|
|
63
|
+
cwd: dir,
|
|
64
|
+
env: process.env,
|
|
65
|
+
logs,
|
|
66
|
+
});
|
|
67
|
+
const failure = await run.then(
|
|
68
|
+
() => null,
|
|
69
|
+
(/** @type {unknown} */ error) => error,
|
|
70
|
+
);
|
|
71
|
+
const output = treeOf(logs)?.["frontend-build.log"] ?? "";
|
|
72
|
+
rmSync(join(logs, "frontend-build.log"));
|
|
73
|
+
return { failure, output };
|
|
74
|
+
};
|
|
75
|
+
return { frontend, workspace, build };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @param {unknown} failure
|
|
80
|
+
* @returns {boolean}
|
|
81
|
+
*/
|
|
82
|
+
function refused(failure) {
|
|
83
|
+
return (
|
|
84
|
+
failure instanceof ExitError &&
|
|
85
|
+
failure.exitCode === PREPARED_WORKSPACE_REFUSED
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
await test("the build's preload makes it read the prepared workspace, and ends it with its own code when it cannot", async (t) => {
|
|
90
|
+
const plain = frontendPackage(t, "export");
|
|
91
|
+
write(join(plain.workspace, ".deps-hash"), "a\n");
|
|
92
|
+
const prepared = await plain.build(plain.workspace);
|
|
93
|
+
assert.equal(prepared.failure, null, prepared.output);
|
|
94
|
+
assert.deepEqual(
|
|
95
|
+
JSON.parse(prepared.output),
|
|
96
|
+
{ workspaceDir: plain.workspace, layers: [] },
|
|
97
|
+
"a preload that leaves the step alone builds sources copied after the key was read",
|
|
98
|
+
);
|
|
99
|
+
const unprepared = await plain.build(join(plain.workspace, "..", "other"));
|
|
100
|
+
assert.ok(
|
|
101
|
+
refused(unprepared.failure),
|
|
102
|
+
"a workspace that was not prepared fails the build as a broken build would",
|
|
103
|
+
);
|
|
104
|
+
const reExported = frontendPackage(t, "re-export");
|
|
105
|
+
write(join(reExported.workspace, ".deps-hash"), "a\n");
|
|
106
|
+
const accessor = await reExported.build(reExported.workspace);
|
|
107
|
+
assert.ok(
|
|
108
|
+
refused(accessor.failure),
|
|
109
|
+
"a step it cannot replace fails the build as a broken build would",
|
|
110
|
+
);
|
|
111
|
+
assert.match(accessor.output, /cannot be replaced/u);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
await test("a dms-frontend whose workspace step cannot be replaced fails the setup, before it sets anything up", async (t) => {
|
|
115
|
+
const plain = frontendPackage(t, "export");
|
|
116
|
+
await setUpFrontendWorkspace(plain.frontend, plain.workspace, "docs", false);
|
|
117
|
+
assert.ok(existsSync(plain.workspace));
|
|
118
|
+
const reExported = frontendPackage(t, "re-export");
|
|
119
|
+
await assert.rejects(
|
|
120
|
+
setUpFrontendWorkspace(
|
|
121
|
+
reExported.frontend,
|
|
122
|
+
reExported.workspace,
|
|
123
|
+
"docs",
|
|
124
|
+
false,
|
|
125
|
+
),
|
|
126
|
+
/cannot be replaced/u,
|
|
127
|
+
);
|
|
128
|
+
assert.equal(
|
|
129
|
+
existsSync(reExported.workspace),
|
|
130
|
+
false,
|
|
131
|
+
"a setup check that only reads the step lets up key a build it cannot run",
|
|
132
|
+
);
|
|
133
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets variables of `process.env` for one test, and restores them after it:
|
|
3
|
+
* a value is set, `undefined` removes the variable.
|
|
4
|
+
*
|
|
5
|
+
* @param {import("node:test").TestContext} t The test
|
|
6
|
+
* @param {Record<string, string | undefined>} values The variables
|
|
7
|
+
*/
|
|
8
|
+
export function setEnv(t, values) {
|
|
9
|
+
const previous = Object.fromEntries(
|
|
10
|
+
Object.keys(values).map((name) => [name, process.env[name]]),
|
|
11
|
+
);
|
|
12
|
+
/** @param {Record<string, string | undefined>} entries */
|
|
13
|
+
const apply = (entries) => {
|
|
14
|
+
for (const [name, value] of Object.entries(entries)) {
|
|
15
|
+
if (value === undefined) delete process.env[name];
|
|
16
|
+
else process.env[name] = value;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
t.after(() => apply(previous));
|
|
20
|
+
apply(values);
|
|
21
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The text of a file a step wrote into its evidence folder.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} folder The step folder
|
|
8
|
+
* @param {string} name The file
|
|
9
|
+
* @returns {string} Its text
|
|
10
|
+
*/
|
|
11
|
+
export function evidenceText(folder, name) {
|
|
12
|
+
return readFileSync(join(folder, name), "utf8");
|
|
13
|
+
}
|