@yolo-labs/yolo-cli 0.42.1 → 0.44.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/README.md +51 -42
- package/dist/artifact-get.js +2 -9
- package/dist/cli.js +24 -126
- package/dist/deploy-cli.js +119 -9
- package/dist/deploy-client.js +10 -20
- package/dist/deploy-config.js +82 -4
- package/dist/exit.js +34 -0
- package/dist/kanban-cli.js +352 -0
- package/dist/preview.js +145 -0
- package/dist/tileapp-oci-assembler.js +2 -2
- package/dist/tileapp-validator.js +6 -0
- package/dist/work-client.js +11 -60
- package/package.json +4 -7
- package/dist/canonicalizer.js +0 -208
- package/dist/lockfile.js +0 -203
- package/dist/plan-frontmatter.schema.json +0 -191
- package/dist/plan-validate.js +0 -270
- package/dist/revision.js +0 -43
- package/dist/run-list.js +0 -148
- package/dist/webapp-url.js +0 -61
package/dist/deploy-client.js
CHANGED
|
@@ -93,21 +93,11 @@ export async function uploadAssetBucket(ctx, projectId, shipId, files) {
|
|
|
93
93
|
* | 410 upload-expired (ship session lapsed → rerun)
|
|
94
94
|
*/
|
|
95
95
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* forward them and the server stamps release PROVENANCE — letting an auditor
|
|
99
|
-
* walk a live URL back to the run that shipped it. These are CLIENT-ASSERTED
|
|
100
|
-
* over the user JWT (NOT cryptographically bound like the MCP token path), so
|
|
101
|
-
* the server records them as `source: 'cli-env'`: unverified audit metadata,
|
|
102
|
-
* never authorizing. Omitted entirely for an interactive `yolo deploy` run
|
|
103
|
-
* outside a lane (none of the vars set).
|
|
96
|
+
* Caller-reported session provenance for hosting audit metadata.
|
|
97
|
+
* This header never authorizes a deployment or rollback.
|
|
104
98
|
*/
|
|
105
|
-
export function
|
|
99
|
+
export function sessionContextHeaders(env) {
|
|
106
100
|
const headers = {};
|
|
107
|
-
if (env.YOLO_RUN_PLAN_ID)
|
|
108
|
-
headers['X-Yolo-Run-Plan-Id'] = env.YOLO_RUN_PLAN_ID;
|
|
109
|
-
if (env.YOLO_RUN_STEP_RUN_ID)
|
|
110
|
-
headers['X-Yolo-Run-Step-Run-Id'] = env.YOLO_RUN_STEP_RUN_ID;
|
|
111
101
|
if (env.SESSION_ID)
|
|
112
102
|
headers['X-Yolo-Session-Id'] = env.SESSION_ID;
|
|
113
103
|
return headers;
|
|
@@ -127,14 +117,14 @@ export async function finalizeShip(ctx, projectId, shipId, modules = [], sourceM
|
|
|
127
117
|
if (!fetchImpl)
|
|
128
118
|
return noFetchFailure();
|
|
129
119
|
const url = `${stripTrailingSlash(ctx.commonApiUrl)}/v1/deploy/projects/${enc(projectId)}/ship/${enc(shipId)}/finalize`;
|
|
130
|
-
//
|
|
131
|
-
const
|
|
120
|
+
// Optional session provenance for the hosting audit record.
|
|
121
|
+
const requestHeaders = sessionContextHeaders(ctx.env);
|
|
132
122
|
// Resume-after-grant: the pending-store rerun redeems the operator's grant
|
|
133
123
|
// explicitly (x-approval-id → approvalNonce server-side), which yields crisp
|
|
134
124
|
// approval-pending/-denied/-expired outcomes instead of the nonce-less
|
|
135
125
|
// retry-redeem's blended ones.
|
|
136
126
|
if (opts.approvalId)
|
|
137
|
-
|
|
127
|
+
requestHeaders['x-approval-id'] = opts.approvalId;
|
|
138
128
|
let response;
|
|
139
129
|
try {
|
|
140
130
|
if (modules.length > 0) {
|
|
@@ -150,14 +140,14 @@ export async function finalizeShip(ctx, projectId, shipId, modules = [], sourceM
|
|
|
150
140
|
// No explicit Content-Type — fetch sets the multipart boundary itself.
|
|
151
141
|
response = await fetchImpl(url, {
|
|
152
142
|
method: 'POST',
|
|
153
|
-
headers: { Authorization: `Bearer ${token}`, ...
|
|
143
|
+
headers: { Authorization: `Bearer ${token}`, ...requestHeaders },
|
|
154
144
|
body: form,
|
|
155
145
|
});
|
|
156
146
|
}
|
|
157
147
|
else {
|
|
158
148
|
response = await fetchImpl(url, {
|
|
159
149
|
method: 'POST',
|
|
160
|
-
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...
|
|
150
|
+
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', ...requestHeaders },
|
|
161
151
|
body: JSON.stringify({}),
|
|
162
152
|
});
|
|
163
153
|
}
|
|
@@ -238,12 +228,12 @@ export async function listProjects(ctx) {
|
|
|
238
228
|
}
|
|
239
229
|
/** POST /v1/deploy/projects/:id/rollback */
|
|
240
230
|
export async function rollbackProject(ctx, projectId, request = {}) {
|
|
241
|
-
// Rollback is a prod re-activation — forward
|
|
231
|
+
// Rollback is a prod re-activation — forward session context so the server can
|
|
242
232
|
// stamp `rollback.provenance` (same audit basis as a ship's finalize leg).
|
|
243
233
|
return jsonLeg(ctx, `/deploy/projects/${enc(projectId)}/rollback`, {
|
|
244
234
|
method: 'POST',
|
|
245
235
|
jsonBody: request,
|
|
246
|
-
headers:
|
|
236
|
+
headers: sessionContextHeaders(ctx.env),
|
|
247
237
|
});
|
|
248
238
|
}
|
|
249
239
|
/** POST /v1/deploy/projects/:id/unpublish — take a live project offline (reversible). */
|
package/dist/deploy-config.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* is the session JWT; `projectId` is non-secret and ownership-checked
|
|
10
10
|
* server-side on every call.
|
|
11
11
|
*
|
|
12
|
-
* Canonical write shape
|
|
12
|
+
* Canonical write shape:
|
|
13
13
|
* - JSON, 2-space indent, single trailing `\n`.
|
|
14
14
|
* - Fixed key order per the spec example: $version, projectId, slug,
|
|
15
15
|
* type, build{command,outputDir}, worker{entry,assetsDir},
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
* (e.g. `compatibilityDate`, which the platform fixes and does not read)
|
|
27
27
|
* surfaces in `yolo deploy validate` instead of misleading the author.
|
|
28
28
|
*/
|
|
29
|
-
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
29
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
30
30
|
import path from 'node:path';
|
|
31
31
|
export class DeployConfigError extends Error {
|
|
32
32
|
code;
|
|
@@ -43,6 +43,85 @@ export class DeployConfigError extends Error {
|
|
|
43
43
|
export function deployConfigPath(cwd) {
|
|
44
44
|
return path.join(cwd, '.yolo', 'deploy.json');
|
|
45
45
|
}
|
|
46
|
+
/** How far up to look before giving up. A backstop against a pathological tree. */
|
|
47
|
+
const ANCESTOR_WALK_MAX_DEPTH = 24;
|
|
48
|
+
/**
|
|
49
|
+
* Walk UP from `cwd` looking for an ancestor already linked to a hosting project.
|
|
50
|
+
*
|
|
51
|
+
* WHY: `yolo deploy init` inside `apps/api` mints a fully independent project
|
|
52
|
+
* named after the directory — which is exactly how `El Paso Ballroom API` came
|
|
53
|
+
* to exist as an unrelated top-level card next to the site it serves. The
|
|
54
|
+
* relationship already exists in the filesystem; nothing recorded it. This finds
|
|
55
|
+
* it so `init` can offer it (nesting plan §10 S4).
|
|
56
|
+
*
|
|
57
|
+
* 🚨 THE STOP CONDITIONS ARE THE DESIGN. An unbounded walk is actively harmful:
|
|
58
|
+
*
|
|
59
|
+
* - **Never `cwd` itself.** That is the already-linked case, which `init`
|
|
60
|
+
* handles separately; treating it as an ancestor would make every project
|
|
61
|
+
* its own parent.
|
|
62
|
+
* - **Stop at a `.git` directory.** That is the repo boundary and therefore
|
|
63
|
+
* the monorepo boundary. The repo root is CHECKED first and only then
|
|
64
|
+
* becomes a stopping point, so a deployed monorepo root is still found.
|
|
65
|
+
* - **Stop BEFORE `$HOME`, and never inspect it.** Someone who once ran
|
|
66
|
+
* `yolo deploy init` in their home directory would otherwise have every
|
|
67
|
+
* future project on the machine silently adopted by that one — a
|
|
68
|
+
* machine-wide capture from a single stray file.
|
|
69
|
+
* - **Depth cap** as a backstop, and the filesystem root always terminates.
|
|
70
|
+
*
|
|
71
|
+
* Pure and injectable so the walk is testable without touching a real tree.
|
|
72
|
+
*/
|
|
73
|
+
export function findAncestorDeployLink(cwd, readFileImpl = defaultReadFile, opts = {}) {
|
|
74
|
+
const exists = opts.existsImpl ?? ((p) => existsSync(p));
|
|
75
|
+
const maxDepth = opts.maxDepth ?? ANCESTOR_WALK_MAX_DEPTH;
|
|
76
|
+
const home = opts.home ? path.resolve(opts.home) : undefined;
|
|
77
|
+
const start = path.resolve(cwd);
|
|
78
|
+
// The repo boundary applies to `cwd` ITSELF (codex P2). Running `init` at the
|
|
79
|
+
// root of a nested repository would otherwise begin the walk one level ABOVE
|
|
80
|
+
// it and adopt a parent from the enclosing tree — crossing exactly the
|
|
81
|
+
// boundary the ancestor-level check below exists to hold.
|
|
82
|
+
if (exists(path.join(start, '.git')))
|
|
83
|
+
return null;
|
|
84
|
+
let dir = path.dirname(start);
|
|
85
|
+
for (let depth = 0; depth < maxDepth; depth += 1) {
|
|
86
|
+
// Home is a hard FLOOR: reaching it ends the walk, and it is never
|
|
87
|
+
// inspected. Walking upward from anywhere inside home always arrives here
|
|
88
|
+
// before `/`, so this one check covers the whole case — an earlier version
|
|
89
|
+
// tried to test "is dir inside home" and had the sense inverted, which
|
|
90
|
+
// refused every directory UNDER home and quietly disabled the feature for
|
|
91
|
+
// every user whose repos live there (i.e. almost all of them).
|
|
92
|
+
if (home && dir === home)
|
|
93
|
+
return null;
|
|
94
|
+
const filePath = deployConfigPath(dir);
|
|
95
|
+
const text = readFileImpl(filePath);
|
|
96
|
+
if (text !== undefined && text.trim() !== '') {
|
|
97
|
+
try {
|
|
98
|
+
const parsed = JSON.parse(text);
|
|
99
|
+
if (typeof parsed.projectId === 'string' && parsed.projectId.length > 0) {
|
|
100
|
+
return {
|
|
101
|
+
projectId: parsed.projectId,
|
|
102
|
+
...(typeof parsed.slug === 'string' && parsed.slug ? { slug: parsed.slug } : {}),
|
|
103
|
+
dir,
|
|
104
|
+
path: filePath,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
// A malformed ancestor link is not this command's problem to report —
|
|
110
|
+
// it belongs to that directory, and failing `init` here would block a
|
|
111
|
+
// child on a file the user may not even know exists. Keep walking.
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// Repo boundary. Checked AFTER the link above so a deployed monorepo root
|
|
115
|
+
// still counts as an ancestor.
|
|
116
|
+
if (exists(path.join(dir, '.git')))
|
|
117
|
+
return null;
|
|
118
|
+
const parent = path.dirname(dir);
|
|
119
|
+
if (parent === dir)
|
|
120
|
+
return null; // filesystem root
|
|
121
|
+
dir = parent;
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
46
125
|
/**
|
|
47
126
|
* Read `.yolo/deploy.json` from a project root. Absent file is the
|
|
48
127
|
* non-error `{ ok: true, config: null }` case (detection falls through
|
|
@@ -226,8 +305,7 @@ function collectUnknownKeyWarnings(obj) {
|
|
|
226
305
|
/**
|
|
227
306
|
* Reorder to the spec's fixed key order, omitting absent optionals.
|
|
228
307
|
* Binding entries keep `kind`, `binding` first; extra hint keys follow
|
|
229
|
-
*
|
|
230
|
-
* free-form maps). Pure — does not mutate input.
|
|
308
|
+
* in lexicographic order. Pure — does not mutate input.
|
|
231
309
|
*/
|
|
232
310
|
function canonicalize(config) {
|
|
233
311
|
const out = { $version: 1 };
|
package/dist/exit.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exit only after stdout and stderr have flushed.
|
|
3
|
+
*
|
|
4
|
+
* When stdout is a pipe (`yolo kanban export <tile> | jq`), Node writes to it
|
|
5
|
+
* asynchronously, and `process.exit()` drops whatever the kernel pipe buffer
|
|
6
|
+
* (64 KiB on Linux) has not accepted yet: a large export arrived as exactly
|
|
7
|
+
* 65,536 bytes of truncated JSON, with exit status 0. A file or TTY is written
|
|
8
|
+
* synchronously, which is why `-o <file>` was unaffected.
|
|
9
|
+
*
|
|
10
|
+
* A zero-length write's callback runs after every earlier write on that
|
|
11
|
+
* stream has been handed to the OS, so exiting from both callbacks loses
|
|
12
|
+
* nothing. The explicit exit is kept (rather than just setting exitCode)
|
|
13
|
+
* because some commands can leave handles open that would otherwise keep the
|
|
14
|
+
* process alive.
|
|
15
|
+
*/
|
|
16
|
+
export function exitAfterFlush(code, streams = [process.stdout, process.stderr], exit = (c) => process.exit(c)) {
|
|
17
|
+
process.exitCode = code;
|
|
18
|
+
let pending = streams.length;
|
|
19
|
+
if (pending === 0) {
|
|
20
|
+
exit(code);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const settle = () => { if (--pending === 0)
|
|
24
|
+
exit(code); };
|
|
25
|
+
for (const stream of streams) {
|
|
26
|
+
// A destroyed or errored stream (e.g. the reader closed the pipe) never
|
|
27
|
+
// calls back; treat it as flushed rather than hanging the process.
|
|
28
|
+
if (stream.destroyed || !stream.writable) {
|
|
29
|
+
settle();
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
stream.write('', settle);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `yolo kanban export` / `yolo kanban import` — move a board seed document
|
|
3
|
+
* between a workspace and a file, with no model in the loop.
|
|
4
|
+
*
|
|
5
|
+
* A board lives in Mongo, scoped to a workspace that can wedge. The seed
|
|
6
|
+
* document (`common-api/src/services/kanban-board-seed.ts`) is the only copy
|
|
7
|
+
* of a card graph that survives the workspace it was built in, and the card
|
|
8
|
+
* descriptions it carries are verbatim dispatch contracts — thousands of
|
|
9
|
+
* bytes of them. Re-emitting those by hand through a chat transcript is how a
|
|
10
|
+
* recovery silently loses a paragraph. These two verbs make the path
|
|
11
|
+
* mechanical: bytes off the wire onto disk, bytes off disk onto the wire.
|
|
12
|
+
*
|
|
13
|
+
* Two properties this module deliberately does NOT have:
|
|
14
|
+
*
|
|
15
|
+
* - **It does not know the seed format.** The format has exactly one owner,
|
|
16
|
+
* server-side. `export` writes the response body unmodified; `import`
|
|
17
|
+
* sends the file unparsed apart from a `JSON.parse` that catches a
|
|
18
|
+
* truncated file before it becomes an HTTP 400. A serializer here and a
|
|
19
|
+
* parser there would drift, and the drift would only surface during a
|
|
20
|
+
* recovery — the worst possible moment.
|
|
21
|
+
* - **It has no MCP surface and needs no new scope.** These are the
|
|
22
|
+
* user-token REST routes in `common-api/src/routes/kanban.ts`, which is
|
|
23
|
+
* what makes them operator-only — the same policy board create/delete
|
|
24
|
+
* already follow (`vibe-mcp-client.ts`). So this goes through
|
|
25
|
+
* `userRouteRequest` (the USER-token helper), not `authenticatedRequest`,
|
|
26
|
+
* and never mints a delegated token.
|
|
27
|
+
*
|
|
28
|
+
* Exit codes (as `artifact-list.ts` documents them):
|
|
29
|
+
* - 0 = success
|
|
30
|
+
* - 1 = http (server error, including a 409 key collision)
|
|
31
|
+
* - 64 = usage (missing env, unreadable file, refusing to overwrite,
|
|
32
|
+
* `--workspace` mismatch)
|
|
33
|
+
*/
|
|
34
|
+
import * as fs from 'node:fs';
|
|
35
|
+
import { userRouteRequest } from './work-client.js';
|
|
36
|
+
import { resolveSubstrateContext } from './auth-context.js';
|
|
37
|
+
// ─── export ───────────────────────────────────────────────────────────────
|
|
38
|
+
export async function runKanbanExport(options) {
|
|
39
|
+
const env = options.env ?? process.env;
|
|
40
|
+
if (!options.tileId)
|
|
41
|
+
return fail('usage', 'a tileId is required');
|
|
42
|
+
const resolved = resolveContext(env, options.workspaceFlag);
|
|
43
|
+
if (!resolved.ok)
|
|
44
|
+
return resolved.failure;
|
|
45
|
+
const { commonApiUrl, userToken, workspaceId } = resolved;
|
|
46
|
+
// Refuse an occupied destination BEFORE the request, not after. An export
|
|
47
|
+
// is reached for when a workspace is already in trouble; spending the round
|
|
48
|
+
// trip only to discard the one readable copy of the board would be the
|
|
49
|
+
// wrong half of the operation to get right.
|
|
50
|
+
if (options.outFile && fs.existsSync(options.outFile)) {
|
|
51
|
+
return fail('usage', `refusing to overwrite existing file '${options.outFile}'; pick another path or remove it first`);
|
|
52
|
+
}
|
|
53
|
+
const path = `/workspaces/${encodeURIComponent(workspaceId)}/kanban/boards/${encodeURIComponent(options.tileId)}/seed`;
|
|
54
|
+
let response;
|
|
55
|
+
try {
|
|
56
|
+
response = await userRouteRequest({ commonApiUrl, userToken, fetchImpl: options.fetchImpl }, path, {
|
|
57
|
+
method: 'GET',
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
return fail('http', `kanban.board.seed.export failed: ${describeError(err)}`);
|
|
62
|
+
}
|
|
63
|
+
const body = await safeReadText(response);
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
return fail('http', `kanban.board.seed.export failed: HTTP ${response.status} — ${body}`, {
|
|
66
|
+
status: response.status,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (!options.outFile) {
|
|
70
|
+
return { ok: true, output: body, raw: true, workspaceId };
|
|
71
|
+
}
|
|
72
|
+
const write = options.writeFileImpl ?? defaultWriteFileExclusive;
|
|
73
|
+
try {
|
|
74
|
+
write(options.outFile, body);
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
if (isEexist(err)) {
|
|
78
|
+
return fail('usage', `refusing to overwrite existing file '${options.outFile}'; pick another path or remove it first`);
|
|
79
|
+
}
|
|
80
|
+
return fail('usage', `could not write '${options.outFile}': ${describeError(err)}`);
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
ok: true,
|
|
84
|
+
output: `Wrote ${Buffer.byteLength(body, 'utf-8')} bytes to ${options.outFile}`,
|
|
85
|
+
raw: false,
|
|
86
|
+
workspaceId,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
// ─── import ───────────────────────────────────────────────────────────────
|
|
90
|
+
export async function runKanbanImport(options) {
|
|
91
|
+
const env = options.env ?? process.env;
|
|
92
|
+
if (!options.boardId)
|
|
93
|
+
return fail('usage', 'a boardId is required');
|
|
94
|
+
if (!options.file)
|
|
95
|
+
return fail('usage', 'a seed document path is required');
|
|
96
|
+
const resolved = resolveContext(env, options.workspaceFlag);
|
|
97
|
+
if (!resolved.ok)
|
|
98
|
+
return resolved.failure;
|
|
99
|
+
const { commonApiUrl, userToken, workspaceId } = resolved;
|
|
100
|
+
const read = options.readFileImpl ?? defaultReadFile;
|
|
101
|
+
let contents;
|
|
102
|
+
try {
|
|
103
|
+
contents = read(options.file);
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
return fail('usage', `could not read '${options.file}': ${describeError(err)}`);
|
|
107
|
+
}
|
|
108
|
+
// The ONLY inspection of the document on this side. It catches a truncated
|
|
109
|
+
// or half-pasted file locally, where the message can name the file. Every
|
|
110
|
+
// other rule about what a seed means — keys, dependsOn, columns, cycles —
|
|
111
|
+
// belongs to the server, which reports all of them at once.
|
|
112
|
+
try {
|
|
113
|
+
JSON.parse(contents);
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
return fail('usage', `'${options.file}' is not valid JSON: ${describeError(err)}`);
|
|
117
|
+
}
|
|
118
|
+
const path = `/workspaces/${encodeURIComponent(workspaceId)}/kanban/boards/${encodeURIComponent(options.boardId)}/seed`;
|
|
119
|
+
let response;
|
|
120
|
+
try {
|
|
121
|
+
response = await userRouteRequest({ commonApiUrl, userToken, fetchImpl: options.fetchImpl }, path, {
|
|
122
|
+
method: 'POST',
|
|
123
|
+
// Verbatim: what the server validates is the file on disk, not a
|
|
124
|
+
// re-serialization of it.
|
|
125
|
+
rawBody: contents,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
return fail('http', `kanban.board.seed.import failed: ${describeError(err)}`);
|
|
130
|
+
}
|
|
131
|
+
const body = await safeReadText(response);
|
|
132
|
+
if (!response.ok) {
|
|
133
|
+
// A 409 names every colliding key. Pass the server's words through
|
|
134
|
+
// unedited — an operator paraphrased at this point has to bisect the
|
|
135
|
+
// document by hand to find out which card is already there.
|
|
136
|
+
return fail('http', `kanban.board.seed.import failed: HTTP ${response.status} — ${body}`, {
|
|
137
|
+
status: response.status,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
ok: true,
|
|
142
|
+
output: formatImportSummary(body, options.boardId),
|
|
143
|
+
raw: false,
|
|
144
|
+
workspaceId,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* `key → cardId`, one per line, under a count. That map is what a caller
|
|
149
|
+
* needs to say anything at all about the board it just rebuilt, and it is
|
|
150
|
+
* the only part of the response worth reading by eye.
|
|
151
|
+
*/
|
|
152
|
+
export function formatImportSummary(body, boardId) {
|
|
153
|
+
let parsed;
|
|
154
|
+
try {
|
|
155
|
+
parsed = JSON.parse(body);
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
return `Imported seed into board ${boardId}`;
|
|
159
|
+
}
|
|
160
|
+
const keyToCardId = parsed?.keyToCardId ?? {};
|
|
161
|
+
const keys = Object.keys(keyToCardId);
|
|
162
|
+
const count = Array.isArray(parsed?.cards) ? parsed.cards.length : keys.length;
|
|
163
|
+
const lines = [`Imported ${count} card${count === 1 ? '' : 's'} into board ${boardId}`];
|
|
164
|
+
if (keys.length > 0) {
|
|
165
|
+
const keyCol = Math.max(...keys.map((k) => k.length));
|
|
166
|
+
for (const key of keys)
|
|
167
|
+
lines.push(` ${key.padEnd(keyCol)} ${keyToCardId[key]}`);
|
|
168
|
+
}
|
|
169
|
+
return lines.join('\n');
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Auth first, so a missing token reports the message `resolveSubstrateContext`
|
|
173
|
+
* already writes (which names both places a token can live) rather than a
|
|
174
|
+
* workspace complaint that would send an operator looking in the wrong place.
|
|
175
|
+
*/
|
|
176
|
+
function resolveContext(env, workspaceFlag) {
|
|
177
|
+
const auth = resolveSubstrateContext(env);
|
|
178
|
+
if (!auth.ok)
|
|
179
|
+
return { ok: false, failure: fail('auth', auth.message) };
|
|
180
|
+
const { commonApiUrl, userToken } = auth.context;
|
|
181
|
+
const sessionWorkspace = env.WORKSPACE_ID?.trim() || undefined;
|
|
182
|
+
if (workspaceFlag && sessionWorkspace && workspaceFlag !== sessionWorkspace) {
|
|
183
|
+
return {
|
|
184
|
+
ok: false,
|
|
185
|
+
failure: fail('workspace_mismatch', `--workspace ${workspaceFlag} does not match the workspace bound to this session (${sessionWorkspace}).`),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const workspaceId = workspaceFlag ?? sessionWorkspace;
|
|
189
|
+
if (!workspaceId) {
|
|
190
|
+
return {
|
|
191
|
+
ok: false,
|
|
192
|
+
failure: fail('usage', 'no workspace: WORKSPACE_ID is unset in this session — pass --workspace <workspaceId>'),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
return { ok: true, commonApiUrl, userToken, workspaceId };
|
|
196
|
+
}
|
|
197
|
+
// ─── CLI surface ──────────────────────────────────────────────────────────
|
|
198
|
+
const EXPORT_USAGE = 'Usage: yolo kanban export <tileId> [-o <file>] [--workspace <wsId>]\n';
|
|
199
|
+
const IMPORT_USAGE = 'Usage: yolo kanban import <boardId> <file> [--workspace <wsId>]\n';
|
|
200
|
+
export function parseKanbanExportArgs(args) {
|
|
201
|
+
let tileId;
|
|
202
|
+
let outFile;
|
|
203
|
+
let workspaceFlag;
|
|
204
|
+
for (let i = 0; i < args.length; i++) {
|
|
205
|
+
const a = args[i];
|
|
206
|
+
if (a === '--workspace' || a === '-o' || a === '--out') {
|
|
207
|
+
const v = args[++i];
|
|
208
|
+
if (!v || v.startsWith('--'))
|
|
209
|
+
return { ok: false, message: `${a} requires a value` };
|
|
210
|
+
if (a === '--workspace')
|
|
211
|
+
workspaceFlag = v;
|
|
212
|
+
else
|
|
213
|
+
outFile = v;
|
|
214
|
+
}
|
|
215
|
+
else if (a.startsWith('--workspace=')) {
|
|
216
|
+
const v = a.slice('--workspace='.length);
|
|
217
|
+
if (!v)
|
|
218
|
+
return { ok: false, message: '--workspace requires a value' };
|
|
219
|
+
workspaceFlag = v;
|
|
220
|
+
}
|
|
221
|
+
else if (a.startsWith('--out=')) {
|
|
222
|
+
const v = a.slice('--out='.length);
|
|
223
|
+
if (!v)
|
|
224
|
+
return { ok: false, message: '--out requires a value' };
|
|
225
|
+
outFile = v;
|
|
226
|
+
}
|
|
227
|
+
else if (a.startsWith('-')) {
|
|
228
|
+
return { ok: false, message: `unknown option: ${a}` };
|
|
229
|
+
}
|
|
230
|
+
else if (tileId === undefined) {
|
|
231
|
+
tileId = a;
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
return { ok: false, message: `unexpected positional argument: ${a}` };
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (!tileId)
|
|
238
|
+
return { ok: false, message: 'a <tileId> is required' };
|
|
239
|
+
return { ok: true, tileId, outFile, workspaceFlag };
|
|
240
|
+
}
|
|
241
|
+
export function parseKanbanImportArgs(args) {
|
|
242
|
+
const positional = [];
|
|
243
|
+
let workspaceFlag;
|
|
244
|
+
for (let i = 0; i < args.length; i++) {
|
|
245
|
+
const a = args[i];
|
|
246
|
+
if (a === '--workspace') {
|
|
247
|
+
const v = args[++i];
|
|
248
|
+
if (!v || v.startsWith('--'))
|
|
249
|
+
return { ok: false, message: '--workspace requires a value' };
|
|
250
|
+
workspaceFlag = v;
|
|
251
|
+
}
|
|
252
|
+
else if (a.startsWith('--workspace=')) {
|
|
253
|
+
const v = a.slice('--workspace='.length);
|
|
254
|
+
if (!v)
|
|
255
|
+
return { ok: false, message: '--workspace requires a value' };
|
|
256
|
+
workspaceFlag = v;
|
|
257
|
+
}
|
|
258
|
+
else if (a.startsWith('-')) {
|
|
259
|
+
return { ok: false, message: `unknown option: ${a}` };
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
positional.push(a);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (positional.length < 2)
|
|
266
|
+
return { ok: false, message: 'a <boardId> and a <file> are required' };
|
|
267
|
+
if (positional.length > 2)
|
|
268
|
+
return { ok: false, message: `unexpected positional argument: ${positional[2]}` };
|
|
269
|
+
return { ok: true, boardId: positional[0], file: positional[1], workspaceFlag };
|
|
270
|
+
}
|
|
271
|
+
/** `args` is everything after `kanban`. */
|
|
272
|
+
export async function runKanbanCmd(args) {
|
|
273
|
+
const sub = args[0];
|
|
274
|
+
if (sub === 'export') {
|
|
275
|
+
const parsed = parseKanbanExportArgs(args.slice(1));
|
|
276
|
+
if (!parsed.ok) {
|
|
277
|
+
process.stderr.write(`yolo: kanban export: ${parsed.message}\n`);
|
|
278
|
+
process.stderr.write(EXPORT_USAGE);
|
|
279
|
+
return 64;
|
|
280
|
+
}
|
|
281
|
+
return report(await runKanbanExport({
|
|
282
|
+
tileId: parsed.tileId,
|
|
283
|
+
outFile: parsed.outFile,
|
|
284
|
+
workspaceFlag: parsed.workspaceFlag,
|
|
285
|
+
}));
|
|
286
|
+
}
|
|
287
|
+
if (sub === 'import') {
|
|
288
|
+
const parsed = parseKanbanImportArgs(args.slice(1));
|
|
289
|
+
if (!parsed.ok) {
|
|
290
|
+
process.stderr.write(`yolo: kanban import: ${parsed.message}\n`);
|
|
291
|
+
process.stderr.write(IMPORT_USAGE);
|
|
292
|
+
return 64;
|
|
293
|
+
}
|
|
294
|
+
return report(await runKanbanImport({
|
|
295
|
+
boardId: parsed.boardId,
|
|
296
|
+
file: parsed.file,
|
|
297
|
+
workspaceFlag: parsed.workspaceFlag,
|
|
298
|
+
}));
|
|
299
|
+
}
|
|
300
|
+
if (!sub) {
|
|
301
|
+
process.stderr.write('yolo: kanban requires a subcommand (export, import)\n');
|
|
302
|
+
return 64;
|
|
303
|
+
}
|
|
304
|
+
process.stderr.write(`yolo: unknown kanban subcommand '${sub}'\n`);
|
|
305
|
+
process.stderr.write('Subcommands: export, import\n');
|
|
306
|
+
return 64;
|
|
307
|
+
}
|
|
308
|
+
function report(result) {
|
|
309
|
+
if (result.ok) {
|
|
310
|
+
// `raw` output is the document: no trailing newline, so a redirect or a
|
|
311
|
+
// pipe gets the same bytes `-o` would have written.
|
|
312
|
+
process.stdout.write(result.raw ? result.output : `${result.output}\n`);
|
|
313
|
+
return 0;
|
|
314
|
+
}
|
|
315
|
+
process.stderr.write(`FAIL [${result.kind}]: ${result.message}\n`);
|
|
316
|
+
return exitCodeForFailure(result.kind);
|
|
317
|
+
}
|
|
318
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────
|
|
319
|
+
function fail(kind, message, detail) {
|
|
320
|
+
return { ok: false, kind, message, detail };
|
|
321
|
+
}
|
|
322
|
+
function describeError(err) {
|
|
323
|
+
return err instanceof Error ? err.message : String(err);
|
|
324
|
+
}
|
|
325
|
+
async function safeReadText(response) {
|
|
326
|
+
try {
|
|
327
|
+
return await response.text();
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
return '<no body>';
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
function isEexist(err) {
|
|
334
|
+
return typeof err === 'object' && err !== null && err.code === 'EEXIST';
|
|
335
|
+
}
|
|
336
|
+
/** `wx` so the refusal is the filesystem's, not a check racing the write. */
|
|
337
|
+
function defaultWriteFileExclusive(filePath, contents) {
|
|
338
|
+
fs.writeFileSync(filePath, contents, { encoding: 'utf-8', flag: 'wx' });
|
|
339
|
+
}
|
|
340
|
+
function defaultReadFile(filePath) {
|
|
341
|
+
return fs.readFileSync(filePath, 'utf-8');
|
|
342
|
+
}
|
|
343
|
+
export function exitCodeForFailure(kind) {
|
|
344
|
+
switch (kind) {
|
|
345
|
+
case 'usage':
|
|
346
|
+
case 'auth':
|
|
347
|
+
case 'workspace_mismatch':
|
|
348
|
+
return 64;
|
|
349
|
+
case 'http':
|
|
350
|
+
return 1;
|
|
351
|
+
}
|
|
352
|
+
}
|