@devel-solid/agentscli-hub 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +65 -2
- package/dist/assets/.agents/skills/agentscli-hub-edit-agents/SKILL.md +23 -0
- package/dist/assets/.agents/skills/agentscli-hub-edit-agents/scripts/THIRD_PARTY_LICENSES.txt +916 -0
- package/dist/assets/.agents/skills/agentscli-hub-edit-agents/scripts/support.mjs +16705 -0
- package/dist/assets/.agents/skills/agentscli-hub-edit-agents/scripts/validate.mjs +43 -0
- package/dist/assets/.agents/skills/agentscli-hub-publish/SKILL.md +25 -0
- package/dist/assets/.agents/skills/agentscli-hub-publish/references/publication.md +13 -0
- package/dist/assets/.agents/skills/agentscli-hub-publish/scripts/THIRD_PARTY_LICENSES.txt +916 -0
- package/dist/assets/.agents/skills/agentscli-hub-publish/scripts/release.mjs +551 -0
- package/dist/assets/.agents/skills/agentscli-hub-publish/scripts/support.mjs +16705 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-conventions/SKILL.md +23 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-conventions/scripts/THIRD_PARTY_LICENSES.txt +916 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-conventions/scripts/support.mjs +16705 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-conventions/scripts/validate.mjs +43 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-shared-conventions/SKILL.md +23 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-shared-conventions/scripts/THIRD_PARTY_LICENSES.txt +916 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-shared-conventions/scripts/support.mjs +16705 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-shared-conventions/scripts/validate.mjs +43 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-shared-skills/SKILL.md +25 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-shared-skills/references/resource-format.md +47 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-shared-skills/scripts/THIRD_PARTY_LICENSES.txt +916 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-shared-skills/scripts/support.mjs +16705 -0
- package/dist/assets/.agents/skills/agentscli-hub-write-shared-skills/scripts/validate.mjs +43 -0
- package/dist/assets/.gitattributes +4 -0
- package/dist/assets/AGENTS.md +23 -0
- package/dist/assets/README.md +13 -0
- package/dist/assets/agentscli-hub.yml +2 -0
- package/dist/assets/conventions/.gitkeep +0 -0
- package/dist/assets/docs/conventions/resource-authoring.md +13 -0
- package/dist/assets/gitignore.tpl +2 -0
- package/dist/assets/skills/.gitkeep +0 -0
- package/dist/cli.js +9 -0
- package/dist/runtime.js +610 -0
- package/package.json +37 -4
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
import * as nodeFs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
4
|
+
import { spawnSync } from 'node:child_process';
|
|
5
|
+
import { randomUUID } from 'node:crypto';
|
|
6
|
+
|
|
7
|
+
export const skillNames = Object.freeze([
|
|
8
|
+
'agentscli-hub-write-conventions',
|
|
9
|
+
'agentscli-hub-edit-agents',
|
|
10
|
+
'agentscli-hub-write-shared-conventions',
|
|
11
|
+
'agentscli-hub-write-shared-skills',
|
|
12
|
+
'agentscli-hub-publish',
|
|
13
|
+
]);
|
|
14
|
+
export class ApplyError extends Error {
|
|
15
|
+
constructor(code, message, file = '') {
|
|
16
|
+
super(message);
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.file = file;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function reject(code, message, file) {
|
|
22
|
+
throw new ApplyError(code, message, file);
|
|
23
|
+
}
|
|
24
|
+
function maybeStat(file, fs) {
|
|
25
|
+
try {
|
|
26
|
+
return fs.lstatSync(file);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (error.code === 'ENOENT') return undefined;
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function parseArgs(args) {
|
|
33
|
+
if (args.length === 1 && ['--help', '-h'].includes(args[0]))
|
|
34
|
+
return { command: 'help' };
|
|
35
|
+
if (args.length === 1 && ['--version', '-v'].includes(args[0]))
|
|
36
|
+
return { command: 'version' };
|
|
37
|
+
if (args[0] !== 'apply')
|
|
38
|
+
reject('arguments', 'Use agentscli-hub apply <directory> [--dry-run].');
|
|
39
|
+
let directory;
|
|
40
|
+
let dryRun = false;
|
|
41
|
+
for (const value of args.slice(1)) {
|
|
42
|
+
if (value === '--dry-run' && !dryRun) dryRun = true;
|
|
43
|
+
else if (value.startsWith('-') || directory)
|
|
44
|
+
reject('arguments', 'Unsupported or repeated argument.');
|
|
45
|
+
else directory = value;
|
|
46
|
+
}
|
|
47
|
+
if (!directory) reject('arguments', 'apply requires one target directory.');
|
|
48
|
+
return { command: 'apply', directory, dryRun };
|
|
49
|
+
}
|
|
50
|
+
export function nativeGit(args, { cwd, allowFailure = false } = {}) {
|
|
51
|
+
const result = spawnSync('git', args, {
|
|
52
|
+
cwd,
|
|
53
|
+
encoding: 'utf8',
|
|
54
|
+
windowsHide: true,
|
|
55
|
+
shell: false,
|
|
56
|
+
});
|
|
57
|
+
if (result.error || result.status !== 0) {
|
|
58
|
+
if (allowFailure) return null;
|
|
59
|
+
throw new ApplyError(
|
|
60
|
+
'git',
|
|
61
|
+
'Native Git is unavailable or the Git operation failed.',
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
return result.stdout.trim();
|
|
65
|
+
}
|
|
66
|
+
function interruptedBootstrap(root, assets, fs, git) {
|
|
67
|
+
const metadata = maybeStat(path.join(root, '.git'), fs);
|
|
68
|
+
if (!metadata || !metadata.isDirectory() || metadata.isSymbolicLink())
|
|
69
|
+
return false;
|
|
70
|
+
const allowedDirectories = new Set(['conventions', 'skills']);
|
|
71
|
+
for (const file of assets.keys()) {
|
|
72
|
+
const parts = file.split('/');
|
|
73
|
+
for (let index = 1; index < parts.length; index++)
|
|
74
|
+
allowedDirectories.add(parts.slice(0, index).join('/'));
|
|
75
|
+
}
|
|
76
|
+
const scan = (absolute, relative) => {
|
|
77
|
+
const stat = fs.lstatSync(absolute);
|
|
78
|
+
if (stat.isSymbolicLink() || (stat.isFile() && stat.nlink > 1))
|
|
79
|
+
reject(
|
|
80
|
+
'unsafe-path',
|
|
81
|
+
'An interrupted bootstrap contains an unsafe path.',
|
|
82
|
+
relative,
|
|
83
|
+
);
|
|
84
|
+
if (stat.isDirectory()) {
|
|
85
|
+
if (!allowedDirectories.has(relative))
|
|
86
|
+
reject(
|
|
87
|
+
'reconcile',
|
|
88
|
+
'Unidentified nonempty target contains unrelated directories. Reconcile before applying.',
|
|
89
|
+
relative,
|
|
90
|
+
);
|
|
91
|
+
for (const name of fs.readdirSync(absolute))
|
|
92
|
+
scan(path.join(absolute, name), `${relative}/${name}`);
|
|
93
|
+
} else if (
|
|
94
|
+
!stat.isFile() ||
|
|
95
|
+
!assets.has(relative) ||
|
|
96
|
+
!Buffer.from(fs.readFileSync(absolute)).equals(assets.get(relative))
|
|
97
|
+
)
|
|
98
|
+
reject(
|
|
99
|
+
'reconcile',
|
|
100
|
+
'Unidentified target contains authored, changed, or unrelated content. Reconcile before applying.',
|
|
101
|
+
relative,
|
|
102
|
+
);
|
|
103
|
+
};
|
|
104
|
+
for (const name of fs.readdirSync(root))
|
|
105
|
+
if (name !== '.git') scan(path.join(root, name), name);
|
|
106
|
+
if (
|
|
107
|
+
git(['rev-parse', '--verify', 'HEAD'], { cwd: root, allowFailure: true }) ||
|
|
108
|
+
git(['for-each-ref', '--format=%(refname)'], { cwd: root }) ||
|
|
109
|
+
git(['diff', '--cached', '--name-only', '-z'], { cwd: root }) ||
|
|
110
|
+
git(['remote'], { cwd: root })
|
|
111
|
+
)
|
|
112
|
+
reject(
|
|
113
|
+
'reconcile',
|
|
114
|
+
'The unidentified Git repository has history, refs, staging, or remotes. Reconcile it explicitly before applying.',
|
|
115
|
+
root,
|
|
116
|
+
);
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
function readPackage(root, fs) {
|
|
120
|
+
const file = path.join(root, 'package.json');
|
|
121
|
+
if (!fs.existsSync(file)) return null;
|
|
122
|
+
try {
|
|
123
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
124
|
+
} catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function isTool(metadata) {
|
|
129
|
+
return (
|
|
130
|
+
(metadata?.name === '@devel-solid/agentscli' &&
|
|
131
|
+
typeof metadata.bin?.agentscli === 'string') ||
|
|
132
|
+
(metadata?.name === '@devel-solid/agentscli-hub' &&
|
|
133
|
+
typeof metadata.bin?.['agentscli-hub'] === 'string')
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
export function guardRole(root, fs = nodeFs) {
|
|
137
|
+
let current = root;
|
|
138
|
+
while (true) {
|
|
139
|
+
const stat = maybeStat(current, fs);
|
|
140
|
+
if (stat) {
|
|
141
|
+
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
142
|
+
reject(
|
|
143
|
+
'unsafe-path',
|
|
144
|
+
'Target ancestors must be real directories.',
|
|
145
|
+
current,
|
|
146
|
+
);
|
|
147
|
+
if (isTool(readPackage(current, fs)))
|
|
148
|
+
reject(
|
|
149
|
+
'role',
|
|
150
|
+
'Tool package sources and installations cannot be hub targets.',
|
|
151
|
+
current,
|
|
152
|
+
);
|
|
153
|
+
const metadata = readPackage(current, fs);
|
|
154
|
+
if (
|
|
155
|
+
metadata?.private === true &&
|
|
156
|
+
Array.isArray(metadata.workspaces) &&
|
|
157
|
+
['packages/agentscli', 'packages/agentscli-hub'].every((item) =>
|
|
158
|
+
metadata.workspaces.includes(item),
|
|
159
|
+
) &&
|
|
160
|
+
['agentscli', 'agentscli-hub'].every((item) =>
|
|
161
|
+
isTool(readPackage(path.join(current, 'packages', item), fs)),
|
|
162
|
+
)
|
|
163
|
+
)
|
|
164
|
+
reject(
|
|
165
|
+
'role',
|
|
166
|
+
'The verified tool monorepo cannot be a hub target.',
|
|
167
|
+
current,
|
|
168
|
+
);
|
|
169
|
+
if (
|
|
170
|
+
fs.existsSync(path.join(current, '.agentscli/config.yml')) ||
|
|
171
|
+
fs.existsSync(path.join(current, 'agentscli-compose.yml'))
|
|
172
|
+
)
|
|
173
|
+
reject(
|
|
174
|
+
'role',
|
|
175
|
+
'Consumer projects cannot contain a hub target.',
|
|
176
|
+
current,
|
|
177
|
+
);
|
|
178
|
+
if (
|
|
179
|
+
current !== root &&
|
|
180
|
+
fs.existsSync(path.join(current, 'agentscli-hub.yml'))
|
|
181
|
+
)
|
|
182
|
+
reject(
|
|
183
|
+
'role',
|
|
184
|
+
'An existing hub cannot contain another hub target.',
|
|
185
|
+
current,
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
const parent = path.dirname(current);
|
|
189
|
+
if (parent === current) break;
|
|
190
|
+
current = parent;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function assertRegular(file, fs) {
|
|
194
|
+
const stat = maybeStat(file, fs);
|
|
195
|
+
if (!stat) return;
|
|
196
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.nlink > 1)
|
|
197
|
+
reject(
|
|
198
|
+
'unsafe-path',
|
|
199
|
+
'Affected files must be independent regular files.',
|
|
200
|
+
file,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
function assertParents(file, root, fs) {
|
|
204
|
+
let current = path.dirname(file);
|
|
205
|
+
while (true) {
|
|
206
|
+
const stat = maybeStat(current, fs);
|
|
207
|
+
if (stat) {
|
|
208
|
+
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
209
|
+
reject(
|
|
210
|
+
'unsafe-path',
|
|
211
|
+
'Affected parent is not an independent directory.',
|
|
212
|
+
current,
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
if (current === root) break;
|
|
216
|
+
const parent = path.dirname(current);
|
|
217
|
+
if (parent === current || !current.startsWith(`${root}${path.sep}`))
|
|
218
|
+
reject('unsafe-path', 'Effect escaped its target.', current);
|
|
219
|
+
current = parent;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
export async function loadAssets(
|
|
223
|
+
root = path.resolve(fileURLToPath(new URL('..', import.meta.url))),
|
|
224
|
+
fs = nodeFs,
|
|
225
|
+
) {
|
|
226
|
+
const directory = path.join(root, 'dist/assets');
|
|
227
|
+
const helper = path.join(
|
|
228
|
+
directory,
|
|
229
|
+
'.agents/skills/agentscli-hub-publish/scripts/support.mjs',
|
|
230
|
+
);
|
|
231
|
+
if (!fs.existsSync(helper))
|
|
232
|
+
reject(
|
|
233
|
+
'assets',
|
|
234
|
+
'Prepared hub assets are missing. Reinstall or build this package.',
|
|
235
|
+
);
|
|
236
|
+
const contract = await import(pathToFileURL(helper).href);
|
|
237
|
+
const files = contract.safeWalk(directory, fs, { allowMissing: false });
|
|
238
|
+
if (files.has('gitignore.tpl')) {
|
|
239
|
+
files.set('.gitignore', files.get('gitignore.tpl'));
|
|
240
|
+
files.delete('gitignore.tpl');
|
|
241
|
+
}
|
|
242
|
+
const metadata = JSON.parse(
|
|
243
|
+
fs.readFileSync(path.join(root, 'package.json'), 'utf8'),
|
|
244
|
+
);
|
|
245
|
+
for (const name of skillNames) {
|
|
246
|
+
const file = `.agents/skills/${name}/SKILL.md`;
|
|
247
|
+
if (!files.has(file))
|
|
248
|
+
reject('assets', 'An operational skill is missing.', file);
|
|
249
|
+
const data = contract.parseSkill(
|
|
250
|
+
contract.textFile(files.get(file), file),
|
|
251
|
+
file,
|
|
252
|
+
name,
|
|
253
|
+
).data;
|
|
254
|
+
if (
|
|
255
|
+
data.license !== 'MIT' ||
|
|
256
|
+
data.compatibility !== 'Requires Node.js >= 22.' ||
|
|
257
|
+
data.metadata?.generatedBy !== metadata.name ||
|
|
258
|
+
data.metadata?.generatorVersion !== metadata.version
|
|
259
|
+
)
|
|
260
|
+
reject(
|
|
261
|
+
'assets',
|
|
262
|
+
'A prepared skill has invalid or stale generator metadata.',
|
|
263
|
+
file,
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
contract.validateHubFiles(files);
|
|
267
|
+
return { files, contract };
|
|
268
|
+
}
|
|
269
|
+
export function planApply(
|
|
270
|
+
directory,
|
|
271
|
+
{ assets, contract, fs = nodeFs, git = nativeGit, cwd = process.cwd() } = {},
|
|
272
|
+
) {
|
|
273
|
+
if (!assets || !contract)
|
|
274
|
+
reject('assets', 'Authoritative prepared package assets are required.');
|
|
275
|
+
for (const name of skillNames)
|
|
276
|
+
if (!assets.has(`.agents/skills/${name}/SKILL.md`))
|
|
277
|
+
reject(
|
|
278
|
+
'assets',
|
|
279
|
+
'The prepared hub must contain all five independent operational skills.',
|
|
280
|
+
);
|
|
281
|
+
contract.validateHubFiles(assets);
|
|
282
|
+
const root = path.resolve(cwd, directory);
|
|
283
|
+
guardRole(root, fs);
|
|
284
|
+
const exists = !!maybeStat(root, fs);
|
|
285
|
+
const names = exists ? fs.readdirSync(root) : [];
|
|
286
|
+
const manifestPath = path.join(root, 'agentscli-hub.yml');
|
|
287
|
+
const existing = !!maybeStat(manifestPath, fs);
|
|
288
|
+
const resumed =
|
|
289
|
+
!existing && names.length
|
|
290
|
+
? interruptedBootstrap(root, assets, fs, git)
|
|
291
|
+
: false;
|
|
292
|
+
if (!existing && names.length && !resumed)
|
|
293
|
+
reject(
|
|
294
|
+
'role',
|
|
295
|
+
'First apply requires a new or empty target directory, or a provable interrupted bootstrap.',
|
|
296
|
+
root,
|
|
297
|
+
);
|
|
298
|
+
if (existing) {
|
|
299
|
+
assertRegular(manifestPath, fs);
|
|
300
|
+
contract.parseManifest(fs.readFileSync(manifestPath, 'utf8'));
|
|
301
|
+
if (!fs.existsSync(path.join(root, '.git')))
|
|
302
|
+
reject(
|
|
303
|
+
'git',
|
|
304
|
+
'An existing hub must retain its native Git metadata.',
|
|
305
|
+
root,
|
|
306
|
+
);
|
|
307
|
+
} else git(['--version'], { cwd });
|
|
308
|
+
const current = new Map();
|
|
309
|
+
for (const prefix of [
|
|
310
|
+
'docs/conventions',
|
|
311
|
+
'conventions',
|
|
312
|
+
'skills',
|
|
313
|
+
...skillNames.map((name) => `.agents/skills/${name}`),
|
|
314
|
+
]) {
|
|
315
|
+
assertParents(path.join(root, prefix, '__preflight__'), root, fs);
|
|
316
|
+
for (const [relative, bytes] of contract.safeWalk(
|
|
317
|
+
path.join(root, prefix),
|
|
318
|
+
fs,
|
|
319
|
+
))
|
|
320
|
+
current.set(`${prefix}/${relative}`, bytes);
|
|
321
|
+
}
|
|
322
|
+
for (const file of [
|
|
323
|
+
'agentscli-hub.yml',
|
|
324
|
+
'AGENTS.md',
|
|
325
|
+
'README.md',
|
|
326
|
+
'.gitattributes',
|
|
327
|
+
'.gitignore',
|
|
328
|
+
]) {
|
|
329
|
+
const absolute = path.join(root, file);
|
|
330
|
+
assertRegular(absolute, fs);
|
|
331
|
+
if (fs.existsSync(absolute)) current.set(file, fs.readFileSync(absolute));
|
|
332
|
+
}
|
|
333
|
+
const portable = new Map();
|
|
334
|
+
for (const file of [...current.keys(), ...assets.keys()]) {
|
|
335
|
+
const parts = file.split('/');
|
|
336
|
+
for (let index = 1; index <= parts.length; index++) {
|
|
337
|
+
const prefix = parts.slice(0, index).join('/'),
|
|
338
|
+
key = prefix.toLowerCase();
|
|
339
|
+
if (portable.has(key) && portable.get(key) !== prefix)
|
|
340
|
+
reject(
|
|
341
|
+
'collision',
|
|
342
|
+
'Existing and desired managed paths have a portable alias.',
|
|
343
|
+
file,
|
|
344
|
+
);
|
|
345
|
+
portable.set(key, prefix);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
const desired = new Map(current);
|
|
349
|
+
const managed = (file) =>
|
|
350
|
+
file === 'README.md' ||
|
|
351
|
+
skillNames.some((name) => file.startsWith(`.agents/skills/${name}/`));
|
|
352
|
+
for (const file of desired.keys()) if (managed(file)) desired.delete(file);
|
|
353
|
+
for (const [file, bytes] of assets)
|
|
354
|
+
if (!existing || managed(file)) desired.set(file, Buffer.from(bytes));
|
|
355
|
+
// Only desired canonical operational declarations are authoritative. Edited
|
|
356
|
+
// managed installations are disposable, including their requirement copies.
|
|
357
|
+
contract.validateHubFiles(desired, { projected: true });
|
|
358
|
+
const effects = [];
|
|
359
|
+
if (!existing && !resumed) effects.push({ kind: 'git-init', path: '.git' });
|
|
360
|
+
if (!existing)
|
|
361
|
+
for (const directory of ['conventions', 'skills'])
|
|
362
|
+
if (!maybeStat(path.join(root, directory), fs))
|
|
363
|
+
effects.push({ kind: 'mkdir', path: directory });
|
|
364
|
+
const removed = new Set();
|
|
365
|
+
for (const file of current.keys())
|
|
366
|
+
if (managed(file) && !desired.has(file)) {
|
|
367
|
+
removed.add(file);
|
|
368
|
+
effects.push({ kind: 'remove', path: file, previous: current.get(file) });
|
|
369
|
+
}
|
|
370
|
+
const requiredDirectories = new Set();
|
|
371
|
+
for (const file of desired.keys())
|
|
372
|
+
if (managed(file)) {
|
|
373
|
+
const parts = file.split('/');
|
|
374
|
+
for (let index = 1; index < parts.length; index++)
|
|
375
|
+
requiredDirectories.add(parts.slice(0, index).join('/'));
|
|
376
|
+
}
|
|
377
|
+
const managedDirectories = [];
|
|
378
|
+
const directories = (absolute, relative) => {
|
|
379
|
+
const stat = maybeStat(absolute, fs);
|
|
380
|
+
if (!stat || !stat.isDirectory()) return;
|
|
381
|
+
for (const name of fs.readdirSync(absolute))
|
|
382
|
+
if (maybeStat(path.join(absolute, name), fs)?.isDirectory())
|
|
383
|
+
directories(path.join(absolute, name), `${relative}/${name}`);
|
|
384
|
+
managedDirectories.push(relative);
|
|
385
|
+
};
|
|
386
|
+
for (const name of skillNames)
|
|
387
|
+
directories(
|
|
388
|
+
path.join(root, '.agents/skills', name),
|
|
389
|
+
`.agents/skills/${name}`,
|
|
390
|
+
);
|
|
391
|
+
for (const directory of managedDirectories)
|
|
392
|
+
if (!requiredDirectories.has(directory)) {
|
|
393
|
+
removed.add(directory);
|
|
394
|
+
effects.push({ kind: 'rmdir', path: directory });
|
|
395
|
+
}
|
|
396
|
+
for (const [file, bytes] of desired) {
|
|
397
|
+
if (existing && !managed(file)) continue;
|
|
398
|
+
const before = current.get(file);
|
|
399
|
+
if (!before || !Buffer.from(before).equals(bytes))
|
|
400
|
+
effects.push({
|
|
401
|
+
kind: before ? 'update' : 'create',
|
|
402
|
+
path: file,
|
|
403
|
+
bytes,
|
|
404
|
+
previous: before,
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
if (!existing)
|
|
408
|
+
effects.sort(
|
|
409
|
+
(left, right) =>
|
|
410
|
+
Number(left.path === 'agentscli-hub.yml') -
|
|
411
|
+
Number(right.path === 'agentscli-hub.yml'),
|
|
412
|
+
);
|
|
413
|
+
const projected = {
|
|
414
|
+
...fs,
|
|
415
|
+
lstatSync(absolute) {
|
|
416
|
+
const relative = path.relative(root, absolute).split(path.sep).join('/');
|
|
417
|
+
if (removed.has(relative)) {
|
|
418
|
+
const error = new Error('Planned removal');
|
|
419
|
+
error.code = 'ENOENT';
|
|
420
|
+
throw error;
|
|
421
|
+
}
|
|
422
|
+
return fs.lstatSync(absolute);
|
|
423
|
+
},
|
|
424
|
+
};
|
|
425
|
+
for (const effect of effects) {
|
|
426
|
+
if (effect.kind === 'git-init') continue;
|
|
427
|
+
if (!contract.portablePath(effect.path))
|
|
428
|
+
reject('unsafe-path', 'Effect is not portable.', effect.path);
|
|
429
|
+
const absolute = path.resolve(root, effect.path);
|
|
430
|
+
if (!absolute.startsWith(`${root}${path.sep}`))
|
|
431
|
+
reject('unsafe-path', 'Effect escaped the target.', effect.path);
|
|
432
|
+
const view = ['remove', 'rmdir'].includes(effect.kind) ? fs : projected;
|
|
433
|
+
assertParents(absolute, root, view);
|
|
434
|
+
if (!['mkdir', 'rmdir'].includes(effect.kind))
|
|
435
|
+
assertRegular(absolute, view);
|
|
436
|
+
}
|
|
437
|
+
return { command: 'apply', root, existing, resumed, effects };
|
|
438
|
+
}
|
|
439
|
+
function replaceWholeFile(fs, root, effect) {
|
|
440
|
+
const absolute = path.resolve(root, effect.path);
|
|
441
|
+
assertParents(absolute, root, fs);
|
|
442
|
+
assertRegular(absolute, fs);
|
|
443
|
+
const before = maybeStat(absolute, fs)
|
|
444
|
+
? fs.readFileSync(absolute)
|
|
445
|
+
: undefined;
|
|
446
|
+
if (
|
|
447
|
+
!!before !== !!effect.previous ||
|
|
448
|
+
(before && !Buffer.from(before).equals(effect.previous))
|
|
449
|
+
)
|
|
450
|
+
reject(
|
|
451
|
+
'destination-changed',
|
|
452
|
+
'Destination changed after preflight. Repeat apply to recompute its plan.',
|
|
453
|
+
effect.path,
|
|
454
|
+
);
|
|
455
|
+
if (effect.kind === 'remove') {
|
|
456
|
+
fs.unlinkSync(absolute);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
fs.mkdirSync(path.dirname(absolute), { recursive: true });
|
|
460
|
+
const temporary = path.join(
|
|
461
|
+
path.dirname(absolute),
|
|
462
|
+
`.${path.basename(absolute)}.agentscli-${randomUUID()}.tmp`,
|
|
463
|
+
);
|
|
464
|
+
try {
|
|
465
|
+
fs.writeFileSync(temporary, effect.bytes, { flag: 'wx' });
|
|
466
|
+
assertParents(absolute, root, fs);
|
|
467
|
+
assertRegular(absolute, fs);
|
|
468
|
+
const current = maybeStat(absolute, fs)
|
|
469
|
+
? fs.readFileSync(absolute)
|
|
470
|
+
: undefined;
|
|
471
|
+
if (
|
|
472
|
+
!!current !== !!before ||
|
|
473
|
+
(current && !Buffer.from(current).equals(before))
|
|
474
|
+
)
|
|
475
|
+
reject(
|
|
476
|
+
'destination-changed',
|
|
477
|
+
'Destination changed during whole-file preparation. Repeat apply.',
|
|
478
|
+
effect.path,
|
|
479
|
+
);
|
|
480
|
+
fs.renameSync(temporary, absolute);
|
|
481
|
+
} finally {
|
|
482
|
+
if (maybeStat(temporary, fs)) fs.unlinkSync(temporary);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
export function executeApply(
|
|
486
|
+
plan,
|
|
487
|
+
{ fs = nodeFs, git = nativeGit, dryRun = false } = {},
|
|
488
|
+
) {
|
|
489
|
+
const completed = [];
|
|
490
|
+
const publicEffect = ({ kind, path: file }) => ({ kind, path: file });
|
|
491
|
+
if (dryRun)
|
|
492
|
+
return {
|
|
493
|
+
ok: true,
|
|
494
|
+
dryRun: true,
|
|
495
|
+
root: plan.root,
|
|
496
|
+
changes: plan.effects.map(publicEffect),
|
|
497
|
+
completed: [],
|
|
498
|
+
pending: [],
|
|
499
|
+
};
|
|
500
|
+
try {
|
|
501
|
+
for (const effect of plan.effects) {
|
|
502
|
+
fs.mkdirSync(plan.root, { recursive: true });
|
|
503
|
+
if (effect.kind === 'git-init')
|
|
504
|
+
git(['init', '--', plan.root], { cwd: path.dirname(plan.root) });
|
|
505
|
+
else if (effect.kind === 'mkdir') {
|
|
506
|
+
const absolute = path.join(plan.root, effect.path);
|
|
507
|
+
const stat = maybeStat(absolute, fs);
|
|
508
|
+
assertParents(absolute, plan.root, fs);
|
|
509
|
+
if (stat && (!stat.isDirectory() || stat.isSymbolicLink()))
|
|
510
|
+
reject(
|
|
511
|
+
'unsafe-path',
|
|
512
|
+
'Directory destination changed after preflight.',
|
|
513
|
+
effect.path,
|
|
514
|
+
);
|
|
515
|
+
fs.mkdirSync(absolute, { recursive: true });
|
|
516
|
+
} else if (effect.kind === 'rmdir') {
|
|
517
|
+
const absolute = path.resolve(plan.root, effect.path);
|
|
518
|
+
assertParents(absolute, plan.root, fs);
|
|
519
|
+
const stat = maybeStat(absolute, fs);
|
|
520
|
+
if (stat && (!stat.isDirectory() || stat.isSymbolicLink()))
|
|
521
|
+
reject(
|
|
522
|
+
'unsafe-path',
|
|
523
|
+
'Managed directory changed after preflight.',
|
|
524
|
+
effect.path,
|
|
525
|
+
);
|
|
526
|
+
if (stat) fs.rmdirSync(absolute);
|
|
527
|
+
} else replaceWholeFile(fs, plan.root, effect);
|
|
528
|
+
completed.push(publicEffect(effect));
|
|
529
|
+
}
|
|
530
|
+
return {
|
|
531
|
+
ok: true,
|
|
532
|
+
dryRun: false,
|
|
533
|
+
root: plan.root,
|
|
534
|
+
changes: completed,
|
|
535
|
+
completed,
|
|
536
|
+
pending: [],
|
|
537
|
+
};
|
|
538
|
+
} catch {
|
|
539
|
+
return {
|
|
540
|
+
ok: false,
|
|
541
|
+
dryRun: false,
|
|
542
|
+
root: plan.root,
|
|
543
|
+
changes: plan.effects.map(publicEffect),
|
|
544
|
+
completed,
|
|
545
|
+
pending: plan.effects.slice(completed.length).map(publicEffect),
|
|
546
|
+
diagnostics: [
|
|
547
|
+
{
|
|
548
|
+
code: 'operation',
|
|
549
|
+
message:
|
|
550
|
+
'Apply stopped after partial effects. Retry the same apply command to recompute remaining work.',
|
|
551
|
+
},
|
|
552
|
+
],
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
export async function runCli(
|
|
557
|
+
args,
|
|
558
|
+
{
|
|
559
|
+
version,
|
|
560
|
+
log = console.log,
|
|
561
|
+
error = console.error,
|
|
562
|
+
fs = nodeFs,
|
|
563
|
+
git = nativeGit,
|
|
564
|
+
assets,
|
|
565
|
+
contract,
|
|
566
|
+
cwd = process.cwd(),
|
|
567
|
+
} = {},
|
|
568
|
+
) {
|
|
569
|
+
try {
|
|
570
|
+
const parsed = parseArgs(args);
|
|
571
|
+
if (parsed.command === 'help') {
|
|
572
|
+
log(
|
|
573
|
+
'Usage: agentscli-hub apply <directory> [--dry-run]\n\nCreate or refresh a standalone resource hub.\nOptions: --help, --version',
|
|
574
|
+
);
|
|
575
|
+
return 0;
|
|
576
|
+
}
|
|
577
|
+
if (parsed.command === 'version') {
|
|
578
|
+
log(version);
|
|
579
|
+
return 0;
|
|
580
|
+
}
|
|
581
|
+
if (!assets)
|
|
582
|
+
({ files: assets, contract } = await loadAssets(undefined, fs));
|
|
583
|
+
const plan = planApply(parsed.directory, {
|
|
584
|
+
assets,
|
|
585
|
+
contract,
|
|
586
|
+
fs,
|
|
587
|
+
git,
|
|
588
|
+
cwd,
|
|
589
|
+
});
|
|
590
|
+
const result = executeApply(plan, { fs, git, dryRun: parsed.dryRun });
|
|
591
|
+
log(JSON.stringify(result));
|
|
592
|
+
return result.ok ? 0 : 3;
|
|
593
|
+
} catch (cause) {
|
|
594
|
+
error(
|
|
595
|
+
JSON.stringify({
|
|
596
|
+
ok: false,
|
|
597
|
+
diagnostics: [
|
|
598
|
+
{
|
|
599
|
+
code: cause.code ?? 'operation',
|
|
600
|
+
file: cause.file ?? '',
|
|
601
|
+
message: cause.code
|
|
602
|
+
? cause.message
|
|
603
|
+
: 'Hub application failed before completion.',
|
|
604
|
+
},
|
|
605
|
+
],
|
|
606
|
+
}),
|
|
607
|
+
);
|
|
608
|
+
return cause.code ? 2 : 3;
|
|
609
|
+
}
|
|
610
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devel-solid/agentscli-hub",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Create and maintain standalone Git hubs for portable conventions and skills.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/devel-solid/agentscli.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/devel-solid/agentscli#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/devel-solid/agentscli/issues"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"bin": {
|
|
16
|
+
"agentscli-hub": "dist/cli.js"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist/**/*.js",
|
|
20
|
+
"dist/assets/",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": "^22.0.0 || ^24.0.0 || ^26.0.0"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "node scripts/build.mjs",
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"test": "node scripts/test.mjs",
|
|
34
|
+
"format": "prettier --write src scripts tests/unit assets package.json tsconfig.json README.md",
|
|
35
|
+
"format:check": "prettier --check src scripts tests/unit assets package.json tsconfig.json README.md",
|
|
36
|
+
"check": "npm run typecheck && npm run format:check && npm test",
|
|
37
|
+
"prepack": "npm run build"
|
|
38
|
+
}
|
|
39
|
+
}
|