@operad-io/shell 0.0.1 → 0.1.1

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 (3) hide show
  1. package/README.md +7 -18
  2. package/package.json +3 -3
  3. package/src/index.ts +64 -2
package/README.md CHANGED
@@ -1,24 +1,13 @@
1
1
  # @operad-io/shell
2
2
 
3
- The base `stage` plugin. It provides `shell/command`, which runs one shell
4
- line; `shell/check`, which runs a list of lines and stops at the first failure;
5
- and `shell/probe`, which a time or condition wait can call.
3
+ **The base stage plugin.** Provides `shell/command`, `shell/check` and `shell/probe`: run one shell line, run a list of lines that stops at the first failure, and a probe a wait can call.
6
4
 
7
- ## `shell/check` result data
5
+ This package is part of [operad](https://github.com/operad-io/operad). Most people install only [`@operad-io/cli`](https://www.npmjs.com/package/@operad-io/cli).
8
6
 
9
- `shell/check` ends as `pass`, `fail` or `interrupted`. Its result's `data`:
7
+ ## Documentation
10
8
 
11
- | field | when | meaning |
12
- |---|---|---|
13
- | `log` | always | the step log's path |
14
- | `logStart` | always | the byte offset in `log` where this invocation's output starts; a caller reading this invocation's output starts here, so output an earlier invocation wrote to the same run log is left out |
15
- | `tail` | always | the last output lines |
16
- | `lines` | on `pass` | how many lines ran |
17
- | `line`, `lineNumber` | when the check stops on a line | that line and its 1-based position in `params.lines` |
18
- | `exitCode`, `signal` | when the outcome has them | how the line's command ended |
19
- | `reason` | on `fail` and `interrupted` | `cwd`, `spawn`, `timeout`, `signal`, `exit` or `cancelled` |
20
- | `message` | when the outcome has one | the human text for the failure |
9
+ - User guide: [docs/user/index.md](https://github.com/operad-io/operad/blob/main/docs/user/index.md)
10
+ - Website: <https://operad.io>
11
+ - Developer reference: `REFERENCE.md` in this directory
21
12
 
22
- The log is the kernel logger's file for the step and run. It is appended to
23
- across every visit of the step in that run, so an earlier visit's output stays
24
- before `logStart`.
13
+ MIT licensed.
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@operad-io/shell",
3
- "version": "0.0.1",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
7
7
  },
8
8
  "dependencies": {
9
- "@operad-io/engine": "0.0.1"
9
+ "@operad-io/engine": "0.1.1"
10
10
  },
11
- "description": "operad: the shell plugin's command and check steps (in development)",
11
+ "description": "operad: the shell plugin's command and check steps",
12
12
  "repository": {
13
13
  "type": "git",
14
14
  "url": "git+https://github.com/operad-io/operad.git",
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  * a command itself.
5
5
  */
6
6
 
7
+ import pkg from "../package.json" with { type: "json" };
7
8
  import { createHash } from "node:crypto";
8
9
  import { stat } from "node:fs/promises";
9
10
  import { definePlugin, paramsObject, type PluginLogger, type PluginStepResult, type StepContext } from "@operad-io/engine";
@@ -42,6 +43,18 @@ function timeoutParam(
42
43
  return { ms: secs * 1000, secs };
43
44
  }
44
45
 
46
+ function stallParam(
47
+ params: Record<string, unknown>,
48
+ at: string,
49
+ ): { readonly ms: number | undefined; readonly secs: number | undefined } {
50
+ const secs = params.stallSecs;
51
+ if (secs === undefined) return { ms: undefined, secs: undefined };
52
+ if (typeof secs !== "number" || !Number.isFinite(secs) || secs <= 0) {
53
+ throw new Error(`${at}: params.stallSecs: must be a positive number of seconds`);
54
+ }
55
+ return { ms: secs * 1000, secs };
56
+ }
57
+
45
58
  function envParam(
46
59
  params: Record<string, unknown>,
47
60
  at: string,
@@ -100,9 +113,40 @@ function failureData(
100
113
  };
101
114
  }
102
115
 
116
+ function stallDuration(secs: number): string {
117
+ const minutes = secs / 60;
118
+ return Number.isInteger(minutes) && minutes >= 1 ? `${minutes} min` : `${secs}s`;
119
+ }
120
+
121
+ /** The last test file header, then the last lines, for a stall's reason. */
122
+ function stallLines(outcome: LineOutcome): readonly string[] {
123
+ const all = outcome.stallTail;
124
+ const header = [...all]
125
+ .reverse()
126
+ .find((line) => /\.(?:test|spec)\.[cm]?[jt]sx?:\s*$/.test(line));
127
+ const tail = outcome.tail.slice(-8);
128
+ return header === undefined || tail.includes(header) ? tail : [header, ...tail];
129
+ }
130
+
131
+ function stallData(
132
+ outcome: LineOutcome,
133
+ secs: number | undefined,
134
+ extra: Readonly<Record<string, unknown>> = {},
135
+ ): Record<string, unknown> {
136
+ const duration = secs === undefined ? "too long" : stallDuration(secs);
137
+ const lines = stallLines(outcome).join("\n");
138
+ return failureData(outcome, {
139
+ reason: "stall",
140
+ message: `check wrote nothing for ${duration}; last lines: ${lines}`,
141
+ ...(secs === undefined ? {} : { stallSecs: secs }),
142
+ ...extra,
143
+ });
144
+ }
145
+
103
146
  function commandResult(
104
147
  outcome: LineOutcome,
105
148
  timeoutSecs: number | undefined,
149
+ stallSecs?: number | undefined,
106
150
  ): PluginStepResult {
107
151
  if (outcome.spawnError !== undefined) {
108
152
  return {
@@ -119,6 +163,9 @@ function commandResult(
119
163
  data: failureData(outcome, { reason: "cancelled" }),
120
164
  };
121
165
  }
166
+ if (outcome.stalled) {
167
+ return { result: "fail", data: stallData(outcome, stallSecs) };
168
+ }
122
169
  if (outcome.timedOut) {
123
170
  const limit = timeoutSecs === undefined ? "" : ` after ${timeoutSecs}s`;
124
171
  return {
@@ -161,7 +208,7 @@ export function createShellPlugin(options: ShellPluginOptions = {}) {
161
208
  return definePlugin({
162
209
  name: "shell",
163
210
  kind: "stage",
164
- version: "0.0.0",
211
+ version: pkg.version,
165
212
  steps: [
166
213
  {
167
214
  name: "command",
@@ -173,6 +220,7 @@ export function createShellPlugin(options: ShellPluginOptions = {}) {
173
220
  const cwd = cwdParam(ctx, input, at);
174
221
  const env = envParam(input, at);
175
222
  const timeout = timeoutParam(input, at);
223
+ const stall = stallParam(input, at);
176
224
 
177
225
  const cwdProblem = await directoryError(cwd);
178
226
  if (cwdProblem !== undefined) {
@@ -194,11 +242,12 @@ export function createShellPlugin(options: ShellPluginOptions = {}) {
194
242
  cwd,
195
243
  env,
196
244
  timeoutMs: timeout.ms,
245
+ stallMs: stall.ms,
197
246
  signal: ctx.signal,
198
247
  logger: ctx.logger,
199
248
  clock,
200
249
  });
201
- return commandResult(outcome, timeout.secs);
250
+ return commandResult(outcome, timeout.secs, stall.secs);
202
251
  },
203
252
  },
204
253
  {
@@ -221,6 +270,7 @@ export function createShellPlugin(options: ShellPluginOptions = {}) {
221
270
  }
222
271
  const cwd = cwdParam(ctx, input, at);
223
272
  const timeout = timeoutParam(input, at);
273
+ const stall = stallParam(input, at);
224
274
  const start = await logStart(ctx.logger);
225
275
 
226
276
  const cwdProblem = await directoryError(cwd);
@@ -266,6 +316,7 @@ export function createShellPlugin(options: ShellPluginOptions = {}) {
266
316
  line,
267
317
  cwd,
268
318
  timeoutMs: timeout.ms,
319
+ stallMs: stall.ms,
269
320
  signal: ctx.signal,
270
321
  logger: ctx.logger,
271
322
  clock,
@@ -283,6 +334,17 @@ export function createShellPlugin(options: ShellPluginOptions = {}) {
283
334
  };
284
335
  }
285
336
 
337
+ if (outcome.stalled) {
338
+ return {
339
+ result: "fail",
340
+ data: stallData(outcome, stall.secs, {
341
+ line,
342
+ lineNumber: index + 1,
343
+ logStart: start,
344
+ }),
345
+ };
346
+ }
347
+
286
348
  if (
287
349
  outcome.spawnError !== undefined ||
288
350
  outcome.timedOut ||