@operad-io/dev 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +12 -2
- package/dev.schema.json +631 -0
- package/flows/chat-worktree.flow.ts +18 -0
- package/flows/dev.flow.ts +7 -0
- package/package.json +29 -4
- package/src/actions.ts +591 -0
- package/src/announce.ts +225 -0
- package/src/chat.ts +234 -0
- package/src/check.ts +80 -0
- package/src/commands.ts +417 -0
- package/src/editor.ts +55 -0
- package/src/flaky.ts +716 -0
- package/src/flow-compiler.ts +858 -0
- package/src/followups.ts +223 -0
- package/src/gate.ts +2950 -0
- package/src/git.ts +138 -0
- package/src/index.ts +227 -0
- package/src/integrate.ts +1213 -0
- package/src/integration-queue.ts +393 -0
- package/src/lock.ts +36 -0
- package/src/mainclone.ts +102 -0
- package/src/marks.ts +126 -0
- package/src/model.ts +1171 -0
- package/src/notcode.ts +305 -0
- package/src/place-order.ts +161 -0
- package/src/place-wait.ts +64 -0
- package/src/places.ts +368 -0
- package/src/prepare.ts +106 -0
- package/src/progress.ts +115 -0
- package/src/project-flows.ts +160 -0
- package/src/project-settings.ts +192 -0
- package/src/refusals.ts +336 -0
- package/src/rerun.ts +390 -0
- package/src/reruns.ts +62 -0
- package/src/retry.ts +212 -0
- package/src/rework.ts +366 -0
- package/src/schedule.ts +1052 -0
- package/src/service.ts +2160 -0
- package/src/settings.ts +1495 -0
- package/src/standing.ts +87 -0
- package/src/stdin.ts +105 -0
- package/src/steps.ts +1775 -0
- package/src/stops.ts +103 -0
- package/src/throughput.ts +347 -0
- package/src/timeouts.ts +60 -0
- package/src/views.ts +1167 -0
- package/src/waits.ts +105 -0
package/src/commands.ts
ADDED
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/** The command verbs the dev plugin adds to `operad`. */
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
CommandContext,
|
|
5
|
+
CommandDefinition,
|
|
6
|
+
StoredObject,
|
|
7
|
+
} from "@operad-io/engine";
|
|
8
|
+
import { DevError, bareId, briefBody, storeKey, taskData, type RepositoryData } from "./model.ts";
|
|
9
|
+
import { chatWorktreeOf } from "./chat.ts";
|
|
10
|
+
import { flakyTasksByName } from "./flaky.ts";
|
|
11
|
+
import { listRerunCounts } from "./reruns.ts";
|
|
12
|
+
import { loadProjectFlows } from "./project-flows.ts";
|
|
13
|
+
import { readDevSettings } from "./settings.ts";
|
|
14
|
+
import { readStdinText, readStdinWithin, type StdinReadOptions } from "./stdin.ts";
|
|
15
|
+
import { editText, editorCommand } from "./editor.ts";
|
|
16
|
+
import {
|
|
17
|
+
addPlan,
|
|
18
|
+
validatePlanConcurrency,
|
|
19
|
+
addRepository,
|
|
20
|
+
addTask,
|
|
21
|
+
editTaskBrief,
|
|
22
|
+
getTask,
|
|
23
|
+
listRepositories,
|
|
24
|
+
removeRepository,
|
|
25
|
+
requestTaskStart,
|
|
26
|
+
resolveObjectRepository,
|
|
27
|
+
resolveRepository,
|
|
28
|
+
updateTask,
|
|
29
|
+
} from "./service.ts";
|
|
30
|
+
|
|
31
|
+
interface ParsedArguments {
|
|
32
|
+
readonly positionals: readonly string[];
|
|
33
|
+
readonly flags: ReadonlyMap<string, readonly string[]>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parseArguments(
|
|
37
|
+
argv: readonly string[],
|
|
38
|
+
allowed: readonly string[],
|
|
39
|
+
): ParsedArguments {
|
|
40
|
+
const positionals: string[] = [];
|
|
41
|
+
const flags = new Map<string, string[]>();
|
|
42
|
+
const known = new Set(allowed);
|
|
43
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
44
|
+
const arg = argv[index] ?? "";
|
|
45
|
+
if (!arg.startsWith("--")) {
|
|
46
|
+
positionals.push(arg);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const equals = arg.indexOf("=");
|
|
50
|
+
const name = equals < 0 ? arg : arg.slice(0, equals);
|
|
51
|
+
if (!known.has(name)) {
|
|
52
|
+
throw new DevError(name, "unknown option");
|
|
53
|
+
}
|
|
54
|
+
let value = equals < 0 ? undefined : arg.slice(equals + 1);
|
|
55
|
+
if (value === undefined) {
|
|
56
|
+
const next = argv[index + 1];
|
|
57
|
+
if (next === undefined || next.startsWith("--")) {
|
|
58
|
+
throw new DevError(name, "needs a value");
|
|
59
|
+
}
|
|
60
|
+
value = next;
|
|
61
|
+
index += 1;
|
|
62
|
+
}
|
|
63
|
+
const values = flags.get(name) ?? [];
|
|
64
|
+
values.push(value);
|
|
65
|
+
flags.set(name, values);
|
|
66
|
+
}
|
|
67
|
+
return { positionals, flags };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function many(parsed: ParsedArguments, flag: string): readonly string[] {
|
|
71
|
+
return parsed.flags.get(flag) ?? [];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function one(parsed: ParsedArguments, flag: string): string | undefined {
|
|
75
|
+
const values = many(parsed, flag);
|
|
76
|
+
if (values.length === 0) return undefined;
|
|
77
|
+
if (values.length > 1) throw new DevError(flag, "given more than once");
|
|
78
|
+
return values[0];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function required(parsed: ParsedArguments, flag: string, at: string): string {
|
|
82
|
+
const value = one(parsed, flag);
|
|
83
|
+
if (value === undefined) {
|
|
84
|
+
throw new DevError(flag, `${at} needs ${flag} <value>`);
|
|
85
|
+
}
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function subcommand(
|
|
90
|
+
parsed: ParsedArguments,
|
|
91
|
+
allowed: readonly string[],
|
|
92
|
+
at: string,
|
|
93
|
+
): { readonly name: string; readonly rest: readonly string[] } {
|
|
94
|
+
const name = parsed.positionals[0];
|
|
95
|
+
if (name === undefined) {
|
|
96
|
+
throw new DevError("command", `${at} needs one of: ${allowed.join(", ")}`);
|
|
97
|
+
}
|
|
98
|
+
if (!allowed.includes(name)) {
|
|
99
|
+
throw new DevError("command", `${at}: unknown subcommand "${name}"`);
|
|
100
|
+
}
|
|
101
|
+
return { name, rest: parsed.positionals.slice(1) };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function objectByPlan(
|
|
105
|
+
parsed: ParsedArguments,
|
|
106
|
+
store: CommandContext["store"],
|
|
107
|
+
cwd: string,
|
|
108
|
+
): Promise<RepositoryData> {
|
|
109
|
+
return resolveRepository(store, {
|
|
110
|
+
name: one(parsed, "--repo"),
|
|
111
|
+
cwd,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function objectById(
|
|
116
|
+
parsed: ParsedArguments,
|
|
117
|
+
ctx: CommandContext,
|
|
118
|
+
kind: "plan" | "task",
|
|
119
|
+
id: string,
|
|
120
|
+
): Promise<{ readonly repository: RepositoryData; readonly id: string }> {
|
|
121
|
+
return resolveObjectRepository(ctx.store, kind, id, {
|
|
122
|
+
name: one(parsed, "--repo"),
|
|
123
|
+
cwd: ctx.cwd,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function runRepo(
|
|
128
|
+
ctx: CommandContext,
|
|
129
|
+
argv: readonly string[],
|
|
130
|
+
): Promise<string> {
|
|
131
|
+
const parsed = parseArguments(argv, ["--name"]);
|
|
132
|
+
const call = subcommand(parsed, ["add", "list", "remove"], "repo");
|
|
133
|
+
if (call.name === "add") {
|
|
134
|
+
const path = call.rest[0];
|
|
135
|
+
if (path === undefined) throw new DevError("path", "repo add needs a path");
|
|
136
|
+
if (call.rest.length > 1) {
|
|
137
|
+
throw new DevError("path", "repo add takes one path");
|
|
138
|
+
}
|
|
139
|
+
const repository = await addRepository(ctx.store, {
|
|
140
|
+
path,
|
|
141
|
+
name: one(parsed, "--name"),
|
|
142
|
+
cwd: ctx.cwd,
|
|
143
|
+
});
|
|
144
|
+
return `added ${repository.name} (${repository.path})`;
|
|
145
|
+
}
|
|
146
|
+
if (call.name === "list") {
|
|
147
|
+
if (call.rest.length > 0) throw new DevError("command", "repo list takes no arguments");
|
|
148
|
+
if (many(parsed, "--name").length > 0) {
|
|
149
|
+
throw new DevError("--name", "repo list takes no flags");
|
|
150
|
+
}
|
|
151
|
+
const repositories = await listRepositories(ctx.store);
|
|
152
|
+
if (repositories.length === 0) return "no repositories";
|
|
153
|
+
return repositories
|
|
154
|
+
.map((repository) => `${repository.name} ${repository.path}`)
|
|
155
|
+
.join("\n");
|
|
156
|
+
}
|
|
157
|
+
const name = call.rest[0];
|
|
158
|
+
if (name === undefined) throw new DevError("name", "repo remove needs a name");
|
|
159
|
+
if (call.rest.length > 1) {
|
|
160
|
+
throw new DevError("name", "repo remove takes one name");
|
|
161
|
+
}
|
|
162
|
+
const repository = await removeRepository(ctx.store, name);
|
|
163
|
+
return `removed ${repository.name}`;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function concurrencyFlag(raw: string | undefined): number | undefined {
|
|
167
|
+
if (raw === undefined) return undefined;
|
|
168
|
+
return validatePlanConcurrency(raw.trim() === "" ? Number.NaN : Number(raw));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function flowFlag(raw: string | undefined): string | undefined {
|
|
172
|
+
if (raw === undefined) return undefined;
|
|
173
|
+
const name = raw.trim();
|
|
174
|
+
if (name.length === 0) throw new DevError("--flow", "must be a flow name");
|
|
175
|
+
return name;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function assertFlow(repository: RepositoryData, flow: string): Promise<void> {
|
|
179
|
+
const settings = await readDevSettings(repository.path);
|
|
180
|
+
const project = await loadProjectFlows(repository.path, repository.name, settings);
|
|
181
|
+
const refused = project.refused.get(flow);
|
|
182
|
+
if (refused !== undefined) throw new DevError("--flow", refused);
|
|
183
|
+
if (flow === "default") return;
|
|
184
|
+
if (settings.flows[flow] === undefined && !project.flows.has(flow)) {
|
|
185
|
+
throw new DevError(
|
|
186
|
+
"--flow",
|
|
187
|
+
`no flow "${flow}" in repository "${repository.name}"`,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function runPlan(
|
|
193
|
+
ctx: CommandContext,
|
|
194
|
+
argv: readonly string[],
|
|
195
|
+
): Promise<string> {
|
|
196
|
+
const parsed = parseArguments(argv, ["--repo", "--slug", "--title", "--concurrency", "--flow"]);
|
|
197
|
+
const call = subcommand(parsed, ["add"], "plan");
|
|
198
|
+
if (call.rest.length > 0) {
|
|
199
|
+
throw new DevError("command", `plan ${call.name} takes flags, not ${call.rest[0]}`);
|
|
200
|
+
}
|
|
201
|
+
const repository = await objectByPlan(parsed, ctx.store, ctx.cwd);
|
|
202
|
+
const flow = flowFlag(one(parsed, "--flow"));
|
|
203
|
+
if (flow !== undefined) await assertFlow(repository, flow);
|
|
204
|
+
const plan = await addPlan(ctx.store, {
|
|
205
|
+
repository,
|
|
206
|
+
slug: required(parsed, "--slug", "plan add"),
|
|
207
|
+
title: required(parsed, "--title", "plan add"),
|
|
208
|
+
request: await readStdinText(ctx.input),
|
|
209
|
+
concurrency: concurrencyFlag(one(parsed, "--concurrency")),
|
|
210
|
+
auto: (await readDevSettings(repository.path)).autoDefault,
|
|
211
|
+
...(flow === undefined ? {} : { flow }),
|
|
212
|
+
});
|
|
213
|
+
return bareId(plan.id);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function editable(task: StoredObject): void {
|
|
217
|
+
const phase = taskData(task.data).phase;
|
|
218
|
+
if (phase !== "proposed" && phase !== "ready") {
|
|
219
|
+
throw new DevError(
|
|
220
|
+
"phase",
|
|
221
|
+
`task is ${phase}; only proposed or ready tasks can change`,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** How long `task edit` waits for a piped brief before it uses the editor. */
|
|
227
|
+
const STDIN_WAIT_MS = 1_000;
|
|
228
|
+
|
|
229
|
+
/** Test-only: what a bounded standard-input read injects. */
|
|
230
|
+
export const commandTestHooks: {
|
|
231
|
+
stdin?: StdinReadOptions | undefined;
|
|
232
|
+
} = {};
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The new brief for `task edit`: the text on stdin when stdin is not a
|
|
236
|
+
* terminal and carries some, else what the person saves in `$EDITOR`. A caller
|
|
237
|
+
* that passes its input (the MCP `command` tool) never gets an editor: its
|
|
238
|
+
* stdio is not a terminal.
|
|
239
|
+
*
|
|
240
|
+
* A non-terminal stdin can be a pipe nobody closes. The wait is bounded, and
|
|
241
|
+
* a read that times out falls back to the editor; without an editor it fails
|
|
242
|
+
* and says what it waited for instead of hanging silently.
|
|
243
|
+
*/
|
|
244
|
+
async function newBrief(ctx: CommandContext, body: string): Promise<string> {
|
|
245
|
+
if (ctx.input !== undefined) {
|
|
246
|
+
const piped = await readStdinText(ctx.input);
|
|
247
|
+
if (piped.trim().length > 0) return piped;
|
|
248
|
+
throw new DevError("brief", "task edit needs the new brief as input");
|
|
249
|
+
}
|
|
250
|
+
if (process.stdin.isTTY === true) {
|
|
251
|
+
return editText(body, { root: ctx.store.root });
|
|
252
|
+
}
|
|
253
|
+
const read = await readStdinWithin(undefined, {
|
|
254
|
+
timeoutMs: STDIN_WAIT_MS,
|
|
255
|
+
...commandTestHooks.stdin,
|
|
256
|
+
});
|
|
257
|
+
if (read.text.trim().length > 0) return read.text;
|
|
258
|
+
if (read.timedOut && editorCommand() === undefined) {
|
|
259
|
+
throw new DevError(
|
|
260
|
+
"brief",
|
|
261
|
+
`task edit waited ${STDIN_WAIT_MS / 1_000}s for the brief on standard input; nothing arrived, and no editor is set; pipe the brief or set $EDITOR`,
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
return editText(body, { root: ctx.store.root });
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
async function runTask(
|
|
268
|
+
ctx: CommandContext,
|
|
269
|
+
argv: readonly string[],
|
|
270
|
+
): Promise<string> { const parsed = parseArguments(argv, [
|
|
271
|
+
"--repo",
|
|
272
|
+
"--plan",
|
|
273
|
+
"--slug",
|
|
274
|
+
"--title",
|
|
275
|
+
"--after",
|
|
276
|
+
"--area",
|
|
277
|
+
"--flow",
|
|
278
|
+
"--chat",
|
|
279
|
+
]);
|
|
280
|
+
const call = subcommand(parsed, ["add", "edit", "update"], "task");
|
|
281
|
+
|
|
282
|
+
if (call.name === "add") {
|
|
283
|
+
if (call.rest.length > 0) {
|
|
284
|
+
throw new DevError("command", `task add takes flags, not ${call.rest[0]}`);
|
|
285
|
+
}
|
|
286
|
+
const repository = await objectByPlan(parsed, ctx.store, ctx.cwd);
|
|
287
|
+
const plan = required(parsed, "--plan", "task add");
|
|
288
|
+
const planKey = storeKey(repository.name, plan);
|
|
289
|
+
const planObject = await ctx.store.get("plan", planKey);
|
|
290
|
+
if (planObject === undefined) {
|
|
291
|
+
throw new DevError("plan", `no plan "${plan}"`);
|
|
292
|
+
}
|
|
293
|
+
if (
|
|
294
|
+
(planObject.data as { repository?: unknown }).repository !==
|
|
295
|
+
repository.name
|
|
296
|
+
) {
|
|
297
|
+
throw new DevError(
|
|
298
|
+
"plan",
|
|
299
|
+
`plan "${plan}" is not in repository "${repository.name}"`,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
const flow = flowFlag(one(parsed, "--flow"));
|
|
303
|
+
if (flow !== undefined) await assertFlow(repository, flow);
|
|
304
|
+
const chat = one(parsed, "--chat");
|
|
305
|
+
const worktree =
|
|
306
|
+
chat === undefined ? undefined : await chatWorktreeOf(ctx.store, repository, chat);
|
|
307
|
+
const task = await addTask(ctx.store, {
|
|
308
|
+
plan: planKey,
|
|
309
|
+
slug: required(parsed, "--slug", "task add"),
|
|
310
|
+
title: required(parsed, "--title", "task add"),
|
|
311
|
+
brief: await readStdinText(ctx.input),
|
|
312
|
+
after: many(parsed, "--after"),
|
|
313
|
+
area: one(parsed, "--area"),
|
|
314
|
+
...(flow === undefined ? {} : { flow }),
|
|
315
|
+
...(worktree === undefined ? {} : { worktree }),
|
|
316
|
+
});
|
|
317
|
+
return bareId(task.id);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const id = call.rest[0];
|
|
321
|
+
if (id === undefined) {
|
|
322
|
+
throw new DevError("id", `task ${call.name} needs a task id`);
|
|
323
|
+
}
|
|
324
|
+
if (call.rest.length > 1) {
|
|
325
|
+
throw new DevError("id", `task ${call.name} takes one task id`);
|
|
326
|
+
}
|
|
327
|
+
const target = await objectById(parsed, ctx, "task", id);
|
|
328
|
+
const key = storeKey(target.repository.name, target.id);
|
|
329
|
+
const task = await getTask(ctx.store, key);
|
|
330
|
+
|
|
331
|
+
if (call.name === "edit") {
|
|
332
|
+
editable(task);
|
|
333
|
+
const body = briefBody(taskData(task.data).brief);
|
|
334
|
+
const edited = await newBrief(ctx, body);
|
|
335
|
+
await editTaskBrief(ctx.store, key, edited);
|
|
336
|
+
return target.id;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const title = one(parsed, "--title");
|
|
340
|
+
// `--after ""` clears the dependencies, as `--area ""` clears the area.
|
|
341
|
+
const afterGiven = many(parsed, "--after");
|
|
342
|
+
const after = afterGiven.filter((id) => id !== "");
|
|
343
|
+
const area = one(parsed, "--area");
|
|
344
|
+
const flow = flowFlag(one(parsed, "--flow"));
|
|
345
|
+
if (flow !== undefined) await assertFlow(target.repository, flow);
|
|
346
|
+
return bareId(
|
|
347
|
+
(
|
|
348
|
+
await updateTask(ctx.store, key, {
|
|
349
|
+
...(title === undefined ? {} : { title }),
|
|
350
|
+
...(afterGiven.length === 0 ? {} : { after }),
|
|
351
|
+
...(area === undefined ? {} : { area }),
|
|
352
|
+
...(flow === undefined ? {} : { flow }),
|
|
353
|
+
runs: ctx.runs,
|
|
354
|
+
})
|
|
355
|
+
).id,
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
async function runStart(
|
|
360
|
+
ctx: CommandContext,
|
|
361
|
+
argv: readonly string[],
|
|
362
|
+
): Promise<string> {
|
|
363
|
+
const parsed = parseArguments(argv, ["--repo"]);
|
|
364
|
+
const id = parsed.positionals[0];
|
|
365
|
+
if (id === undefined) throw new DevError("id", "start needs a task id");
|
|
366
|
+
if (parsed.positionals.length > 1) {
|
|
367
|
+
throw new DevError("id", "start takes one task id");
|
|
368
|
+
}
|
|
369
|
+
const target = await objectById(parsed, ctx, "task", id);
|
|
370
|
+
await requestTaskStart(ctx.store, storeKey(target.repository.name, target.id));
|
|
371
|
+
return `start requested for ${target.id}`;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
async function runReruns(
|
|
375
|
+
ctx: CommandContext,
|
|
376
|
+
argv: readonly string[],
|
|
377
|
+
): Promise<string> {
|
|
378
|
+
if (argv.length > 0) throw new DevError("reruns", "takes no arguments");
|
|
379
|
+
const counts = await listRerunCounts(ctx.store);
|
|
380
|
+
if (counts.length === 0) return "no test has passed on a rerun yet";
|
|
381
|
+
const tasks = await flakyTasksByName(ctx.store);
|
|
382
|
+
return counts
|
|
383
|
+
.map((entry) => {
|
|
384
|
+
const task = tasks.get(entry.name);
|
|
385
|
+
return `${entry.count}\t${entry.name}${task === undefined ? "" : `\t${task}`}`;
|
|
386
|
+
})
|
|
387
|
+
.join("\n");
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
export const commands: readonly CommandDefinition[] = [
|
|
391
|
+
{
|
|
392
|
+
name: "repo",
|
|
393
|
+
args: "add <path> [--name <name>] | list | remove <name>",
|
|
394
|
+
run: runRepo,
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
name: "plan",
|
|
398
|
+
args: "add --slug <slug> --title <title> [--concurrency <n>] [--flow <name>] [--repo <name>]",
|
|
399
|
+
run: runPlan,
|
|
400
|
+
},
|
|
401
|
+
{
|
|
402
|
+
name: "task",
|
|
403
|
+
args:
|
|
404
|
+
"add --plan <plan> --slug <slug> --title <title> [--after <task>]... [--area <area>] [--flow <name>] [--repo <name>] [--chat <id>] | edit <id> [--repo <name>] (the new brief on stdin, or $EDITOR) | update <id> [--title <title>] [--after <task>]... [--area <area>] [--flow <name>] [--repo <name>]",
|
|
405
|
+
run: runTask,
|
|
406
|
+
},
|
|
407
|
+
{
|
|
408
|
+
name: "reruns",
|
|
409
|
+
args: "",
|
|
410
|
+
run: runReruns,
|
|
411
|
+
},
|
|
412
|
+
{
|
|
413
|
+
name: "start",
|
|
414
|
+
args: "<task> [--repo <name>]",
|
|
415
|
+
run: runStart,
|
|
416
|
+
},
|
|
417
|
+
];
|
package/src/editor.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** Opens `$EDITOR` on a task's editable brief body. */
|
|
2
|
+
|
|
3
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { DevError } from "./model.ts";
|
|
6
|
+
|
|
7
|
+
export interface EditorOptions {
|
|
8
|
+
/** A directory the temporary file lives under. */
|
|
9
|
+
readonly root: string;
|
|
10
|
+
readonly editor?: string | undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function quote(path: string): string {
|
|
14
|
+
return `"${path.replace(/"/g, '\\"')}"`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The editor command `$EDITOR` or `$VISUAL` names, when it names one. */
|
|
18
|
+
export function editorCommand(): string | undefined {
|
|
19
|
+
const editor = process.env.EDITOR ?? process.env.VISUAL;
|
|
20
|
+
if (editor === undefined || editor.trim().length === 0) return undefined;
|
|
21
|
+
return editor;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Runs the editor on `text` and returns what the person saved. */
|
|
25
|
+
export async function editText(
|
|
26
|
+
text: string,
|
|
27
|
+
options: EditorOptions,
|
|
28
|
+
): Promise<string> {
|
|
29
|
+
const editor = options.editor ?? editorCommand();
|
|
30
|
+
if (editor === undefined) {
|
|
31
|
+
throw new DevError("EDITOR", "no editor is set; set $EDITOR");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const directory = await mkdtemp(join(options.root, "dev-edit-"));
|
|
35
|
+
const file = join(directory, "brief.md");
|
|
36
|
+
await writeFile(file, text, "utf8");
|
|
37
|
+
try {
|
|
38
|
+
const child = Bun.spawn({
|
|
39
|
+
cmd: ["/bin/sh", "-c", `${editor} "$1"`, "sh", file],
|
|
40
|
+
stdin: "inherit",
|
|
41
|
+
stdout: "inherit",
|
|
42
|
+
stderr: "inherit",
|
|
43
|
+
});
|
|
44
|
+
const code = await child.exited;
|
|
45
|
+
if (code !== 0) {
|
|
46
|
+
throw new DevError(
|
|
47
|
+
"EDITOR",
|
|
48
|
+
`${quote(editor)} exited with code ${code}`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return await readFile(file, "utf8");
|
|
52
|
+
} finally {
|
|
53
|
+
await rm(directory, { recursive: true, force: true });
|
|
54
|
+
}
|
|
55
|
+
}
|