@eserstack/workflows 0.0.0-stage → 5.0.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 +13 -0
- package/README.md +310 -2
- package/dist/builder.d.ts +45 -0
- package/dist/builder.js +61 -0
- package/dist/engine.d.ts +92 -0
- package/dist/engine.js +408 -0
- package/dist/function-tools.d.ts +125 -0
- package/dist/function-tools.js +159 -0
- package/dist/go-workflow-tool.d.ts +88 -0
- package/dist/go-workflow-tool.js +142 -0
- package/dist/list.d.ts +20 -0
- package/dist/list.js +94 -0
- package/dist/loader.d.ts +32 -0
- package/dist/loader.js +109 -0
- package/dist/main.d.ts +22 -0
- package/dist/main.js +46 -0
- package/dist/mod.d.ts +55 -0
- package/dist/mod.js +11 -0
- package/dist/module.d.ts +21 -0
- package/dist/module.js +41 -0
- package/dist/native-functions.d.ts +49 -0
- package/dist/native-functions.js +2 -0
- package/dist/package.json +71 -0
- package/dist/registry.d.ts +47 -0
- package/dist/registry.js +23 -0
- package/dist/run.d.ts +41 -0
- package/dist/run.js +283 -0
- package/dist/shell-tool.d.ts +18 -0
- package/dist/shell-tool.js +96 -0
- package/dist/types.d.ts +143 -0
- package/dist/types.js +2 -0
- package/package.json +57 -3
package/dist/module.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Workflows module definition for @eserstack/shell integration.
|
|
4
|
+
*
|
|
5
|
+
* Tool injection is the caller's responsibility — pass tools via
|
|
6
|
+
* createModuleDef() or let the CLI layer compose them.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import * as module from "@eserstack/shell/module";
|
|
11
|
+
/**
|
|
12
|
+
* Build the `workflows` command module with its `run` and `list` subcommands.
|
|
13
|
+
*
|
|
14
|
+
* @param tools - Tools to register next to the built-in shell tool
|
|
15
|
+
* @returns The module, ready to mount with `toCommand()`
|
|
16
|
+
*/
|
|
17
|
+
export const createModuleDef = (tools) => new module.Module({
|
|
18
|
+
description: "Workflow engine — run tool pipelines",
|
|
19
|
+
modules: {
|
|
20
|
+
run: {
|
|
21
|
+
description: "Run workflows by event or id",
|
|
22
|
+
load: async () => {
|
|
23
|
+
const workflowsRun = await import("./run.js");
|
|
24
|
+
return {
|
|
25
|
+
main: (args) => workflowsRun.main(args, { tools: tools ?? [] }),
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
list: {
|
|
30
|
+
description: "List available workflows and tools",
|
|
31
|
+
load: async () => {
|
|
32
|
+
const workflowsList = await import("./list.js");
|
|
33
|
+
return {
|
|
34
|
+
main: (args) => workflowsList.main(args, { tools: tools ?? [] }),
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
/** Module with only the built-in shell tool, for standalone use. */
|
|
41
|
+
export const moduleDef = createModuleDef();
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The input and output types of the native functions this package owns, for
|
|
3
|
+
* `fromNative` of `@eserstack/functions/native`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Input of the native function `workflow-steps`.
|
|
9
|
+
*
|
|
10
|
+
* Runs a workflow of a manifest on the Go engine and answers each step as it ends
|
|
11
|
+
*/
|
|
12
|
+
export type WorkflowStepsInput = {
|
|
13
|
+
readonly args?: readonly string[];
|
|
14
|
+
readonly changedFiles?: readonly string[];
|
|
15
|
+
readonly dryRun?: boolean;
|
|
16
|
+
readonly fix?: boolean;
|
|
17
|
+
readonly manifest: string;
|
|
18
|
+
readonly only?: string;
|
|
19
|
+
readonly root?: string;
|
|
20
|
+
readonly timeoutMs?: number;
|
|
21
|
+
readonly workflow: string;
|
|
22
|
+
readonly [key: string]: unknown;
|
|
23
|
+
};
|
|
24
|
+
/** Output of the native function `workflow-steps`. */
|
|
25
|
+
export type WorkflowStepsOutput = {
|
|
26
|
+
readonly durationMs: number;
|
|
27
|
+
readonly issues?: ReadonlyArray<{
|
|
28
|
+
readonly fixed?: boolean;
|
|
29
|
+
readonly line?: number;
|
|
30
|
+
readonly message: string;
|
|
31
|
+
readonly path?: string;
|
|
32
|
+
}>;
|
|
33
|
+
readonly mutations?: ReadonlyArray<{
|
|
34
|
+
readonly newContent: string;
|
|
35
|
+
readonly oldContent: string;
|
|
36
|
+
readonly path: string;
|
|
37
|
+
}>;
|
|
38
|
+
readonly name: string;
|
|
39
|
+
readonly passed: boolean;
|
|
40
|
+
readonly stats?: Readonly<Record<string, unknown>>;
|
|
41
|
+
};
|
|
42
|
+
/** The native functions of this package, by name. */
|
|
43
|
+
export type NativeFunctions = {
|
|
44
|
+
readonly "workflow-steps": {
|
|
45
|
+
readonly input: WorkflowStepsInput;
|
|
46
|
+
readonly output: WorkflowStepsOutput;
|
|
47
|
+
readonly stream: true;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eserstack/workflows",
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/eser/stack",
|
|
8
|
+
"directory": "pkg/@eserstack/workflows"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist/**/*.js",
|
|
12
|
+
"dist/**/*.d.ts",
|
|
13
|
+
"dist/**/*.json"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./main.ts",
|
|
17
|
+
"./mod": "./mod.ts",
|
|
18
|
+
"./module": "./module.ts",
|
|
19
|
+
"./loader": "./loader.ts",
|
|
20
|
+
"./run": "./run.ts",
|
|
21
|
+
"./list": "./list.ts",
|
|
22
|
+
"./native-functions": "./native-functions.ts"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/main.d.ts",
|
|
28
|
+
"import": "./dist/main.js"
|
|
29
|
+
},
|
|
30
|
+
"./mod": {
|
|
31
|
+
"types": "./dist/mod.d.ts",
|
|
32
|
+
"import": "./dist/mod.js"
|
|
33
|
+
},
|
|
34
|
+
"./module": {
|
|
35
|
+
"types": "./dist/module.d.ts",
|
|
36
|
+
"import": "./dist/module.js"
|
|
37
|
+
},
|
|
38
|
+
"./loader": {
|
|
39
|
+
"types": "./dist/loader.d.ts",
|
|
40
|
+
"import": "./dist/loader.js"
|
|
41
|
+
},
|
|
42
|
+
"./run": {
|
|
43
|
+
"types": "./dist/run.d.ts",
|
|
44
|
+
"import": "./dist/run.js"
|
|
45
|
+
},
|
|
46
|
+
"./list": {
|
|
47
|
+
"types": "./dist/list.d.ts",
|
|
48
|
+
"import": "./dist/list.js"
|
|
49
|
+
},
|
|
50
|
+
"./native-functions": {
|
|
51
|
+
"types": "./dist/native-functions.d.ts",
|
|
52
|
+
"import": "./dist/native-functions.js"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "node ../../../etc/scripts/build-library.ts"
|
|
58
|
+
},
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"@eserstack/config": "workspace:*",
|
|
61
|
+
"@eserstack/functions": "workspace:*",
|
|
62
|
+
"@eserstack/primitives": "workspace:*",
|
|
63
|
+
"@eserstack/shell": "workspace:*",
|
|
64
|
+
"@eserstack/standards": "workspace:*",
|
|
65
|
+
"@eserstack/streams": "workspace:*",
|
|
66
|
+
"yaml": "^2.9.1"
|
|
67
|
+
},
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@eserstack/test-kit": "workspace:*"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool registry — a container for workflow tools, looked up by name.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import * as workflows from "@eserstack/workflows/mod";
|
|
7
|
+
*
|
|
8
|
+
* const registry = workflows.createRegistry();
|
|
9
|
+
* registry.register({
|
|
10
|
+
* name: "fix-eof",
|
|
11
|
+
* description: "Ensure files end with a newline",
|
|
12
|
+
* run: () =>
|
|
13
|
+
* Promise.resolve({
|
|
14
|
+
* name: "fix-eof",
|
|
15
|
+
* passed: true,
|
|
16
|
+
* issues: [],
|
|
17
|
+
* mutations: [],
|
|
18
|
+
* stats: {},
|
|
19
|
+
* }),
|
|
20
|
+
* });
|
|
21
|
+
* registry.get("fix-eof");
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @module
|
|
25
|
+
*/
|
|
26
|
+
import type { WorkflowTool } from "./types.ts";
|
|
27
|
+
/** A tool registry instance. */
|
|
28
|
+
export type Registry = {
|
|
29
|
+
/** Register a single tool. */
|
|
30
|
+
readonly register: (tool: WorkflowTool) => void;
|
|
31
|
+
/** Register multiple tools at once. */
|
|
32
|
+
readonly registerAll: (tools: readonly WorkflowTool[]) => void;
|
|
33
|
+
/** Get a tool by name. */
|
|
34
|
+
readonly get: (name: string) => WorkflowTool | undefined;
|
|
35
|
+
/** Get all registered tools. */
|
|
36
|
+
readonly getAll: () => readonly WorkflowTool[];
|
|
37
|
+
/** Check if a tool is registered. */
|
|
38
|
+
readonly has: (name: string) => boolean;
|
|
39
|
+
/** Get all registered tool names. */
|
|
40
|
+
readonly names: () => readonly string[];
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Create a new tool registry.
|
|
44
|
+
*
|
|
45
|
+
* @returns An empty Registry instance
|
|
46
|
+
*/
|
|
47
|
+
export declare const createRegistry: () => Registry;
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Create a new tool registry.
|
|
4
|
+
*
|
|
5
|
+
* @returns An empty Registry instance
|
|
6
|
+
*/
|
|
7
|
+
export const createRegistry = () => {
|
|
8
|
+
const tools = new Map();
|
|
9
|
+
return {
|
|
10
|
+
register: (tool) => {
|
|
11
|
+
tools.set(tool.name, tool);
|
|
12
|
+
},
|
|
13
|
+
registerAll: (toolList) => {
|
|
14
|
+
for (const tool of toolList) {
|
|
15
|
+
tools.set(tool.name, tool);
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
get: (name) => tools.get(name),
|
|
19
|
+
getAll: () => [...tools.values()],
|
|
20
|
+
has: (name) => tools.has(name),
|
|
21
|
+
names: () => [...tools.keys()],
|
|
22
|
+
};
|
|
23
|
+
};
|
package/dist/run.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type * as shellArgs from "@eserstack/shell/args";
|
|
2
|
+
import type { WorkflowTool } from "./types.ts";
|
|
3
|
+
/** The flags of `eser workflows run`. */
|
|
4
|
+
export type RunFlags = {
|
|
5
|
+
readonly event: string | undefined;
|
|
6
|
+
readonly workflow: string | undefined;
|
|
7
|
+
readonly config: string;
|
|
8
|
+
readonly fix: boolean;
|
|
9
|
+
readonly dryRun: boolean;
|
|
10
|
+
readonly only: string | undefined;
|
|
11
|
+
readonly args: readonly string[];
|
|
12
|
+
readonly verbose: boolean;
|
|
13
|
+
readonly json: boolean;
|
|
14
|
+
readonly changed: boolean;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Options injected by the CLI dispatcher.
|
|
18
|
+
*/
|
|
19
|
+
export type RunCliOptions = {
|
|
20
|
+
readonly tools?: readonly WorkflowTool[];
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Parse the `eser workflows run` arguments: `--event` (`-e`), `--workflow`
|
|
24
|
+
* (`-w`), `--only`, `--config`, `--fix`, `--dry-run`, `--verbose` (`-v`),
|
|
25
|
+
* `--json`, `--changed`, `--help` (`-h`). Positionals are passed on to the
|
|
26
|
+
* tools.
|
|
27
|
+
*/
|
|
28
|
+
export declare const parseCliArgs: (cliArgs?: readonly string[]) => {
|
|
29
|
+
readonly help: boolean;
|
|
30
|
+
readonly flags: RunFlags;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* CLI main function.
|
|
34
|
+
*
|
|
35
|
+
* Usage errors (no `-e` or `-w`, no manifest) come back as the `message` of
|
|
36
|
+
* the failed result, for the entry point to print.
|
|
37
|
+
*
|
|
38
|
+
* @param cliArgs - CLI arguments
|
|
39
|
+
* @param cliOptions - Injected options (tools, config)
|
|
40
|
+
*/
|
|
41
|
+
export declare const main: (cliArgs?: readonly string[], cliOptions?: RunCliOptions) => Promise<shellArgs.CliResult<void>>;
|
package/dist/run.js
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* CLI command handler for `eser workflows run`.
|
|
4
|
+
*
|
|
5
|
+
* This module is the glue between the engine and the terminal.
|
|
6
|
+
* It accepts tools injected by the CLI dispatcher (to avoid coupling
|
|
7
|
+
* this package to any specific tool provider like `@eserstack/codebase`).
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import * as shellFlags from "@eserstack/shell/args/flags";
|
|
12
|
+
import * as span from "@eserstack/streams/span";
|
|
13
|
+
import * as streams from "@eserstack/streams";
|
|
14
|
+
import * as results from "@eserstack/primitives/results";
|
|
15
|
+
import * as task from "@eserstack/functions/task";
|
|
16
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
17
|
+
import * as shellExec from "@eserstack/shell/exec";
|
|
18
|
+
import * as registry from "./registry.js";
|
|
19
|
+
import * as engine from "./engine.js";
|
|
20
|
+
import * as loader from "./loader.js";
|
|
21
|
+
import * as shellTool from "./shell-tool.js";
|
|
22
|
+
// =============================================================================
|
|
23
|
+
// Output formatting
|
|
24
|
+
// =============================================================================
|
|
25
|
+
const COLUMN_WIDTH = 50;
|
|
26
|
+
const createLineWriter = () => {
|
|
27
|
+
const renderer = streams.renderers.ansi();
|
|
28
|
+
const encoder = new TextEncoder();
|
|
29
|
+
return {
|
|
30
|
+
writeln: (...args) => {
|
|
31
|
+
const rendered = renderer.render(span.normalize(args));
|
|
32
|
+
runtime.process.writeToStdout(encoder.encode(`${rendered}\n`));
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
// `git diff --name-only` returns in well under a second; the bound keeps a hung
|
|
37
|
+
// git (a held index lock, a credential prompt) from stalling the whole run.
|
|
38
|
+
const GIT_DIFF_TIMEOUT_MS = 30_000;
|
|
39
|
+
const formatStepLine = (name, result, verbose) => {
|
|
40
|
+
const dots = ".".repeat(Math.max(1, COLUMN_WIDTH - name.length));
|
|
41
|
+
const timing = `${(result.durationMs / 1000).toFixed(1)}s`;
|
|
42
|
+
let status;
|
|
43
|
+
if (result.passed && result.mutations.length > 0) {
|
|
44
|
+
status = span.yellow(`Fixed (${result.mutations.length} file${result.mutations.length === 1 ? "" : "s"}, ${timing})`);
|
|
45
|
+
}
|
|
46
|
+
else if (result.passed) {
|
|
47
|
+
if (verbose && Object.keys(result.stats).length > 0) {
|
|
48
|
+
const statsStr = Object.entries(result.stats)
|
|
49
|
+
.map(([k, v]) => `${v} ${k}`)
|
|
50
|
+
.join(", ");
|
|
51
|
+
status = span.green(`Passed (${statsStr}, ${timing})`);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
status = span.green(`Passed (${timing})`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
status = span.red(`Failed (${timing})`);
|
|
59
|
+
}
|
|
60
|
+
return [span.text(`${name}${dots}`), status];
|
|
61
|
+
};
|
|
62
|
+
const formatIssueLine = (issue) => {
|
|
63
|
+
const location = issue.path === undefined
|
|
64
|
+
? ""
|
|
65
|
+
: issue.line === undefined
|
|
66
|
+
? issue.path
|
|
67
|
+
: `${issue.path}:${issue.line}`;
|
|
68
|
+
const prefix = location.length > 0 ? `${location}: ` : "";
|
|
69
|
+
return [
|
|
70
|
+
span.text(" "),
|
|
71
|
+
span.red("✗"),
|
|
72
|
+
span.text(` ${prefix}${issue.message}`),
|
|
73
|
+
];
|
|
74
|
+
};
|
|
75
|
+
const writeStepResult = (out, result, verbose) => {
|
|
76
|
+
out.writeln(...formatStepLine(result.name, result, verbose));
|
|
77
|
+
// Print issues under failed steps (or all steps when verbose)
|
|
78
|
+
if (!result.passed || verbose) {
|
|
79
|
+
for (const issue of result.issues) {
|
|
80
|
+
out.writeln(...formatIssueLine(issue));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const writeHelp = (out) => {
|
|
85
|
+
out.writeln("eser workflows run — Run workflow pipelines\n");
|
|
86
|
+
out.writeln("Usage:");
|
|
87
|
+
out.writeln(" eser workflows run -e <event> Run by event");
|
|
88
|
+
out.writeln(" eser workflows run -w <workflow-id> Run by workflow id");
|
|
89
|
+
out.writeln();
|
|
90
|
+
out.writeln("Options:");
|
|
91
|
+
out.writeln(" -e, --event <name> Event to trigger (precommit, commitmsg, prepush)");
|
|
92
|
+
out.writeln(" -w, --workflow <id> Workflow id to run");
|
|
93
|
+
out.writeln(" --fix Auto-fix issues where supported");
|
|
94
|
+
out.writeln(" --dry-run Preview mutations without writing");
|
|
95
|
+
out.writeln(" --only <step> Run only the steps with this tool or step name");
|
|
96
|
+
out.writeln(" --config <path> Config directory (default: .)");
|
|
97
|
+
out.writeln(" -v, --verbose Show stats and issues for all steps");
|
|
98
|
+
out.writeln(" --json Output results as JSON");
|
|
99
|
+
out.writeln(" --changed Only check files changed in git");
|
|
100
|
+
out.writeln(" -h, --help Show this help");
|
|
101
|
+
};
|
|
102
|
+
const summarize = (workflowResults) => {
|
|
103
|
+
let failedSteps = 0;
|
|
104
|
+
let issues = 0;
|
|
105
|
+
for (const workflowResult of workflowResults) {
|
|
106
|
+
for (const step of workflowResult.steps) {
|
|
107
|
+
if (!step.passed) {
|
|
108
|
+
failedSteps++;
|
|
109
|
+
issues += step.issues.length;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
passed: workflowResults.every((workflowResult) => workflowResult.passed),
|
|
115
|
+
failedSteps,
|
|
116
|
+
issues,
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
const reportResults = (out, workflowResults, jsonOutput) => {
|
|
120
|
+
const summary = summarize(workflowResults);
|
|
121
|
+
if (jsonOutput) {
|
|
122
|
+
out.writeln(JSON.stringify(workflowResults, null, 2));
|
|
123
|
+
}
|
|
124
|
+
else if (summary.passed) {
|
|
125
|
+
out.writeln(span.green("\nAll checks passed!"));
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
out.writeln(span.red(`\n${summary.failedSteps} check(s) failed with ${summary.issues} issue(s)`));
|
|
129
|
+
}
|
|
130
|
+
return summary.passed ? results.ok(undefined) : results.fail({ exitCode: 1 });
|
|
131
|
+
};
|
|
132
|
+
// =============================================================================
|
|
133
|
+
// Execution
|
|
134
|
+
// =============================================================================
|
|
135
|
+
const writeMutations = async (mutations) => {
|
|
136
|
+
for (const mutation of mutations) {
|
|
137
|
+
if (mutation.oldContent === mutation.newContent) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
// One write at a time on purpose: a step can fix hundreds of files, so
|
|
141
|
+
// this keeps one file open at a time, and a later mutation of the same
|
|
142
|
+
// path overwrites an earlier one instead of racing it.
|
|
143
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
144
|
+
await runtime.fs.writeTextFile(mutation.path, mutation.newContent);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const failRun = (out, message) => {
|
|
148
|
+
out.writeln(span.red(`Error: ${message}`));
|
|
149
|
+
return results.fail({ exitCode: 1 });
|
|
150
|
+
};
|
|
151
|
+
const runCommand = async (out, flags, cliOptions) => {
|
|
152
|
+
if (flags.event === undefined && flags.workflow === undefined) {
|
|
153
|
+
return results.fail({
|
|
154
|
+
message: "Error: specify -e <event> or -w <workflow-id>. Use --help for usage.",
|
|
155
|
+
exitCode: 1,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
// --- Create registry (shell tool built-in, then injected tools) ---
|
|
159
|
+
const toolRegistry = registry.createRegistry();
|
|
160
|
+
toolRegistry.register(shellTool.shellTool);
|
|
161
|
+
if (cliOptions?.tools !== undefined) {
|
|
162
|
+
toolRegistry.registerAll(cliOptions.tools);
|
|
163
|
+
}
|
|
164
|
+
// --- Load config ---
|
|
165
|
+
const config = await loader.loadFromFile(flags.config);
|
|
166
|
+
if (config === null) {
|
|
167
|
+
return results.fail({
|
|
168
|
+
message: "Error: no .eser/manifest.yml found in current directory.",
|
|
169
|
+
exitCode: 1,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
// --- Detect changed files ---
|
|
173
|
+
let changedFiles;
|
|
174
|
+
if (flags.changed) {
|
|
175
|
+
try {
|
|
176
|
+
changedFiles = await shellExec.exec `git diff --name-only HEAD`
|
|
177
|
+
.cwd(flags.config)
|
|
178
|
+
.timeout(GIT_DIFF_TIMEOUT_MS)
|
|
179
|
+
.noThrow()
|
|
180
|
+
.lines();
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
out.writeln(span.yellow("Warning: could not run git, running without file filtering."));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// --- Build run options ---
|
|
187
|
+
const runOptions = {
|
|
188
|
+
root: flags.config,
|
|
189
|
+
fix: flags.fix,
|
|
190
|
+
dryRun: flags.dryRun,
|
|
191
|
+
only: flags.only,
|
|
192
|
+
args: flags.args,
|
|
193
|
+
changedFiles,
|
|
194
|
+
onStepEnd: flags.json
|
|
195
|
+
? undefined
|
|
196
|
+
: (result) => writeStepResult(out, result, flags.verbose),
|
|
197
|
+
onMutations: flags.dryRun ? undefined : writeMutations,
|
|
198
|
+
};
|
|
199
|
+
// --- Run ---
|
|
200
|
+
// Every workflow runs on the TypeScript engine; WIRING.md says why the Go
|
|
201
|
+
// engine of the native library is not used.
|
|
202
|
+
try {
|
|
203
|
+
let workflowResults;
|
|
204
|
+
if (flags.event !== undefined) {
|
|
205
|
+
const eventResult = await task.runTask(engine.runByEvent(flags.event, config.workflows, toolRegistry, runOptions));
|
|
206
|
+
if (results.isFail(eventResult)) {
|
|
207
|
+
return failRun(out, eventResult.error.message);
|
|
208
|
+
}
|
|
209
|
+
workflowResults = eventResult.value;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
const workflowResult = await task.runTask(engine.runWorkflowWithConfig(
|
|
213
|
+
// Defined: the guard at the top returns when neither -e nor -w is set.
|
|
214
|
+
flags.workflow, config, toolRegistry, runOptions));
|
|
215
|
+
if (results.isFail(workflowResult)) {
|
|
216
|
+
return failRun(out, workflowResult.error.message);
|
|
217
|
+
}
|
|
218
|
+
workflowResults = [workflowResult.value];
|
|
219
|
+
}
|
|
220
|
+
return reportResults(out, workflowResults, flags.json);
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
// The engine throws for circular or unknown includes and malformed steps,
|
|
224
|
+
// and writing a fixed file to disk can fail.
|
|
225
|
+
return failRun(out, error instanceof Error ? error.message : String(error));
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const stringValue = (value) => typeof value === "string" ? value : undefined;
|
|
229
|
+
/**
|
|
230
|
+
* Parse the `eser workflows run` arguments: `--event` (`-e`), `--workflow`
|
|
231
|
+
* (`-w`), `--only`, `--config`, `--fix`, `--dry-run`, `--verbose` (`-v`),
|
|
232
|
+
* `--json`, `--changed`, `--help` (`-h`). Positionals are passed on to the
|
|
233
|
+
* tools.
|
|
234
|
+
*/
|
|
235
|
+
export const parseCliArgs = (cliArgs) => {
|
|
236
|
+
const { values, positionals } = shellFlags.parseArgs(cliArgs ?? [], {
|
|
237
|
+
options: {
|
|
238
|
+
event: { type: "string", short: "e" },
|
|
239
|
+
workflow: { type: "string", short: "w" },
|
|
240
|
+
only: { type: "string" },
|
|
241
|
+
config: { type: "string" },
|
|
242
|
+
fix: { type: "boolean" },
|
|
243
|
+
"dry-run": { type: "boolean" },
|
|
244
|
+
help: { type: "boolean", short: "h" },
|
|
245
|
+
verbose: { type: "boolean", short: "v" },
|
|
246
|
+
json: { type: "boolean" },
|
|
247
|
+
changed: { type: "boolean" },
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
return {
|
|
251
|
+
help: values["help"] === true,
|
|
252
|
+
flags: {
|
|
253
|
+
event: stringValue(values["event"]),
|
|
254
|
+
workflow: stringValue(values["workflow"]),
|
|
255
|
+
config: stringValue(values["config"]) ?? ".",
|
|
256
|
+
fix: values["fix"] === true,
|
|
257
|
+
dryRun: values["dry-run"] === true,
|
|
258
|
+
only: stringValue(values["only"]),
|
|
259
|
+
args: positionals,
|
|
260
|
+
verbose: values["verbose"] === true,
|
|
261
|
+
json: values["json"] === true,
|
|
262
|
+
changed: values["changed"] === true,
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
};
|
|
266
|
+
/**
|
|
267
|
+
* CLI main function.
|
|
268
|
+
*
|
|
269
|
+
* Usage errors (no `-e` or `-w`, no manifest) come back as the `message` of
|
|
270
|
+
* the failed result, for the entry point to print.
|
|
271
|
+
*
|
|
272
|
+
* @param cliArgs - CLI arguments
|
|
273
|
+
* @param cliOptions - Injected options (tools, config)
|
|
274
|
+
*/
|
|
275
|
+
export const main = async (cliArgs, cliOptions) => {
|
|
276
|
+
const { help, flags } = parseCliArgs(cliArgs);
|
|
277
|
+
const out = createLineWriter();
|
|
278
|
+
if (help) {
|
|
279
|
+
writeHelp(out);
|
|
280
|
+
return results.ok(undefined);
|
|
281
|
+
}
|
|
282
|
+
return await runCommand(out, flags, cliOptions);
|
|
283
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { WorkflowTool } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Built-in shell tool — executes commands via `sh -c`.
|
|
4
|
+
*
|
|
5
|
+
* Options:
|
|
6
|
+
* - `command` (string, required) — the command to execute
|
|
7
|
+
* - `fixCommand` (string, optional) — command to run when `fix: true`
|
|
8
|
+
* - `name` (string, optional) — display name (defaults to command)
|
|
9
|
+
* - `workingDirectory` (string, optional) — working directory (relative to root)
|
|
10
|
+
* - `fix` (boolean) — injected by engine, selects fixCommand if present
|
|
11
|
+
* - `dryRun` (boolean): injected by engine; a dry run never runs fixCommand,
|
|
12
|
+
* since it writes files, and runs `command` instead
|
|
13
|
+
* - `root` (string) — injected by engine, default working directory
|
|
14
|
+
*
|
|
15
|
+
* The command is stopped when it runs past the step's timeout
|
|
16
|
+
* (`context.timeoutMs`).
|
|
17
|
+
*/
|
|
18
|
+
export declare const shellTool: WorkflowTool;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Built-in shell command tool for the workflow engine.
|
|
4
|
+
*
|
|
5
|
+
* Executes shell commands via subprocess. The command is passed to `sh -c`.
|
|
6
|
+
*
|
|
7
|
+
* **Security:** Only use with trusted config files. Never load workflow
|
|
8
|
+
* config from untrusted sources when shell steps are present — the command
|
|
9
|
+
* string is executed as-is with full shell interpretation.
|
|
10
|
+
*
|
|
11
|
+
* @example YAML
|
|
12
|
+
* ```yaml
|
|
13
|
+
* - shell:
|
|
14
|
+
* name: format
|
|
15
|
+
* command: npx oxfmt --check
|
|
16
|
+
* fixCommand: npx oxfmt
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @module
|
|
20
|
+
*/
|
|
21
|
+
import * as shellExec from "@eserstack/shell/exec";
|
|
22
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
23
|
+
/**
|
|
24
|
+
* Built-in shell tool — executes commands via `sh -c`.
|
|
25
|
+
*
|
|
26
|
+
* Options:
|
|
27
|
+
* - `command` (string, required) — the command to execute
|
|
28
|
+
* - `fixCommand` (string, optional) — command to run when `fix: true`
|
|
29
|
+
* - `name` (string, optional) — display name (defaults to command)
|
|
30
|
+
* - `workingDirectory` (string, optional) — working directory (relative to root)
|
|
31
|
+
* - `fix` (boolean) — injected by engine, selects fixCommand if present
|
|
32
|
+
* - `dryRun` (boolean): injected by engine; a dry run never runs fixCommand,
|
|
33
|
+
* since it writes files, and runs `command` instead
|
|
34
|
+
* - `root` (string) — injected by engine, default working directory
|
|
35
|
+
*
|
|
36
|
+
* The command is stopped when it runs past the step's timeout
|
|
37
|
+
* (`context.timeoutMs`).
|
|
38
|
+
*/
|
|
39
|
+
export const shellTool = {
|
|
40
|
+
name: "shell",
|
|
41
|
+
description: "Execute shell commands",
|
|
42
|
+
run: async (options, context) => {
|
|
43
|
+
const command = options["command"];
|
|
44
|
+
const fixCommand = options["fixCommand"];
|
|
45
|
+
const displayName = options["name"] ?? command ?? "shell";
|
|
46
|
+
const fix = options["fix"] ?? false;
|
|
47
|
+
const dryRun = options["dryRun"] ?? false;
|
|
48
|
+
const root = options["root"] ?? ".";
|
|
49
|
+
const workingDirectory = options["workingDirectory"];
|
|
50
|
+
if (command === undefined || command.length === 0) {
|
|
51
|
+
throw new Error("Shell tool requires a 'command' option. " +
|
|
52
|
+
'Usage: { shell: { command: "npx oxfmt --check" } }');
|
|
53
|
+
}
|
|
54
|
+
const cmd = fix && !dryRun && fixCommand !== undefined ? fixCommand : command;
|
|
55
|
+
// Stopped at the step's timeout, so the command does not run on after the
|
|
56
|
+
// engine has failed the step. spawn() then throws CommandRunError, which
|
|
57
|
+
// the engine reports as the step's timeout.
|
|
58
|
+
const output = await new shellExec.CommandBuilder("sh", ["-c", cmd])
|
|
59
|
+
.cwd(workingDirectory !== undefined
|
|
60
|
+
? runtime.path.resolve(root, workingDirectory)
|
|
61
|
+
: root)
|
|
62
|
+
.timeout(context.timeoutMs)
|
|
63
|
+
.noThrow()
|
|
64
|
+
.spawn();
|
|
65
|
+
const exitCode = output.code;
|
|
66
|
+
if (exitCode === 0) {
|
|
67
|
+
return {
|
|
68
|
+
name: displayName,
|
|
69
|
+
passed: true,
|
|
70
|
+
issues: [],
|
|
71
|
+
mutations: [],
|
|
72
|
+
stats: { exitCode: 0 },
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
// Combine stdout + stderr for the failure message
|
|
76
|
+
const decoder = new TextDecoder();
|
|
77
|
+
const stderr = decoder.decode(output.stderr).trim();
|
|
78
|
+
const stdout = decoder.decode(output.stdout).trim();
|
|
79
|
+
const combinedOutput = [stdout, stderr]
|
|
80
|
+
.filter((s) => s.length > 0)
|
|
81
|
+
.join("\n");
|
|
82
|
+
return {
|
|
83
|
+
name: displayName,
|
|
84
|
+
passed: false,
|
|
85
|
+
issues: [
|
|
86
|
+
{
|
|
87
|
+
message: combinedOutput.length > 0
|
|
88
|
+
? combinedOutput
|
|
89
|
+
: `Command exited with code ${exitCode}`,
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
mutations: [],
|
|
93
|
+
stats: { exitCode },
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
};
|