@gadicc/loom 1.17.0 → 1.19.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/README.md +120 -43
- package/deno.json +3 -0
- package/esm/src/cli/mod.d.ts +3 -1
- package/esm/src/cli/mod.d.ts.map +1 -1
- package/esm/src/cli/mod.js +968 -342
- package/esm/src/cli/projectTooling.d.ts +138 -0
- package/esm/src/cli/projectTooling.d.ts.map +1 -0
- package/esm/src/cli/projectTooling.js +614 -0
- package/esm/src/jobs/controller.d.ts +14 -1
- package/esm/src/jobs/controller.d.ts.map +1 -1
- package/esm/src/jobs/controller.js +54 -3
- package/esm/src/jobs/events.d.ts +9 -1
- package/esm/src/jobs/events.d.ts.map +1 -1
- package/esm/src/jobs/events.js +15 -2
- package/esm/src/jobs/mod.d.ts +1 -0
- package/esm/src/jobs/mod.d.ts.map +1 -1
- package/esm/src/jobs/mod.js +1 -0
- package/esm/src/jobs/run.d.ts +20 -1
- package/esm/src/jobs/run.d.ts.map +1 -1
- package/esm/src/jobs/run.js +29 -4
- package/esm/src/jobs/sanitize.d.ts +30 -0
- package/esm/src/jobs/sanitize.d.ts.map +1 -0
- package/esm/src/jobs/sanitize.js +91 -0
- package/esm/src/mail/postal.d.ts.map +1 -1
- package/esm/src/mail/postal.js +25 -20
- package/esm/src/mail/sender.d.ts +5 -0
- package/esm/src/mail/sender.d.ts.map +1 -0
- package/esm/src/mail/sender.js +48 -0
- package/esm/src/mail/service.d.ts.map +1 -1
- package/esm/src/mail/service.js +7 -5
- package/esm/src/next/jobs/route.d.ts.map +1 -1
- package/esm/src/next/jobs/route.js +1 -1
- package/package.json +3 -2
- package/script/src/cli/mod.d.ts +3 -1
- package/script/src/cli/mod.d.ts.map +1 -1
- package/script/src/cli/mod.js +968 -342
- package/script/src/cli/projectTooling.d.ts +138 -0
- package/script/src/cli/projectTooling.d.ts.map +1 -0
- package/script/src/cli/projectTooling.js +634 -0
- package/script/src/jobs/controller.d.ts +14 -1
- package/script/src/jobs/controller.d.ts.map +1 -1
- package/script/src/jobs/controller.js +54 -3
- package/script/src/jobs/events.d.ts +9 -1
- package/script/src/jobs/events.d.ts.map +1 -1
- package/script/src/jobs/events.js +15 -2
- package/script/src/jobs/mod.d.ts +1 -0
- package/script/src/jobs/mod.d.ts.map +1 -1
- package/script/src/jobs/mod.js +1 -0
- package/script/src/jobs/run.d.ts +20 -1
- package/script/src/jobs/run.d.ts.map +1 -1
- package/script/src/jobs/run.js +29 -4
- package/script/src/jobs/sanitize.d.ts +30 -0
- package/script/src/jobs/sanitize.d.ts.map +1 -0
- package/script/src/jobs/sanitize.js +97 -0
- package/script/src/mail/postal.d.ts.map +1 -1
- package/script/src/mail/postal.js +25 -20
- package/script/src/mail/sender.d.ts +5 -0
- package/script/src/mail/sender.d.ts.map +1 -0
- package/script/src/mail/sender.js +52 -0
- package/script/src/mail/service.d.ts.map +1 -1
- package/script/src/mail/service.js +7 -5
- package/script/src/next/jobs/route.d.ts.map +1 -1
- package/script/src/next/jobs/route.js +1 -1
- package/skills/loom/SKILL.md +38 -23
- package/skills/loom-db/SKILL.md +9 -7
- package/skills/loom-db/references/db-stack.md +2 -1
- package/skills/loom-jobs/SKILL.md +36 -6
- package/skills/loom-lingui/SKILL.md +38 -0
- package/skills/loom-lingui/agents/openai.yaml +4 -0
- package/skills/loom-mail/SKILL.md +14 -3
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
const jsoncRequire = createRequire(globalThis[Symbol.for("import-meta-ponyfill-esmodule")](import.meta).url);
|
|
5
|
+
let cachedJsoncParser;
|
|
6
|
+
function getJsoncParser() {
|
|
7
|
+
cachedJsoncParser ??= jsoncRequire("jsonc-parser");
|
|
8
|
+
return cachedJsoncParser;
|
|
9
|
+
}
|
|
10
|
+
async function readTextIfExists(path) {
|
|
11
|
+
try {
|
|
12
|
+
return await readFile(path, "utf8");
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error.code === "ENOENT")
|
|
16
|
+
return null;
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Parses Deno JSON/JSONC while reporting every syntax error. */
|
|
21
|
+
export function parseJsonc(text, filename) {
|
|
22
|
+
const { parse, printParseErrorCode } = getJsoncParser();
|
|
23
|
+
const errors = [];
|
|
24
|
+
const value = parse(text, errors, {
|
|
25
|
+
allowEmptyContent: false,
|
|
26
|
+
allowTrailingComma: true,
|
|
27
|
+
disallowComments: false,
|
|
28
|
+
});
|
|
29
|
+
if (errors.length) {
|
|
30
|
+
const detail = errors.map((error) => `${printParseErrorCode(error.error)} at offset ${error.offset}`).join(", ");
|
|
31
|
+
throw new Error(`Failed to parse ${filename}: ${detail}`);
|
|
32
|
+
}
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
function parseStrictJson(text, filename) {
|
|
36
|
+
try {
|
|
37
|
+
return JSON.parse(text);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
throw new Error(`Failed to parse ${filename}: ${error.message}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function formattingOptions(text) {
|
|
44
|
+
const eol = text.includes("\r\n") ? "\r\n" : "\n";
|
|
45
|
+
const indentation = text.match(/(?:^|\r?\n)([\t ]+)\S/m)?.[1] ?? " ";
|
|
46
|
+
const insertSpaces = !indentation.includes("\t");
|
|
47
|
+
return {
|
|
48
|
+
eol,
|
|
49
|
+
insertFinalNewline: text.endsWith("\n"),
|
|
50
|
+
insertSpaces,
|
|
51
|
+
keepLines: true,
|
|
52
|
+
tabSize: insertSpaces ? Math.max(1, indentation.length) : 1,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function updateJsonPath(text, path, value) {
|
|
56
|
+
const { applyEdits, modify } = getJsoncParser();
|
|
57
|
+
return applyEdits(text, modify(text, path, value, { formattingOptions: formattingOptions(text) }));
|
|
58
|
+
}
|
|
59
|
+
function hasOwn(value, key) {
|
|
60
|
+
return Object.prototype.hasOwnProperty.call(value, key);
|
|
61
|
+
}
|
|
62
|
+
function definitionsEqual(left, right) {
|
|
63
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
64
|
+
}
|
|
65
|
+
function definitionsConflict(left, right) {
|
|
66
|
+
if (definitionsEqual(left, right))
|
|
67
|
+
return false;
|
|
68
|
+
const leftCommand = getTaskCommand(left);
|
|
69
|
+
const rightCommand = getTaskCommand(right);
|
|
70
|
+
if (leftCommand !== rightCommand)
|
|
71
|
+
return true;
|
|
72
|
+
const leftDependencies = getTaskDependencies(left);
|
|
73
|
+
const rightDependencies = getTaskDependencies(right);
|
|
74
|
+
if (JSON.stringify(leftDependencies) !== JSON.stringify(rightDependencies)) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
const executionKeys = ["env", "files", "output"];
|
|
78
|
+
return executionKeys.some((key) => JSON.stringify(typeof left === "object" ? left[key] : undefined) !==
|
|
79
|
+
JSON.stringify(typeof right === "object" ? right[key] : undefined));
|
|
80
|
+
}
|
|
81
|
+
function packageManagerFromField(value) {
|
|
82
|
+
for (const manager of ["pnpm", "yarn", "npm", "deno"]) {
|
|
83
|
+
if (value?.startsWith(`${manager}@`))
|
|
84
|
+
return manager;
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
async function detectDependencyManager(cwd, packageJson, denoConfig) {
|
|
89
|
+
const declared = packageManagerFromField(packageJson?.packageManager);
|
|
90
|
+
if (declared)
|
|
91
|
+
return declared;
|
|
92
|
+
if (await readTextIfExists(join(cwd, "pnpm-lock.yaml")))
|
|
93
|
+
return "pnpm";
|
|
94
|
+
if (await readTextIfExists(join(cwd, "yarn.lock")))
|
|
95
|
+
return "yarn";
|
|
96
|
+
if (await readTextIfExists(join(cwd, "package-lock.json")))
|
|
97
|
+
return "npm";
|
|
98
|
+
if (denoConfig || await readTextIfExists(join(cwd, "deno.lock"))) {
|
|
99
|
+
return "deno";
|
|
100
|
+
}
|
|
101
|
+
return "npm";
|
|
102
|
+
}
|
|
103
|
+
function validateRecordedTooling(tooling, available) {
|
|
104
|
+
const configs = new Set([
|
|
105
|
+
"deno.json",
|
|
106
|
+
"deno.jsonc",
|
|
107
|
+
"package.json",
|
|
108
|
+
]);
|
|
109
|
+
const managers = new Set(["deno", "npm", "pnpm", "yarn"]);
|
|
110
|
+
if (!tooling || typeof tooling !== "object" ||
|
|
111
|
+
!tooling.tasks || typeof tooling.tasks !== "object" ||
|
|
112
|
+
!tooling.dependencies || typeof tooling.dependencies !== "object" ||
|
|
113
|
+
!configs.has(tooling.tasks.config) ||
|
|
114
|
+
!configs.has(tooling.dependencies.config) ||
|
|
115
|
+
!managers.has(tooling.tasks.runner) ||
|
|
116
|
+
!managers.has(tooling.dependencies.manager)) {
|
|
117
|
+
throw new Error("loom.json records invalid project tooling.");
|
|
118
|
+
}
|
|
119
|
+
if (!available.has(tooling.tasks.config)) {
|
|
120
|
+
throw new Error(`loom.json records missing task config: ${tooling.tasks.config}`);
|
|
121
|
+
}
|
|
122
|
+
if (!available.has(tooling.dependencies.config)) {
|
|
123
|
+
throw new Error(`loom.json records missing dependency config: ${tooling.dependencies.config}`);
|
|
124
|
+
}
|
|
125
|
+
if (tooling.tasks.config !== "package.json" && tooling.tasks.runner !== "deno") {
|
|
126
|
+
throw new Error(`${tooling.tasks.config} tasks must use the deno task runner.`);
|
|
127
|
+
}
|
|
128
|
+
if (tooling.dependencies.config !== "package.json" &&
|
|
129
|
+
tooling.dependencies.manager !== "deno") {
|
|
130
|
+
throw new Error(`${tooling.dependencies.config} dependencies must use the deno manager.`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Detects project tooling once during adoption. A recorded Loom choice is
|
|
135
|
+
* authoritative on later runs and is validated instead of inferred again.
|
|
136
|
+
*/
|
|
137
|
+
export async function detectProjectTooling(cwd, recorded) {
|
|
138
|
+
const packageText = await readTextIfExists(join(cwd, "package.json"));
|
|
139
|
+
const denoText = await readTextIfExists(join(cwd, "deno.json"));
|
|
140
|
+
const denoJsoncText = await readTextIfExists(join(cwd, "deno.jsonc"));
|
|
141
|
+
if (denoText !== null && denoJsoncText !== null) {
|
|
142
|
+
throw new Error("Both deno.json and deno.jsonc exist. Keep one Deno config or record a single unambiguous project config before running Loom.");
|
|
143
|
+
}
|
|
144
|
+
if (packageText === null && denoText === null && denoJsoncText === null) {
|
|
145
|
+
throw new Error(`No package.json, deno.json, or deno.jsonc found in ${cwd}`);
|
|
146
|
+
}
|
|
147
|
+
const denoConfigPath = denoText !== null
|
|
148
|
+
? "deno.json"
|
|
149
|
+
: denoJsoncText !== null
|
|
150
|
+
? "deno.jsonc"
|
|
151
|
+
: null;
|
|
152
|
+
const denoConfigText = denoText ?? denoJsoncText;
|
|
153
|
+
const packageJson = packageText === null
|
|
154
|
+
? null
|
|
155
|
+
: parseStrictJson(packageText, "package.json");
|
|
156
|
+
const denoConfig = denoConfigText === null || !denoConfigPath
|
|
157
|
+
? null
|
|
158
|
+
: parseJsonc(denoConfigText, denoConfigPath);
|
|
159
|
+
const available = new Set();
|
|
160
|
+
if (packageText !== null)
|
|
161
|
+
available.add("package.json");
|
|
162
|
+
if (denoConfigPath)
|
|
163
|
+
available.add(denoConfigPath);
|
|
164
|
+
if (recorded) {
|
|
165
|
+
validateRecordedTooling(recorded, available);
|
|
166
|
+
return recorded;
|
|
167
|
+
}
|
|
168
|
+
const dependencyManager = await detectDependencyManager(cwd, packageJson, denoConfig);
|
|
169
|
+
const dependencyConfig = packageJson
|
|
170
|
+
? "package.json"
|
|
171
|
+
: denoConfigPath;
|
|
172
|
+
const denoOwnsTasks = Boolean(denoConfig && hasOwn(denoConfig, "tasks"));
|
|
173
|
+
const taskConfig = denoOwnsTasks || !packageJson
|
|
174
|
+
? denoConfigPath
|
|
175
|
+
: "package.json";
|
|
176
|
+
const taskRunner = taskConfig === "package.json"
|
|
177
|
+
? dependencyManager
|
|
178
|
+
: "deno";
|
|
179
|
+
return {
|
|
180
|
+
dependencies: {
|
|
181
|
+
config: dependencyConfig,
|
|
182
|
+
manager: dependencyManager,
|
|
183
|
+
},
|
|
184
|
+
tasks: {
|
|
185
|
+
config: taskConfig,
|
|
186
|
+
runner: taskRunner,
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
/** Reads the task definitions from the recorded authoritative config. */
|
|
191
|
+
export async function readTaskStore(cwd, tooling) {
|
|
192
|
+
const text = await readTextIfExists(join(cwd, tooling.config));
|
|
193
|
+
if (text === null) {
|
|
194
|
+
throw new Error(`Missing authoritative task config: ${tooling.config}`);
|
|
195
|
+
}
|
|
196
|
+
if (tooling.config === "package.json") {
|
|
197
|
+
const packageJson = parseStrictJson(text, tooling.config);
|
|
198
|
+
const definitions = packageJson.scripts ?? {};
|
|
199
|
+
validateTaskDefinitions(definitions, tooling.config, false);
|
|
200
|
+
return {
|
|
201
|
+
config: tooling.config,
|
|
202
|
+
definitions,
|
|
203
|
+
rootKey: "scripts",
|
|
204
|
+
runner: tooling.runner,
|
|
205
|
+
text,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
const denoConfig = parseJsonc(text, tooling.config);
|
|
209
|
+
const definitions = denoConfig.tasks ?? {};
|
|
210
|
+
validateTaskDefinitions(definitions, tooling.config, true);
|
|
211
|
+
return {
|
|
212
|
+
config: tooling.config,
|
|
213
|
+
definitions,
|
|
214
|
+
rootKey: "tasks",
|
|
215
|
+
runner: tooling.runner,
|
|
216
|
+
text,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
function validateTaskDefinitions(definitions, config, allowStructured) {
|
|
220
|
+
if (!definitions || typeof definitions !== "object" ||
|
|
221
|
+
Array.isArray(definitions)) {
|
|
222
|
+
throw new Error(`${config} tasks/scripts must be an object.`);
|
|
223
|
+
}
|
|
224
|
+
for (const [name, definition] of Object.entries(definitions)) {
|
|
225
|
+
if (typeof definition === "string")
|
|
226
|
+
continue;
|
|
227
|
+
if (!allowStructured || !definition || typeof definition !== "object" ||
|
|
228
|
+
Array.isArray(definition)) {
|
|
229
|
+
throw new Error(`${config} task '${name}' has an invalid definition.`);
|
|
230
|
+
}
|
|
231
|
+
const task = definition;
|
|
232
|
+
if (task.command !== undefined && typeof task.command !== "string") {
|
|
233
|
+
throw new Error(`${config} task '${name}' has a non-string command.`);
|
|
234
|
+
}
|
|
235
|
+
if (task.dependencies !== undefined &&
|
|
236
|
+
(!Array.isArray(task.dependencies) ||
|
|
237
|
+
task.dependencies.some((dependency) => typeof dependency !== "string"))) {
|
|
238
|
+
throw new Error(`${config} task '${name}' has invalid dependencies.`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function parseNpmImport(value) {
|
|
243
|
+
if (typeof value !== "string")
|
|
244
|
+
return undefined;
|
|
245
|
+
const match = value.match(/^npm:((?:@[^/]+\/)?[^@/]+)(?:@([^/]+))?(?:\/.*)?$/);
|
|
246
|
+
return match ? { name: match[1], version: match[2] ?? "*" } : undefined;
|
|
247
|
+
}
|
|
248
|
+
/** Projects a Deno import map and task table into Loom's dependency signals. */
|
|
249
|
+
export function denoConfigAsPackageJson(config) {
|
|
250
|
+
const dependencies = {};
|
|
251
|
+
for (const value of Object.values(config.imports ?? {})) {
|
|
252
|
+
const dependency = parseNpmImport(value);
|
|
253
|
+
if (dependency)
|
|
254
|
+
dependencies[dependency.name] = dependency.version;
|
|
255
|
+
}
|
|
256
|
+
const scripts = Object.fromEntries(Object.entries(config.tasks ?? {}).flatMap(([name, definition]) => {
|
|
257
|
+
const command = getTaskCommand(definition);
|
|
258
|
+
return command ? [[name, command]] : [];
|
|
259
|
+
}));
|
|
260
|
+
return {
|
|
261
|
+
dependencies,
|
|
262
|
+
name: config.name,
|
|
263
|
+
packageManager: "deno",
|
|
264
|
+
scripts,
|
|
265
|
+
version: config.version,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
/** Reads package metadata, falling back to Deno imports/tasks for Deno-only projects. */
|
|
269
|
+
export async function readProjectPackageMetadata(cwd) {
|
|
270
|
+
const packageText = await readTextIfExists(join(cwd, "package.json"));
|
|
271
|
+
if (packageText !== null) {
|
|
272
|
+
return {
|
|
273
|
+
config: "package.json",
|
|
274
|
+
packageJson: parseStrictJson(packageText, "package.json"),
|
|
275
|
+
path: join(cwd, "package.json"),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
const denoJsonText = await readTextIfExists(join(cwd, "deno.json"));
|
|
279
|
+
const denoJsoncText = await readTextIfExists(join(cwd, "deno.jsonc"));
|
|
280
|
+
if (denoJsonText !== null && denoJsoncText !== null) {
|
|
281
|
+
throw new Error("Both deno.json and deno.jsonc exist.");
|
|
282
|
+
}
|
|
283
|
+
const config = denoJsonText !== null ? "deno.json" : "deno.jsonc";
|
|
284
|
+
const text = denoJsonText ?? denoJsoncText;
|
|
285
|
+
if (text === null) {
|
|
286
|
+
throw new Error(`No package.json, deno.json, or deno.jsonc found in ${cwd}`);
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
config: config,
|
|
290
|
+
packageJson: denoConfigAsPackageJson(parseJsonc(text, config)),
|
|
291
|
+
path: join(cwd, config),
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function npmImport(name, version) {
|
|
295
|
+
return `npm:${name}@${version}`;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Applies dependency changes to the recorded dependency owner. Deno projects
|
|
299
|
+
* use imports and share one dependency class; package projects retain the
|
|
300
|
+
* dependencies/devDependencies distinction.
|
|
301
|
+
*/
|
|
302
|
+
export async function mutateProjectDependencies(cwd, tooling, mutation) {
|
|
303
|
+
const path = join(cwd, tooling.config);
|
|
304
|
+
const original = await readTextIfExists(path);
|
|
305
|
+
if (original === null) {
|
|
306
|
+
throw new Error(`Missing authoritative dependency config: ${tooling.config}`);
|
|
307
|
+
}
|
|
308
|
+
let text = original;
|
|
309
|
+
const changedKeys = [];
|
|
310
|
+
const removedKeys = [];
|
|
311
|
+
if (tooling.config === "package.json") {
|
|
312
|
+
let config = parseStrictJson(text, tooling.config);
|
|
313
|
+
for (const [field, additions] of [
|
|
314
|
+
["dependencies", mutation.dependencies],
|
|
315
|
+
["devDependencies", mutation.devDependencies],
|
|
316
|
+
]) {
|
|
317
|
+
for (const [name, version] of Object.entries(additions ?? {})) {
|
|
318
|
+
if (!mutation.replace && config[field]?.[name])
|
|
319
|
+
continue;
|
|
320
|
+
if (config[field]?.[name] === version)
|
|
321
|
+
continue;
|
|
322
|
+
text = updateJsonPath(text, [field, name], version);
|
|
323
|
+
config = parseStrictJson(text, tooling.config);
|
|
324
|
+
changedKeys.push(name);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
for (const name of mutation.remove ?? []) {
|
|
328
|
+
for (const field of ["dependencies", "devDependencies"]) {
|
|
329
|
+
if (!config[field]?.[name])
|
|
330
|
+
continue;
|
|
331
|
+
text = updateJsonPath(text, [field, name], undefined);
|
|
332
|
+
config = parseStrictJson(text, tooling.config);
|
|
333
|
+
removedKeys.push(name);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
let config = parseJsonc(text, tooling.config);
|
|
339
|
+
const additions = {
|
|
340
|
+
...mutation.devDependencies,
|
|
341
|
+
...mutation.dependencies,
|
|
342
|
+
};
|
|
343
|
+
for (const [name, version] of Object.entries(additions)) {
|
|
344
|
+
const desired = npmImport(name, version);
|
|
345
|
+
if (!mutation.replace && config.imports?.[name])
|
|
346
|
+
continue;
|
|
347
|
+
if (config.imports?.[name] === desired)
|
|
348
|
+
continue;
|
|
349
|
+
text = updateJsonPath(text, ["imports", name], desired);
|
|
350
|
+
config = parseJsonc(text, tooling.config);
|
|
351
|
+
changedKeys.push(name);
|
|
352
|
+
}
|
|
353
|
+
for (const name of mutation.remove ?? []) {
|
|
354
|
+
if (!config.imports?.[name])
|
|
355
|
+
continue;
|
|
356
|
+
text = updateJsonPath(text, ["imports", name], undefined);
|
|
357
|
+
config = parseJsonc(text, tooling.config);
|
|
358
|
+
removedKeys.push(name);
|
|
359
|
+
}
|
|
360
|
+
if (config.nodeModulesDir === undefined) {
|
|
361
|
+
text = updateJsonPath(text, ["nodeModulesDir"], "auto");
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if (text !== original)
|
|
365
|
+
await writeFile(path, text, "utf8");
|
|
366
|
+
return {
|
|
367
|
+
changed: text !== original,
|
|
368
|
+
changedKeys: [...new Set(changedKeys)],
|
|
369
|
+
config: tooling.config,
|
|
370
|
+
removedKeys: [...new Set(removedKeys)],
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
/** Adds or removes local dependency links in a Deno config. */
|
|
374
|
+
export async function mutateDenoLinks(cwd, config, mutation) {
|
|
375
|
+
const path = join(cwd, config);
|
|
376
|
+
const original = await readTextIfExists(path);
|
|
377
|
+
if (original === null)
|
|
378
|
+
throw new Error(`Missing Deno config: ${config}`);
|
|
379
|
+
const denoConfig = parseJsonc(original, config);
|
|
380
|
+
let links = [...(denoConfig.links ?? [])];
|
|
381
|
+
if (mutation.remove) {
|
|
382
|
+
links = links.filter((link) => link !== mutation.remove);
|
|
383
|
+
}
|
|
384
|
+
if (mutation.add && !links.includes(mutation.add))
|
|
385
|
+
links.push(mutation.add);
|
|
386
|
+
const text = JSON.stringify(links) === JSON.stringify(denoConfig.links ?? [])
|
|
387
|
+
? original
|
|
388
|
+
: updateJsonPath(original, ["links"], links.length ? links : undefined);
|
|
389
|
+
if (text !== original)
|
|
390
|
+
await writeFile(path, text, "utf8");
|
|
391
|
+
return { changed: text !== original, links };
|
|
392
|
+
}
|
|
393
|
+
/** Returns the configured local links without resolving their paths. */
|
|
394
|
+
export async function readDenoLinks(cwd, config) {
|
|
395
|
+
const text = await readTextIfExists(join(cwd, config));
|
|
396
|
+
if (text === null)
|
|
397
|
+
throw new Error(`Missing Deno config: ${config}`);
|
|
398
|
+
return parseJsonc(text, config).links ?? [];
|
|
399
|
+
}
|
|
400
|
+
/** Returns the executable command portion of either task representation. */
|
|
401
|
+
export function getTaskCommand(definition) {
|
|
402
|
+
if (typeof definition === "string")
|
|
403
|
+
return definition;
|
|
404
|
+
return typeof definition?.command === "string"
|
|
405
|
+
? definition.command
|
|
406
|
+
: undefined;
|
|
407
|
+
}
|
|
408
|
+
export function getTaskDependencies(definition) {
|
|
409
|
+
return typeof definition === "object" &&
|
|
410
|
+
Array.isArray(definition.dependencies)
|
|
411
|
+
? definition.dependencies.filter((value) => typeof value === "string")
|
|
412
|
+
: [];
|
|
413
|
+
}
|
|
414
|
+
/** Formats a task invocation for the selected project runner. */
|
|
415
|
+
export function formatTaskInvocation(tasks, name) {
|
|
416
|
+
switch (tasks.runner) {
|
|
417
|
+
case "deno":
|
|
418
|
+
return `deno task ${name}`;
|
|
419
|
+
case "npm":
|
|
420
|
+
return `npm run ${name}`;
|
|
421
|
+
case "pnpm":
|
|
422
|
+
return `pnpm ${name}`;
|
|
423
|
+
case "yarn":
|
|
424
|
+
return `yarn ${name}`;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
function taskInvocationPatterns(name) {
|
|
428
|
+
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
429
|
+
const tail = `(?:[\\s;&|)]|$)`;
|
|
430
|
+
return [
|
|
431
|
+
new RegExp(`(?:^|[\\s;&|($])deno\\s+task\\s+${escaped}${tail}`),
|
|
432
|
+
new RegExp(`(?:^|[\\s;&|($])npm\\s+run\\s+${escaped}${tail}`),
|
|
433
|
+
new RegExp(`(?:^|[\\s;&|($])pnpm\\s+(?:run\\s+)?${escaped}${tail}`),
|
|
434
|
+
new RegExp(`(?:^|[\\s;&|($])yarn\\s+(?:run\\s+)?${escaped}${tail}`),
|
|
435
|
+
];
|
|
436
|
+
}
|
|
437
|
+
function commandInvokesTask(command, name) {
|
|
438
|
+
return Boolean(command &&
|
|
439
|
+
taskInvocationPatterns(name).some((pattern) => pattern.test(command)));
|
|
440
|
+
}
|
|
441
|
+
/** Checks direct and transitive task references without executing shell code. */
|
|
442
|
+
export function taskInvokes(store, source, target) {
|
|
443
|
+
const visiting = new Set();
|
|
444
|
+
const visit = (name) => {
|
|
445
|
+
if (name === target)
|
|
446
|
+
return true;
|
|
447
|
+
if (visiting.has(name))
|
|
448
|
+
return false;
|
|
449
|
+
visiting.add(name);
|
|
450
|
+
const definition = store.definitions[name];
|
|
451
|
+
const command = getTaskCommand(definition);
|
|
452
|
+
if (commandInvokesTask(command, target))
|
|
453
|
+
return true;
|
|
454
|
+
if (getTaskDependencies(definition).some(visit))
|
|
455
|
+
return true;
|
|
456
|
+
return Object.keys(store.definitions).some((candidate) => candidate !== name && commandInvokesTask(command, candidate) &&
|
|
457
|
+
visit(candidate));
|
|
458
|
+
};
|
|
459
|
+
return source !== target && visit(source);
|
|
460
|
+
}
|
|
461
|
+
/** Checks whether a task, or a task it invokes, contains a command fragment. */
|
|
462
|
+
export function taskInvokesCommand(store, source, expectedCommand) {
|
|
463
|
+
const normalizedExpected = expectedCommand.trim().replace(/\s+/g, " ");
|
|
464
|
+
const visiting = new Set();
|
|
465
|
+
const visit = (name) => {
|
|
466
|
+
if (visiting.has(name))
|
|
467
|
+
return false;
|
|
468
|
+
visiting.add(name);
|
|
469
|
+
const definition = store.definitions[name];
|
|
470
|
+
const command = getTaskCommand(definition)?.replace(/\s+/g, " ");
|
|
471
|
+
if (command?.includes(normalizedExpected))
|
|
472
|
+
return true;
|
|
473
|
+
if (getTaskDependencies(definition).some(visit))
|
|
474
|
+
return true;
|
|
475
|
+
return Object.keys(store.definitions).some((candidate) => candidate !== name && commandInvokesTask(command, candidate) &&
|
|
476
|
+
visit(candidate));
|
|
477
|
+
};
|
|
478
|
+
return normalizedExpected.length > 0 && visit(source);
|
|
479
|
+
}
|
|
480
|
+
/** Verifies that a task invocation is ordered before a command fragment. */
|
|
481
|
+
export function taskRunsBeforeCommand(store, source, prerequisite, beforeCommand) {
|
|
482
|
+
const definition = store.definitions[source];
|
|
483
|
+
const command = getTaskCommand(definition);
|
|
484
|
+
if (!command || !command.includes(beforeCommand))
|
|
485
|
+
return false;
|
|
486
|
+
if (getTaskDependencies(definition).some((dependency) => dependency === prerequisite ||
|
|
487
|
+
taskInvokes(store, dependency, prerequisite))) {
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
const commandIndex = command.indexOf(beforeCommand);
|
|
491
|
+
return taskInvocationPatterns(prerequisite).some((pattern) => {
|
|
492
|
+
const match = pattern.exec(command);
|
|
493
|
+
return match !== null && match.index < commandIndex;
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
/** Ensures one task through a localized JSON/JSONC edit. */
|
|
497
|
+
export async function ensureTask(cwd, tooling, options) {
|
|
498
|
+
const store = await readTaskStore(cwd, tooling);
|
|
499
|
+
validateTaskDefinitions({ [options.name]: options.definition }, store.config, store.config !== "package.json");
|
|
500
|
+
const previous = store.definitions[options.name];
|
|
501
|
+
if (previous !== undefined && definitionsEqual(previous, options.definition)) {
|
|
502
|
+
return { changed: false, config: store.config, previous, preserved: false };
|
|
503
|
+
}
|
|
504
|
+
if (previous !== undefined && options.onConflict === "preserve") {
|
|
505
|
+
return { changed: false, config: store.config, previous, preserved: true };
|
|
506
|
+
}
|
|
507
|
+
const text = updateJsonPath(store.text, [store.rootKey, options.name], options.definition);
|
|
508
|
+
if (text !== store.text) {
|
|
509
|
+
await writeFile(join(cwd, store.config), text, "utf8");
|
|
510
|
+
}
|
|
511
|
+
return {
|
|
512
|
+
changed: text !== store.text,
|
|
513
|
+
config: store.config,
|
|
514
|
+
previous,
|
|
515
|
+
preserved: false,
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Ensures a prerequisite runs before a named task. Deno tasks use their native
|
|
520
|
+
* dependency graph; package scripts receive the runner-specific invocation.
|
|
521
|
+
*/
|
|
522
|
+
export async function ensureTaskPrerequisite(cwd, tooling, taskName, prerequisite) {
|
|
523
|
+
const store = await readTaskStore(cwd, tooling);
|
|
524
|
+
const current = store.definitions[taskName];
|
|
525
|
+
if (current === undefined) {
|
|
526
|
+
throw new Error(`Cannot compose missing task: ${taskName}`);
|
|
527
|
+
}
|
|
528
|
+
if (taskInvokes(store, taskName, prerequisite)) {
|
|
529
|
+
return { changed: false, config: store.config };
|
|
530
|
+
}
|
|
531
|
+
let definition;
|
|
532
|
+
if (store.config !== "package.json") {
|
|
533
|
+
if (typeof current === "string") {
|
|
534
|
+
definition = { command: current, dependencies: [prerequisite] };
|
|
535
|
+
}
|
|
536
|
+
else {
|
|
537
|
+
definition = {
|
|
538
|
+
...current,
|
|
539
|
+
dependencies: [...getTaskDependencies(current), prerequisite],
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
const command = getTaskCommand(current);
|
|
545
|
+
if (!command)
|
|
546
|
+
throw new Error(`Task has no command: ${taskName}`);
|
|
547
|
+
definition = `${formatTaskInvocation(tooling, prerequisite)} && (${command})`;
|
|
548
|
+
}
|
|
549
|
+
const result = await ensureTask(cwd, tooling, {
|
|
550
|
+
definition,
|
|
551
|
+
name: taskName,
|
|
552
|
+
});
|
|
553
|
+
return { changed: result.changed, config: result.config };
|
|
554
|
+
}
|
|
555
|
+
/** Ensures a named task runs after another task's existing command succeeds. */
|
|
556
|
+
export async function ensureTaskFollowup(cwd, tooling, taskName, followup) {
|
|
557
|
+
const store = await readTaskStore(cwd, tooling);
|
|
558
|
+
const current = store.definitions[taskName];
|
|
559
|
+
if (current === undefined) {
|
|
560
|
+
throw new Error(`Cannot compose missing task: ${taskName}`);
|
|
561
|
+
}
|
|
562
|
+
if (taskInvokes(store, taskName, followup)) {
|
|
563
|
+
return { changed: false, config: store.config };
|
|
564
|
+
}
|
|
565
|
+
const command = getTaskCommand(current);
|
|
566
|
+
if (!command)
|
|
567
|
+
throw new Error(`Task has no command: ${taskName}`);
|
|
568
|
+
const composed = `(${command}) && ${formatTaskInvocation(tooling, followup)}`;
|
|
569
|
+
const definition = typeof current === "string"
|
|
570
|
+
? composed
|
|
571
|
+
: { ...current, command: composed };
|
|
572
|
+
const result = await ensureTask(cwd, tooling, {
|
|
573
|
+
definition,
|
|
574
|
+
name: taskName,
|
|
575
|
+
});
|
|
576
|
+
return { changed: result.changed, config: result.config };
|
|
577
|
+
}
|
|
578
|
+
function delegatesToAuthoritativeDenoTask(primaryConfig, secondaryConfig, name, definition) {
|
|
579
|
+
return primaryConfig !== "package.json" &&
|
|
580
|
+
secondaryConfig === "package.json" &&
|
|
581
|
+
getTaskCommand(definition)?.trim() === `deno task ${name}`;
|
|
582
|
+
}
|
|
583
|
+
/** Finds duplicate task names that differ and do not delegate to Deno. */
|
|
584
|
+
export async function findTaskConflicts(cwd, authoritative) {
|
|
585
|
+
const primary = await readTaskStore(cwd, authoritative);
|
|
586
|
+
const secondaryConfigs = primary.config === "package.json"
|
|
587
|
+
? ["deno.json", "deno.jsonc"]
|
|
588
|
+
: ["package.json"];
|
|
589
|
+
const conflicts = [];
|
|
590
|
+
for (const secondaryConfig of secondaryConfigs) {
|
|
591
|
+
const text = await readTextIfExists(join(cwd, secondaryConfig));
|
|
592
|
+
if (text === null)
|
|
593
|
+
continue;
|
|
594
|
+
const secondary = await readTaskStore(cwd, {
|
|
595
|
+
config: secondaryConfig,
|
|
596
|
+
runner: secondaryConfig === "package.json" ? "npm" : "deno",
|
|
597
|
+
});
|
|
598
|
+
for (const [name, definition] of Object.entries(primary.definitions)) {
|
|
599
|
+
const other = secondary.definitions[name];
|
|
600
|
+
if (other === undefined || !definitionsConflict(definition, other) ||
|
|
601
|
+
delegatesToAuthoritativeDenoTask(primary.config, secondaryConfig, name, other)) {
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
conflicts.push({
|
|
605
|
+
authoritative: definition,
|
|
606
|
+
authoritativeConfig: primary.config,
|
|
607
|
+
name,
|
|
608
|
+
secondary: other,
|
|
609
|
+
secondaryConfig,
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return conflicts;
|
|
614
|
+
}
|
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
import { type JobOutputSanitizer } from "./sanitize.js";
|
|
1
2
|
import type { DeferredTask } from "./types.js";
|
|
2
3
|
export interface DeferredTaskErrorContext {
|
|
4
|
+
controller: JobController;
|
|
3
5
|
deferred: DeferredTask;
|
|
4
6
|
error: unknown;
|
|
5
7
|
}
|
|
6
8
|
export type DeferredTaskErrorHandler = (context: DeferredTaskErrorContext) => void;
|
|
9
|
+
/** Aggregate result for one pass over a controller's deferred tasks. */
|
|
10
|
+
export interface DeferredTaskRunResult {
|
|
11
|
+
failureCount: number;
|
|
12
|
+
}
|
|
7
13
|
export declare function defaultDeferredTaskErrorHandler(context: DeferredTaskErrorContext): void;
|
|
8
14
|
export declare class JobController {
|
|
9
15
|
static readonly _logLevels: readonly ["fatal", "error", "warn", "info", "log", "debug", "trace"];
|
|
@@ -12,8 +18,13 @@ export declare class JobController {
|
|
|
12
18
|
_cancelled: boolean;
|
|
13
19
|
_deferred: DeferredTask[];
|
|
14
20
|
_maxLogLevel: number;
|
|
15
|
-
|
|
21
|
+
_outputSanitationFailed: boolean;
|
|
22
|
+
_outputSanitationFailureEmitted: boolean;
|
|
23
|
+
_outputSanitizer?: JobOutputSanitizer;
|
|
24
|
+
constructor(controller: ReadableStreamDefaultController<string>, maxLogLevel?: number, outputSanitizer?: JobOutputSanitizer);
|
|
16
25
|
json<T>(obj: T): void;
|
|
26
|
+
/** @internal Emit one fixed failure event without invoking a failed policy. */
|
|
27
|
+
_emitOutputSanitationFailure(): void;
|
|
17
28
|
_log(levelInt: number, args: unknown[]): void;
|
|
18
29
|
fatal(...args: unknown[]): void;
|
|
19
30
|
error(...args: unknown[]): void;
|
|
@@ -24,5 +35,7 @@ export declare class JobController {
|
|
|
24
35
|
trace(...args: unknown[]): void;
|
|
25
36
|
defer(task: () => Promise<void> | void, label?: string): void;
|
|
26
37
|
runDeferredTasks(onError?: DeferredTaskErrorHandler): Promise<void>;
|
|
38
|
+
/** Run deferred tasks while reporting an additive aggregate failure count. */
|
|
39
|
+
runDeferredTasksWithResult(onError?: DeferredTaskErrorHandler): Promise<DeferredTaskRunResult>;
|
|
27
40
|
}
|
|
28
41
|
//# sourceMappingURL=controller.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../../src/src/jobs/controller.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,WAAW,wBAAwB;IACvC,QAAQ,EAAE,YAAY,CAAC;IACvB,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,MAAM,wBAAwB,GAAG,CACrC,OAAO,EAAE,wBAAwB,KAC9B,IAAI,CAAC;AAEV,wBAAgB,+BAA+B,CAC7C,OAAO,EAAE,wBAAwB,QAYlC;AAED,qBAAa,aAAa;IACxB,MAAM,CAAC,QAAQ,CAAC,UAAU,uEAAkB;IAC5C,MAAM,CAAC,QAAQ,CAAC,aAAa,4FAAwB;IAErD,WAAW,EAAE,+BAA+B,CAAC,MAAM,CAAC,CAAC;IACrD,UAAU,UAAS;IACnB,SAAS,EAAE,YAAY,EAAE,CAAM;IAC/B,YAAY,SAA6B;
|
|
1
|
+
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../../src/src/jobs/controller.ts"],"names":[],"mappings":"AAKA,OAAO,EAGL,KAAK,kBAAkB,EACxB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,WAAW,wBAAwB;IACvC,UAAU,EAAE,aAAa,CAAC;IAC1B,QAAQ,EAAE,YAAY,CAAC;IACvB,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,MAAM,wBAAwB,GAAG,CACrC,OAAO,EAAE,wBAAwB,KAC9B,IAAI,CAAC;AAEV,wEAAwE;AACxE,MAAM,WAAW,qBAAqB;IACpC,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,+BAA+B,CAC7C,OAAO,EAAE,wBAAwB,QAYlC;AAED,qBAAa,aAAa;IACxB,MAAM,CAAC,QAAQ,CAAC,UAAU,uEAAkB;IAC5C,MAAM,CAAC,QAAQ,CAAC,aAAa,4FAAwB;IAErD,WAAW,EAAE,+BAA+B,CAAC,MAAM,CAAC,CAAC;IACrD,UAAU,UAAS;IACnB,SAAS,EAAE,YAAY,EAAE,CAAM;IAC/B,YAAY,SAA6B;IACzC,uBAAuB,UAAS;IAChC,+BAA+B,UAAS;IACxC,gBAAgB,CAAC,EAAE,kBAAkB,CAAC;gBAGpC,UAAU,EAAE,+BAA+B,CAAC,MAAM,CAAC,EACnD,WAAW,CAAC,EAAE,MAAM,EACpB,eAAe,CAAC,EAAE,kBAAkB;IAStC,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC;IAed,+EAA+E;IAC/E,4BAA4B;IAc5B,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE;IAMtC,KAAK,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE;IAIxB,KAAK,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE;IAIxB,IAAI,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE;IAIvB,IAAI,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE;IAIvB,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE;IAItB,KAAK,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE;IAIxB,KAAK,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE;IAIxB,KAAK,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,EAAE,KAAK,CAAC,EAAE,MAAM;IAIhD,gBAAgB,CACpB,OAAO,GAAE,wBAA0D,GAClE,OAAO,CAAC,IAAI,CAAC;IAIhB,8EAA8E;IACxE,0BAA0B,CAC9B,OAAO,GAAE,wBAA0D,GAClE,OAAO,CAAC,qBAAqB,CAAC;CAYlC"}
|