@pluginfinity/engine 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.
- package/agents.js +88 -0
- package/body.js +110 -0
- package/component.js +157 -0
- package/discovery.js +1 -1
- package/doctor.js +1 -1
- package/emit.js +133 -0
- package/errors.js +198 -5
- package/frontmatter.js +169 -0
- package/hooks.js +118 -0
- package/index.d.ts +566 -9
- package/index.js +9 -2
- package/loader.js +3 -1
- package/manifest.js +45 -0
- package/operations.js +254 -8
- package/package.json +4 -3
- package/skills.js +132 -0
- package/version.js +1 -1
package/errors.js
CHANGED
|
@@ -82,7 +82,8 @@ var ConfigInvalid = class extends Schema.TaggedError()("ConfigInvalid", {
|
|
|
82
82
|
};
|
|
83
83
|
/**
|
|
84
84
|
* The config sets a top-level key that is neither a base field nor a known
|
|
85
|
-
* target id.
|
|
85
|
+
* target id. Base fields and target ids share one key space, so the key may
|
|
86
|
+
* be a misspelt field as easily as an unknown target; the message lists both.
|
|
86
87
|
*
|
|
87
88
|
* @public
|
|
88
89
|
*/
|
|
@@ -91,13 +92,16 @@ var UnknownTarget = class extends Schema.TaggedError()("UnknownTarget", {
|
|
|
91
92
|
/** The unknown keys, in the order the config spells them. */
|
|
92
93
|
targets: Schema.Array(Schema.String),
|
|
93
94
|
/** Every known target id. */
|
|
94
|
-
known: Schema.Array(Schema.String)
|
|
95
|
+
known: Schema.Array(Schema.String),
|
|
96
|
+
/** Every base config field. */
|
|
97
|
+
fields: Schema.Array(Schema.String)
|
|
95
98
|
}) {
|
|
96
99
|
get message() {
|
|
97
|
-
|
|
100
|
+
const keys = this.targets.map((target) => `"${target}"`).join(", ");
|
|
101
|
+
return `${this.path} has unknown key ${keys}: not a config field (${this.fields.join(", ")}) or a known target (${this.known.join(", ")})`;
|
|
98
102
|
}
|
|
99
103
|
get remediation() {
|
|
100
|
-
return { hint: `
|
|
104
|
+
return { hint: `Fix the spelling of the key in ${this.path}, or remove it.` };
|
|
101
105
|
}
|
|
102
106
|
};
|
|
103
107
|
/**
|
|
@@ -143,6 +147,195 @@ operation: Schema.String }) {
|
|
|
143
147
|
return `pluginfinity ${this.operation} is not implemented yet`;
|
|
144
148
|
}
|
|
145
149
|
};
|
|
150
|
+
/**
|
|
151
|
+
* The `package.json` beside a config is missing, unreadable, or has no
|
|
152
|
+
* `version` string. Every manifest copies its version from there.
|
|
153
|
+
*
|
|
154
|
+
* @public
|
|
155
|
+
*/
|
|
156
|
+
var PackageVersionMissing = class extends Schema.TaggedError()("PackageVersionMissing", {
|
|
157
|
+
/** The `package.json` path that was read. */
|
|
158
|
+
path: Schema.String }) {
|
|
159
|
+
get message() {
|
|
160
|
+
return `${this.path} is missing or has no "version" string`;
|
|
161
|
+
}
|
|
162
|
+
get remediation() {
|
|
163
|
+
return { hint: `Add a package.json with a "version" field beside the config; every manifest copies it.` };
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* How one target's `builds/<id>/` differs from a fresh build.
|
|
168
|
+
*
|
|
169
|
+
* @public
|
|
170
|
+
*/
|
|
171
|
+
var TargetDrift = class extends Schema.Class("TargetDrift")({
|
|
172
|
+
target: Schema.String,
|
|
173
|
+
added: Schema.Array(Schema.String),
|
|
174
|
+
changed: Schema.Array(Schema.String),
|
|
175
|
+
removed: Schema.Array(Schema.String)
|
|
176
|
+
}) {};
|
|
177
|
+
/**
|
|
178
|
+
* `build --check` or `validate` found `builds/` out of date with the source.
|
|
179
|
+
*
|
|
180
|
+
* @public
|
|
181
|
+
*/
|
|
182
|
+
var BuildStale = class extends Schema.TaggedError()("BuildStale", {
|
|
183
|
+
/** The config whose builds drifted. */
|
|
184
|
+
path: Schema.String,
|
|
185
|
+
targets: Schema.Array(TargetDrift)
|
|
186
|
+
}) {
|
|
187
|
+
get message() {
|
|
188
|
+
const describe = (drift) => [
|
|
189
|
+
["added", drift.added],
|
|
190
|
+
["changed", drift.changed],
|
|
191
|
+
["removed", drift.removed]
|
|
192
|
+
].filter(([, files]) => files.length > 0).map(([verb, files]) => `${verb} ${files.join(", ")}`).join("; ");
|
|
193
|
+
return `builds are out of date for ${this.path}: ${this.targets.map((drift) => `${drift.target} would have ${describe(drift)}`).join("; ")}`;
|
|
194
|
+
}
|
|
195
|
+
get remediation() {
|
|
196
|
+
return { hint: "Run `pluginfinity build` and commit the result." };
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
/**
|
|
200
|
+
* A host's own CLI rejected a build, or could not be run to check it.
|
|
201
|
+
*
|
|
202
|
+
* @public
|
|
203
|
+
*/
|
|
204
|
+
var HostRejected = class extends Schema.TaggedError()("HostRejected", {
|
|
205
|
+
/** The build directory the host checked. */
|
|
206
|
+
path: Schema.String,
|
|
207
|
+
target: Schema.String,
|
|
208
|
+
/** The command line, as run. */
|
|
209
|
+
command: Schema.String,
|
|
210
|
+
/** What the host printed, stdout then stderr, trimmed. */
|
|
211
|
+
output: Schema.String
|
|
212
|
+
}) {
|
|
213
|
+
get message() {
|
|
214
|
+
return `${this.target} rejected ${this.path}: ${this.output === "" ? `\`${this.command}\` failed` : this.output}`;
|
|
215
|
+
}
|
|
216
|
+
get remediation() {
|
|
217
|
+
return { hint: `Run \`${this.command}\` to see the host's report, or pass --no-host to skip host checks.` };
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* A target lacks a hook event the config uses, and an entry for it does not
|
|
222
|
+
* set `fallback: "omit"`.
|
|
223
|
+
*
|
|
224
|
+
* @public
|
|
225
|
+
*/
|
|
226
|
+
var HookEventUnsupported = class extends Schema.TaggedError()("HookEventUnsupported", {
|
|
227
|
+
/** The config. */
|
|
228
|
+
path: Schema.String,
|
|
229
|
+
target: Schema.String,
|
|
230
|
+
events: Schema.Array(Schema.String)
|
|
231
|
+
}) {
|
|
232
|
+
get message() {
|
|
233
|
+
return `${this.target} has no hook event ${this.events.map((event) => `"${event}"`).join(", ")}, used in ${this.path}`;
|
|
234
|
+
}
|
|
235
|
+
get remediation() {
|
|
236
|
+
return { hint: `Set \`fallback: "omit"\` on those entries to skip them on ${this.target}, or give ${this.target} its own hooks for the event.` };
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
/**
|
|
240
|
+
* What is wrong with a hook script: absent from the plugin, or lacking the
|
|
241
|
+
* executable bit under `scripts.invoke: "exec"`.
|
|
242
|
+
*
|
|
243
|
+
* @public
|
|
244
|
+
*/
|
|
245
|
+
const HookScriptProblem = Schema.Literals(["missing", "not-executable"]);
|
|
246
|
+
/**
|
|
247
|
+
* A hook `script` cannot be shipped as configured.
|
|
248
|
+
*
|
|
249
|
+
* @public
|
|
250
|
+
*/
|
|
251
|
+
var HookScriptInvalid = class extends Schema.TaggedError()("HookScriptInvalid", {
|
|
252
|
+
/** The config. */
|
|
253
|
+
path: Schema.String,
|
|
254
|
+
/** The script, relative to the plugin root. */
|
|
255
|
+
script: Schema.String,
|
|
256
|
+
problem: HookScriptProblem
|
|
257
|
+
}) {
|
|
258
|
+
get message() {
|
|
259
|
+
return this.problem === "missing" ? `hook script ${this.script} named in ${this.path} does not exist` : `hook script ${this.script} is not executable, and ${this.path} sets scripts.invoke to "exec"`;
|
|
260
|
+
}
|
|
261
|
+
get remediation() {
|
|
262
|
+
return this.problem === "missing" ? { hint: `Create ${this.script} under the plugin root, or fix the path in ${this.path}.` } : { hint: `Run \`chmod +x ${this.script}\`, or drop scripts.invoke so hooks run through bash.` };
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* A copied source file and a generated file would land on the same build path.
|
|
267
|
+
*
|
|
268
|
+
* @public
|
|
269
|
+
*/
|
|
270
|
+
var PathConflict = class extends Schema.TaggedError()("PathConflict", {
|
|
271
|
+
/** The config. */
|
|
272
|
+
path: Schema.String,
|
|
273
|
+
target: Schema.String,
|
|
274
|
+
/** The build path both claim, relative to `builds/<target>/`. */
|
|
275
|
+
file: Schema.String
|
|
276
|
+
}) {
|
|
277
|
+
get message() {
|
|
278
|
+
return `${this.target} generates ${this.file}, but the plugin also ships a source file at that path`;
|
|
279
|
+
}
|
|
280
|
+
get remediation() {
|
|
281
|
+
return { hint: `Delete or move the source ${this.file}; pluginfinity writes that file itself.` };
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
/**
|
|
285
|
+
* A skill or agent file does not decode, or cannot be built for a target:
|
|
286
|
+
* bad frontmatter, a name that breaks the rules, a malformed host block, or a
|
|
287
|
+
* field the target cannot place.
|
|
288
|
+
*
|
|
289
|
+
* @public
|
|
290
|
+
*/
|
|
291
|
+
var ComponentInvalid = class extends Schema.TaggedError()("ComponentInvalid", {
|
|
292
|
+
/** The component file, or its directory when the file is missing. */
|
|
293
|
+
path: Schema.String,
|
|
294
|
+
/** The target the problem is specific to, when it is. */
|
|
295
|
+
target: Schema.optionalKey(Schema.String),
|
|
296
|
+
issues: Schema.Array(ConfigIssue)
|
|
297
|
+
}) {
|
|
298
|
+
get message() {
|
|
299
|
+
return `${this.target === void 0 ? this.path : `${this.path} (for ${this.target})`}: ${this.issues.map((issue) => issue.key === "" ? issue.message : `${issue.key}: ${issue.message}`).join("; ")}`;
|
|
300
|
+
}
|
|
301
|
+
get remediation() {
|
|
302
|
+
return this.target === void 0 ? { hint: `Correct the listed problems in ${this.path}.` } : { hint: `Correct the listed problems in ${this.path}, or set the field for ${this.target} in its \`targets.${this.target}\` block.` };
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
/**
|
|
306
|
+
* Every skill and agent problem one build found, so a single run reports them
|
|
307
|
+
* all.
|
|
308
|
+
*
|
|
309
|
+
* @public
|
|
310
|
+
*/
|
|
311
|
+
var ComponentsInvalid = class extends Schema.TaggedError()("ComponentsInvalid", {
|
|
312
|
+
/** The config. */
|
|
313
|
+
path: Schema.String,
|
|
314
|
+
components: Schema.Array(ComponentInvalid)
|
|
315
|
+
}) {
|
|
316
|
+
get message() {
|
|
317
|
+
const count = this.components.length;
|
|
318
|
+
return [`${count} ${count === 1 ? "component is" : "components are"} invalid in the plugin of ${this.path}:`, ...this.components.map((component) => ` - ${component.message}`)].join("\n");
|
|
319
|
+
}
|
|
320
|
+
get remediation() {
|
|
321
|
+
return { hint: "Correct each listed file; a field one host cannot take can be set in that host's `targets` block." };
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
const BUILD_ERROR_TAGS = [
|
|
325
|
+
"PackageVersionMissing",
|
|
326
|
+
"BuildStale",
|
|
327
|
+
"HostRejected",
|
|
328
|
+
"HookEventUnsupported",
|
|
329
|
+
"HookScriptInvalid",
|
|
330
|
+
"PathConflict",
|
|
331
|
+
"ComponentsInvalid"
|
|
332
|
+
];
|
|
333
|
+
/**
|
|
334
|
+
* Whether `error` is one of the {@link BuildError} members.
|
|
335
|
+
*
|
|
336
|
+
* @public
|
|
337
|
+
*/
|
|
338
|
+
const isBuildError = (error) => typeof error === "object" && error !== null && "_tag" in error && typeof error._tag === "string" && BUILD_ERROR_TAGS.includes(error._tag);
|
|
146
339
|
|
|
147
340
|
//#endregion
|
|
148
|
-
export { ConfigAmbiguous, ConfigInvalid, ConfigIssue, ConfigLoadFailed, ConfigNotFound, NotImplemented, TargetNotEnabled, UnknownTarget, isConfigError };
|
|
341
|
+
export { BuildStale, ComponentInvalid, ComponentsInvalid, ConfigAmbiguous, ConfigInvalid, ConfigIssue, ConfigLoadFailed, ConfigNotFound, HookEventUnsupported, HookScriptInvalid, HookScriptProblem, HostRejected, NotImplemented, PackageVersionMissing, PathConflict, TargetDrift, TargetNotEnabled, UnknownTarget, isBuildError, isConfigError };
|
package/frontmatter.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
//#region src/frontmatter.ts
|
|
2
|
+
/**
|
|
3
|
+
* Split `text` at a frontmatter block that opens on the first line with `---`
|
|
4
|
+
* and closes at the next line holding only `---`; `undefined` when there is
|
|
5
|
+
* none.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
const splitFrontmatter = (text) => {
|
|
10
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/.exec(text);
|
|
11
|
+
return match === null ? void 0 : {
|
|
12
|
+
frontmatter: match[1] ?? "",
|
|
13
|
+
body: text.slice(match[0].length)
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* How a degraded field is phrased when it moves into `description`, by field.
|
|
18
|
+
* A field with no entry uses its own name as the label.
|
|
19
|
+
*/
|
|
20
|
+
const SUFFIX_LABELS = {
|
|
21
|
+
when_to_use: "Also use when",
|
|
22
|
+
paths: "Applies to files matching"
|
|
23
|
+
};
|
|
24
|
+
const asText = (value) => Array.isArray(value) ? value.map(String).join(", ") : String(value);
|
|
25
|
+
/**
|
|
26
|
+
* A tool list as entries: an array as is, a string split on commas and
|
|
27
|
+
* whitespace outside parentheses, so a rule such as `Bash(git log:*)` stays
|
|
28
|
+
* one entry.
|
|
29
|
+
*/
|
|
30
|
+
const toolNames = (value) => {
|
|
31
|
+
if (Array.isArray(value)) return value.map(String);
|
|
32
|
+
const names = [];
|
|
33
|
+
let current = "";
|
|
34
|
+
let depth = 0;
|
|
35
|
+
for (const char of String(value)) {
|
|
36
|
+
if (char === "(") depth += 1;
|
|
37
|
+
if (char === ")") depth = Math.max(0, depth - 1);
|
|
38
|
+
if (depth === 0 && /[\s,]/.test(char)) {
|
|
39
|
+
if (current.length > 0) names.push(current);
|
|
40
|
+
current = "";
|
|
41
|
+
} else current += char;
|
|
42
|
+
}
|
|
43
|
+
if (current.length > 0) names.push(current);
|
|
44
|
+
return names;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* A Claude Code MCP tool name, `mcp__<server>__<tool>`, in the target's MCP
|
|
48
|
+
* spelling, or `undefined` when it is not one the target can spell: any other
|
|
49
|
+
* name, or a server named `plugin_…`, which belongs to another plugin whose
|
|
50
|
+
* server name on the target is unknown.
|
|
51
|
+
*/
|
|
52
|
+
const mcpToolName = (target, name) => {
|
|
53
|
+
const match = /^mcp__(.+?)__(.+)$/.exec(name);
|
|
54
|
+
if (match === null || (match[1] ?? "").startsWith("plugin_")) return void 0;
|
|
55
|
+
return target.tools.mcp.replace("{server}", match[1] ?? "").replace("{tool}", match[2] ?? "");
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Map one component's frontmatter onto a target through the target's field
|
|
59
|
+
* map.
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* The component's `targets` block for this target overlays the base fields:
|
|
63
|
+
* a core field there replaces the base value before mapping, and a host field
|
|
64
|
+
* (one of `hostFields`) is written as is. When the block sets `description`,
|
|
65
|
+
* no field is degraded into it: the author wrote that host's description.
|
|
66
|
+
*
|
|
67
|
+
* @public
|
|
68
|
+
*/
|
|
69
|
+
const mapFrontmatter = (target, map, hostFields, base, block) => {
|
|
70
|
+
const coreOverlay = Object.fromEntries(Object.entries(block).filter(([key]) => key in map));
|
|
71
|
+
const hostOverlay = Object.entries(block).filter(([key]) => hostFields.includes(key));
|
|
72
|
+
const unknown = Object.keys(block).filter((key) => !(key in map) && !hostFields.includes(key));
|
|
73
|
+
const source = {
|
|
74
|
+
...base,
|
|
75
|
+
...coreOverlay
|
|
76
|
+
};
|
|
77
|
+
delete source.targets;
|
|
78
|
+
const fields = {};
|
|
79
|
+
const suffixes = [];
|
|
80
|
+
const sections = [];
|
|
81
|
+
const unresolved = [];
|
|
82
|
+
for (const [field, value] of Object.entries(source)) {
|
|
83
|
+
if (value === void 0) continue;
|
|
84
|
+
const entry = map[field];
|
|
85
|
+
if (entry === void 0) continue;
|
|
86
|
+
switch (entry._tag) {
|
|
87
|
+
case "keep":
|
|
88
|
+
fields[field] = value;
|
|
89
|
+
break;
|
|
90
|
+
case "rename":
|
|
91
|
+
fields[entry.to] = value;
|
|
92
|
+
break;
|
|
93
|
+
case "drop": break;
|
|
94
|
+
case "translate": {
|
|
95
|
+
const name = entry.to ?? field;
|
|
96
|
+
if (entry.table !== "tools") {
|
|
97
|
+
const mapped = target[entry.table][String(value)];
|
|
98
|
+
if (mapped === void 0) fields[name] = value;
|
|
99
|
+
else if (typeof mapped === "string") fields[name] = mapped;
|
|
100
|
+
else if (mapped._tag === "unresolved") unresolved.push({
|
|
101
|
+
field: `${field}: ${String(value)}`,
|
|
102
|
+
note: mapped.note
|
|
103
|
+
});
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
const names = [];
|
|
107
|
+
for (const name of toolNames(value)) {
|
|
108
|
+
const rule = /^([^(]+)\(.*\)$/.exec(name);
|
|
109
|
+
if (rule !== null && target.tools.names[rule[1] ?? ""] !== void 0) {
|
|
110
|
+
unresolved.push({
|
|
111
|
+
field: `${field}: ${name}`,
|
|
112
|
+
note: `${rule[1]} is renamed on this host, which has no per-command tool rules; set ${field} in this host's targets block`
|
|
113
|
+
});
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const mapped = target.tools.names[name] ?? mcpToolName(target, name) ?? (target.tools.unlisted === "keep" ? name : void 0);
|
|
117
|
+
if (mapped === void 0) continue;
|
|
118
|
+
if (typeof mapped === "string") {
|
|
119
|
+
if (!names.includes(mapped)) names.push(mapped);
|
|
120
|
+
} else if (mapped._tag === "unresolved") unresolved.push({
|
|
121
|
+
field: `${field}: ${name}`,
|
|
122
|
+
note: mapped.note
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
fields[name] = names;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case "degrade":
|
|
129
|
+
if (entry.form === "description-suffix") {
|
|
130
|
+
if (!("description" in block)) suffixes.push(`${SUFFIX_LABELS[field] ?? field}: ${asText(value)}`);
|
|
131
|
+
} else sections.push({
|
|
132
|
+
field,
|
|
133
|
+
value
|
|
134
|
+
});
|
|
135
|
+
break;
|
|
136
|
+
case "unresolved": unresolved.push({
|
|
137
|
+
field,
|
|
138
|
+
note: entry.note
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (suffixes.length > 0 && typeof fields.description === "string") fields.description = [fields.description.trimEnd(), ...suffixes].join(" ");
|
|
143
|
+
for (const [key, value] of hostOverlay) fields[key] = value;
|
|
144
|
+
return {
|
|
145
|
+
fields,
|
|
146
|
+
sections,
|
|
147
|
+
unresolved,
|
|
148
|
+
unknown
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
/** The heading a field degraded to a body section is written under, by field. */
|
|
152
|
+
const SECTION_TITLES = { skills: "Skills" };
|
|
153
|
+
/**
|
|
154
|
+
* Append each degraded field to `body` as a section: a level-two heading and
|
|
155
|
+
* the value as a bullet list, marked like the body's first bullet list (`-`
|
|
156
|
+
* when it has none), so a linter that wants one marker per document passes.
|
|
157
|
+
*
|
|
158
|
+
* @public
|
|
159
|
+
*/
|
|
160
|
+
const appendSections = (body, sections) => {
|
|
161
|
+
const marker = /^[-+*](?= )/m.exec(body)?.[0] ?? "-";
|
|
162
|
+
return sections.reduce((text, { field, value }) => {
|
|
163
|
+
const items = (Array.isArray(value) ? value : [value]).map((item) => `${marker} ${String(item)}`).join("\n");
|
|
164
|
+
return `${text.trimEnd()}\n\n## ${SECTION_TITLES[field] ?? field}\n\n${items}\n`;
|
|
165
|
+
}, body);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
//#endregion
|
|
169
|
+
export { appendSections, mapFrontmatter, splitFrontmatter };
|
package/hooks.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
//#region src/hooks.ts
|
|
2
|
+
/**
|
|
3
|
+
* The hooks a target builds, before rendering: the base `hooks` with the
|
|
4
|
+
* target's per-event overrides applied (an override replaces the base entries
|
|
5
|
+
* for its event, and `[]` removes them), each event resolved to the target's
|
|
6
|
+
* name. An event the target lacks drops its `"omit"` entries and reports the
|
|
7
|
+
* event when any entry would fail.
|
|
8
|
+
*
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
const targetHooks = (target, id, config) => {
|
|
12
|
+
const setting = config[id];
|
|
13
|
+
const override = typeof setting === "object" ? setting.hooks ?? {} : {};
|
|
14
|
+
const merged = {
|
|
15
|
+
...config.hooks,
|
|
16
|
+
...override
|
|
17
|
+
};
|
|
18
|
+
const events = [];
|
|
19
|
+
const unsupported = [];
|
|
20
|
+
for (const [event, entries] of Object.entries(merged)) {
|
|
21
|
+
if (entries === void 0 || entries.length === 0) continue;
|
|
22
|
+
const mapped = target.hooks.ownEvents.includes(event) ? event : target.hooks.events[event];
|
|
23
|
+
if (typeof mapped === "string") events.push({
|
|
24
|
+
event,
|
|
25
|
+
name: mapped,
|
|
26
|
+
entries
|
|
27
|
+
});
|
|
28
|
+
else if (entries.some((entry) => entry.fallback !== "omit")) unsupported.push({ event });
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
events,
|
|
32
|
+
unsupported
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Every `script` path the given events run.
|
|
37
|
+
*
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
const hookScripts = (events) => [...new Set(events.flatMap((event) => event.entries.flatMap((entry) => "script" in entry ? [entry.script] : [])))];
|
|
41
|
+
const COMMAND_FILE = /\$\{PLUGIN_ROOT\}\/([^\s"'`;|&<>()$]+)/g;
|
|
42
|
+
/**
|
|
43
|
+
* Every file the given events' `command` entries name as
|
|
44
|
+
* `${PLUGIN_ROOT}/<path>`, so the build can ship and check them as it does
|
|
45
|
+
* `script` paths.
|
|
46
|
+
*
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
const hookCommandFiles = (events) => [...new Set(events.flatMap((event) => event.entries.flatMap((entry) => "command" in entry ? [...entry.command.matchAll(COMMAND_FILE)].map((match) => match[1] ?? "") : [])))];
|
|
50
|
+
const shellQuote = (word) => /^[A-Za-z0-9_\-./=:@%+,]+$/.test(word) ? word : `'${word.replaceAll("'", `'\\''`)}'`;
|
|
51
|
+
const PLUGIN_ROOT = `\${PLUGIN_ROOT}`;
|
|
52
|
+
const SAFE_PATH = /^[A-Za-z0-9_\-./@%+,=:]+$/;
|
|
53
|
+
/**
|
|
54
|
+
* The shell command a hook entry runs on a target: a `script` through `bash`
|
|
55
|
+
* (or executed directly under `scripts.invoke: "exec"`) at the target's
|
|
56
|
+
* plugin root, or a `command` with `${PLUGIN_ROOT}` spelled the target's way.
|
|
57
|
+
*
|
|
58
|
+
* @public
|
|
59
|
+
*/
|
|
60
|
+
const hookCommand = (entry, root, invoke) => {
|
|
61
|
+
if ("command" in entry) return entry.command.replaceAll(PLUGIN_ROOT, root);
|
|
62
|
+
const path = SAFE_PATH.test(entry.script) ? `"${root}/${entry.script}"` : `"${root}"/${shellQuote(entry.script)}`;
|
|
63
|
+
const args = (entry.args ?? []).map(shellQuote);
|
|
64
|
+
return [...invoke === "bash" ? ["bash", path] : [path], ...args].join(" ");
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* A `script` entry in exec form, for a host that spawns `command` with `args`
|
|
68
|
+
* and no shell: `bash` with the script path as its first argument, or under
|
|
69
|
+
* `scripts.invoke: "exec"` the script itself. The root placeholder is
|
|
70
|
+
* substituted by the host in `args`, and no shell ever parses the path.
|
|
71
|
+
*
|
|
72
|
+
* @public
|
|
73
|
+
*/
|
|
74
|
+
const hookExec = (entry, root, invoke) => {
|
|
75
|
+
const path = `${root}/${entry.script}`;
|
|
76
|
+
const args = [...entry.args ?? []];
|
|
77
|
+
return invoke === "bash" ? {
|
|
78
|
+
command: "bash",
|
|
79
|
+
args: [path, ...args]
|
|
80
|
+
} : {
|
|
81
|
+
command: path,
|
|
82
|
+
args
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
const FORMATS = {
|
|
86
|
+
"claude-hooks-json": (events, command, exec) => ({ hooks: Object.fromEntries(events.map(({ name, entries }) => [name, entries.map((entry) => ({
|
|
87
|
+
...entry.matcher === void 0 ? {} : { matcher: entry.matcher },
|
|
88
|
+
hooks: [{
|
|
89
|
+
type: "command",
|
|
90
|
+
..."script" in entry ? exec(entry) : { command: command(entry) },
|
|
91
|
+
...entry.timeout === void 0 ? {} : { timeout: entry.timeout }
|
|
92
|
+
}]
|
|
93
|
+
}))])) }),
|
|
94
|
+
"copilot-hooks-v1": (events, command) => ({
|
|
95
|
+
version: 1,
|
|
96
|
+
hooks: Object.fromEntries(events.map(({ name, entries }) => [name, entries.map((entry) => ({
|
|
97
|
+
type: "command",
|
|
98
|
+
bash: command(entry),
|
|
99
|
+
...entry.matcher === void 0 ? {} : { matcher: entry.matcher },
|
|
100
|
+
...entry.timeout === void 0 ? {} : { timeoutSec: entry.timeout }
|
|
101
|
+
}))]))
|
|
102
|
+
})
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Render a target's hooks file as JSON text, or `undefined` when the target
|
|
106
|
+
* builds no hooks.
|
|
107
|
+
*
|
|
108
|
+
* @public
|
|
109
|
+
*/
|
|
110
|
+
const renderHooks = (target, events, invoke) => {
|
|
111
|
+
if (events.length === 0) return void 0;
|
|
112
|
+
const root = target.pluginRoot.hooks;
|
|
113
|
+
if (typeof root !== "string") throw new Error(`target has no plugin-root spelling for hook commands: ${root.note}`);
|
|
114
|
+
return `${JSON.stringify(FORMATS[target.hooks.format](events, (entry) => hookCommand(entry, root, invoke), (entry) => hookExec(entry, root, invoke)), null, " ")}\n`;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
//#endregion
|
|
118
|
+
export { hookCommand, hookCommandFiles, hookExec, hookScripts, renderHooks, targetHooks };
|