agent-plugin-lifecycle 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/README.md +243 -90
- package/bin/cli.js +8 -1
- package/dist/cjs/apm.d.cts +62 -13
- package/dist/cjs/apm.d.ts +62 -13
- package/dist/cjs/apm.js +534 -115
- package/dist/cjs/apm.js.map +1 -1
- package/dist/cjs/approvals.d.cts +26 -0
- package/dist/cjs/approvals.d.ts +26 -0
- package/dist/cjs/approvals.js +302 -0
- package/dist/cjs/approvals.js.map +1 -0
- package/dist/cjs/cli/index.d.cts +5 -0
- package/dist/cjs/cli/index.d.ts +5 -0
- package/dist/cjs/cli/index.js +53 -0
- package/dist/cjs/cli/index.js.map +1 -0
- package/dist/cjs/cli/install.d.cts +17 -0
- package/dist/cjs/cli/install.d.ts +17 -0
- package/dist/cjs/cli/install.js +308 -0
- package/dist/cjs/cli/install.js.map +1 -0
- package/dist/cjs/cli/report.d.cts +6 -0
- package/dist/cjs/cli/report.d.ts +6 -0
- package/dist/cjs/cli/report.js +47 -0
- package/dist/cjs/cli/report.js.map +1 -0
- package/dist/cjs/cli/status.d.cts +2 -0
- package/dist/cjs/cli/status.d.ts +2 -0
- package/dist/cjs/cli/status.js +222 -0
- package/dist/cjs/cli/status.js.map +1 -0
- package/dist/cjs/cli/types.d.cts +10 -0
- package/dist/cjs/cli/types.d.ts +10 -0
- package/dist/cjs/cli/types.js +6 -0
- package/dist/cjs/cli/types.js.map +1 -0
- package/dist/cjs/cli/usage.d.cts +2 -0
- package/dist/cjs/cli/usage.d.ts +2 -0
- package/dist/cjs/cli/usage.js +21 -0
- package/dist/cjs/cli/usage.js.map +1 -0
- package/dist/cjs/cli.d.cts +2 -1
- package/dist/cjs/cli.d.ts +2 -1
- package/dist/cjs/cli.js +65 -204
- package/dist/cjs/cli.js.map +1 -1
- package/dist/cjs/constants.d.cts +35 -0
- package/dist/cjs/constants.d.ts +35 -0
- package/dist/cjs/constants.js +115 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/copies.d.cts +40 -0
- package/dist/cjs/copies.d.ts +40 -0
- package/dist/cjs/copies.js +937 -0
- package/dist/cjs/copies.js.map +1 -0
- package/dist/cjs/index.d.cts +3 -6
- package/dist/cjs/index.d.ts +3 -6
- package/dist/cjs/index.js +20 -19
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/install.d.cts +6 -0
- package/dist/cjs/install.d.ts +6 -0
- package/dist/cjs/install.js +511 -0
- package/dist/cjs/install.js.map +1 -0
- package/dist/cjs/lib/errors.d.cts +5 -0
- package/dist/cjs/lib/errors.d.ts +5 -0
- package/dist/cjs/lib/errors.js +41 -0
- package/dist/cjs/lib/errors.js.map +1 -0
- package/dist/cjs/lib/object.d.cts +2 -0
- package/dist/cjs/lib/object.d.ts +2 -0
- package/dist/cjs/lib/object.js +18 -0
- package/dist/cjs/lib/object.js.map +1 -0
- package/dist/cjs/lib/options.d.cts +10 -0
- package/dist/cjs/lib/options.d.ts +10 -0
- package/dist/cjs/lib/options.js +26 -0
- package/dist/cjs/lib/options.js.map +1 -0
- package/dist/cjs/lib/run.d.cts +2 -0
- package/dist/cjs/lib/run.d.ts +2 -0
- package/dist/cjs/lib/run.js +40 -0
- package/dist/cjs/lib/run.js.map +1 -0
- package/dist/cjs/lib/store.d.cts +9 -0
- package/dist/cjs/lib/store.d.ts +9 -0
- package/dist/cjs/lib/store.js +418 -0
- package/dist/cjs/lib/store.js.map +1 -0
- package/dist/cjs/lib/visible.d.cts +9 -0
- package/dist/cjs/lib/visible.d.ts +9 -0
- package/dist/cjs/lib/visible.js +26 -0
- package/dist/cjs/lib/visible.js.map +1 -0
- package/dist/cjs/manifest.d.cts +4 -0
- package/dist/cjs/manifest.d.ts +4 -0
- package/dist/cjs/{config.js → manifest.js} +33 -94
- package/dist/cjs/manifest.js.map +1 -0
- package/dist/cjs/state.d.cts +17 -0
- package/dist/cjs/state.d.ts +17 -0
- package/dist/cjs/state.js +143 -0
- package/dist/cjs/state.js.map +1 -0
- package/dist/cjs/status.d.cts +3 -0
- package/dist/cjs/status.d.ts +3 -0
- package/dist/cjs/status.js +273 -0
- package/dist/cjs/status.js.map +1 -0
- package/dist/cjs/types.d.cts +39 -0
- package/dist/cjs/types.d.ts +39 -0
- package/dist/cjs/types.js +5 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/apm.d.ts +62 -13
- package/dist/esm/apm.js +208 -96
- package/dist/esm/apm.js.map +1 -1
- package/dist/esm/approvals.d.ts +26 -0
- package/dist/esm/approvals.js +43 -0
- package/dist/esm/approvals.js.map +1 -0
- package/dist/esm/cli/index.d.ts +5 -0
- package/dist/esm/cli/index.js +5 -0
- package/dist/esm/cli/index.js.map +1 -0
- package/dist/esm/cli/install.d.ts +17 -0
- package/dist/esm/cli/install.js +89 -0
- package/dist/esm/cli/install.js.map +1 -0
- package/dist/esm/cli/report.d.ts +6 -0
- package/dist/esm/cli/report.js +21 -0
- package/dist/esm/cli/report.js.map +1 -0
- package/dist/esm/cli/status.d.ts +2 -0
- package/dist/esm/cli/status.js +41 -0
- package/dist/esm/cli/status.js.map +1 -0
- package/dist/esm/cli/types.d.ts +10 -0
- package/dist/esm/cli/types.js +2 -0
- package/dist/esm/cli/types.js.map +1 -0
- package/dist/esm/cli/usage.d.ts +2 -0
- package/dist/esm/cli/usage.js +2 -0
- package/dist/esm/cli/usage.js.map +1 -0
- package/dist/esm/cli.d.ts +2 -1
- package/dist/esm/cli.js +46 -65
- package/dist/esm/cli.js.map +1 -1
- package/dist/esm/constants.d.ts +35 -0
- package/dist/esm/constants.js +37 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/copies.d.ts +40 -0
- package/dist/esm/copies.js +226 -0
- package/dist/esm/copies.js.map +1 -0
- package/dist/esm/index.d.ts +3 -6
- package/dist/esm/index.js +3 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/install.d.ts +6 -0
- package/dist/esm/install.js +116 -0
- package/dist/esm/install.js.map +1 -0
- package/dist/esm/lib/errors.d.ts +5 -0
- package/dist/esm/lib/errors.js +9 -0
- package/dist/esm/lib/errors.js.map +1 -0
- package/dist/esm/lib/object.d.ts +2 -0
- package/dist/esm/lib/object.js +3 -0
- package/dist/esm/lib/object.js.map +1 -0
- package/dist/esm/lib/options.d.ts +10 -0
- package/dist/esm/lib/options.js +15 -0
- package/dist/esm/lib/options.js.map +1 -0
- package/dist/esm/lib/run.d.ts +2 -0
- package/dist/esm/lib/run.js +29 -0
- package/dist/esm/lib/run.js.map +1 -0
- package/dist/esm/lib/store.d.ts +9 -0
- package/dist/esm/lib/store.js +91 -0
- package/dist/esm/lib/store.js.map +1 -0
- package/dist/esm/lib/visible.d.ts +9 -0
- package/dist/esm/lib/visible.js +12 -0
- package/dist/esm/lib/visible.js.map +1 -0
- package/dist/esm/manifest.d.ts +4 -0
- package/dist/esm/manifest.js +25 -0
- package/dist/esm/manifest.js.map +1 -0
- package/dist/esm/state.d.ts +17 -0
- package/dist/esm/state.js +32 -0
- package/dist/esm/state.js.map +1 -0
- package/dist/esm/status.d.ts +3 -0
- package/dist/esm/status.js +44 -0
- package/dist/esm/status.js.map +1 -0
- package/dist/esm/types.d.ts +39 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/types.js.map +1 -0
- package/package.json +25 -9
- package/config/config.example.json +0 -22
- package/dist/cjs/config.d.cts +0 -16
- package/dist/cjs/config.d.ts +0 -16
- package/dist/cjs/config.js.map +0 -1
- package/dist/cjs/runner.d.cts +0 -19
- package/dist/cjs/runner.d.ts +0 -19
- package/dist/cjs/runner.js +0 -1391
- package/dist/cjs/runner.js.map +0 -1
- package/dist/esm/config.d.ts +0 -16
- package/dist/esm/config.js +0 -74
- package/dist/esm/config.js.map +0 -1
- package/dist/esm/runner.d.ts +0 -19
- package/dist/esm/runner.js +0 -335
- package/dist/esm/runner.js.map +0 -1
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { realpath } from 'node:fs/promises';
|
|
3
|
+
import { dirname, join, resolve } from 'node:path';
|
|
4
|
+
import isPathInside from 'is-path-inside';
|
|
5
|
+
import { isUnreadable, packageFolders, readLock, recordedFolders, resolveScope } from './apm.js';
|
|
6
|
+
import { CACHE_ONLY, INSTALL, MANIFEST, NO_PROJECT_LOCK } from './constants.js';
|
|
7
|
+
import { message, missing } from './lib/errors.js';
|
|
8
|
+
import { readManifest } from './manifest.js';
|
|
9
|
+
/** `<host>/<repo>[/<path>]` for a Git package, the absolute source path for a local one. */ export function packageId(item, manifestRoot) {
|
|
10
|
+
if (item.localPath !== undefined) return resolve(manifestRoot, item.localPath);
|
|
11
|
+
return `${item.host === undefined ? '' : `${item.host}/`}${item.repo}${item.path === '.' ? '' : `/${item.path}`}`;
|
|
12
|
+
}
|
|
13
|
+
/** The package id, then `:<folder>` when `apl.json` sits below the package root. */ export function copyId(base, within) {
|
|
14
|
+
return within ? `${base}:${within}` : base;
|
|
15
|
+
}
|
|
16
|
+
/** `bound` when `real` is not inside its real path. */ async function outside(bound, real) {
|
|
17
|
+
return bound !== undefined && !isPathInside(real, await realpath(bound)) ? bound : undefined;
|
|
18
|
+
}
|
|
19
|
+
/** The real path of `path`, or undefined when it does not exist. */ async function realOrMissing(path) {
|
|
20
|
+
try {
|
|
21
|
+
return await realpath(path);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (missing(error)) return undefined;
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** `path` is `parent` or inside it. */ const under = (path, parent)=>parent !== undefined && (path === parent || isPathInside(path, parent));
|
|
28
|
+
/**
|
|
29
|
+
* The copy in a folder the lock names, if it holds an `apl.json`. A folder or `apl.json` that resolves into APM's cache (`cache`, a real
|
|
30
|
+
* path) is skipped with `CACHE_ONLY`, and one that resolves outside `bound` is failed; neither is read or run.
|
|
31
|
+
*/ async function inspect(id, folder, bound, cache, place) {
|
|
32
|
+
const real = await realOrMissing(folder);
|
|
33
|
+
if (real === undefined) return undefined;
|
|
34
|
+
if (under(real, cache)) return existsSync(join(real, MANIFEST)) ? {
|
|
35
|
+
id,
|
|
36
|
+
folder,
|
|
37
|
+
place,
|
|
38
|
+
skipped: CACHE_ONLY
|
|
39
|
+
} : undefined;
|
|
40
|
+
const left = await outside(bound, real);
|
|
41
|
+
if (left !== undefined) return existsSync(join(real, MANIFEST)) ? {
|
|
42
|
+
id,
|
|
43
|
+
folder,
|
|
44
|
+
place,
|
|
45
|
+
error: `resolves to ${real}, outside ${left}`
|
|
46
|
+
} : undefined;
|
|
47
|
+
const file = await realOrMissing(join(folder, MANIFEST));
|
|
48
|
+
if (file === undefined) return undefined;
|
|
49
|
+
if (under(file, cache)) return {
|
|
50
|
+
id,
|
|
51
|
+
folder,
|
|
52
|
+
place,
|
|
53
|
+
skipped: CACHE_ONLY
|
|
54
|
+
};
|
|
55
|
+
const fileBound = await outside(bound, file);
|
|
56
|
+
if (fileBound !== undefined) return {
|
|
57
|
+
id,
|
|
58
|
+
folder,
|
|
59
|
+
place,
|
|
60
|
+
error: `resolves to ${file}, outside ${fileBound}`
|
|
61
|
+
};
|
|
62
|
+
try {
|
|
63
|
+
const install = await readManifest(folder);
|
|
64
|
+
return install === undefined ? undefined : {
|
|
65
|
+
id,
|
|
66
|
+
folder,
|
|
67
|
+
place,
|
|
68
|
+
install
|
|
69
|
+
};
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (missing(error)) return undefined;
|
|
72
|
+
return {
|
|
73
|
+
id,
|
|
74
|
+
folder,
|
|
75
|
+
place,
|
|
76
|
+
error: message(error)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** One copy per real path; a folder named must hold an `apl.json` with a `scripts.install`. */ async function namedCopies(cwd, folders) {
|
|
81
|
+
const copies = new Map();
|
|
82
|
+
for (const argument of folders){
|
|
83
|
+
const path = resolve(cwd, argument);
|
|
84
|
+
let folder;
|
|
85
|
+
try {
|
|
86
|
+
folder = await realpath(path);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (!missing(error)) throw error;
|
|
89
|
+
copies.set(path, {
|
|
90
|
+
id: path,
|
|
91
|
+
folder: path,
|
|
92
|
+
place: path,
|
|
93
|
+
error: `no ${MANIFEST}`
|
|
94
|
+
});
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (copies.has(folder)) continue;
|
|
98
|
+
try {
|
|
99
|
+
const install = await readManifest(folder);
|
|
100
|
+
copies.set(folder, install === undefined ? {
|
|
101
|
+
id: folder,
|
|
102
|
+
folder,
|
|
103
|
+
place: folder,
|
|
104
|
+
error: `${MANIFEST} has no scripts.${INSTALL}`
|
|
105
|
+
} : {
|
|
106
|
+
id: folder,
|
|
107
|
+
folder,
|
|
108
|
+
place: folder,
|
|
109
|
+
install
|
|
110
|
+
});
|
|
111
|
+
} catch (error) {
|
|
112
|
+
copies.set(folder, {
|
|
113
|
+
id: folder,
|
|
114
|
+
folder,
|
|
115
|
+
place: folder,
|
|
116
|
+
error: missing(error) ? `no ${MANIFEST}` : message(error)
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return [
|
|
121
|
+
...copies.values()
|
|
122
|
+
];
|
|
123
|
+
}
|
|
124
|
+
/** Copies of one id that carry different commands cannot share one approval: each is failed, and nothing is asked for them. */ function disagreeing(copies) {
|
|
125
|
+
var _commands_get;
|
|
126
|
+
const commands = new Map();
|
|
127
|
+
for (const { id, install } of copies)if (install !== undefined) commands.set(id, ((_commands_get = commands.get(id)) !== null && _commands_get !== void 0 ? _commands_get : new Set()).add(install));
|
|
128
|
+
return copies.map((copy)=>{
|
|
129
|
+
var _ref;
|
|
130
|
+
var _commands_get;
|
|
131
|
+
return ((_ref = (_commands_get = commands.get(copy.id)) === null || _commands_get === void 0 ? void 0 : _commands_get.size) !== null && _ref !== void 0 ? _ref : 0) > 1 ? {
|
|
132
|
+
id: copy.id,
|
|
133
|
+
folder: copy.folder,
|
|
134
|
+
place: copy.place,
|
|
135
|
+
error: `copies of ${copy.id} disagree on install`,
|
|
136
|
+
revision: copy.revision,
|
|
137
|
+
lock: copy.lock
|
|
138
|
+
} : copy;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/** The copies of the named folders, or of every package in the scope's APM lock. */ export async function findCopies(options) {
|
|
142
|
+
const { home, apmHome, cwd, global, folders } = options;
|
|
143
|
+
if (folders === null || folders === void 0 ? void 0 : folders.length) {
|
|
144
|
+
if (global) throw new Error('-g cannot be combined with folders');
|
|
145
|
+
return {
|
|
146
|
+
copies: disagreeing(await namedCopies(cwd, folders)),
|
|
147
|
+
named: new Set()
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
const scope = await resolveScope({
|
|
151
|
+
global,
|
|
152
|
+
cwd,
|
|
153
|
+
home,
|
|
154
|
+
apmHome
|
|
155
|
+
});
|
|
156
|
+
// a user-scope lock is the user's own file, written by APM, and may record absolute paths
|
|
157
|
+
const packages = await readLock(scope.lock, scope.global);
|
|
158
|
+
if (!packages && !scope.global) throw new Error(NO_PROJECT_LOCK);
|
|
159
|
+
const copies = [];
|
|
160
|
+
const named = new Set();
|
|
161
|
+
if (!packages) return {
|
|
162
|
+
copies,
|
|
163
|
+
scope,
|
|
164
|
+
named
|
|
165
|
+
};
|
|
166
|
+
// the place of every copy in the scope: the real path of the folder holding the lock
|
|
167
|
+
const place = await realpath(dirname(scope.lock));
|
|
168
|
+
// nothing runs under APM's cache, whatever path or link leads there
|
|
169
|
+
const cacheRoot = await realOrMissing(scope.modules);
|
|
170
|
+
for (const item of packages){
|
|
171
|
+
if (isUnreadable(item)) {
|
|
172
|
+
// its folders are still the lock's, so none of them is reported as left
|
|
173
|
+
for (const { folder } of (await recordedFolders(item, scope)))named.add(folder);
|
|
174
|
+
copies.push({
|
|
175
|
+
id: item.name,
|
|
176
|
+
folder: scope.lock,
|
|
177
|
+
place,
|
|
178
|
+
error: item.reason
|
|
179
|
+
});
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const base = packageId(item, scope.manifestRoot);
|
|
183
|
+
const { folders: recorded, cache } = await packageFolders(item, scope);
|
|
184
|
+
for (const { folder, within, bound } of recorded){
|
|
185
|
+
if (named.has(folder)) continue;
|
|
186
|
+
named.add(folder);
|
|
187
|
+
const copy = await inspect(copyId(base, within), folder, bound, cacheRoot, place);
|
|
188
|
+
if (copy) copies.push({
|
|
189
|
+
...copy,
|
|
190
|
+
revision: item.revision,
|
|
191
|
+
lock: scope.lock
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
for (const { folder, within } of cache){
|
|
195
|
+
if (named.has(folder)) continue;
|
|
196
|
+
named.add(folder);
|
|
197
|
+
// never read or run; reported, so an apl.json that reaches only APM's cache is not silently skipped
|
|
198
|
+
if (existsSync(join(folder, MANIFEST))) copies.push({
|
|
199
|
+
id: copyId(base, within),
|
|
200
|
+
folder,
|
|
201
|
+
place,
|
|
202
|
+
skipped: CACHE_ONLY,
|
|
203
|
+
revision: item.revision,
|
|
204
|
+
lock: scope.lock
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
copies: disagreeing(copies),
|
|
210
|
+
scope,
|
|
211
|
+
named
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
/** The line for a copy with no command: `skipped` when APM's cache is its only copy, otherwise `failed`. */ export function cannotRun({ id, folder, error, skipped }) {
|
|
215
|
+
return skipped === undefined ? {
|
|
216
|
+
status: 'failed',
|
|
217
|
+
id,
|
|
218
|
+
folder,
|
|
219
|
+
reason: error
|
|
220
|
+
} : {
|
|
221
|
+
status: 'skipped',
|
|
222
|
+
id,
|
|
223
|
+
folder,
|
|
224
|
+
reason: skipped
|
|
225
|
+
};
|
|
226
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/open-source/ai/agent-plugin-lifecycle/src/copies.ts"],"sourcesContent":["import { existsSync } from 'node:fs';\nimport { realpath } from 'node:fs/promises';\nimport { dirname, join, resolve } from 'node:path';\nimport isPathInside from 'is-path-inside';\nimport { isUnreadable, type LockedPackage, packageFolders, readLock, recordedFolders, resolveScope, type Scope } from './apm.ts';\nimport { CACHE_ONLY, INSTALL, MANIFEST, NO_PROJECT_LOCK } from './constants.ts';\nimport { message, missing } from './lib/errors.ts';\nimport { readManifest } from './manifest.ts';\nimport type { CopyResult } from './types.ts';\n\n/** A folder holding an `apl.json`, the id a person approves, and, when found through APM, the revision APM locked. */\nexport interface Copy {\n id: string;\n folder: string;\n /** Where its approval is kept: the real path of the folder holding the lock (project or APM home), or of the named folder. */\n place: string;\n /** The command in `apl.json`; absent when `error` or `skipped` is set. */\n install?: string;\n /** Why the copy's `apl.json` cannot be used. */\n error?: string;\n /** Why the copy is not run and not counted against the exit code: APM's cache is its only copy. */\n skipped?: string;\n revision?: string;\n /** The APM lock file the copy was found through. */\n lock?: string;\n}\nexport interface Found {\n copies: Copy[];\n /** Set when the copies came from an APM scope. */\n scope?: Scope;\n /** Every folder the scope's lock names, with or without an `apl.json`. */\n named: Set<string>;\n}\nexport interface FindOptions {\n home: string;\n apmHome: string;\n cwd: string;\n global: boolean;\n folders?: string[];\n}\n\n/** `<host>/<repo>[/<path>]` for a Git package, the absolute source path for a local one. */\nexport function packageId(item: LockedPackage, manifestRoot: string): string {\n if (item.localPath !== undefined) return resolve(manifestRoot, item.localPath);\n return `${item.host === undefined ? '' : `${item.host}/`}${item.repo}${item.path === '.' ? '' : `/${item.path}`}`;\n}\n/** The package id, then `:<folder>` when `apl.json` sits below the package root. */\nexport function copyId(base: string, within: string): string {\n return within ? `${base}:${within}` : base;\n}\n\n/** `bound` when `real` is not inside its real path. */\nasync function outside(bound: string | undefined, real: string): Promise<string | undefined> {\n return bound !== undefined && !isPathInside(real, await realpath(bound)) ? bound : undefined;\n}\n\n/** The real path of `path`, or undefined when it does not exist. */\nasync function realOrMissing(path: string): Promise<string | undefined> {\n try {\n return await realpath(path);\n } catch (error) {\n if (missing(error)) return undefined;\n throw error;\n }\n}\n\n/** `path` is `parent` or inside it. */\nconst under = (path: string, parent: string | undefined) => parent !== undefined && (path === parent || isPathInside(path, parent));\n\n/**\n * The copy in a folder the lock names, if it holds an `apl.json`. A folder or `apl.json` that resolves into APM's cache (`cache`, a real\n * path) is skipped with `CACHE_ONLY`, and one that resolves outside `bound` is failed; neither is read or run.\n */\nasync function inspect(id: string, folder: string, bound: string | undefined, cache: string | undefined, place: string): Promise<Copy | undefined> {\n const real = await realOrMissing(folder);\n if (real === undefined) return undefined;\n if (under(real, cache)) return existsSync(join(real, MANIFEST)) ? { id, folder, place, skipped: CACHE_ONLY } : undefined;\n const left = await outside(bound, real);\n if (left !== undefined) return existsSync(join(real, MANIFEST)) ? { id, folder, place, error: `resolves to ${real}, outside ${left}` } : undefined;\n const file = await realOrMissing(join(folder, MANIFEST));\n if (file === undefined) return undefined;\n if (under(file, cache)) return { id, folder, place, skipped: CACHE_ONLY };\n const fileBound = await outside(bound, file);\n if (fileBound !== undefined) return { id, folder, place, error: `resolves to ${file}, outside ${fileBound}` };\n try {\n const install = await readManifest(folder);\n return install === undefined ? undefined : { id, folder, place, install };\n } catch (error) {\n if (missing(error)) return undefined;\n return { id, folder, place, error: message(error) };\n }\n}\n\n/** One copy per real path; a folder named must hold an `apl.json` with a `scripts.install`. */\nasync function namedCopies(cwd: string, folders: string[]): Promise<Copy[]> {\n const copies = new Map<string, Copy>();\n for (const argument of folders) {\n const path = resolve(cwd, argument);\n let folder: string;\n try {\n folder = await realpath(path);\n } catch (error) {\n if (!missing(error)) throw error;\n copies.set(path, { id: path, folder: path, place: path, error: `no ${MANIFEST}` });\n continue;\n }\n if (copies.has(folder)) continue;\n try {\n const install = await readManifest(folder);\n copies.set(folder, install === undefined ? { id: folder, folder, place: folder, error: `${MANIFEST} has no scripts.${INSTALL}` } : { id: folder, folder, place: folder, install });\n } catch (error) {\n copies.set(folder, { id: folder, folder, place: folder, error: missing(error) ? `no ${MANIFEST}` : message(error) });\n }\n }\n return [...copies.values()];\n}\n\n/** Copies of one id that carry different commands cannot share one approval: each is failed, and nothing is asked for them. */\nfunction disagreeing(copies: Copy[]): Copy[] {\n const commands = new Map<string, Set<string>>();\n for (const { id, install } of copies) if (install !== undefined) commands.set(id, (commands.get(id) ?? new Set()).add(install));\n return copies.map((copy) => ((commands.get(copy.id)?.size ?? 0) > 1 ? { id: copy.id, folder: copy.folder, place: copy.place, error: `copies of ${copy.id} disagree on install`, revision: copy.revision, lock: copy.lock } : copy));\n}\n\n/** The copies of the named folders, or of every package in the scope's APM lock. */\nexport async function findCopies(options: FindOptions): Promise<Found> {\n const { home, apmHome, cwd, global, folders } = options;\n if (folders?.length) {\n if (global) throw new Error('-g cannot be combined with folders');\n return { copies: disagreeing(await namedCopies(cwd, folders)), named: new Set() };\n }\n const scope = await resolveScope({ global, cwd, home, apmHome });\n // a user-scope lock is the user's own file, written by APM, and may record absolute paths\n const packages = await readLock(scope.lock, scope.global);\n if (!packages && !scope.global) throw new Error(NO_PROJECT_LOCK);\n const copies: Copy[] = [];\n const named = new Set<string>();\n if (!packages) return { copies, scope, named };\n // the place of every copy in the scope: the real path of the folder holding the lock\n const place = await realpath(dirname(scope.lock));\n // nothing runs under APM's cache, whatever path or link leads there\n const cacheRoot = await realOrMissing(scope.modules);\n for (const item of packages) {\n if (isUnreadable(item)) {\n // its folders are still the lock's, so none of them is reported as left\n for (const { folder } of await recordedFolders(item, scope)) named.add(folder);\n copies.push({ id: item.name, folder: scope.lock, place, error: item.reason });\n continue;\n }\n const base = packageId(item, scope.manifestRoot);\n const { folders: recorded, cache } = await packageFolders(item, scope);\n for (const { folder, within, bound } of recorded) {\n if (named.has(folder)) continue;\n named.add(folder);\n const copy = await inspect(copyId(base, within), folder, bound, cacheRoot, place);\n if (copy) copies.push({ ...copy, revision: item.revision, lock: scope.lock });\n }\n for (const { folder, within } of cache) {\n if (named.has(folder)) continue;\n named.add(folder);\n // never read or run; reported, so an apl.json that reaches only APM's cache is not silently skipped\n if (existsSync(join(folder, MANIFEST))) copies.push({ id: copyId(base, within), folder, place, skipped: CACHE_ONLY, revision: item.revision, lock: scope.lock });\n }\n }\n return { copies: disagreeing(copies), scope, named };\n}\n\n/** The line for a copy with no command: `skipped` when APM's cache is its only copy, otherwise `failed`. */\nexport function cannotRun({ id, folder, error, skipped }: Copy): CopyResult {\n return skipped === undefined ? { status: 'failed', id, folder, reason: error } : { status: 'skipped', id, folder, reason: skipped };\n}\n"],"names":["existsSync","realpath","dirname","join","resolve","isPathInside","isUnreadable","packageFolders","readLock","recordedFolders","resolveScope","CACHE_ONLY","INSTALL","MANIFEST","NO_PROJECT_LOCK","message","missing","readManifest","packageId","item","manifestRoot","localPath","undefined","host","repo","path","copyId","base","within","outside","bound","real","realOrMissing","error","under","parent","inspect","id","folder","cache","place","skipped","left","file","fileBound","install","namedCopies","cwd","folders","copies","Map","argument","set","has","values","disagreeing","commands","get","Set","add","map","copy","size","revision","lock","findCopies","options","home","apmHome","global","length","Error","named","scope","packages","cacheRoot","modules","push","name","reason","recorded","cannotRun","status"],"mappings":"AAAA,SAASA,UAAU,QAAQ,UAAU;AACrC,SAASC,QAAQ,QAAQ,mBAAmB;AAC5C,SAASC,OAAO,EAAEC,IAAI,EAAEC,OAAO,QAAQ,YAAY;AACnD,OAAOC,kBAAkB,iBAAiB;AAC1C,SAASC,YAAY,EAAsBC,cAAc,EAAEC,QAAQ,EAAEC,eAAe,EAAEC,YAAY,QAAoB,WAAW;AACjI,SAASC,UAAU,EAAEC,OAAO,EAAEC,QAAQ,EAAEC,eAAe,QAAQ,iBAAiB;AAChF,SAASC,OAAO,EAAEC,OAAO,QAAQ,kBAAkB;AACnD,SAASC,YAAY,QAAQ,gBAAgB;AAkC7C,0FAA0F,GAC1F,OAAO,SAASC,UAAUC,IAAmB,EAAEC,YAAoB;IACjE,IAAID,KAAKE,SAAS,KAAKC,WAAW,OAAOlB,QAAQg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AQV,OAAOC,MAAMC,SAASU,QAAQR,OAAO6C,WAAWnC;YAC3E,IAAIqB,MAAMZ,OAAO4B,IAAI,CAAC;gBAAE,GAAGhB,IAAI;gBAAEE,UAAU5C,KAAK4C,QAAQ;gBAAEC,MAAMS,MAAMT,IAAI;YAAC;QAC7E;QACA,KAAK,MAAM,EAAE1B,MAAM,EAAEV,MAAM,EAAE,IAAIW,MAAO;YACtC,IAAIiC,MAAMnB,GAAG,CAACf,SAAS;YACvBkC,MAAMb,GAAG,CAACrB;YACV,oGAAoG;YACpG,IAAItC,WAAWG,KAAKmC,QAAQzB,YAAYoC,OAAO4B,IAAI,CAAC;gBAAExC,IAAIX,OAAOC,MAAMC;gBAASU;gBAAQE;gBAAOC,SAAS9B;gBAAYoD,UAAU5C,KAAK4C,QAAQ;gBAAEC,MAAMS,MAAMT,IAAI;YAAC;QAChK;IACF;IACA,OAAO;QAAEf,QAAQM,YAAYN;QAASwB;QAAOD;IAAM;AACrD;AAEA,0GAA0G,GAC1G,OAAO,SAASS,UAAU,EAAE5C,EAAE,EAAEC,MAAM,EAAEL,KAAK,EAAEQ,OAAO,EAAQ;IAC5D,OAAOA,YAAYnB,YAAY;QAAE4D,QAAQ;QAAU7C;QAAIC;QAAQyC,QAAQ9C;IAAM,IAAI;QAAEiD,QAAQ;QAAW7C;QAAIC;QAAQyC,QAAQtC;IAAQ;AACpI"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export {
|
|
3
|
-
export
|
|
4
|
-
export { parseConfig, recordId } from './config.js';
|
|
5
|
-
export type { LifecycleEvent, Mode, RunOptions } from './runner.js';
|
|
6
|
-
export { runLifecycle } from './runner.js';
|
|
1
|
+
export { install } from './install.js';
|
|
2
|
+
export { status } from './status.js';
|
|
3
|
+
export * from './types.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export
|
|
1
|
+
export { install } from './install.js';
|
|
2
|
+
export { status } from './status.js';
|
|
3
|
+
export * from './types.js';
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/open-source/ai/agent-plugin-lifecycle/src/index.ts"],"sourcesContent":["export
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/open-source/ai/agent-plugin-lifecycle/src/index.ts"],"sourcesContent":["export { install } from './install.ts';\nexport { status } from './status.ts';\nexport * from './types.ts';\n"],"names":["install","status"],"mappings":"AAAA,SAASA,OAAO,QAAQ,eAAe;AACvC,SAASC,MAAM,QAAQ,cAAc;AACrC,cAAc,aAAa"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CopyResult, InstallOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Runs the `install` script of every copy approved in its place and not already current, asking `approve` first, once per id and command.
|
|
4
|
+
* A copy from APM is current when its last successful run had the same command and revision; a named folder always runs.
|
|
5
|
+
*/
|
|
6
|
+
export declare function install(options?: InstallOptions): Promise<CopyResult[]>;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { isApproved, readApprovals, saveApprovals } from './approvals.js';
|
|
2
|
+
import { cannotRun, findCopies } from './copies.js';
|
|
3
|
+
import { message } from './lib/errors.js';
|
|
4
|
+
import { resolveOptions } from './lib/options.js';
|
|
5
|
+
import { runScript } from './lib/run.js';
|
|
6
|
+
import { isCurrent, readState, updateState } from './state.js';
|
|
7
|
+
/**
|
|
8
|
+
* Runs the `install` script of every copy approved in its place and not already current, asking `approve` first, once per id and command.
|
|
9
|
+
* A copy from APM is current when its last successful run had the same command and revision; a named folder always runs.
|
|
10
|
+
*/ export async function install(options = {}) {
|
|
11
|
+
const { home, apmHome, cwd, environment, global, folders } = resolveOptions(options);
|
|
12
|
+
const { copies } = await findCopies({
|
|
13
|
+
home,
|
|
14
|
+
apmHome,
|
|
15
|
+
cwd,
|
|
16
|
+
global,
|
|
17
|
+
folders
|
|
18
|
+
});
|
|
19
|
+
let approvals = await readApprovals(home);
|
|
20
|
+
if (options.approve) {
|
|
21
|
+
// one request per place, id and command; the approval is saved under that place
|
|
22
|
+
const requests = new Map();
|
|
23
|
+
for (const { id, folder, place, install } of copies){
|
|
24
|
+
var _requests_get;
|
|
25
|
+
if (install === undefined || isApproved(approvals, place, id, install)) continue;
|
|
26
|
+
const key = JSON.stringify([
|
|
27
|
+
place,
|
|
28
|
+
id,
|
|
29
|
+
install
|
|
30
|
+
]);
|
|
31
|
+
const pending = (_requests_get = requests.get(key)) !== null && _requests_get !== void 0 ? _requests_get : {
|
|
32
|
+
place,
|
|
33
|
+
request: {
|
|
34
|
+
id,
|
|
35
|
+
scripts: {
|
|
36
|
+
install
|
|
37
|
+
},
|
|
38
|
+
folders: []
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
pending.request.folders.push(folder);
|
|
42
|
+
requests.set(key, pending);
|
|
43
|
+
}
|
|
44
|
+
const approved = [];
|
|
45
|
+
for (const { place, request } of requests.values())if (await options.approve(request)) approved.push({
|
|
46
|
+
place,
|
|
47
|
+
id: request.id,
|
|
48
|
+
install: request.scripts.install
|
|
49
|
+
});
|
|
50
|
+
// saved after the last answer, so a run interrupted at a question saves nothing
|
|
51
|
+
if (approved.length) approvals = await saveApprovals(home, approved);
|
|
52
|
+
}
|
|
53
|
+
const state = await readState(home);
|
|
54
|
+
const results = [];
|
|
55
|
+
const done = (result)=>{
|
|
56
|
+
var _options_onResult;
|
|
57
|
+
results.push(result);
|
|
58
|
+
(_options_onResult = options.onResult) === null || _options_onResult === void 0 ? void 0 : _options_onResult.call(options, result);
|
|
59
|
+
};
|
|
60
|
+
for (const copy of copies){
|
|
61
|
+
const { id, folder, place, install, revision, lock } = copy;
|
|
62
|
+
if (install === undefined) {
|
|
63
|
+
done(cannotRun(copy));
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (!isApproved(approvals, place, id, install)) {
|
|
67
|
+
done({
|
|
68
|
+
status: 'unapproved',
|
|
69
|
+
id,
|
|
70
|
+
folder
|
|
71
|
+
});
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (revision !== undefined && isCurrent(state.runs[folder], install, revision)) {
|
|
75
|
+
done({
|
|
76
|
+
status: 'current',
|
|
77
|
+
id,
|
|
78
|
+
folder
|
|
79
|
+
});
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
// no record while the script runs, so a failed or killed run leaves the copy missing
|
|
83
|
+
await updateState(home, (value)=>{
|
|
84
|
+
delete value.runs[folder];
|
|
85
|
+
});
|
|
86
|
+
try {
|
|
87
|
+
await runScript(install, folder, environment);
|
|
88
|
+
} catch (failure) {
|
|
89
|
+
done({
|
|
90
|
+
status: 'failed',
|
|
91
|
+
id,
|
|
92
|
+
folder,
|
|
93
|
+
reason: message(failure)
|
|
94
|
+
});
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
await updateState(home, (value)=>{
|
|
98
|
+
value.runs[folder] = {
|
|
99
|
+
id,
|
|
100
|
+
install,
|
|
101
|
+
...revision === undefined ? {} : {
|
|
102
|
+
revision
|
|
103
|
+
},
|
|
104
|
+
...lock === undefined ? {} : {
|
|
105
|
+
lock
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
});
|
|
109
|
+
done({
|
|
110
|
+
status: 'ran',
|
|
111
|
+
id,
|
|
112
|
+
folder
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return results;
|
|
116
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/open-source/ai/agent-plugin-lifecycle/src/install.ts"],"sourcesContent":["import { type Approved, isApproved, readApprovals, saveApprovals } from './approvals.ts';\nimport { cannotRun, findCopies } from './copies.ts';\nimport { message } from './lib/errors.ts';\nimport { resolveOptions } from './lib/options.ts';\nimport { runScript } from './lib/run.ts';\nimport { isCurrent, readState, updateState } from './state.ts';\nimport type { ApprovalRequest, CopyResult, InstallOptions } from './types.ts';\n\n/**\n * Runs the `install` script of every copy approved in its place and not already current, asking `approve` first, once per id and command.\n * A copy from APM is current when its last successful run had the same command and revision; a named folder always runs.\n */\nexport async function install(options: InstallOptions = {}): Promise<CopyResult[]> {\n const { home, apmHome, cwd, environment, global, folders } = resolveOptions(options);\n const { copies } = await findCopies({ home, apmHome, cwd, global, folders });\n let approvals = await readApprovals(home);\n if (options.approve) {\n // one request per place, id and command; the approval is saved under that place\n const requests = new Map<string, { place: string; request: ApprovalRequest }>();\n for (const { id, folder, place, install } of copies) {\n if (install === undefined || isApproved(approvals, place, id, install)) continue;\n const key = JSON.stringify([place, id, install]);\n const pending = requests.get(key) ?? { place, request: { id, scripts: { install }, folders: [] } };\n pending.request.folders.push(folder);\n requests.set(key, pending);\n }\n const approved: Approved[] = [];\n for (const { place, request } of requests.values()) if (await options.approve(request)) approved.push({ place, id: request.id, install: request.scripts.install });\n // saved after the last answer, so a run interrupted at a question saves nothing\n if (approved.length) approvals = await saveApprovals(home, approved);\n }\n const state = await readState(home);\n const results: CopyResult[] = [];\n const done = (result: CopyResult) => {\n results.push(result);\n options.onResult?.(result);\n };\n for (const copy of copies) {\n const { id, folder, place, install, revision, lock } = copy;\n if (install === undefined) {\n done(cannotRun(copy));\n continue;\n }\n if (!isApproved(approvals, place, id, install)) {\n done({ status: 'unapproved', id, folder });\n continue;\n }\n if (revision !== undefined && isCurrent(state.runs[folder], install, revision)) {\n done({ status: 'current', id, folder });\n continue;\n }\n // no record while the script runs, so a failed or killed run leaves the copy missing\n await updateState(home, (value) => {\n delete value.runs[folder];\n });\n try {\n await runScript(install, folder, environment);\n } catch (failure) {\n done({ status: 'failed', id, folder, reason: message(failure) });\n continue;\n }\n await updateState(home, (value) => {\n value.runs[folder] = { id, install, ...(revision === undefined ? {} : { revision }), ...(lock === undefined ? {} : { lock }) };\n });\n done({ status: 'ran', id, folder });\n }\n return results;\n}\n"],"names":["isApproved","readApprovals","saveApprovals","cannotRun","findCopies","message","resolveOptions","runScript","isCurrent","readState","updateState","install","options","home","apmHome","cwd","environment","global","folders","copies","approvals","approve","requests","Map","id","folder","place","undefined","key","JSON","stringify","pending","get","request","scripts","push","set","approved","values","length","state","results","done","result","onResult","copy","revision","lock","status","runs","value","failure","reason"],"mappings":"AAAA,SAAwBA,UAAU,EAAEC,aAAa,EAAEC,aAAa,QAAQ,iBAAiB;AACzF,SAASC,SAAS,EAAEC,UAAU,QAAQ,cAAc;AACpD,SAASC,OAAO,QAAQ,kBAAkB;AAC1C,SAASC,cAAc,QAAQ,mBAAmB;AAClD,SAASC,SAAS,QAAQ,eAAe;AACzC,SAASC,SAAS,EAAEC,SAAS,EAAEC,WAAW,QAAQ,aAAa;AAG/D;;;CAGC,GACD,OAAO,eAAeC,QAAQC,UAA0B,CAAC,CAAC;IACxD,MAAM,EAAEC,IAAI,EAAEC,OAAO,EAAEC,GAAG,EAAEC,WAAW,EAAEC,MAAM,EAAEC,OAAO,EAAE,GAAGZ,eAAeM;IAC5E,MAAM,EAAEO,MAAM,EAAE,GAAG,MAAMf,WAAW;QAAES;QAAMC;QAASC;QAAKE;QAAQC;IAAQ;IAC1E,IAAIE,YAAY,MAAMnB,cAAcY;IACpC,IAAID,QAAQS,OAAO,EAAE;QACnB,gFAAgF;QAChF,MAAMC,WAAW,IAAIC;QACrB,KAAK,MAAM,EAAEC,EAAE,EAAEC,MAAM,EAAEC,KAAK,EAAEf,OAAO,EAAE,IAAIQ,OAAQ;gBAGnCG;YAFhB,IAAIX,YAAYgB,aAAa3B,WAAWoB,WAAWM,OAAOF,IAAIb,UAAU;YACxE,MAAMiB,MAAMC,KAAKC,SAAS,CAAC;gBAACJ;gBAAOF;gBAAIb;aAAQ;YAC/C,MAAMoB,WAAUT,gBAAAA,SAASU,GAAG,CAACJ,kBAAbN,2BAAAA,gBAAqB;gBAAEI;gBAAOO,SAAS;oBAAET;oBAAIU,SAAS;wBAAEvB;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ACA,IAAI;YACF,MAAMlB,UAAUI,SAASc,QAAQT;QACnC,EAAE,OAAOmC,SAAS;YAChBT,KAAK;gBAAEM,QAAQ;gBAAUxB;gBAAIC;gBAAQ2B,QAAQ/C,QAAQ8C;YAAS;YAC9D;QACF;QACA,MAAMzC,YAAYG,MAAM,CAACqC;YACvBA,MAAMD,IAAI,CAACxB,OAAO,GAAG;gBAAED;gBAAIb;gBAAS,GAAImC,aAAanB,YAAY,CAAC,IAAI;oBAAEmB;gBAAS,CAAC;gBAAG,GAAIC,SAASpB,YAAY,CAAC,IAAI;oBAAEoB;gBAAK,CAAC;YAAE;QAC/H;QACAL,KAAK;YAAEM,QAAQ;YAAOxB;YAAIC;QAAO;IACnC;IACA,OAAOgB;AACT"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** A path, or a parent of it, does not exist. */
|
|
2
|
+
export declare function missing(error: unknown): boolean;
|
|
3
|
+
export declare function message(error: unknown): string;
|
|
4
|
+
/** Text without a leading UTF-8 byte order mark, which some Windows editors write and `JSON.parse` rejects. */
|
|
5
|
+
export declare function withoutBom(text: string): string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** A path, or a parent of it, does not exist. */ export function missing(error) {
|
|
2
|
+
return !!error && typeof error === 'object' && 'code' in error && (error.code === 'ENOENT' || error.code === 'ENOTDIR');
|
|
3
|
+
}
|
|
4
|
+
export function message(error) {
|
|
5
|
+
return error instanceof Error ? error.message : String(error);
|
|
6
|
+
}
|
|
7
|
+
/** Text without a leading UTF-8 byte order mark, which some Windows editors write and `JSON.parse` rejects. */ export function withoutBom(text) {
|
|
8
|
+
return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/open-source/ai/agent-plugin-lifecycle/src/lib/errors.ts"],"sourcesContent":["/** A path, or a parent of it, does not exist. */\nexport function missing(error: unknown): boolean {\n return !!error && typeof error === 'object' && 'code' in error && (error.code === 'ENOENT' || error.code === 'ENOTDIR');\n}\n\nexport function message(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\n/** Text without a leading UTF-8 byte order mark, which some Windows editors write and `JSON.parse` rejects. */\nexport function withoutBom(text: string): string {\n return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;\n}\n"],"names":["missing","error","code","message","Error","String","withoutBom","text","charCodeAt","slice"],"mappings":"AAAA,+CAA+C,GAC/C,OAAO,SAASA,QAAQC,KAAc;IACpC,OAAO,CAAC,CAACA,SAAS,OAAOA,UAAU,YAAY,UAAUA,SAAUA,CAAAA,MAAMC,IAAI,KAAK,YAAYD,MAAMC,IAAI,KAAK,SAAQ;AACvH;AAEA,OAAO,SAASC,QAAQF,KAAc;IACpC,OAAOA,iBAAiBG,QAAQH,MAAME,OAAO,GAAGE,OAAOJ;AACzD;AAEA,6GAA6G,GAC7G,OAAO,SAASK,WAAWC,IAAY;IACrC,OAAOA,KAAKC,UAAU,CAAC,OAAO,SAASD,KAAKE,KAAK,CAAC,KAAKF;AACzD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/open-source/ai/agent-plugin-lifecycle/src/lib/object.ts"],"sourcesContent":["/** `value` is a plain object, as JSON and YAML parse one: not null, not an array. */\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return !!value && typeof value === 'object' && !Array.isArray(value);\n}\n"],"names":["isRecord","value","Array","isArray"],"mappings":"AAAA,mFAAmF,GACnF,OAAO,SAASA,SAASC,KAAc;IACrC,OAAO,CAAC,CAACA,SAAS,OAAOA,UAAU,YAAY,CAACC,MAAMC,OAAO,CAACF;AAChE"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { StatusOptions } from '../types.js';
|
|
2
|
+
export interface Resolved {
|
|
3
|
+
home: string;
|
|
4
|
+
apmHome: string;
|
|
5
|
+
cwd: string;
|
|
6
|
+
environment: NodeJS.ProcessEnv;
|
|
7
|
+
global: boolean;
|
|
8
|
+
folders?: string[];
|
|
9
|
+
}
|
|
10
|
+
export declare function resolveOptions(options: StatusOptions): Resolved;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join, resolve } from 'node:path';
|
|
3
|
+
import { APM_DIR } from '../constants.js';
|
|
4
|
+
export function resolveOptions(options) {
|
|
5
|
+
var _options_home, _options_apmHome, _options_cwd, _options_environment;
|
|
6
|
+
const home = resolve((_options_home = options.home) !== null && _options_home !== void 0 ? _options_home : homedir());
|
|
7
|
+
return {
|
|
8
|
+
home,
|
|
9
|
+
apmHome: resolve((_options_apmHome = options.apmHome) !== null && _options_apmHome !== void 0 ? _options_apmHome : join(home, APM_DIR)),
|
|
10
|
+
cwd: resolve((_options_cwd = options.cwd) !== null && _options_cwd !== void 0 ? _options_cwd : process.cwd()),
|
|
11
|
+
environment: (_options_environment = options.environment) !== null && _options_environment !== void 0 ? _options_environment : process.env,
|
|
12
|
+
global: !!options.global,
|
|
13
|
+
folders: options.folders
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/open-source/ai/agent-plugin-lifecycle/src/lib/options.ts"],"sourcesContent":["import { homedir } from 'node:os';\nimport { join, resolve } from 'node:path';\nimport { APM_DIR } from '../constants.ts';\nimport type { StatusOptions } from '../types.ts';\n\nexport interface Resolved {\n home: string;\n apmHome: string;\n cwd: string;\n environment: NodeJS.ProcessEnv;\n global: boolean;\n folders?: string[];\n}\n\nexport function resolveOptions(options: StatusOptions): Resolved {\n const home = resolve(options.home ?? homedir());\n return {\n home,\n apmHome: resolve(options.apmHome ?? join(home, APM_DIR)),\n cwd: resolve(options.cwd ?? process.cwd()),\n environment: options.environment ?? process.env,\n global: !!options.global,\n folders: options.folders,\n };\n}\n"],"names":["homedir","join","resolve","APM_DIR","resolveOptions","options","home","apmHome","cwd","process","environment","env","global","folders"],"mappings":"AAAA,SAASA,OAAO,QAAQ,UAAU;AAClC,SAASC,IAAI,EAAEC,OAAO,QAAQ,YAAY;AAC1C,SAASC,OAAO,QAAQ,kBAAkB;AAY1C,OAAO,SAASC,eAAeC,OAAsB;QAC9BA,eAGFA,kBACJA,cACAA;IALf,MAAMC,OAAOJ,SAAQG,gBAAAA,QAAQC,IAAI,cAAZD,2BAAAA,gBAAgBL;IACrC,OAAO;QACLM;QACAC,SAASL,SAAQG,mBAAAA,QAAQE,OAAO,cAAfF,8BAAAA,mBAAmBJ,KAAKK,MAAMH;QAC/CK,KAAKN,SAAQG,eAAAA,QAAQG,GAAG,cAAXH,0BAAAA,eAAeI,QAAQD,GAAG;QACvCE,WAAW,GAAEL,uBAAAA,QAAQK,WAAW,cAAnBL,kCAAAA,uBAAuBI,QAAQE,GAAG;QAC/CC,QAAQ,CAAC,CAACP,QAAQO,MAAM;QACxBC,SAASR,QAAQQ,OAAO;IAC1B;AACF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
/** Runs a command string through the platform shell, as written, in `cwd`; standard input is closed and all its output goes to our standard error. */ export function runScript(command, cwd, env) {
|
|
3
|
+
return new Promise((resolve, reject)=>{
|
|
4
|
+
let settled = false;
|
|
5
|
+
const settle = (error)=>{
|
|
6
|
+
if (settled) return;
|
|
7
|
+
settled = true;
|
|
8
|
+
if (error) reject(error);
|
|
9
|
+
else resolve();
|
|
10
|
+
};
|
|
11
|
+
// our standard output holds only apl's result lines
|
|
12
|
+
const child = spawn(command, {
|
|
13
|
+
shell: true,
|
|
14
|
+
cwd,
|
|
15
|
+
env,
|
|
16
|
+
stdio: [
|
|
17
|
+
'ignore',
|
|
18
|
+
2,
|
|
19
|
+
'inherit'
|
|
20
|
+
]
|
|
21
|
+
});
|
|
22
|
+
child.on('error', settle);
|
|
23
|
+
child.on('close', (code, signal)=>{
|
|
24
|
+
if (signal) settle(new Error(`killed by ${signal}`));
|
|
25
|
+
else if (code !== 0) settle(new Error(`exit code ${code}`));
|
|
26
|
+
else settle();
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/open-source/ai/agent-plugin-lifecycle/src/lib/run.ts"],"sourcesContent":["import { spawn } from 'node:child_process';\n\n/** Runs a command string through the platform shell, as written, in `cwd`; standard input is closed and all its output goes to our standard error. */\nexport function runScript(command: string, cwd: string, env: NodeJS.ProcessEnv): Promise<void> {\n return new Promise((resolve, reject) => {\n let settled = false;\n const settle = (error?: Error) => {\n if (settled) return;\n settled = true;\n if (error) reject(error);\n else resolve();\n };\n // our standard output holds only apl's result lines\n const child = spawn(command, { shell: true, cwd, env, stdio: ['ignore', 2, 'inherit'] });\n child.on('error', settle);\n child.on('close', (code, signal) => {\n if (signal) settle(new Error(`killed by ${signal}`));\n else if (code !== 0) settle(new Error(`exit code ${code}`));\n else settle();\n });\n });\n}\n"],"names":["spawn","runScript","command","cwd","env","Promise","resolve","reject","settled","settle","error","child","shell","stdio","on","code","signal","Error"],"mappings":"AAAA,SAASA,KAAK,QAAQ,qBAAqB;AAE3C,oJAAoJ,GACpJ,OAAO,SAASC,UAAUC,OAAe,EAAEC,GAAW,EAAEC,GAAsB;IAC5E,OAAO,IAAIC,QAAQ,CAACC,SAASC;QAC3B,IAAIC,UAAU;QACd,MAAMC,SAAS,CAACC;YACd,IAAIF,SAAS;YACbA,UAAU;YACV,IAAIE,OAAOH,OAAOG;iBACbJ;QACP;QACA,oDAAoD;QACpD,MAAMK,QAAQX,MAAME,SAAS;YAAEU,OAAO;YAAMT;YAAKC;YAAKS,OAAO;gBAAC;gBAAU;gBAAG;aAAU;QAAC;QACtFF,MAAMG,EAAE,CAAC,SAASL;QAClBE,MAAMG,EAAE,CAAC,SAAS,CAACC,MAAMC;YACvB,IAAIA,QAAQP,OAAO,IAAIQ,MAAM,CAAC,UAAU,EAAED,QAAQ;iBAC7C,IAAID,SAAS,GAAGN,OAAO,IAAIQ,MAAM,CAAC,UAAU,EAAEF,MAAM;iBACpDN;QACP;IACF;AACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Checks the content of one store file (without its `version`), throwing when it is not the expected shape. */
|
|
2
|
+
export type Parse<T> = (value: Record<string, unknown>) => T;
|
|
3
|
+
/** Reads a store file; a file that does not exist reads as `empty()`. The folder and the file must be private (`checkPrivate`). */
|
|
4
|
+
export declare function readStore<T>(path: string, parse: Parse<T>, empty: () => T): Promise<T>;
|
|
5
|
+
/**
|
|
6
|
+
* Changes one store file in `dir` while holding the folder's lock: reads the current content, lets `change` edit it, and writes it
|
|
7
|
+
* under a temporary name renamed into place. Any failure to create or write `dir` names the folder.
|
|
8
|
+
*/
|
|
9
|
+
export declare function updateStore<T>(dir: string, file: string, parse: Parse<T>, empty: () => T, change: (value: T) => void): Promise<T>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { mkdir, readFile, stat } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import lockfile from 'proper-lockfile';
|
|
4
|
+
import writeFileAtomic from 'write-file-atomic';
|
|
5
|
+
import { FILE_VERSION, LOCK_NAME, LOCK_RETRIES } from '../constants.js';
|
|
6
|
+
import { message, missing, withoutBom } from './errors.js';
|
|
7
|
+
import { isRecord } from './object.js';
|
|
8
|
+
function check(path, value, parse) {
|
|
9
|
+
if (!isRecord(value)) throw new Error(`${path} must be an object`);
|
|
10
|
+
if (value.version !== FILE_VERSION) throw new Error(`${path} has unsupported version ${JSON.stringify(value.version)}`);
|
|
11
|
+
try {
|
|
12
|
+
return parse(value);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
throw new Error(`${path}: ${message(error)}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** On macOS and Linux, `path` (when it exists) must be the user's and not writable by group or others, as OpenSSH requires of `~/.ssh`. */ async function checkPrivate(path) {
|
|
18
|
+
var _process_getuid, _process;
|
|
19
|
+
if (process.platform === 'win32') return;
|
|
20
|
+
let info;
|
|
21
|
+
try {
|
|
22
|
+
info = await stat(path);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
if (missing(error)) return;
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
if (info.uid !== ((_process_getuid = (_process = process).getuid) === null || _process_getuid === void 0 ? void 0 : _process_getuid.call(_process))) throw new Error(`${path} is not owned by you`);
|
|
28
|
+
if (info.mode & 0o022) throw new Error(`${path} is writable by group or others: run chmod go-w ${path}`);
|
|
29
|
+
}
|
|
30
|
+
/** Reads a store file; a file that does not exist reads as `empty()`. The folder and the file must be private (`checkPrivate`). */ export async function readStore(path, parse, empty) {
|
|
31
|
+
await checkPrivate(dirname(path));
|
|
32
|
+
await checkPrivate(path);
|
|
33
|
+
let text;
|
|
34
|
+
try {
|
|
35
|
+
text = await readFile(path, 'utf8');
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (missing(error)) return empty();
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
let value;
|
|
41
|
+
try {
|
|
42
|
+
value = JSON.parse(withoutBom(text));
|
|
43
|
+
} catch (error) {
|
|
44
|
+
throw new Error(`${path} is not valid JSON: ${message(error)}`);
|
|
45
|
+
}
|
|
46
|
+
return check(path, value, parse);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Changes one store file in `dir` while holding the folder's lock: reads the current content, lets `change` edit it, and writes it
|
|
50
|
+
* under a temporary name renamed into place. Any failure to create or write `dir` names the folder.
|
|
51
|
+
*/ export async function updateStore(dir, file, parse, empty, change) {
|
|
52
|
+
const path = join(dir, file);
|
|
53
|
+
let release;
|
|
54
|
+
try {
|
|
55
|
+
await mkdir(dir, {
|
|
56
|
+
recursive: true,
|
|
57
|
+
mode: 0o700
|
|
58
|
+
});
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw new Error(`cannot write ${dir}: ${message(error)}`);
|
|
61
|
+
}
|
|
62
|
+
// before the lock, which proper-lockfile creates inside the folder
|
|
63
|
+
await checkPrivate(dir);
|
|
64
|
+
try {
|
|
65
|
+
release = await lockfile.lock(dir, {
|
|
66
|
+
realpath: false,
|
|
67
|
+
lockfilePath: join(dir, LOCK_NAME),
|
|
68
|
+
retries: LOCK_RETRIES
|
|
69
|
+
});
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw new Error(`cannot write ${dir}: ${message(error)}`);
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const value = await readStore(path, parse, empty);
|
|
75
|
+
change(value);
|
|
76
|
+
try {
|
|
77
|
+
// written under a temporary name and renamed into place, private to the user
|
|
78
|
+
await writeFileAtomic(path, `${JSON.stringify({
|
|
79
|
+
version: FILE_VERSION,
|
|
80
|
+
...value
|
|
81
|
+
}, null, 2)}\n`, {
|
|
82
|
+
mode: 0o600
|
|
83
|
+
});
|
|
84
|
+
} catch (error) {
|
|
85
|
+
throw new Error(`cannot write ${dir}: ${message(error)}`);
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
} finally{
|
|
89
|
+
await release();
|
|
90
|
+
}
|
|
91
|
+
}
|