mandrel 2.124.0 → 2.126.0
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/.agents/docs/agentrc-reference.json +1 -0
- package/.agents/docs/configuration.md +2 -0
- package/.agents/docs/runbooks/dispatch-board.md +199 -0
- package/.agents/docs/runbooks/dispatch.md +219 -0
- package/.agents/docs/runbooks/session-titles.md +52 -0
- package/.agents/schemas/agentrc.schema.json +60 -0
- package/.agents/scripts/acceptance-eval.js +11 -2
- package/.agents/scripts/dispatch.js +411 -0
- package/.agents/scripts/lib/child-detached.js +48 -0
- package/.agents/scripts/lib/child-exec.js +11 -0
- package/.agents/scripts/lib/config/delivery-routing.js +15 -12
- package/.agents/scripts/lib/config/temp-paths.js +26 -0
- package/.agents/scripts/lib/config-settings-schema-delivery.js +45 -0
- package/.agents/scripts/lib/dispatch/environments.js +135 -0
- package/.agents/scripts/lib/dispatch/launch-cloud.js +164 -0
- package/.agents/scripts/lib/dispatch/launch-local.js +77 -0
- package/.agents/scripts/lib/dispatch/liveness.js +21 -0
- package/.agents/scripts/lib/dispatch/record.js +54 -0
- package/.agents/scripts/lib/dispatch/status.js +329 -0
- package/.agents/scripts/lib/dispatch/targets.js +101 -0
- package/.agents/scripts/lib/dispatch/verdict.js +207 -0
- package/.agents/scripts/lib/generated/agentrc-validator.js +2 -2
- package/.agents/scripts/lib/observability/source-classifier.js +2 -0
- package/.agents/scripts/lib/orchestration/single-story-close/runner.js +2 -0
- package/.agents/scripts/lib/orchestration/story-handoff.js +6 -0
- package/.agents/scripts/lib/orchestration/story-stage-publish.js +147 -0
- package/.agents/scripts/lib/orchestration/ticketing/reads.js +2 -0
- package/.agents/scripts/lib/story-body/story-body.js +5 -2
- package/.agents/scripts/session-title.js +187 -0
- package/.agents/scripts/single-story-confirm-merge.js +7 -0
- package/.agents/scripts/single-story-init.js +3 -0
- package/.agents/workflows/helpers/audit-lens-core.md +4 -0
- package/.agents/workflows/helpers/deliver-light.md +4 -1
- package/.agents/workflows/helpers/qa-core.md +2 -0
- package/.agents/workflows/mandrel-deliver.md +7 -3
- package/.agents/workflows/mandrel-orchestrate.md +2 -0
- package/.agents/workflows/mandrel-plan.md +4 -0
- package/README.md +8 -0
- package/bin/mandrel.js +35 -13
- package/docs/CHANGELOG.md +27 -0
- package/lib/cli/dispatch.js +28 -0
- package/lib/cli/ui/accounts.js +81 -0
- package/lib/cli/ui/assignee-filter.js +91 -0
- package/lib/cli/ui/board-model.js +399 -0
- package/lib/cli/ui/board.css +578 -0
- package/lib/cli/ui/board.html +84 -0
- package/lib/cli/ui/board.js +755 -0
- package/lib/cli/ui/log-tail.js +89 -0
- package/lib/cli/ui/panel-model.js +100 -0
- package/lib/cli/ui/preferences.js +136 -0
- package/lib/cli/ui/request-guard.js +108 -0
- package/lib/cli/ui/server.js +230 -0
- package/lib/cli/ui/sources.js +258 -0
- package/lib/cli/ui/story-detail.js +99 -0
- package/lib/cli/ui.js +124 -0
- package/package.json +1 -1
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// lib/cli/ui/sources.js
|
|
2
|
+
/**
|
|
3
|
+
* What the `mandrel ui` server reads and runs, recomputed on every call:
|
|
4
|
+
* the dispatch status read model joined with each in-flight Story's
|
|
5
|
+
* published delivery stage, the dispatch action itself (run in process
|
|
6
|
+
* through `runDispatch`), and connection presence with the accounts in use.
|
|
7
|
+
* Nothing here returns or logs a credential value.
|
|
8
|
+
*
|
|
9
|
+
* @module cli/ui/sources
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
|
|
14
|
+
import { runDispatch } from '../../../.agents/scripts/dispatch.js';
|
|
15
|
+
import { resolveConfig } from '../../../.agents/scripts/lib/config-resolver.js';
|
|
16
|
+
import { resolveEnvironments } from '../../../.agents/scripts/lib/dispatch/environments.js';
|
|
17
|
+
import { processState } from '../../../.agents/scripts/lib/dispatch/liveness.js';
|
|
18
|
+
import { readRecord } from '../../../.agents/scripts/lib/dispatch/record.js';
|
|
19
|
+
import { DEFAULT_DONE_LIMIT } from '../../../.agents/scripts/lib/dispatch/status.js';
|
|
20
|
+
import { createProvider } from '../../../.agents/scripts/lib/provider-factory.js';
|
|
21
|
+
import { readJsonSafe } from '../doctor-checks/read-json.js';
|
|
22
|
+
import { spawn } from '../doctor-checks/spawn.js';
|
|
23
|
+
import { runGithubToken } from '../doctor-checks/toolchain.js';
|
|
24
|
+
import { createAccountProbe } from './accounts.js';
|
|
25
|
+
import { readStoryDetail, stageOf } from './story-detail.js';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A state read that failed, with a code safe to send to the page (`code`)
|
|
29
|
+
* and the cause for the operator's terminal only (`detail`).
|
|
30
|
+
*/
|
|
31
|
+
class StateError extends Error {
|
|
32
|
+
constructor(code, detail = code) {
|
|
33
|
+
super(detail);
|
|
34
|
+
this.code = code;
|
|
35
|
+
this.detail = detail;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The error for a `dispatch --status` run that exited non-zero. */
|
|
40
|
+
function statusFailure(code, envelope) {
|
|
41
|
+
return new StateError(
|
|
42
|
+
'state-unavailable',
|
|
43
|
+
envelope?.error ?? `dispatch --status exited ${code}`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Columns whose Stories are in flight and may carry a published stage. */
|
|
48
|
+
const IN_FLIGHT = new Set(['delivering', 'blocked']);
|
|
49
|
+
|
|
50
|
+
const ACTION_FLAGS = {
|
|
51
|
+
dispatch: [],
|
|
52
|
+
resume: ['--resume'],
|
|
53
|
+
stop: ['--stop'],
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const statusArgv = (doneLimit) => [
|
|
57
|
+
'--status',
|
|
58
|
+
'--json',
|
|
59
|
+
'--done-limit',
|
|
60
|
+
String(doneLimit),
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
/** Where an environment is defined: the local overlay, the committed file, or the implicit default. */
|
|
64
|
+
function definedIn(name, config, localOverlay) {
|
|
65
|
+
if (localOverlay?.delivery?.environments?.[name]) {
|
|
66
|
+
return '.agentrc.local.json';
|
|
67
|
+
}
|
|
68
|
+
return config?.delivery?.environments?.[name] ? '.agentrc.json' : 'default';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Why an environment cannot launch, or `null` when it can. */
|
|
72
|
+
function offlineReason(e, github, claude) {
|
|
73
|
+
if (!github.ok) return 'no GitHub token';
|
|
74
|
+
if (e.offlineReason) return 'cloud token not set';
|
|
75
|
+
if (e.command?.[0] === 'claude' && !claude.ok) return 'claude CLI not found';
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {{
|
|
81
|
+
* cwd?: string,
|
|
82
|
+
* env?: Record<string, string|undefined>,
|
|
83
|
+
* runner?: typeof spawn,
|
|
84
|
+
* dispatch?: typeof runDispatch,
|
|
85
|
+
* resolveConfigImpl?: typeof resolveConfig,
|
|
86
|
+
* createProviderImpl?: typeof createProvider,
|
|
87
|
+
* readJson?: (file: string) => object|null,
|
|
88
|
+
* readRecordImpl?: typeof readRecord,
|
|
89
|
+
* kill?: (pid: number, signal: 0) => void,
|
|
90
|
+
* now?: () => number,
|
|
91
|
+
* }} [deps]
|
|
92
|
+
*/
|
|
93
|
+
export function createSources({
|
|
94
|
+
cwd = process.cwd(),
|
|
95
|
+
env = process.env,
|
|
96
|
+
runner = spawn,
|
|
97
|
+
dispatch = runDispatch,
|
|
98
|
+
resolveConfigImpl = resolveConfig,
|
|
99
|
+
createProviderImpl = createProvider,
|
|
100
|
+
readJson = readJsonSafe,
|
|
101
|
+
readRecordImpl = readRecord,
|
|
102
|
+
kill = process.kill.bind(process),
|
|
103
|
+
now = Date.now,
|
|
104
|
+
} = {}) {
|
|
105
|
+
const config = () => resolveConfigImpl({ cwd });
|
|
106
|
+
const accounts = createAccountProbe({ env, runner, now });
|
|
107
|
+
const liveness = (record) => processState(record, { kill });
|
|
108
|
+
|
|
109
|
+
async function inFlight(story, provider, resolved) {
|
|
110
|
+
const stage = await stageOf(provider, story.id);
|
|
111
|
+
return {
|
|
112
|
+
...story,
|
|
113
|
+
stage: stage?.stage ?? null,
|
|
114
|
+
stageUpdatedAt: stage?.updatedAt ?? null,
|
|
115
|
+
process: liveness(readRecordImpl(story.id, { config: resolved })),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function invoke(argv, provider, resolved) {
|
|
120
|
+
let out = '';
|
|
121
|
+
const code = await dispatch(argv, {
|
|
122
|
+
config: resolved,
|
|
123
|
+
provider,
|
|
124
|
+
cwd,
|
|
125
|
+
write: (s) => {
|
|
126
|
+
out += s;
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
return { code, envelope: JSON.parse(out) };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Why this directory cannot back a board, or `null` when it can: the
|
|
134
|
+
* board reads one GitHub repository, named by `github.owner`/`repo`.
|
|
135
|
+
*
|
|
136
|
+
* @returns {{ code: string, detail: string }|null}
|
|
137
|
+
*/
|
|
138
|
+
function projectProblem() {
|
|
139
|
+
let resolved;
|
|
140
|
+
try {
|
|
141
|
+
resolved = config();
|
|
142
|
+
} catch (err) {
|
|
143
|
+
return { code: 'config-invalid', detail: err.message };
|
|
144
|
+
}
|
|
145
|
+
const { owner, repo } = resolved?.github ?? {};
|
|
146
|
+
if (owner && repo) return null;
|
|
147
|
+
return {
|
|
148
|
+
code: 'no-github-repo',
|
|
149
|
+
detail: `no GitHub repository configured in ${cwd}: run mandrel ui from a project root whose .agentrc.json sets github.owner and github.repo`,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function assertProject() {
|
|
154
|
+
const problem = projectProblem();
|
|
155
|
+
if (problem) throw new StateError(problem.code, problem.detail);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** @param {number} [doneLimit] One of `dispatch --done-limit`'s values. */
|
|
159
|
+
async function readState(doneLimit = DEFAULT_DONE_LIMIT) {
|
|
160
|
+
assertProject();
|
|
161
|
+
const resolved = config();
|
|
162
|
+
const provider = createProviderImpl(resolved);
|
|
163
|
+
const { code, envelope } = await invoke(
|
|
164
|
+
statusArgv(doneLimit),
|
|
165
|
+
provider,
|
|
166
|
+
resolved,
|
|
167
|
+
);
|
|
168
|
+
if (code !== 0) throw statusFailure(code, envelope);
|
|
169
|
+
const stories = await Promise.all(
|
|
170
|
+
envelope.stories.map((story) =>
|
|
171
|
+
IN_FLIGHT.has(story.column)
|
|
172
|
+
? inFlight(story, provider, resolved)
|
|
173
|
+
: story,
|
|
174
|
+
),
|
|
175
|
+
);
|
|
176
|
+
const { owner, repo } = resolved.github;
|
|
177
|
+
return { ...envelope, repository: `${owner}/${repo}`, stories };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** The detail for a Story on `state`'s board, or `null` when it is not there. */
|
|
181
|
+
function readStory(storyId, state) {
|
|
182
|
+
const row = state.stories.find((s) => s.id === storyId);
|
|
183
|
+
if (!row) return Promise.resolve(null);
|
|
184
|
+
const resolved = config();
|
|
185
|
+
const record = readRecordImpl(storyId, { config: resolved });
|
|
186
|
+
return readStoryDetail({
|
|
187
|
+
row,
|
|
188
|
+
record,
|
|
189
|
+
liveness: liveness(record),
|
|
190
|
+
provider: createProviderImpl(resolved),
|
|
191
|
+
config: resolved,
|
|
192
|
+
repository: state.repository,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function runAction({ action, storyId, environment }) {
|
|
197
|
+
const resolved = config();
|
|
198
|
+
const argv = [...ACTION_FLAGS[action], '--env', environment, `${storyId}`];
|
|
199
|
+
return invoke(argv, createProviderImpl(resolved), resolved);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function githubPresence() {
|
|
203
|
+
if (env.GITHUB_TOKEN || env.GH_TOKEN) {
|
|
204
|
+
return { ok: true, source: 'environment' };
|
|
205
|
+
}
|
|
206
|
+
const result = await runGithubToken({ env, runner });
|
|
207
|
+
return result.ok ? { ok: true, source: 'gh' } : { ok: false, source: null };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function readConnections() {
|
|
211
|
+
const resolved = config();
|
|
212
|
+
const [presence, probe, account] = await Promise.all([
|
|
213
|
+
githubPresence(),
|
|
214
|
+
runner('claude', ['--version'], {
|
|
215
|
+
shell: process.platform === 'win32',
|
|
216
|
+
timeout: 10_000,
|
|
217
|
+
}),
|
|
218
|
+
accounts(),
|
|
219
|
+
]);
|
|
220
|
+
const github = { ...presence, ...account.github };
|
|
221
|
+
const claude = { ok: !probe.error && probe.status === 0 };
|
|
222
|
+
const overlay = readJson(path.join(cwd, '.agentrc.local.json'));
|
|
223
|
+
const environments = resolveEnvironments(resolved, env).map((e) => {
|
|
224
|
+
const reason = offlineReason(e, github, claude);
|
|
225
|
+
return {
|
|
226
|
+
name: e.name,
|
|
227
|
+
target: e.target,
|
|
228
|
+
online: reason === null,
|
|
229
|
+
reason,
|
|
230
|
+
definedIn: definedIn(e.name, resolved, overlay),
|
|
231
|
+
routine: e.routine ?? null,
|
|
232
|
+
};
|
|
233
|
+
});
|
|
234
|
+
return {
|
|
235
|
+
github,
|
|
236
|
+
claude: { ...claude, account: account.claude },
|
|
237
|
+
environments,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
projectProblem,
|
|
243
|
+
readState,
|
|
244
|
+
readStory,
|
|
245
|
+
runAction,
|
|
246
|
+
readConnections,
|
|
247
|
+
environmentNames: () => resolveEnvironments(config()).map((e) => e.name),
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** The board's sources, or `null` after saying why this project cannot back one. */
|
|
252
|
+
export function projectSources(sources = createSources(), errOut, exit) {
|
|
253
|
+
const problem = sources.projectProblem();
|
|
254
|
+
if (!problem) return sources;
|
|
255
|
+
errOut(`mandrel ui: ${problem.detail}\n`);
|
|
256
|
+
exit(1);
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// lib/cli/ui/story-detail.js
|
|
2
|
+
/**
|
|
3
|
+
* The `GET /api/story` detail: the id check, a Story's latest published
|
|
4
|
+
* stage, and the response built from a board row, its dispatch record, its
|
|
5
|
+
* stage record and its log tail. The record is copied field by field, so
|
|
6
|
+
* its `logPath` never leaves the server.
|
|
7
|
+
*
|
|
8
|
+
* @module cli/ui/story-detail
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
|
|
13
|
+
import { dispatchLogPath } from '../../../.agents/scripts/lib/config/temp-paths.js';
|
|
14
|
+
import { parseDeliveryStage } from '../../../.agents/scripts/lib/orchestration/story-stage-publish.js';
|
|
15
|
+
import { readLogTail } from './log-tail.js';
|
|
16
|
+
|
|
17
|
+
const STORY_ID_RE = /^[1-9][0-9]{0,9}$/;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The `id` query parameter as a number, or `null` when malformed.
|
|
21
|
+
*
|
|
22
|
+
* @param {string} url The request URL, path and query.
|
|
23
|
+
* @returns {number|null}
|
|
24
|
+
*/
|
|
25
|
+
export function storyIdFrom(url) {
|
|
26
|
+
const id = new URL(url, 'http://localhost').searchParams.get('id');
|
|
27
|
+
return id !== null && STORY_ID_RE.test(id) ? Number(id) : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The latest published stage record on a Story, or `null`; a read failure is `null`. */
|
|
31
|
+
export async function stageOf(provider, storyId) {
|
|
32
|
+
try {
|
|
33
|
+
const comments = (await provider.getTicketComments(storyId)) ?? [];
|
|
34
|
+
for (const comment of [...comments].reverse()) {
|
|
35
|
+
const record = parseDeliveryStage(comment.body);
|
|
36
|
+
if (record) return record;
|
|
37
|
+
}
|
|
38
|
+
} catch {
|
|
39
|
+
// Advisory only: a Story whose comments cannot be read shows no stage.
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function recordFields(record) {
|
|
45
|
+
if (!record) return null;
|
|
46
|
+
return {
|
|
47
|
+
pid: record.pid ?? null,
|
|
48
|
+
startedAt: record.startedAt ?? null,
|
|
49
|
+
argv: Array.isArray(record.argv) ? record.argv : null,
|
|
50
|
+
sessionId: record.sessionId ?? null,
|
|
51
|
+
sessionUrl: record.sessionUrl ?? null,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** `claude --resume <id>` for a local session; a routine-fired cloud session has none. */
|
|
56
|
+
function resumeCommandOf(record) {
|
|
57
|
+
const sessionId = record?.sessionUrl ? null : record?.sessionId;
|
|
58
|
+
return sessionId ? `claude --resume ${sessionId}` : null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The row's labels, assignees and Epic, as the detail metadata shows them. */
|
|
62
|
+
const ticketMeta = (row) => ({
|
|
63
|
+
labels: row.labels ?? [],
|
|
64
|
+
assignees: row.assignees ?? [],
|
|
65
|
+
epic: row.epic ?? null,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @param {{ row: object, record: object|null, liveness: object,
|
|
70
|
+
* provider: object, config: object, repository: string|null }} parts
|
|
71
|
+
*/
|
|
72
|
+
export async function readStoryDetail({
|
|
73
|
+
row,
|
|
74
|
+
record,
|
|
75
|
+
liveness,
|
|
76
|
+
provider,
|
|
77
|
+
config,
|
|
78
|
+
repository,
|
|
79
|
+
}) {
|
|
80
|
+
const stage = await stageOf(provider, row.id);
|
|
81
|
+
const dir = path.dirname(dispatchLogPath(row.id, config));
|
|
82
|
+
return {
|
|
83
|
+
id: row.id,
|
|
84
|
+
title: row.title ?? '',
|
|
85
|
+
column: row.column,
|
|
86
|
+
environment: row.environment ?? null,
|
|
87
|
+
...ticketMeta(row),
|
|
88
|
+
issueUrl: repository
|
|
89
|
+
? `https://github.com/${repository}/issues/${row.id}`
|
|
90
|
+
: null,
|
|
91
|
+
pr: row.pr ?? null,
|
|
92
|
+
stage: stage?.stage ?? null,
|
|
93
|
+
history: stage?.history ?? [],
|
|
94
|
+
process: liveness,
|
|
95
|
+
record: recordFields(record),
|
|
96
|
+
resumeCommand: resumeCommandOf(record),
|
|
97
|
+
logTail: readLogTail(record?.logPath, { dir }),
|
|
98
|
+
};
|
|
99
|
+
}
|
package/lib/cli/ui.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// lib/cli/ui.js
|
|
2
|
+
/**
|
|
3
|
+
* `mandrel ui` subcommand: start the local dispatch board on 127.0.0.1,
|
|
4
|
+
* print its URL, and run until interrupted. It refuses to start where no
|
|
5
|
+
* GitHub repository is configured, since every board read needs one. The server stores nothing; a
|
|
6
|
+
* random per-process token, embedded in the served page, guards the API.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* mandrel ui # http://127.0.0.1:4319/
|
|
10
|
+
* mandrel ui --port 5000
|
|
11
|
+
*
|
|
12
|
+
* Injectable seams (used by tests): `write`, `errOut`, `exit`, `token`,
|
|
13
|
+
* `sources` and `createServer`.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { randomBytes } from 'node:crypto';
|
|
17
|
+
|
|
18
|
+
import { createUiServer } from './ui/server.js';
|
|
19
|
+
import { projectSources } from './ui/sources.js';
|
|
20
|
+
|
|
21
|
+
export const DEFAULT_PORT = 4319;
|
|
22
|
+
|
|
23
|
+
const UI_USAGE = `Usage: mandrel ui [--port <n>]
|
|
24
|
+
|
|
25
|
+
Serves the local dispatch board on http://127.0.0.1:<port>/ (default ${DEFAULT_PORT})
|
|
26
|
+
until interrupted. Bound to 127.0.0.1 only; see
|
|
27
|
+
.agents/docs/runbooks/dispatch-board.md.
|
|
28
|
+
`;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {string[]} argv
|
|
32
|
+
* @returns {{ port: number, help: boolean }}
|
|
33
|
+
*/
|
|
34
|
+
export function parseUiArgs(argv) {
|
|
35
|
+
let port = DEFAULT_PORT;
|
|
36
|
+
let help = false;
|
|
37
|
+
for (let i = 0; i < argv.length; i++) {
|
|
38
|
+
const arg = argv[i];
|
|
39
|
+
if (arg === '--help' || arg === '-h') help = true;
|
|
40
|
+
else if (arg === '--port') port = parsePort(argv[++i]);
|
|
41
|
+
else if (arg.startsWith('--port=')) port = parsePort(arg.slice(7));
|
|
42
|
+
else throw new Error(`unexpected argument '${arg}'`);
|
|
43
|
+
}
|
|
44
|
+
return { port, help };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function parsePort(value) {
|
|
48
|
+
if (!/^\d{1,5}$/.test(value ?? '') || Number(value) > 65535) {
|
|
49
|
+
throw new Error('--port needs an integer from 0 to 65535');
|
|
50
|
+
}
|
|
51
|
+
return Number(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {string[]} argv
|
|
56
|
+
* @param {{
|
|
57
|
+
* write?: (s: string) => void,
|
|
58
|
+
* errOut?: (s: string) => void,
|
|
59
|
+
* exit?: (code: number) => void,
|
|
60
|
+
* token?: string,
|
|
61
|
+
* sources?: ReturnType<typeof import('./ui/sources.js').createSources>,
|
|
62
|
+
* createServer?: typeof createUiServer,
|
|
63
|
+
* }} [deps]
|
|
64
|
+
* @returns {Promise<import('node:http').Server|null>} The listening server.
|
|
65
|
+
*/
|
|
66
|
+
export async function runUi(
|
|
67
|
+
argv = [],
|
|
68
|
+
{
|
|
69
|
+
write = process.stdout.write.bind(process.stdout),
|
|
70
|
+
errOut = process.stderr.write.bind(process.stderr),
|
|
71
|
+
exit = process.exit.bind(process),
|
|
72
|
+
token = randomBytes(32).toString('hex'),
|
|
73
|
+
sources,
|
|
74
|
+
createServer = createUiServer,
|
|
75
|
+
} = {},
|
|
76
|
+
) {
|
|
77
|
+
let args;
|
|
78
|
+
try {
|
|
79
|
+
args = parseUiArgs(argv);
|
|
80
|
+
} catch (err) {
|
|
81
|
+
errOut(`mandrel ui: ${err.message}\n`);
|
|
82
|
+
exit(1);
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
if (args.help) {
|
|
86
|
+
write(UI_USAGE);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
const io = { write, errOut, exit };
|
|
90
|
+
return serveBoard(args.port, { sources, token, createServer }, io);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Start the board where this project can back one; `null` after saying why not. */
|
|
94
|
+
function serveBoard(port, { sources, token, createServer }, io) {
|
|
95
|
+
const boardSources = projectSources(sources, io.errOut, io.exit);
|
|
96
|
+
if (!boardSources) return null;
|
|
97
|
+
return listenOn(createServer({ token, sources: boardSources }), port, io);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Listen on 127.0.0.1 and print the URL; resolves `null` when the port is taken. */
|
|
101
|
+
function listenOn(server, port, { write, errOut, exit }) {
|
|
102
|
+
return new Promise((resolve) => {
|
|
103
|
+
server.once('error', (err) => {
|
|
104
|
+
errOut(`mandrel ui: cannot listen on port ${port}: ${err.code}\n`);
|
|
105
|
+
exit(1);
|
|
106
|
+
resolve(null);
|
|
107
|
+
});
|
|
108
|
+
server.listen(port, '127.0.0.1', () => {
|
|
109
|
+
write(`mandrel ui: http://127.0.0.1:${server.address().port}/\n`);
|
|
110
|
+
resolve(server);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Default export consumed by `bin/mandrel.js`.
|
|
117
|
+
*
|
|
118
|
+
* @param {string[]} argv
|
|
119
|
+
* @param {Parameters<typeof runUi>[1]} [deps]
|
|
120
|
+
* @returns {Promise<void>}
|
|
121
|
+
*/
|
|
122
|
+
export default async function run(argv, deps) {
|
|
123
|
+
await runUi(argv, deps);
|
|
124
|
+
}
|
package/package.json
CHANGED