@operad-io/shell 0.3.0 → 0.4.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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/index.ts +84 -35
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@operad-io/shell",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
7
7
  },
8
8
  "dependencies": {
9
- "@operad-io/access-match": "0.3.0",
10
- "@operad-io/engine": "0.3.0"
9
+ "@operad-io/access-match": "0.4.0",
10
+ "@operad-io/engine": "0.4.0"
11
11
  },
12
12
  "description": "operad: the shell plugin's command and check steps",
13
13
  "repository": {
package/src/index.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  import pkg from "../package.json" with { type: "json" };
8
8
  import { createHash } from "node:crypto";
9
9
  import { stat } from "node:fs/promises";
10
- import { definePlugin, directoryError, paramsObject, type PluginLogger, type PluginStepResult, type StepContext } from "@operad-io/engine";
10
+ import { defineFlow, definePlugin, directoryError, paramsObject, type PluginLogger, type PluginStepResult, type StepContext } from "@operad-io/engine";
11
11
  import { guardOutward, outwardCommandWrites, personStarted } from "@operad-io/access-match";
12
12
  import {
13
13
  runLine,
@@ -210,6 +210,69 @@ function probeId(runId: string, cwd: string, line: string): string {
210
210
  .digest("hex");
211
211
  }
212
212
 
213
+ /** Runs one command line: the body of the command step and of the scheduled flow's step. */
214
+ async function runCommandLine(
215
+ ctx: StepContext,
216
+ input: Record<string, unknown>,
217
+ at: string,
218
+ line: string,
219
+ cwd: string,
220
+ clock: Clock | undefined,
221
+ ): Promise<PluginStepResult> {
222
+ const env = envParam(input, at);
223
+ const timeout = timeoutParam(input, at);
224
+ const stall = stallParam(input, at);
225
+ const refused = await outwardProblem(ctx, line);
226
+ if (refused !== undefined) {
227
+ return { result: "fail", data: { reason: "outward", message: refused } };
228
+ }
229
+
230
+ const cwdProblem = await directoryError(cwd);
231
+ if (cwdProblem !== undefined) {
232
+ const log = await writeNote(ctx.logger, cwdProblem);
233
+ return {
234
+ result: "fail",
235
+ data: {
236
+ reason: "cwd",
237
+ message: cwdProblem,
238
+ exitCode: null,
239
+ log,
240
+ tail: [cwdProblem],
241
+ },
242
+ };
243
+ }
244
+
245
+ const outcome = await runLine({
246
+ line,
247
+ cwd,
248
+ env,
249
+ timeoutMs: timeout.ms,
250
+ stallMs: stall.ms,
251
+ signal: ctx.signal,
252
+ logger: ctx.logger,
253
+ clock,
254
+ });
255
+ return commandResult(outcome, timeout.secs, stall.secs);
256
+ }
257
+
258
+ /** The flow a routine starts to run one command line: params command and optional cwd. */
259
+ export const commandFlow = defineFlow({
260
+ name: "shell:command",
261
+ entry: "run",
262
+ steps: [
263
+ {
264
+ id: "run",
265
+ kind: "plugin",
266
+ params: { use: "shell/scheduled", params: {} },
267
+ replay: "unsafe",
268
+ next: { pass: "done", fail: "failed", interrupted: "cancelled" },
269
+ },
270
+ { id: "done", kind: "end", params: { status: "done" }, replay: "safe" },
271
+ { id: "failed", kind: "end", params: { status: "failed" }, replay: "safe" },
272
+ { id: "cancelled", kind: "end", params: { status: "cancelled" }, replay: "safe" },
273
+ ],
274
+ });
275
+
213
276
  /** Options for the shell plugin. A test injects a clock to control timeouts. */
214
277
  export interface ShellPluginOptions {
215
278
  readonly clock?: Clock | undefined;
@@ -221,6 +284,7 @@ export function createShellPlugin(options: ShellPluginOptions = {}) {
221
284
  name: "shell",
222
285
  kind: "stage",
223
286
  version: pkg.version,
287
+ flows: [commandFlow],
224
288
  steps: [
225
289
  {
226
290
  name: "command",
@@ -229,42 +293,27 @@ export function createShellPlugin(options: ShellPluginOptions = {}) {
229
293
  async run(ctx, params) {
230
294
  const at = "shell/command";
231
295
  const input = paramsObject(params, at);
232
- const line = lineParam(input, at);
233
- const cwd = cwdParam(ctx, input, at);
234
- const env = envParam(input, at);
235
- const timeout = timeoutParam(input, at);
236
- const stall = stallParam(input, at);
237
- const refused = await outwardProblem(ctx, line);
238
- if (refused !== undefined) {
239
- return { result: "fail", data: { reason: "outward", message: refused } };
296
+ return runCommandLine(ctx, input, at, lineParam(input, at), cwdParam(ctx, input, at), clock);
297
+ },
298
+ },
299
+ {
300
+ // The step of the shell:command flow: the line and directory come from
301
+ // the parameters a routine started the flow with.
302
+ name: "scheduled",
303
+ reachesOutside: true,
304
+ results: ["pass", "fail", "interrupted"],
305
+ async run(ctx) {
306
+ const at = "shell/scheduled";
307
+ const input = paramsObject(ctx.params, at);
308
+ const command = input.command;
309
+ if (typeof command !== "string" || command.trim().length === 0) {
310
+ throw new Error(`${at}: params.command: must be a non-empty string`);
240
311
  }
241
-
242
- const cwdProblem = await directoryError(cwd);
243
- if (cwdProblem !== undefined) {
244
- const log = await writeNote(ctx.logger, cwdProblem);
245
- return {
246
- result: "fail",
247
- data: {
248
- reason: "cwd",
249
- message: cwdProblem,
250
- exitCode: null,
251
- log,
252
- tail: [cwdProblem],
253
- },
254
- };
312
+ const cwd = input.cwd;
313
+ if (cwd !== undefined && (typeof cwd !== "string" || cwd.length === 0)) {
314
+ throw new Error(`${at}: params.cwd: must be a non-empty string`);
255
315
  }
256
-
257
- const outcome = await runLine({
258
- line,
259
- cwd,
260
- env,
261
- timeoutMs: timeout.ms,
262
- stallMs: stall.ms,
263
- signal: ctx.signal,
264
- logger: ctx.logger,
265
- clock,
266
- });
267
- return commandResult(outcome, timeout.secs, stall.secs);
316
+ return runCommandLine(ctx, {}, at, command, cwd ?? ctx.home, clock);
268
317
  },
269
318
  },
270
319
  {