@pluginfinity/engine 0.2.0 → 0.3.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/env-lib.generated.js +12 -0
- package/env.js +128 -0
- package/errors.js +43 -8
- package/hook-lib.generated.js +1 -1
- package/hook-lib.js +15 -8
- package/hook-output.js +68 -0
- package/hooks.js +105 -12
- package/index.d.ts +388 -21
- package/index.js +6 -3
- package/lib-files.js +39 -0
- package/log-lib.generated.js +9 -0
- package/logs.js +147 -0
- package/manifest.js +6 -4
- package/monitor-lib.generated.js +9 -0
- package/monitors.js +88 -0
- package/notes.js +8 -1
- package/operations.js +145 -33
- package/package.json +3 -3
- package/server-lib.generated.js +1 -1
- package/server-lib.js +3 -2
- package/servers.js +30 -23
- package/skills.js +6 -2
- package/tokens.js +72 -6
- package/tool-map.js +35 -0
- package/version.js +1 -1
package/logs.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { preparePlugins } from "./selection.js";
|
|
2
|
+
import { pluginName } from "./manifest.js";
|
|
3
|
+
import { Effect, FileSystem, Path } from "effect";
|
|
4
|
+
|
|
5
|
+
//#region src/logs.ts
|
|
6
|
+
const LINE = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z) \[([^\]]*)\] (hook|server|monitor|script)\/([^:]*): (.*)$/;
|
|
7
|
+
/**
|
|
8
|
+
* Parse one log line, or carry it through as raw when it does not follow the
|
|
9
|
+
* standard.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
const parseLogLine = (line, plugin, file) => {
|
|
14
|
+
const match = LINE.exec(line);
|
|
15
|
+
if (match === null) return {
|
|
16
|
+
raw: line,
|
|
17
|
+
plugin,
|
|
18
|
+
file
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
ts: match[1],
|
|
22
|
+
host: match[2],
|
|
23
|
+
component: match[3],
|
|
24
|
+
script: match[4],
|
|
25
|
+
message: match[5],
|
|
26
|
+
plugin,
|
|
27
|
+
file
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The directory every plugin's logs live under: `<stateHome>/pluginfinity`.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
const logRoot = (stateHome) => Effect.map(Path.Path, (path) => path.join(stateHome, "pluginfinity"));
|
|
36
|
+
/**
|
|
37
|
+
* One plugin's log directory: `<stateHome>/pluginfinity/<plugin>`.
|
|
38
|
+
*
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
const logDirectory = (stateHome, plugin) => Effect.gen(function* () {
|
|
42
|
+
return (yield* Path.Path).join(yield* logRoot(stateHome), plugin);
|
|
43
|
+
});
|
|
44
|
+
/**
|
|
45
|
+
* The plugin names that have a log directory, sorted; none when the state
|
|
46
|
+
* directory does not exist.
|
|
47
|
+
*
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
const listLogPlugins = (stateHome) => Effect.gen(function* () {
|
|
51
|
+
const fs = yield* FileSystem.FileSystem;
|
|
52
|
+
const path = yield* Path.Path;
|
|
53
|
+
const root = yield* logRoot(stateHome);
|
|
54
|
+
if (!(yield* fs.exists(root))) return [];
|
|
55
|
+
const names = [];
|
|
56
|
+
for (const entry of yield* fs.readDirectory(root)) if ((yield* fs.stat(path.join(root, entry))).type === "Directory") names.push(entry);
|
|
57
|
+
return names.sort();
|
|
58
|
+
});
|
|
59
|
+
/**
|
|
60
|
+
* The names the selected config's targets publish under: the config's `name`
|
|
61
|
+
* and each target's own `name` override, deduplicated, in first-seen order.
|
|
62
|
+
* Each target's components log under its own name.
|
|
63
|
+
*
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
66
|
+
const configPluginNames = (selection) => Effect.map(preparePlugins({
|
|
67
|
+
selection,
|
|
68
|
+
targets: []
|
|
69
|
+
}), (prepared) => [...new Set(prepared.flatMap(({ config, targets }) => targets.map((id) => pluginName(config.config, id))))]);
|
|
70
|
+
const DECODER = new TextDecoder();
|
|
71
|
+
const NEWLINE = 10;
|
|
72
|
+
/** The complete lines in `bytes` and how many bytes they cover; a last line with no newline is held back. */
|
|
73
|
+
const completeLines = (bytes) => {
|
|
74
|
+
const end = bytes.lastIndexOf(NEWLINE) + 1;
|
|
75
|
+
if (end === 0) return {
|
|
76
|
+
lines: [],
|
|
77
|
+
length: 0
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
lines: DECODER.decode(bytes.subarray(0, end)).replace(/\r?\n$/, "").split(/\r?\n/),
|
|
81
|
+
length: end
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* The last `lines` complete lines of a plugin's log file.
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* An absent file is `present: false` with no lines, not a failure: a plugin
|
|
89
|
+
* that has logged nothing has no file.
|
|
90
|
+
*
|
|
91
|
+
* @public
|
|
92
|
+
*/
|
|
93
|
+
const readLog = (input) => Effect.gen(function* () {
|
|
94
|
+
const fs = yield* FileSystem.FileSystem;
|
|
95
|
+
const path = (yield* Path.Path).join(yield* logDirectory(input.stateHome, input.plugin), input.file);
|
|
96
|
+
if (!(yield* fs.exists(path))) return {
|
|
97
|
+
plugin: input.plugin,
|
|
98
|
+
file: input.file,
|
|
99
|
+
path,
|
|
100
|
+
present: false,
|
|
101
|
+
lines: [],
|
|
102
|
+
offset: 0
|
|
103
|
+
};
|
|
104
|
+
const { lines, length } = completeLines(yield* fs.readFile(path));
|
|
105
|
+
return {
|
|
106
|
+
plugin: input.plugin,
|
|
107
|
+
file: input.file,
|
|
108
|
+
path,
|
|
109
|
+
present: true,
|
|
110
|
+
lines: input.lines <= 0 ? [] : lines.slice(-input.lines),
|
|
111
|
+
offset: length
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
/**
|
|
115
|
+
* The complete lines appended to `path` after byte `offset`, and the offset to
|
|
116
|
+
* resume from.
|
|
117
|
+
*
|
|
118
|
+
* @remarks
|
|
119
|
+
* A file smaller than `offset` was truncated or replaced, so reading restarts
|
|
120
|
+
* from its beginning. A file that does not exist yields no lines at offset 0.
|
|
121
|
+
*
|
|
122
|
+
* @public
|
|
123
|
+
*/
|
|
124
|
+
const readLogFrom = (path, offset) => Effect.gen(function* () {
|
|
125
|
+
const fs = yield* FileSystem.FileSystem;
|
|
126
|
+
if (!(yield* fs.exists(path))) return {
|
|
127
|
+
lines: [],
|
|
128
|
+
offset: 0
|
|
129
|
+
};
|
|
130
|
+
const size = Number((yield* fs.stat(path)).size);
|
|
131
|
+
const from = size < offset ? 0 : offset;
|
|
132
|
+
if (size === from) return {
|
|
133
|
+
lines: [],
|
|
134
|
+
offset: from
|
|
135
|
+
};
|
|
136
|
+
const { lines, length } = completeLines((yield* fs.readFile(path)).subarray(from));
|
|
137
|
+
return {
|
|
138
|
+
lines,
|
|
139
|
+
offset: from + length
|
|
140
|
+
};
|
|
141
|
+
}).pipe(Effect.catch((error) => error.reason._tag === "NotFound" ? Effect.succeed({
|
|
142
|
+
lines: [],
|
|
143
|
+
offset: 0
|
|
144
|
+
}) : Effect.fail(error)));
|
|
145
|
+
|
|
146
|
+
//#endregion
|
|
147
|
+
export { configPluginNames, listLogPlugins, logDirectory, logRoot, parseLogLine, readLog, readLogFrom };
|
package/manifest.js
CHANGED
|
@@ -15,14 +15,16 @@ const FORMATS = {
|
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
17
|
/**
|
|
18
|
-
* Render a target's manifest from the config
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* Render a target's manifest from the config, the package version and the
|
|
19
|
+
* server maps the target places in its manifest (`ServerRender.manifest`):
|
|
20
|
+
* the format's record, cut to the target's key allowlist, in allowlist order,
|
|
21
|
+
* with unset keys left out.
|
|
21
22
|
*
|
|
22
23
|
* @public
|
|
23
24
|
*/
|
|
24
|
-
const renderManifest = (target, id, config, version) => {
|
|
25
|
+
const renderManifest = (target, id, config, version, servers = {}) => {
|
|
25
26
|
const record = FORMATS[target.manifest.format](target, {
|
|
27
|
+
...servers,
|
|
26
28
|
name: pluginName(config, id),
|
|
27
29
|
version,
|
|
28
30
|
description: config.description,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/monitor-lib.generated.ts
|
|
2
|
+
/** The monitor library's files, embedded so a build needs no asset on disk. */
|
|
3
|
+
const MONITOR_LIB_FILES = [{
|
|
4
|
+
name: "monitor.sh",
|
|
5
|
+
content: "# shellcheck shell=sh\n# pluginfinity monitor library. `pluginfinity build` writes this file into every\n# target that has monitors, at lib/pluginfinity/monitor.sh; do not edit a copy\n# under builds/, the next build overwrites it.\n#\n# POSIX sh. Writes to stdout only through monitor_notify: one line on stdout is\n# one notification. Set _pf_lib_dir to this file's directory, source it, then\n# call the functions. From a script at monitors/x.sh:\n# _pf_lib_dir=\"$(dirname \"$0\")/../lib/pluginfinity\"; . \"$_pf_lib_dir/monitor.sh\"\n#\n# A monitor never exits non-zero because of an error inside one poll: the\n# library logs it and the loop keeps going.\n#\n# monitor_notify cannot tell monitor_every that stdout closed when it runs inside\n# $(...) or a pipeline subshell; call it directly from the polled function.\n#\n# PLUGINFINITY_MONITOR_MAX_TICKS=<n> is a contract every monitor honours: stop\n# after n checks, however triggered (a poll, a startup sweep, or one handled\n# event). n is a positive integer; monitor_every logs any other value once and\n# ignores it (unbounded). monitor_every does it for you; a monitor that does not use it,\n# such as a node `command` monitor, must honour it itself, because the bats\n# helper's run_monitor sets it to bound a test. Do not set it in a plugin.\n\n# POSIX sh cannot find a sourced file's own path, so the sourcer sets _pf_lib_dir\n# (or _pf_log_dir, the log.sh convention) to this file's directory first. log.sh\n# and host.sh live beside this file. Without a readable log.sh the logging\n# functions are no-ops and the monitor still runs.\n_pf_mon_dir=\"${_pf_lib_dir:-${_pf_log_dir:-.}}\"\n_pf_log_dir=\"$_pf_mon_dir\"\nif [ -r \"$_pf_mon_dir/log.sh\" ]; then\n # shellcheck source=/dev/null\n . \"$_pf_mon_dir/log.sh\"\nelse\n pf_log() { return 0; }\n pf_debug() { return 0; }\n pf_debug_on() { return 1; }\nfi\n\n# A closed stdout must surface as a failed write, not as SIGPIPE killing the monitor.\ntrap '' PIPE\n_pf_mon_closed=0\n\nmonitor_name() { printf '%s\\n' \"${PLUGINFINITY_MONITOR:-$(basename \"$0\" .sh)}\"; }\nmonitor_host() { printf '%s\\n' \"${PLUGINFINITY_HOST:-unknown}\"; }\nmonitor_plugin_root() { (cd \"$_pf_mon_dir/../..\" 2>/dev/null && pwd -P) || printf '%s\\n' \"$_pf_mon_dir/../..\"; }\n\nmonitor_project_dir() {\n if [ -n \"${CLAUDE_PROJECT_DIR:-}\" ]; then\n printf '%s\\n' \"$CLAUDE_PROJECT_DIR\"\n return 0\n fi\n _pf_g=$(git rev-parse --show-toplevel 2>/dev/null) || _pf_g=\n printf '%s\\n' \"${_pf_g:-$PWD}\"\n}\n\nmonitor_log() { pf_log monitor \"$@\"; }\nmonitor_debug() { pf_debug monitor \"$@\"; }\n\n# monitor_notify <text...>: one line on stdout, flushed. Newlines become spaces.\n# Returns 1 when stdout is closed (and monitor_every then stops).\nmonitor_notify() {\n _pf_t=$(printf '%s' \"$*\" | tr '\\r\\n' ' ')\n if [ -z \"$_pf_t\" ]; then\n monitor_debug \"monitor_notify: empty text ignored\"\n return 0\n fi\n if ! printf '%s\\n' \"$_pf_t\" 2>/dev/null; then\n _pf_mon_closed=1\n return 1\n fi\n return 0\n}\n\n# monitor_state_dir: $XDG_STATE_HOME/pluginfinity/<plugin>/monitor/<name>/, created.\nmonitor_state_dir() {\n _pf_s=\"${XDG_STATE_HOME:-${HOME:-/nonexistent}/.local/state}/pluginfinity/${PLUGINFINITY_PLUGIN:-unknown}/monitor/$(monitor_name)/\"\n mkdir -p \"$_pf_s\" 2>/dev/null || :\n printf '%s\\n' \"$_pf_s\"\n}\n\n# monitor_once <key> <text...>: monitor_notify only the first time <key> is seen this session.\nmonitor_once() {\n _pf_k=${1:-}\n if [ $# -gt 0 ]; then shift; fi\n _pf_sd=$(monitor_state_dir)\n _pf_sess=\"${CLAUDE_CODE_SESSION_ID:-${CLAUDE_SESSION_ID:-$PPID}}\"\n # Keys are made filename-safe; the session scopes the marker.\n _pf_m=\"$_pf_sd$(printf '%s' \"$_pf_sess.$_pf_k\" | tr -c 'A-Za-z0-9._-' '_')\"\n if [ -e \"$_pf_m\" ]; then\n return 0\n fi\n monitor_notify \"$@\" || return 1\n : >\"$_pf_m\" 2>/dev/null || :\n}\n\n# monitor_every <seconds> <function>: call <function> now and every <seconds>, forever.\n# A failing call is logged and the loop continues; a closed stdout ends it with exit 0.\nmonitor_every() {\n _pf_iv=${1:-}\n case \"$_pf_iv\" in\n '' | *[!0-9]*)\n monitor_log \"monitor_every: interval '$_pf_iv' is not a number of seconds; using 60\"\n _pf_iv=60\n ;;\n esac\n _pf_fn=${2:-:}\n # PLUGINFINITY_MONITOR_MAX_TICKS is a positive integer; anything else is\n # logged once and treated as unset, so a test mistake never loops on an error.\n _pf_max=${PLUGINFINITY_MONITOR_MAX_TICKS:-}\n case \"$_pf_max\" in\n '') ;;\n *[!0-9]*)\n monitor_log \"monitor_every: PLUGINFINITY_MONITOR_MAX_TICKS '$_pf_max' is not a positive integer; ignoring it\"\n _pf_max=\n ;;\n *)\n if [ \"$_pf_max\" -le 0 ] 2>/dev/null; then\n monitor_log \"monitor_every: PLUGINFINITY_MONITOR_MAX_TICKS '$_pf_max' is not a positive integer; ignoring it\"\n _pf_max=\n fi\n ;;\n esac\n _pf_n=0\n while :; do\n if \"$_pf_fn\"; then _pf_rc=0; else _pf_rc=$?; fi\n if [ \"$_pf_mon_closed\" = 1 ]; then\n monitor_debug \"stdout closed; stopping\"\n exit 0\n fi\n if [ \"$_pf_rc\" -ne 0 ]; then\n monitor_log \"$_pf_fn failed (exit $_pf_rc)\"\n fi\n _pf_n=$((_pf_n + 1))\n if [ -n \"$_pf_max\" ] && [ \"$_pf_n\" -ge \"$_pf_max\" ]; then\n return 0\n fi\n sleep \"$_pf_iv\"\n done\n}\n"
|
|
6
|
+
}];
|
|
7
|
+
|
|
8
|
+
//#endregion
|
|
9
|
+
export { MONITOR_LIB_FILES };
|
package/monitors.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ConfigIssue } from "./errors.js";
|
|
2
|
+
import { CONFIG_NOTE_PATH } from "./notes.js";
|
|
3
|
+
import { commandFiles, hookCommand, shellEnvPrefix } from "./hooks.js";
|
|
4
|
+
|
|
5
|
+
//#region src/monitors.ts
|
|
6
|
+
/**
|
|
7
|
+
* The monitors a target builds: the base `monitors` with the target's own
|
|
8
|
+
* replacing the base monitor of the same name.
|
|
9
|
+
*
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
const targetMonitors = (id, config) => {
|
|
13
|
+
const setting = config[id];
|
|
14
|
+
return {
|
|
15
|
+
...config.monitors,
|
|
16
|
+
...typeof setting === "object" ? setting.monitors : void 0
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
const SKILL_WHEN = "on-skill-invoke:";
|
|
20
|
+
/**
|
|
21
|
+
* Render a target's monitors file. A target with no monitors drops every
|
|
22
|
+
* monitor with a `monitor-omitted` note and ships neither file nor script.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* Each entry's command carries `PLUGINFINITY_MONITOR` (the monitor's name,
|
|
26
|
+
* which the monitor library logs under), quoted by `shellEnvPrefix`: a prefix
|
|
27
|
+
* on a script, an `export` before a command.
|
|
28
|
+
*
|
|
29
|
+
* A `when` of `on-skill-invoke:<skill>` is written `on-skill-invoke:<plugin>:<skill>`: Claude
|
|
30
|
+
* Code matches the plugin-qualified skill name only, and a bare name never starts the monitor.
|
|
31
|
+
* A skill the target does not build is an issue, since the monitor would never start.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
const renderMonitors = (target, id, monitors, invoke, context) => {
|
|
36
|
+
const entries = Object.entries(monitors);
|
|
37
|
+
if (entries.length === 0) return {
|
|
38
|
+
notes: [],
|
|
39
|
+
issues: [],
|
|
40
|
+
scripts: [],
|
|
41
|
+
commandFiles: []
|
|
42
|
+
};
|
|
43
|
+
const placement = target.monitors;
|
|
44
|
+
if (!("path" in placement)) return {
|
|
45
|
+
notes: entries.map(([name]) => ({
|
|
46
|
+
target: id,
|
|
47
|
+
path: CONFIG_NOTE_PATH,
|
|
48
|
+
kind: "monitor-omitted",
|
|
49
|
+
name
|
|
50
|
+
})),
|
|
51
|
+
issues: [],
|
|
52
|
+
scripts: [],
|
|
53
|
+
commandFiles: []
|
|
54
|
+
};
|
|
55
|
+
const issues = [];
|
|
56
|
+
const array = entries.map(([name, entry]) => {
|
|
57
|
+
const env = shellEnvPrefix({ PLUGINFINITY_MONITOR: name });
|
|
58
|
+
const command = "script" in entry ? `${env}${hookCommand(entry, placement.root, invoke)}` : `export ${env.trimEnd()}; ${hookCommand(entry, placement.root, invoke)}`;
|
|
59
|
+
let when = entry.when;
|
|
60
|
+
if (when !== void 0 && when.startsWith(SKILL_WHEN)) {
|
|
61
|
+
const skill = when.slice(16);
|
|
62
|
+
if (!context.skills.has(skill)) issues.push(ConfigIssue.make({
|
|
63
|
+
key: `monitors.${name}.when`,
|
|
64
|
+
message: `names the skill "${skill}", which this plugin does not build for ${id}`
|
|
65
|
+
}));
|
|
66
|
+
when = `${SKILL_WHEN}${context.plugin}:${skill}`;
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
name,
|
|
70
|
+
command,
|
|
71
|
+
description: entry.description,
|
|
72
|
+
...when === void 0 ? {} : { when }
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
file: {
|
|
77
|
+
path: placement.path,
|
|
78
|
+
content: `${JSON.stringify(array, null, " ")}\n`
|
|
79
|
+
},
|
|
80
|
+
notes: [],
|
|
81
|
+
issues,
|
|
82
|
+
scripts: [...new Set(entries.flatMap(([, entry]) => "script" in entry ? [entry.script] : []))],
|
|
83
|
+
commandFiles: [...new Set(entries.flatMap(([, entry]) => "command" in entry ? commandFiles(entry.command) : []))]
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
export { renderMonitors, targetMonitors };
|
package/notes.js
CHANGED
|
@@ -8,7 +8,14 @@ const BUILD_NOTE_KINDS = [
|
|
|
8
8
|
"dropped",
|
|
9
9
|
"degraded",
|
|
10
10
|
"tool-dropped",
|
|
11
|
-
"hook-
|
|
11
|
+
"hook-matcher-runtime",
|
|
12
|
+
"hook-output-ignored",
|
|
13
|
+
"hook-omitted",
|
|
14
|
+
"monitor-omitted",
|
|
15
|
+
"env-shell-unsupported",
|
|
16
|
+
"env-wait-timeout",
|
|
17
|
+
"hook-matcher-widened",
|
|
18
|
+
"hook-matcher-regex"
|
|
12
19
|
];
|
|
13
20
|
/** The path of notes about the config: hooks and servers. */
|
|
14
21
|
const CONFIG_NOTE_PATH = "config";
|
package/operations.js
CHANGED
|
@@ -4,13 +4,18 @@ import { readAgents, renderAgent } from "./agents.js";
|
|
|
4
4
|
import { preparePlugins } from "./selection.js";
|
|
5
5
|
import { ENGINE_VERSION } from "./version.js";
|
|
6
6
|
import { applyEmit, planEmit } from "./emit.js";
|
|
7
|
-
import { hookCommandFiles, hookScripts, renderHooks, targetHooks } from "./hooks.js";
|
|
8
|
-
import { pluginName, renderManifest, serializeManifest } from "./manifest.js";
|
|
9
7
|
import { CONFIG_NOTE_PATH, sortNotes } from "./notes.js";
|
|
8
|
+
import { envNotes, envRunnerEntry, envWaitNotes, withEnvRunner } from "./env.js";
|
|
9
|
+
import { hookCommandFiles, hookScripts, renderHooks, sessionStartMatcher, targetHooks, widensSessionStart } from "./hooks.js";
|
|
10
|
+
import { pluginName, renderManifest, serializeManifest } from "./manifest.js";
|
|
11
|
+
import { renderMonitors, targetMonitors } from "./monitors.js";
|
|
10
12
|
import { HOOK_LIB_DIR, hookLibFiles } from "./hook-lib.js";
|
|
13
|
+
import { ignoredOutput } from "./hook-output.js";
|
|
14
|
+
import { LIB_DIR, libFiles } from "./lib-files.js";
|
|
11
15
|
import { SERVER_LIB_DIR, serverLibFiles } from "./server-lib.js";
|
|
12
16
|
import { mcpServerNames, renderServers, serverFiles } from "./servers.js";
|
|
13
17
|
import { readSkills, renderSkill } from "./skills.js";
|
|
18
|
+
import { renderToolMap } from "./tool-map.js";
|
|
14
19
|
import { Effect, FileSystem, Path, Schema } from "effect";
|
|
15
20
|
import { KNOWN_TARGET_IDS, TARGETS } from "@pluginfinity/targets";
|
|
16
21
|
import { Run } from "@effected/commands";
|
|
@@ -56,19 +61,27 @@ const copyFile = (root, file) => Effect.gen(function* () {
|
|
|
56
61
|
};
|
|
57
62
|
});
|
|
58
63
|
/** Fail with `HookScriptInvalid` unless `script` exists, and is executable under `exec`. */
|
|
59
|
-
const checkScript = (config, script, invoke) => Effect.gen(function* () {
|
|
64
|
+
const checkScript = (config, script, invoke, component = "hooks") => Effect.gen(function* () {
|
|
60
65
|
const fs = yield* FileSystem.FileSystem;
|
|
61
66
|
const path = yield* Path.Path;
|
|
67
|
+
if (invoke === "exec" && component === "hooks" && script.includes("=")) return yield* Effect.fail(new HookScriptInvalid({
|
|
68
|
+
path: config.path,
|
|
69
|
+
script,
|
|
70
|
+
problem: "equals-in-path",
|
|
71
|
+
component
|
|
72
|
+
}));
|
|
62
73
|
const info = yield* fs.stat(path.join(config.root, script)).pipe(Effect.option);
|
|
63
74
|
if (info._tag === "None" || info.value.type !== "File") return yield* Effect.fail(new HookScriptInvalid({
|
|
64
75
|
path: config.path,
|
|
65
76
|
script,
|
|
66
|
-
problem: "missing"
|
|
77
|
+
problem: "missing",
|
|
78
|
+
component
|
|
67
79
|
}));
|
|
68
80
|
if (invoke === "exec" && (info.value.mode & 73) === 0) return yield* Effect.fail(new HookScriptInvalid({
|
|
69
81
|
path: config.path,
|
|
70
82
|
script,
|
|
71
|
-
problem: "not-executable"
|
|
83
|
+
problem: "not-executable",
|
|
84
|
+
component
|
|
72
85
|
}));
|
|
73
86
|
});
|
|
74
87
|
/** Whether `real` is `root` or under it; both are real paths. */
|
|
@@ -114,11 +127,11 @@ const expandShipped = (config, file, spelled, referencedBy) => Effect.gen(functi
|
|
|
114
127
|
* The files `entries` name, each canonical: each file, or every file under
|
|
115
128
|
* each directory. Every one must exist and resolve inside the plugin.
|
|
116
129
|
*/
|
|
117
|
-
const listedFiles = (config, entries) => Effect.gen(function* () {
|
|
130
|
+
const listedFiles = (config, entries, referencedBy = "files") => Effect.gen(function* () {
|
|
118
131
|
const fail = (file, problem) => Effect.fail(new ShippedFileInvalid({
|
|
119
132
|
path: config.path,
|
|
120
133
|
file,
|
|
121
|
-
referencedBy
|
|
134
|
+
referencedBy,
|
|
122
135
|
problem
|
|
123
136
|
}));
|
|
124
137
|
const out = [];
|
|
@@ -126,7 +139,7 @@ const listedFiles = (config, entries) => Effect.gen(function* () {
|
|
|
126
139
|
const file = canonical(entry);
|
|
127
140
|
if (file === void 0) return yield* fail(entry, "outside-root");
|
|
128
141
|
if (file === "") return yield* fail(entry, "not-normal");
|
|
129
|
-
out.push(...yield* expandShipped(config, file, file,
|
|
142
|
+
out.push(...yield* expandShipped(config, file, file, referencedBy));
|
|
130
143
|
}
|
|
131
144
|
return out;
|
|
132
145
|
});
|
|
@@ -158,6 +171,8 @@ const serverShipped = (config, spelled, referencedBy, command) => Effect.gen(fun
|
|
|
158
171
|
}
|
|
159
172
|
return files;
|
|
160
173
|
});
|
|
174
|
+
/** The skills or agents a target builds: those whose `targets` do not switch it off. */
|
|
175
|
+
const builtFor = (items, id) => items.filter((item) => item.frontmatter.targets?.[id] !== false);
|
|
161
176
|
/**
|
|
162
177
|
* What a target's skill and agent bodies may name: the skills and agents it
|
|
163
178
|
* builds, every file of those skills, and the plugin's own MCP servers. Agent
|
|
@@ -167,12 +182,12 @@ const serverShipped = (config, spelled, referencedBy, command) => Effect.gen(fun
|
|
|
167
182
|
*/
|
|
168
183
|
const tokenContext = (target, id, config, skills, agents) => {
|
|
169
184
|
const plugin = pluginName(config, id);
|
|
170
|
-
const built = skills
|
|
185
|
+
const built = builtFor(skills, id);
|
|
171
186
|
return {
|
|
172
187
|
target,
|
|
173
188
|
plugin,
|
|
174
189
|
skills: new Set(built.map((skill) => skill.name)),
|
|
175
|
-
agents: new Set(agents
|
|
190
|
+
agents: new Set(builtFor(agents, id).map((agent) => agent.name)),
|
|
176
191
|
skillFiles: new Set(built.flatMap((skill) => [`${skill.name}/SKILL.md`, ...skill.files.map((file) => `${skill.name}/${file}`)])),
|
|
177
192
|
own: {
|
|
178
193
|
plugin: pluginName(config, "claude"),
|
|
@@ -197,9 +212,20 @@ const tokenContext = (target, id, config, skills, agents) => {
|
|
|
197
212
|
* and an executable file when a whole `command`; a named directory ships every
|
|
198
213
|
* file under it), and every file the `files` key lists, each once. A target
|
|
199
214
|
* with a local server gets the server library under `lib/pluginfinity/`,
|
|
200
|
-
* which is reserved the same way.
|
|
215
|
+
* which is reserved the same way. A target that writes its servers inline
|
|
216
|
+
* in its manifest also reserves the server file its host loads by default
|
|
217
|
+
* (Claude Code's `.mcp.json` and `.lsp.json`), but only when this plugin
|
|
218
|
+
* has servers of that kind inline.
|
|
219
|
+
*
|
|
220
|
+
* A target with monitors (Claude Code) writes them to `monitors/monitors.json`,
|
|
221
|
+
* which no source file may occupy, and ships each monitor `script` and each
|
|
222
|
+
* file a `command` names after `${PLUGIN_ROOT}/` (and the monitor library).
|
|
223
|
+
* Those files ship only to targets that build monitors, even from under
|
|
224
|
+
* `hooks/`; a target without monitors notes each as `monitor-omitted` and
|
|
225
|
+
* ships none.
|
|
201
226
|
*/
|
|
202
227
|
const planPlugin = (prepared) => Effect.gen(function* () {
|
|
228
|
+
const fs = yield* FileSystem.FileSystem;
|
|
203
229
|
const path = yield* Path.Path;
|
|
204
230
|
const config = prepared.config;
|
|
205
231
|
const version = yield* readVersion(config.root);
|
|
@@ -223,10 +249,33 @@ const planPlugin = (prepared) => Effect.gen(function* () {
|
|
|
223
249
|
for (const script of hookScripts(events)) yield* checkScript(config, script, invoke);
|
|
224
250
|
for (const file of hookCommandFiles(events)) yield* checkScript(config, file, "bash");
|
|
225
251
|
}
|
|
226
|
-
const hooksDir = yield* sourceFiles(config.root, "hooks");
|
|
227
|
-
const listed = yield* listedFiles(config, config.config.files ?? []);
|
|
228
252
|
const { skills, failures: skillFailures } = yield* readSkills(config.root, KNOWN_TARGET_IDS);
|
|
229
253
|
const { agents, failures: agentFailures } = yield* readAgents(config.root, KNOWN_TARGET_IDS);
|
|
254
|
+
const monitorsOf = new Map(config.targets.map((id) => {
|
|
255
|
+
const rendered = renderMonitors(targetOf(id), id, targetMonitors(id, config.config), invoke, {
|
|
256
|
+
plugin: pluginName(config.config, id),
|
|
257
|
+
skills: new Set(builtFor(skills, id).map((skill) => skill.name))
|
|
258
|
+
});
|
|
259
|
+
return [id, {
|
|
260
|
+
rendered,
|
|
261
|
+
files: [...rendered.scripts, ...rendered.commandFiles]
|
|
262
|
+
}];
|
|
263
|
+
}));
|
|
264
|
+
const monitorFilesOf = (id) => {
|
|
265
|
+
const found = monitorsOf.get(id);
|
|
266
|
+
if (found === void 0) throw new Error(`no monitors for target "${id}"`);
|
|
267
|
+
return found;
|
|
268
|
+
};
|
|
269
|
+
const everyMonitorFile = new Set([...monitorsOf.values()].flatMap((one) => one.files));
|
|
270
|
+
for (const id of prepared.targets) {
|
|
271
|
+
const { rendered } = monitorFilesOf(id);
|
|
272
|
+
for (const script of rendered.scripts) yield* checkScript(config, script, invoke, "monitors");
|
|
273
|
+
for (const file of rendered.commandFiles) yield* checkScript(config, file, "bash", "monitors");
|
|
274
|
+
}
|
|
275
|
+
const env = config.config.env;
|
|
276
|
+
if (env?.setup !== void 0) yield* checkScript(config, env.setup, "bash", "env");
|
|
277
|
+
const hooksDir = yield* sourceFiles(config.root, "hooks");
|
|
278
|
+
const baseListed = yield* listedFiles(config, config.config.files ?? []);
|
|
230
279
|
const failures = [...skillFailures, ...agentFailures];
|
|
231
280
|
const keep = (error) => {
|
|
232
281
|
if (!failures.some((seen) => seen.message === error.message)) failures.push(error);
|
|
@@ -240,33 +289,65 @@ const planPlugin = (prepared) => Effect.gen(function* () {
|
|
|
240
289
|
kind: "hook-omitted",
|
|
241
290
|
name: event
|
|
242
291
|
}));
|
|
292
|
+
for (const { event, entries } of events) {
|
|
293
|
+
if (target.hooks.matcherIgnored.includes(event) && entries.some((entry) => entry.matcher !== void 0)) notes.push({
|
|
294
|
+
target: id,
|
|
295
|
+
path: CONFIG_NOTE_PATH,
|
|
296
|
+
kind: "hook-matcher-runtime",
|
|
297
|
+
name: event
|
|
298
|
+
});
|
|
299
|
+
if (widensSessionStart(target.hooks.matcherIgnored, event)) for (const entry of entries) {
|
|
300
|
+
if (entry.matcher === void 0) continue;
|
|
301
|
+
const spelled = sessionStartMatcher(entry.matcher);
|
|
302
|
+
if (spelled.widened) notes.push({
|
|
303
|
+
target: id,
|
|
304
|
+
path: CONFIG_NOTE_PATH,
|
|
305
|
+
kind: "hook-matcher-widened",
|
|
306
|
+
name: `${event} ${entry.matcher} -> ${spelled.matcher}`
|
|
307
|
+
});
|
|
308
|
+
else if (spelled.unwidened) notes.push({
|
|
309
|
+
target: id,
|
|
310
|
+
path: CONFIG_NOTE_PATH,
|
|
311
|
+
kind: "hook-matcher-regex",
|
|
312
|
+
name: `${event} ${entry.matcher}`
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
const sources = /* @__PURE__ */ new Map();
|
|
317
|
+
for (const script of hookScripts(events)) {
|
|
318
|
+
const text = yield* fs.readFileString(path.join(config.root, script)).pipe(Effect.option);
|
|
319
|
+
if (text._tag === "Some") sources.set(script, text.value);
|
|
320
|
+
}
|
|
321
|
+
notes.push(...ignoredOutput(id, target, events, (script) => sources.get(script)));
|
|
243
322
|
const own = new Set(filesOf(events));
|
|
323
|
+
const monitored = monitorFilesOf(id);
|
|
324
|
+
notes.push(...monitored.rendered.notes);
|
|
325
|
+
if (monitored.rendered.issues.length > 0) {
|
|
326
|
+
const issue = new ComponentInvalid({
|
|
327
|
+
path: config.path,
|
|
328
|
+
target: id,
|
|
329
|
+
issues: monitored.rendered.issues
|
|
330
|
+
});
|
|
331
|
+
if (!failures.some((seen) => seen.message === issue.message)) failures.push(issue);
|
|
332
|
+
}
|
|
333
|
+
const monitorOwn = new Set(monitored.files);
|
|
244
334
|
const servers = serverFiles(target, id, config.config);
|
|
245
335
|
const serverFilesShipped = [];
|
|
246
336
|
for (const file of servers.commands) serverFilesShipped.push(...yield* serverShipped(config, file, servers.owners.get(file) ?? "mcpServers", true));
|
|
247
337
|
for (const file of servers.others) serverFilesShipped.push(...yield* serverShipped(config, file, servers.owners.get(file) ?? "mcpServers", false));
|
|
338
|
+
const setting = config.config[id];
|
|
339
|
+
const targetListed = yield* listedFiles(config, typeof setting === "object" ? setting.files ?? [] : [], `${id}.files`);
|
|
248
340
|
const shipped = [.../* @__PURE__ */ new Set([
|
|
249
|
-
...hooksDir.filter((file) => own.has(file) || !everyScript.has(file)),
|
|
250
|
-
...[...own].filter((script) => !script.startsWith("hooks/")),
|
|
341
|
+
...hooksDir.filter((file) => own.has(file) || monitorOwn.has(file) || !(everyScript.has(file) || everyMonitorFile.has(file))),
|
|
342
|
+
...[...own, ...monitorOwn].filter((script) => !script.startsWith("hooks/")),
|
|
343
|
+
...env?.setup === void 0 ? [] : [env.setup],
|
|
251
344
|
...serverFilesShipped,
|
|
252
|
-
...
|
|
345
|
+
...baseListed,
|
|
346
|
+
...targetListed
|
|
253
347
|
])];
|
|
254
348
|
const copied = [];
|
|
255
349
|
for (const file of shipped) copied.push(yield* copyFile(config.root, file));
|
|
256
|
-
const
|
|
257
|
-
const generated = [{
|
|
258
|
-
path: target.manifest.path,
|
|
259
|
-
content: serializeManifest(manifest)
|
|
260
|
-
}];
|
|
261
|
-
const hooksFile = renderHooks(target, events, invoke);
|
|
262
|
-
if (hooksFile !== void 0) {
|
|
263
|
-
generated.push({
|
|
264
|
-
path: target.hooks.path,
|
|
265
|
-
content: hooksFile
|
|
266
|
-
});
|
|
267
|
-
generated.push(...hookLibFiles(id, String(manifest.name), ENGINE_VERSION));
|
|
268
|
-
}
|
|
269
|
-
const rendered = renderServers(target, id, config.config, String(manifest.name), SERVER_LIB_DIR);
|
|
350
|
+
const rendered = renderServers(target, id, config.config, pluginName(config.config, id), SERVER_LIB_DIR);
|
|
270
351
|
if (rendered.issues.length > 0) {
|
|
271
352
|
const issue = new ComponentInvalid({
|
|
272
353
|
path: config.path,
|
|
@@ -275,8 +356,29 @@ const planPlugin = (prepared) => Effect.gen(function* () {
|
|
|
275
356
|
});
|
|
276
357
|
if (!failures.some((seen) => seen.message === issue.message)) failures.push(issue);
|
|
277
358
|
}
|
|
359
|
+
const manifest = renderManifest(target, id, config.config, version, rendered.manifest);
|
|
360
|
+
const generated = [{
|
|
361
|
+
path: target.manifest.path,
|
|
362
|
+
content: serializeManifest(manifest)
|
|
363
|
+
}];
|
|
364
|
+
const hooksFile = renderHooks(target, env === void 0 ? events : withEnvRunner(target, events, envRunnerEntry(LIB_DIR)), invoke);
|
|
365
|
+
if (hooksFile !== void 0) generated.push({
|
|
366
|
+
path: target.hooks.path,
|
|
367
|
+
content: hooksFile
|
|
368
|
+
});
|
|
369
|
+
if (events.length > 0) generated.push(...hookLibFiles(id, String(manifest.name), ENGINE_VERSION, renderToolMap(target, pluginName(config.config, "claude"), [...mcpServerNames(id, config.config)], {
|
|
370
|
+
skills: builtFor(skills, id).map((skill) => skill.name),
|
|
371
|
+
agents: builtFor(agents, id).map((agent) => agent.name),
|
|
372
|
+
monitors: Object.keys(targetMonitors(id, config.config))
|
|
373
|
+
})));
|
|
374
|
+
if (monitored.rendered.file !== void 0) generated.push(monitored.rendered.file);
|
|
375
|
+
generated.push(...libFiles(id, String(manifest.name), ENGINE_VERSION, {
|
|
376
|
+
monitors: monitored.rendered.file !== void 0,
|
|
377
|
+
...env === void 0 ? {} : { env }
|
|
378
|
+
}));
|
|
278
379
|
generated.push(...rendered.files);
|
|
279
380
|
notes.push(...rendered.notes);
|
|
381
|
+
if (env !== void 0) notes.push(...envNotes(target, id), ...envWaitNotes(id, events));
|
|
280
382
|
if (rendered.stdio) generated.push(...serverLibFiles());
|
|
281
383
|
const tokens = tokenContext(target, id, config.config, skills, agents);
|
|
282
384
|
for (const skill of skills) {
|
|
@@ -291,18 +393,28 @@ const planPlugin = (prepared) => Effect.gen(function* () {
|
|
|
291
393
|
generated.push(agentRender.file);
|
|
292
394
|
notes.push(...agentRender.notes);
|
|
293
395
|
}
|
|
294
|
-
const
|
|
396
|
+
const reservedFiles = new Set([target.mcp.placement, target.lsp.placement].flatMap((placement) => placement._tag === "manifest" && placement.key in rendered.manifest ? [placement.reserves] : []));
|
|
397
|
+
const monitorsFile = monitored.rendered.file?.path;
|
|
398
|
+
if (monitorsFile !== void 0 && (yield* fs.exists(path.join(config.root, monitorsFile)))) return yield* Effect.fail(new PathConflict({
|
|
399
|
+
path: config.path,
|
|
400
|
+
target: id,
|
|
401
|
+
file: monitorsFile,
|
|
402
|
+
conflict: "reserved-monitors-file"
|
|
403
|
+
}));
|
|
404
|
+
const reserved = copied.find((file) => reservedFiles.has(file.path) || ["hooks/lib/pluginfinity", "lib/pluginfinity"].some((dir) => file.path === dir || file.path.startsWith(`${dir}/`)));
|
|
295
405
|
if (reserved !== void 0) return yield* Effect.fail(new PathConflict({
|
|
296
406
|
path: config.path,
|
|
297
407
|
target: id,
|
|
298
|
-
file: reserved.path
|
|
408
|
+
file: reserved.path,
|
|
409
|
+
conflict: reservedFiles.has(reserved.path) ? "reserved-server-file" : "reserved-dir"
|
|
299
410
|
}));
|
|
300
411
|
const copiedPaths = new Set(copied.map((file) => file.path));
|
|
301
412
|
const conflict = generated.find((file) => copiedPaths.has(file.path));
|
|
302
413
|
if (conflict !== void 0) return yield* Effect.fail(new PathConflict({
|
|
303
414
|
path: config.path,
|
|
304
415
|
target: id,
|
|
305
|
-
file: conflict.path
|
|
416
|
+
file: conflict.path,
|
|
417
|
+
conflict: "generated"
|
|
306
418
|
}));
|
|
307
419
|
const files = [...generated, ...copied];
|
|
308
420
|
const out = path.join(config.root, "builds", id);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluginfinity/engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "The pluginfinity build pipeline shared by every pluginfinity front end: config discovery, read, transform per target, emit and check.",
|
|
6
6
|
"keywords": [
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"@effected/semver": "^0.11.0",
|
|
41
41
|
"@effected/walker": "^0.15.0",
|
|
42
42
|
"@effected/yaml": "^0.19.1",
|
|
43
|
-
"@pluginfinity/core": "0.
|
|
44
|
-
"@pluginfinity/targets": "0.
|
|
43
|
+
"@pluginfinity/core": "0.3.0",
|
|
44
|
+
"@pluginfinity/targets": "0.3.0",
|
|
45
45
|
"jiti": "^2.6.1"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
package/server-lib.generated.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/** The server library's files, embedded so a build needs no asset on disk. */
|
|
3
3
|
const SERVER_LIB_FILES = [{
|
|
4
4
|
name: "server.sh",
|
|
5
|
-
content: "# shellcheck shell=sh\n# pluginfinity server library. `pluginfinity build` writes this file into every\n# target that has a local MCP or LSP server; do not edit a copy under builds/,\n# the next build overwrites it.\n#\n# Source it first in a launcher, and keep `set -u` (or write\n# ${PLUGINFINITY_LIB:?run by the host}) so a launcher run outside the host fails loudly:\n# . \"$PLUGINFINITY_LIB/server.sh\"\n#\n# POSIX sh. Writes nothing to stdout, which carries the server's protocol.\n\n_pf_server_lib_dir=$(cd \"$(dirname \"$PLUGINFINITY_LIB/server.sh\")\" && pwd -P)\n\n# claude or copilot.\nserver_host() { printf '%s\\n' \"${PLUGINFINITY_HOST:-unknown}\"; }\n\n# The build root the launcher runs from.\nserver_plugin_root() { (cd \"$_pf_server_lib_dir/../..\" && pwd -P); }\n\n# Append a line to the plugin's
|
|
5
|
+
content: "# shellcheck shell=sh\n# pluginfinity server library. `pluginfinity build` writes this file into every\n# target that has a local MCP or LSP server; do not edit a copy under builds/,\n# the next build overwrites it.\n#\n# Source it first in a launcher, and keep `set -u` (or write\n# ${PLUGINFINITY_LIB:?run by the host}) so a launcher run outside the host fails loudly:\n# . \"$PLUGINFINITY_LIB/server.sh\"\n#\n# POSIX sh. Writes nothing to stdout, which carries the server's protocol.\n\n_pf_server_lib_dir=$(cd \"$(dirname \"$PLUGINFINITY_LIB/server.sh\")\" && pwd -P)\n\n# log.sh sits beside this file.\n_pf_log_dir=$_pf_server_lib_dir\n# shellcheck source=/dev/null\n. \"$_pf_log_dir/log.sh\"\n\n# claude or copilot.\nserver_host() { printf '%s\\n' \"${PLUGINFINITY_HOST:-unknown}\"; }\n\n# The build root the launcher runs from.\nserver_plugin_root() { (cd \"$_pf_server_lib_dir/../..\" && pwd -P); }\n\n# Append a line to the plugin's error log.\nserver_log() { pf_log server \"$*\"; }\n# Append a line to the plugin's debug log when PLUGINFINITY_DEBUG=1.\nserver_debug() { pf_debug server \"$*\"; }\n\n# The project the session works in. Prints it and returns 0, or prints nothing\n# and returns 1 when there is none to report:\n# - Claude with CLAUDE_PROJECT_DIR set: that directory.\n# - Otherwise, when the working directory is the plugin root or inside it:\n# nothing, status 1. Copilot starts an MCP server with its cwd at the plugin\n# root and gives it no project directory, so that cwd says nothing about the\n# project. A Copilot MCP server cannot learn the project: its client offers\n# no roots and no variable names it (measured 2026-10-07).\n# - Otherwise: the closest directory above $PWD holding .git, else $PWD\n# (Copilot starts LSP servers at the git root).\nserver_project_dir() {\n if [ \"${PLUGINFINITY_HOST:-}\" = claude ] && [ -n \"${CLAUDE_PROJECT_DIR:-}\" ]; then\n printf '%s\\n' \"$CLAUDE_PROJECT_DIR\"\n return 0\n fi\n _pf_root=$(server_plugin_root)\n _pf_cwd=$(pwd -P)\n case \"$_pf_cwd\" in\n \"$_pf_root\" | \"$_pf_root\"/*) return 1 ;;\n esac\n _pf_probe=$PWD\n while [ -n \"$_pf_probe\" ] && [ \"$_pf_probe\" != / ]; do\n if [ -e \"$_pf_probe/.git\" ]; then\n printf '%s\\n' \"$_pf_probe\"\n return 0\n fi\n _pf_probe=$(dirname \"$_pf_probe\")\n done\n printf '%s\\n' \"$PWD\"\n}\n\n# npm, pnpm, yarn or bun. With jq on PATH: package.json's devEngines.packageManager\n# (an object, or an array whose first entry is used) name, then packageManager.\n# Without jq only the top-level packageManager field is read (devEngines is\n# ignored). Then lockfiles, else npm. A name other than the four is npm.\n_pf_detect_pm() { # project\n _pf_pm=\"\"\n if [ -f \"$1/package.json\" ]; then\n if command -v jq >/dev/null 2>&1; then\n _pf_pm=$(jq -r '((try (.devEngines.packageManager | if type == \"array\" then .[0] else . end | .name) catch null) | strings) // (.packageManager | strings) // empty' \\\n \"$1/package.json\" 2>/dev/null | cut -d@ -f1)\n else\n _pf_pm=$(grep -o '\"packageManager\"[[:space:]]*:[[:space:]]*\"[^\"]*\"' \"$1/package.json\" 2>/dev/null |\n sed -E 's/.*:[[:space:]]*\"([^@\"]*).*/\\1/')\n fi\n fi\n case \"$_pf_pm\" in\n npm | pnpm | yarn | bun)\n printf '%s\\n' \"$_pf_pm\"\n return 0\n ;;\n *) ;;\n esac\n if [ -f \"$1/pnpm-lock.yaml\" ]; then\n printf 'pnpm\\n'\n elif [ -f \"$1/bun.lock\" ] || [ -f \"$1/bun.lockb\" ]; then\n printf 'bun\\n'\n elif [ -f \"$1/yarn.lock\" ]; then\n printf 'yarn\\n'\n else\n printf 'npm\\n'\n fi\n}\n\n_pf_install_line() { # pm package\n case \"$1\" in\n pnpm) printf ' pnpm add -D %s\\n' \"$2\" ;;\n yarn) printf ' yarn add -D %s\\n' \"$2\" ;;\n bun) printf ' bun add -d %s\\n' \"$2\" ;;\n *) printf ' npm install --save-dev %s\\n' \"$2\" ;;\n esac\n}\n\n# Exec the project's node_modules/.bin/<bin>, else run <package> with the\n# project's package manager (pnpm dlx, yarn dlx, bunx, or npx --yes; see\n# _pf_detect_pm). npm 11 refuses to run in a project whose devEngines names\n# another manager, so npx is only the runner for npm projects, for a manager\n# that is not on PATH (said on stderr), and when there is no project directory\n# (see server_project_dir), which skips the lookup and the install hint. An\n# optional `--install <package>` straight after the two positionals names a\n# different package in the install hint only; the runner still runs <package>.\n# Later args pass through untouched.\nserver_exec_bin() { # bin package [--install install-package] [args...]\n _pf_bin=$1\n _pf_pkg=$2\n _pf_install=$2\n shift 2\n if [ \"${1:-}\" = --install ] && [ $# -ge 2 ]; then\n _pf_install=$2\n shift 2\n fi\n _pf_runner=npx\n if _pf_project=$(server_project_dir); then\n if [ -x \"$_pf_project/node_modules/.bin/$_pf_bin\" ]; then\n exec \"$_pf_project/node_modules/.bin/$_pf_bin\" \"$@\"\n fi\n _pf_pm=$(_pf_detect_pm \"$_pf_project\")\n {\n printf '%s: %s is not installed in %s.\\n' \"${PLUGINFINITY_PLUGIN:-plugin}\" \"$_pf_bin\" \"$_pf_project\"\n printf 'Install it with:\\n'\n _pf_install_line \"$_pf_pm\" \"$_pf_install\"\n } >&2\n case \"$_pf_pm\" in\n pnpm | yarn) _pf_runner=$_pf_pm ;;\n bun) _pf_runner=bunx ;;\n esac\n if [ \"$_pf_runner\" != npx ] && ! command -v \"$_pf_runner\" >/dev/null 2>&1; then\n printf '%s was not found on PATH.\\n' \"$_pf_runner\" >&2\n _pf_runner=npx\n fi\n else\n printf '%s: no project directory is known, so %s cannot be looked up in node_modules.\\n' \\\n \"${PLUGINFINITY_PLUGIN:-plugin}\" \"$_pf_bin\" >&2\n fi\n case \"$_pf_runner\" in\n pnpm | yarn)\n printf 'Falling back to \"%s dlx %s\".\\n' \"$_pf_runner\" \"$_pf_pkg\" >&2\n server_debug \"server_exec_bin: running $_pf_pkg with $_pf_runner dlx\"\n exec \"$_pf_runner\" dlx \"$_pf_pkg\" \"$@\"\n ;;\n bunx)\n printf 'Falling back to \"bunx %s\".\\n' \"$_pf_pkg\" >&2\n server_debug \"server_exec_bin: running $_pf_pkg with bunx\"\n exec bunx \"$_pf_pkg\" \"$@\"\n ;;\n esac\n printf 'Falling back to \"npx --yes %s\".\\n' \"$_pf_pkg\" >&2\n server_debug \"server_exec_bin: running $_pf_pkg with npx --yes\"\n exec npx --yes \"$_pf_pkg\" \"$@\"\n}\n"
|
|
6
6
|
}];
|
|
7
7
|
|
|
8
8
|
//#endregion
|