codemod 2.0.0-alpha.2 → 2.0.0-alpha.3
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 +0 -1
- package/package.json +14 -12
- package/register.d.mts +2 -11
- package/register.mjs +53 -46
- package/runtime/addon.d.mts +114 -0
- package/runtime/addon.mjs +36 -40
- package/runtime/ast-grep.d.mts +20 -0
- package/runtime/ast-grep.mjs +50 -61
- package/runtime/context.d.mts +49 -0
- package/runtime/context.mjs +35 -53
- package/runtime/hooks.d.mts +2 -0
- package/runtime/hooks.mjs +4 -6
- package/runtime/host.d.mts +82 -81
- package/runtime/host.mjs +383 -323
- package/runtime/llm.d.mts +2 -0
- package/runtime/llm.mjs +1 -7
- package/runtime/metrics.d.mts +9 -0
- package/runtime/metrics.mjs +31 -34
- package/runtime/modules.d.mts +3 -0
- package/runtime/modules.mjs +8 -10
- package/runtime/orchestrator-version.d.mts +2 -0
- package/runtime/orchestrator-version.mjs +3 -0
- package/runtime/orchestrator.d.mts +261 -0
- package/runtime/orchestrator.mjs +829 -607
- package/runtime/runtime.d.mts +15 -0
- package/runtime/runtime.mjs +18 -26
- package/runtime/workflow.d.mts +19 -0
- package/runtime/workflow.mjs +83 -92
- package/testing/index.d.mts +109 -155
- package/testing/index.mjs +555 -545
- package/types/langs/package.json +3 -0
- package/types/modules/workflow.d.ts +180 -15
- package/vite.d.mts +4 -21
- package/vite.mjs +23 -17
package/runtime/context.mjs
CHANGED
|
@@ -5,73 +5,55 @@
|
|
|
5
5
|
// The store is shared through a global symbol, so it stays one store when a
|
|
6
6
|
// test runner loads this package twice (once natively, once through its own
|
|
7
7
|
// module graph).
|
|
8
|
-
|
|
9
8
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
10
|
-
|
|
11
9
|
const KEY = Symbol.for("codemod.workflow.context");
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export const context = (globalThis[KEY] ??= new AsyncLocalStorage());
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @typedef {object} StepScope
|
|
18
|
-
* @property {number} position
|
|
19
|
-
* @property {string} label
|
|
20
|
-
* @property {Map<string, unknown>} state state as the step sees it
|
|
21
|
-
* @property {Map<string, string>} initial the state the step started with, as JSON, by key
|
|
22
|
-
* @property {Set<string>} unpersisted keys set with `persist: false`: the step's own
|
|
23
|
-
* @property {Set<string>} removed keys the step removed
|
|
24
|
-
* @property {Map<string, string>} outputs step outputs set here
|
|
25
|
-
* @property {Map<string, Map<string, { cardinality: Record<string, string>, count: number }>>} metrics
|
|
26
|
-
* @property {string | undefined} failure a failure reported with `failFile` or `failStep`
|
|
27
|
-
* @property {string | undefined} language `parse()`'s default language
|
|
28
|
-
*/
|
|
29
|
-
|
|
10
|
+
const shared = globalThis;
|
|
11
|
+
export const context = (shared[KEY] ??= new AsyncLocalStorage());
|
|
30
12
|
/** The run the calling code belongs to; throws outside a run. */
|
|
31
13
|
export function current(what) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
return store;
|
|
14
|
+
const store = context.getStore();
|
|
15
|
+
if (!store) {
|
|
16
|
+
throw new Error(`${what} only works while a workflow runs; run the workflow with codemod (the CLI, or runWorkflow from codemod/testing)`);
|
|
17
|
+
}
|
|
18
|
+
return store;
|
|
39
19
|
}
|
|
40
|
-
|
|
41
20
|
/** A fresh step scope, starting from the workflow state the step began with. */
|
|
42
21
|
export function stepScope(position, label, state, language) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
22
|
+
const entries = Object.entries(state ?? {});
|
|
23
|
+
return {
|
|
24
|
+
position,
|
|
25
|
+
label,
|
|
26
|
+
state: new Map(entries.map(([key, value]) => [key, structuredClone(value)])),
|
|
27
|
+
initial: new Map(entries.map(([key, value]) => [key, JSON.stringify(value)])),
|
|
28
|
+
unpersisted: new Set(),
|
|
29
|
+
removed: new Set(),
|
|
30
|
+
outputs: new Map(),
|
|
31
|
+
metrics: new Map(),
|
|
32
|
+
failure: undefined,
|
|
33
|
+
language,
|
|
34
|
+
};
|
|
56
35
|
}
|
|
57
|
-
|
|
58
36
|
/**
|
|
59
37
|
* The state a step set or removed (`null`), compared with the state it
|
|
60
38
|
* started with: what the engine's isolates report for the same code.
|
|
61
39
|
*/
|
|
62
40
|
export function stateChanges(scope) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
41
|
+
const changes = {};
|
|
42
|
+
for (const [key, value] of scope.state) {
|
|
43
|
+
if (scope.unpersisted.has(key))
|
|
44
|
+
continue;
|
|
45
|
+
// What a step sets was made JSON when it was set (`stateValue`).
|
|
46
|
+
if (scope.initial.get(key) !== JSON.stringify(value))
|
|
47
|
+
changes[key] = value;
|
|
48
|
+
}
|
|
49
|
+
for (const key of scope.removed)
|
|
50
|
+
changes[key] = null;
|
|
51
|
+
return changes;
|
|
70
52
|
}
|
|
71
|
-
|
|
72
53
|
/** The metrics a step recorded, as the engine reports them. */
|
|
73
54
|
export function metricsOf(scope) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
55
|
+
const data = {};
|
|
56
|
+
for (const [name, entries] of scope.metrics)
|
|
57
|
+
data[name] = [...entries.values()];
|
|
58
|
+
return data;
|
|
77
59
|
}
|
package/runtime/hooks.mjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
// Resolve hooks for Node.js versions without `module.registerHooks`
|
|
2
2
|
// (loaded through `module.register`, on the loader thread).
|
|
3
|
-
|
|
4
3
|
import { resolveCodemodModule } from "./modules.mjs";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
4
|
+
export const resolve = async (specifier, context, nextResolve) => {
|
|
5
|
+
const url = resolveCodemodModule(specifier);
|
|
6
|
+
return url ? { url, shortCircuit: true } : nextResolve(specifier, context);
|
|
7
|
+
};
|
package/runtime/host.d.mts
CHANGED
|
@@ -18,103 +18,104 @@
|
|
|
18
18
|
*
|
|
19
19
|
* @module
|
|
20
20
|
*/
|
|
21
|
-
|
|
22
|
-
import type { JsonValue } from "../testing/index.
|
|
23
|
-
|
|
21
|
+
import "../register.mts";
|
|
22
|
+
import type { JsonValue } from "../testing/index.mts";
|
|
24
23
|
/** A `URL` (`new URL("./workflow.ts", import.meta.url)`); only `file:` URLs are paths. */
|
|
25
24
|
export interface UrlLike {
|
|
26
|
-
|
|
25
|
+
readonly href: string;
|
|
27
26
|
}
|
|
28
|
-
|
|
29
|
-
/** An `AbortSignal`. */
|
|
27
|
+
/** An `AbortSignal`, or what a test environment has in its place. */
|
|
30
28
|
export interface AbortSignalLike {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
readonly aborted: boolean;
|
|
30
|
+
addEventListener?(type: "abort", listener: () => void, options?: {
|
|
31
|
+
once?: boolean;
|
|
32
|
+
}): void;
|
|
33
|
+
removeEventListener?(type: "abort", listener: () => void): void;
|
|
34
34
|
}
|
|
35
|
-
|
|
36
35
|
/** A workflow module: what `import("./workflow.ts")` gives. */
|
|
37
36
|
export interface WorkflowModule {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
default: (context: {
|
|
38
|
+
params: Record<string, JsonValue>;
|
|
39
|
+
target: string;
|
|
40
|
+
dryRun: boolean;
|
|
41
|
+
}) => unknown;
|
|
43
42
|
}
|
|
44
|
-
|
|
45
43
|
export interface HostRunOptions {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
44
|
+
/** The directory the workflow changes. Default: the current directory. */
|
|
45
|
+
target?: string;
|
|
46
|
+
/** The workflow's parameters. */
|
|
47
|
+
params?: Record<string, JsonValue>;
|
|
48
|
+
/** A name for the run, where runs are listed. */
|
|
49
|
+
name?: string;
|
|
50
|
+
/** Run every step but change no files. */
|
|
51
|
+
dryRun?: boolean;
|
|
52
|
+
/** Stop after the step with this label (a test run). */
|
|
53
|
+
until?: string;
|
|
54
|
+
/** Results for steps that should not run, by label (a test run). */
|
|
55
|
+
given?: Record<string, JsonValue>;
|
|
56
|
+
/** Give every approval the workflow asks for. Default `true`. */
|
|
57
|
+
approve?: boolean;
|
|
58
|
+
/** Capabilities for host tasks' transforms, on top of those codemod.yaml declares. */
|
|
59
|
+
capabilities?: ("fs" | "fetch" | "child_process")[];
|
|
60
|
+
/** Which workflow to run when codemod.yaml defines several. */
|
|
61
|
+
workflowName?: string;
|
|
62
|
+
/**
|
|
63
|
+
* `"memory"` (the default) keeps nothing; `"local"` records the run where
|
|
64
|
+
* `codemod workflow list` and `status` show it.
|
|
65
|
+
*/
|
|
66
|
+
store?: "memory" | "local";
|
|
67
|
+
/**
|
|
68
|
+
* Continue this run (with `store: "local"`). A run is resumed by the host
|
|
69
|
+
* that started it: one started here cannot be resumed with
|
|
70
|
+
* `codemod workflow resume`, nor one the CLI started here.
|
|
71
|
+
*/
|
|
72
|
+
resume?: string;
|
|
73
|
+
/** The workflow file, when `workflow` is the module itself. */
|
|
74
|
+
path?: string;
|
|
75
|
+
/** Instead of `path`: the directory relative task paths resolve against. */
|
|
76
|
+
workflowDir?: string;
|
|
77
|
+
/** Send `console` output of the workflow's code to the report's events. */
|
|
78
|
+
captureConsole?: boolean;
|
|
79
|
+
/** Stops the run. */
|
|
80
|
+
signal?: AbortSignalLike;
|
|
81
81
|
}
|
|
82
|
-
|
|
83
82
|
export interface HostStepReport {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
83
|
+
readonly position: number;
|
|
84
|
+
readonly label: string;
|
|
85
|
+
/** `"partial"`: the step finished, but some of its files failed. */
|
|
86
|
+
readonly status: "succeeded" | "partial" | "failed";
|
|
87
|
+
readonly result: JsonValue | null;
|
|
88
|
+
readonly error: string | null;
|
|
89
|
+
readonly given: boolean;
|
|
90
90
|
}
|
|
91
|
-
|
|
92
91
|
export interface HostRunReport {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
92
|
+
readonly runId: string;
|
|
93
|
+
readonly status: "succeeded" | "failed" | "canceled" | "awaiting-approval" | "stopped";
|
|
94
|
+
readonly output: JsonValue | null;
|
|
95
|
+
readonly error: string | null;
|
|
96
|
+
/** The approval the run waits for. */
|
|
97
|
+
readonly awaiting: string | null;
|
|
98
|
+
/** The `until` step the run stopped after. */
|
|
99
|
+
readonly stoppedAt: string | null;
|
|
100
|
+
readonly steps: readonly HostStepReport[];
|
|
101
|
+
readonly metrics: Record<string, unknown>;
|
|
102
|
+
readonly stats: {
|
|
103
|
+
readonly filesModified: number;
|
|
104
|
+
readonly filesUnmodified: number;
|
|
105
|
+
readonly filesWithErrors: number;
|
|
106
|
+
};
|
|
107
|
+
/** What the engine reported while the run progressed. */
|
|
108
|
+
readonly events: readonly ({
|
|
109
|
+
readonly event: string;
|
|
110
|
+
} & Record<string, unknown>)[];
|
|
110
111
|
}
|
|
111
|
-
|
|
112
112
|
/**
|
|
113
113
|
* Run a workflow (a workflow file, a package directory, or the workflow
|
|
114
114
|
* module itself). Resolves for every outcome; rejects when the workflow
|
|
115
115
|
* cannot be loaded. YAML workflows run only with the CLI.
|
|
116
|
+
*
|
|
117
|
+
* A module (`await import("./workflow.ts")`), for test runners that should
|
|
118
|
+
* load it through their own module system, needs `options.path` (or
|
|
119
|
+
* `options.workflowDir`) so relative task paths resolve.
|
|
116
120
|
*/
|
|
117
|
-
export function runWorkflow(
|
|
118
|
-
workflow: string | UrlLike | WorkflowModule,
|
|
119
|
-
options?: HostRunOptions,
|
|
120
|
-
): Promise<HostRunReport>;
|
|
121
|
+
export declare function runWorkflow(workflow: string | UrlLike | WorkflowModule, options?: HostRunOptions): Promise<HostRunReport>;
|