@buaa_smat/hometrans 26.0.6-devecocli.1 → 26.0.7
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 +413 -0
- package/dist/cli/config-store.js +128 -0
- package/dist/cli/config.js +48 -0
- package/dist/cli/editors.json +147 -0
- package/dist/cli/env-vars.js +127 -0
- package/dist/cli/index.js +76 -0
- package/dist/cli/init.js +2618 -0
- package/dist/cli/uninstall.js +406 -0
- package/dist/platform/compose.js +232 -0
- package/dist/platform/frontmatter.js +151 -0
- package/dist/platform/installer.js +417 -0
- package/dist/platform/language.js +77 -0
- package/dist/platform/registry.js +340 -0
- package/dist/platform/transform.js +397 -0
- package/dist/platform/types.js +9 -0
- package/dist/platform/validate.js +153 -0
- package/dist/utils/regex.js +7 -0
- package/env-requirements.json +536 -0
- package/package.json +8 -8
- package/resource/choose_editor.png +0 -0
- package/resource/set_sdk.png +0 -0
- package/resource/title_dark.png +0 -0
- package/devecocli-plugin/common.ts +0 -2307
- package/devecocli-plugin/content-resolver.ts +0 -363
- package/devecocli-plugin/install.ts +0 -72
- package/devecocli-plugin/uninstall.ts +0 -26
- package/devecocli-plugin/update.ts +0 -77
- package/index.js +0 -1677
- package/plugin.jsonc +0 -12
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ht uninstall` — remove all skills and agents that `ht init` installed into
|
|
3
|
+
* the configured editors.
|
|
4
|
+
*
|
|
5
|
+
* Determines what to remove by matching bundled skill/agent names against
|
|
6
|
+
* each editor's target directories.
|
|
7
|
+
*
|
|
8
|
+
* Shows a plan and prompts for confirmation before proceeding.
|
|
9
|
+
*/
|
|
10
|
+
import fs from 'node:fs/promises';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { fileURLToPath } from 'node:url';
|
|
13
|
+
import { execFile } from 'node:child_process';
|
|
14
|
+
import { promisify } from 'node:util';
|
|
15
|
+
import chalk from 'chalk';
|
|
16
|
+
import inquirer from 'inquirer';
|
|
17
|
+
import { defaultEditors, LEGACY_ENV_KEYS, legacyHomeTransDir, SHARED_AGENTS_FALLBACK, } from './config-store.js';
|
|
18
|
+
import { dirExists, isPromptAbort, prettyHome, MACHINE_ENV_KEYS } from './init.js';
|
|
19
|
+
import { removeEnvVars } from './env-vars.js';
|
|
20
|
+
import { collectUninstallPaths, listBundledAgents, listBundledCommands, listBundledSkills, } from '../platform/installer.js';
|
|
21
|
+
import { editorDisplayName, platformForEditor } from '../platform/registry.js';
|
|
22
|
+
const execFileAsync = promisify(execFile);
|
|
23
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
24
|
+
const __dirname = path.dirname(__filename);
|
|
25
|
+
/**
|
|
26
|
+
* Global Node CLIs that `ht init` installs as a pair (`npm install -g homegraph
|
|
27
|
+
* arkanalyzer`) plus the required spec-pipeline checker. `ht uninstall` removes
|
|
28
|
+
* the same set so the two commands stay symmetric.
|
|
29
|
+
*/
|
|
30
|
+
const NODE_DEPS = ['homegraph', 'arkanalyzer', 'homecheck'];
|
|
31
|
+
/**
|
|
32
|
+
* Machine env var names `ht init` sets (SDK paths) plus the ones older releases
|
|
33
|
+
* exported (tool dir, multimodal model), limited to those present in this
|
|
34
|
+
* environment. There is no config file recording what init wrote, so presence
|
|
35
|
+
* is the signal; the plan lists them and the user confirms before removal.
|
|
36
|
+
*/
|
|
37
|
+
function plannedEnvVarNames(env = process.env) {
|
|
38
|
+
return [...MACHINE_ENV_KEYS, ...LEGACY_ENV_KEYS].filter((k) => (env[k] ?? '').trim().length > 0);
|
|
39
|
+
}
|
|
40
|
+
/* ------------------------------------------------------------------ */
|
|
41
|
+
/* Bundled content discovery */
|
|
42
|
+
/* ------------------------------------------------------------------ */
|
|
43
|
+
function resolveSkillsRoot() {
|
|
44
|
+
return path.resolve(__dirname, '..', '..', 'skills');
|
|
45
|
+
}
|
|
46
|
+
function resolveAgentsRoot() {
|
|
47
|
+
return path.resolve(__dirname, '..', '..', 'agents');
|
|
48
|
+
}
|
|
49
|
+
function resolveCommandsRoot() {
|
|
50
|
+
return path.resolve(__dirname, '..', '..', 'commands');
|
|
51
|
+
}
|
|
52
|
+
function resolveRoots() {
|
|
53
|
+
return {
|
|
54
|
+
skillsRoot: resolveSkillsRoot(),
|
|
55
|
+
agentsRoot: resolveAgentsRoot(),
|
|
56
|
+
commandsRoot: resolveCommandsRoot(),
|
|
57
|
+
platformsRoot: path.resolve(__dirname, '..', '..', 'platforms'),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/* ------------------------------------------------------------------ */
|
|
61
|
+
/* Global npm dependency removal */
|
|
62
|
+
/* ------------------------------------------------------------------ */
|
|
63
|
+
/**
|
|
64
|
+
* Is `npm` runnable? On Windows the global npm bin is a `.cmd` shim, so the
|
|
65
|
+
* command must run through the shell there (same as init's homegraphVersion()).
|
|
66
|
+
*/
|
|
67
|
+
async function npmAvailable() {
|
|
68
|
+
try {
|
|
69
|
+
await execFileAsync('npm', ['--version'], {
|
|
70
|
+
timeout: 15000,
|
|
71
|
+
shell: process.platform === 'win32',
|
|
72
|
+
});
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Does `homegraph --version` return normally (exit 0)? Used as the presence
|
|
81
|
+
* probe for the homegraph/arkanalyzer pair — the symmetric counterpart to
|
|
82
|
+
* init's idempotency check, which skips the install when homegraph is already
|
|
83
|
+
* present.
|
|
84
|
+
*/
|
|
85
|
+
async function homegraphInstalled() {
|
|
86
|
+
try {
|
|
87
|
+
await execFileAsync('homegraph', ['--version'], {
|
|
88
|
+
timeout: 15000,
|
|
89
|
+
shell: process.platform === 'win32',
|
|
90
|
+
});
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Decide which global npm packages to remove. Returns the homegraph/arkanalyzer
|
|
99
|
+
* pair when homegraph is detected on the machine (mirroring init, which installs
|
|
100
|
+
* them together), or an empty list when npm is unavailable or homegraph is not
|
|
101
|
+
* present — so a machine that never ran `ht init`'s dependency install shows
|
|
102
|
+
* nothing to remove here.
|
|
103
|
+
*/
|
|
104
|
+
async function detectNodeDepsToRemove() {
|
|
105
|
+
if (!(await npmAvailable()))
|
|
106
|
+
return [];
|
|
107
|
+
if (!(await homegraphInstalled()))
|
|
108
|
+
return [];
|
|
109
|
+
return [...NODE_DEPS];
|
|
110
|
+
}
|
|
111
|
+
/* ------------------------------------------------------------------ */
|
|
112
|
+
/* Plan building */
|
|
113
|
+
/* ------------------------------------------------------------------ */
|
|
114
|
+
async function buildPlanForEditor(editor, roots, plan) {
|
|
115
|
+
const platform = platformForEditor(editor);
|
|
116
|
+
if (platform.markerDir && !(await dirExists(platform.markerDir)))
|
|
117
|
+
return;
|
|
118
|
+
const entries = await collectUninstallPaths(platform, roots);
|
|
119
|
+
for (const entry of entries) {
|
|
120
|
+
plan.deletions.push({
|
|
121
|
+
display: `${editorDisplayName(editor.name)}: ${prettyHome(entry.absPath)}${entry.isDir ? '/' : ''}`,
|
|
122
|
+
absPath: entry.absPath,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/** Line-based diff via an LCS table. Files here are small config files. */
|
|
127
|
+
function diffLines(oldLines, newLines) {
|
|
128
|
+
const n = oldLines.length;
|
|
129
|
+
const m = newLines.length;
|
|
130
|
+
const dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
|
|
131
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
132
|
+
for (let j = m - 1; j >= 0; j--) {
|
|
133
|
+
dp[i][j] =
|
|
134
|
+
oldLines[i] === newLines[j]
|
|
135
|
+
? dp[i + 1][j + 1] + 1
|
|
136
|
+
: Math.max(dp[i + 1][j], dp[i][j + 1]);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const out = [];
|
|
140
|
+
let i = 0;
|
|
141
|
+
let j = 0;
|
|
142
|
+
while (i < n && j < m) {
|
|
143
|
+
if (oldLines[i] === newLines[j]) {
|
|
144
|
+
out.push({ type: ' ', text: oldLines[i] });
|
|
145
|
+
i++;
|
|
146
|
+
j++;
|
|
147
|
+
}
|
|
148
|
+
else if (dp[i + 1][j] >= dp[i][j + 1]) {
|
|
149
|
+
out.push({ type: '-', text: oldLines[i] });
|
|
150
|
+
i++;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
out.push({ type: '+', text: newLines[j] });
|
|
154
|
+
j++;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
while (i < n)
|
|
158
|
+
out.push({ type: '-', text: oldLines[i++] });
|
|
159
|
+
while (j < m)
|
|
160
|
+
out.push({ type: '+', text: newLines[j++] });
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Render a unified, git-style diff with `context` unchanged lines around each
|
|
165
|
+
* change; long unchanged runs collapse to a `⋮` marker. Returns coloured,
|
|
166
|
+
* pre-indented lines ready to print.
|
|
167
|
+
*/
|
|
168
|
+
function renderDiff(oldContent, newContent, context = 3) {
|
|
169
|
+
const diff = diffLines(oldContent.split('\n'), newContent.split('\n'));
|
|
170
|
+
const keep = new Array(diff.length).fill(false);
|
|
171
|
+
for (let k = 0; k < diff.length; k++) {
|
|
172
|
+
if (diff[k].type === ' ')
|
|
173
|
+
continue;
|
|
174
|
+
const from = Math.max(0, k - context);
|
|
175
|
+
const to = Math.min(diff.length - 1, k + context);
|
|
176
|
+
for (let c = from; c <= to; c++)
|
|
177
|
+
keep[c] = true;
|
|
178
|
+
}
|
|
179
|
+
const lines = [];
|
|
180
|
+
let gapOpen = false;
|
|
181
|
+
for (let k = 0; k < diff.length; k++) {
|
|
182
|
+
if (!keep[k]) {
|
|
183
|
+
if (!gapOpen) {
|
|
184
|
+
lines.push(chalk.gray(' ⋮'));
|
|
185
|
+
gapOpen = true;
|
|
186
|
+
}
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
gapOpen = false;
|
|
190
|
+
const { type, text } = diff[k];
|
|
191
|
+
if (type === '-')
|
|
192
|
+
lines.push(chalk.red(` - ${text}`));
|
|
193
|
+
else if (type === '+')
|
|
194
|
+
lines.push(chalk.green(` + ${text}`));
|
|
195
|
+
else
|
|
196
|
+
lines.push(chalk.gray(` ${text}`));
|
|
197
|
+
}
|
|
198
|
+
return lines;
|
|
199
|
+
}
|
|
200
|
+
/* ------------------------------------------------------------------ */
|
|
201
|
+
/* Render */
|
|
202
|
+
/* ------------------------------------------------------------------ */
|
|
203
|
+
function isPlanEmpty(plan) {
|
|
204
|
+
return (plan.deletions.length === 0 &&
|
|
205
|
+
plan.modifications.length === 0 &&
|
|
206
|
+
plan.npmDeps.length === 0 &&
|
|
207
|
+
plan.envVars.length === 0 &&
|
|
208
|
+
plan.configDir === null);
|
|
209
|
+
}
|
|
210
|
+
function renderPlan(plan) {
|
|
211
|
+
console.log(chalk.bold('\nHomeTrans uninstall plan\n'));
|
|
212
|
+
if (isPlanEmpty(plan)) {
|
|
213
|
+
console.log(chalk.gray(' Nothing to uninstall — no hometrans files found in configured editors.\n'));
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (plan.deletions.length > 0) {
|
|
217
|
+
console.log(chalk.red.bold(` Will be deleted (${plan.deletions.length} entries):`));
|
|
218
|
+
for (const d of plan.deletions) {
|
|
219
|
+
console.log(` ${chalk.red('-')} ${d.display}`);
|
|
220
|
+
}
|
|
221
|
+
console.log('');
|
|
222
|
+
}
|
|
223
|
+
if (plan.modifications.length > 0) {
|
|
224
|
+
console.log(chalk.yellow.bold(` Will be modified (${plan.modifications.length} files):`));
|
|
225
|
+
for (const m of plan.modifications) {
|
|
226
|
+
console.log(` ${chalk.yellow('~')} ${m.display} ${chalk.gray(`(${m.reason})`)}`);
|
|
227
|
+
for (const line of renderDiff(m.oldContent, m.newContent)) {
|
|
228
|
+
console.log(line);
|
|
229
|
+
}
|
|
230
|
+
console.log('');
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (plan.npmDeps.length > 0) {
|
|
234
|
+
console.log(chalk.red.bold(` Global npm packages to remove (${plan.npmDeps.length}):`));
|
|
235
|
+
console.log(chalk.gray(` (will run: npm uninstall -g ${plan.npmDeps.join(' ')})`));
|
|
236
|
+
for (const dep of plan.npmDeps) {
|
|
237
|
+
console.log(` ${chalk.red('-')} ${dep}`);
|
|
238
|
+
}
|
|
239
|
+
console.log('');
|
|
240
|
+
}
|
|
241
|
+
if (plan.envVars.length > 0) {
|
|
242
|
+
console.log(chalk.red.bold(` Environment variables to remove (${plan.envVars.length}):`));
|
|
243
|
+
console.log(chalk.gray(process.platform === 'win32'
|
|
244
|
+
? ' (Windows: deleted from the per-user environment)'
|
|
245
|
+
: ' (macOS/Linux: the hometrans export block is removed from your shell profile)'));
|
|
246
|
+
for (const name of plan.envVars) {
|
|
247
|
+
console.log(` ${chalk.red('-')} ${name}`);
|
|
248
|
+
}
|
|
249
|
+
console.log('');
|
|
250
|
+
}
|
|
251
|
+
if (plan.configDir) {
|
|
252
|
+
console.log(chalk.red.bold(' Leftover directory from an older release to delete:'));
|
|
253
|
+
console.log(` ${chalk.red('-')} ${prettyHome(plan.configDir)}/`);
|
|
254
|
+
console.log('');
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/* ------------------------------------------------------------------ */
|
|
258
|
+
/* Execute */
|
|
259
|
+
/* ------------------------------------------------------------------ */
|
|
260
|
+
async function executePlan(plan) {
|
|
261
|
+
let deleted = 0;
|
|
262
|
+
let modified = 0;
|
|
263
|
+
let npmNote = null;
|
|
264
|
+
let envNote = null;
|
|
265
|
+
for (const mod of plan.modifications) {
|
|
266
|
+
await fs.writeFile(mod.absPath, mod.newContent, 'utf-8');
|
|
267
|
+
modified++;
|
|
268
|
+
}
|
|
269
|
+
// Remove the global npm CLIs installed by `ht init` (homegraph + arkanalyzer, homecheck).
|
|
270
|
+
// Best-effort: a failure is reported in the note but never aborts the rest of
|
|
271
|
+
// the uninstall.
|
|
272
|
+
if (plan.npmDeps.length > 0) {
|
|
273
|
+
console.log(chalk.gray(` Running: npm uninstall -g ${plan.npmDeps.join(' ')} ...`));
|
|
274
|
+
try {
|
|
275
|
+
await execFileAsync('npm', ['uninstall', '-g', ...plan.npmDeps], {
|
|
276
|
+
timeout: 5 * 60 * 1000,
|
|
277
|
+
shell: process.platform === 'win32',
|
|
278
|
+
});
|
|
279
|
+
npmNote = `removed ${plan.npmDeps.join(', ')}`;
|
|
280
|
+
}
|
|
281
|
+
catch (err) {
|
|
282
|
+
npmNote = `failed (${err.message}) — remove manually: npm uninstall -g ${plan.npmDeps.join(' ')}`;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
for (const del of plan.deletions) {
|
|
286
|
+
try {
|
|
287
|
+
await fs.rm(del.absPath, { recursive: true, force: true });
|
|
288
|
+
deleted++;
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
// best-effort
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
// Remove the machine environment variables, then the leftover ~/.hometrans.
|
|
295
|
+
if (plan.envVars.length > 0) {
|
|
296
|
+
envNote = await removeEnvVars(plan.envVars);
|
|
297
|
+
}
|
|
298
|
+
if (plan.configDir) {
|
|
299
|
+
try {
|
|
300
|
+
await fs.rm(plan.configDir, { recursive: true, force: true });
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
// best-effort
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return { deleted, modified, npmNote, envNote };
|
|
307
|
+
}
|
|
308
|
+
/* ------------------------------------------------------------------ */
|
|
309
|
+
/* Entry point */
|
|
310
|
+
/* ------------------------------------------------------------------ */
|
|
311
|
+
export async function uninstallCommand() {
|
|
312
|
+
const roots = resolveRoots();
|
|
313
|
+
const skillNames = await listBundledSkills(roots.skillsRoot);
|
|
314
|
+
const agentNames = await listBundledAgents(roots.agentsRoot);
|
|
315
|
+
const commandNames = await listBundledCommands(roots.commandsRoot);
|
|
316
|
+
const totalBundled = skillNames.length + agentNames.length + commandNames.length;
|
|
317
|
+
if (totalBundled === 0) {
|
|
318
|
+
console.log(chalk.yellow('\n No bundled skills or agents found — cannot determine what to uninstall.'));
|
|
319
|
+
console.log(chalk.gray(' Reinstall hometrans or manually remove skill/agent directories from your editors.\n'));
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
// Same editor table `ht init` installs from (bundled editors.json).
|
|
323
|
+
const editors = defaultEditors();
|
|
324
|
+
const plan = {
|
|
325
|
+
deletions: [],
|
|
326
|
+
modifications: [],
|
|
327
|
+
npmDeps: [],
|
|
328
|
+
envVars: [],
|
|
329
|
+
configDir: null,
|
|
330
|
+
};
|
|
331
|
+
// The shared ~/.agents location is checked too: `ht init` installs there
|
|
332
|
+
// when no editor was selected, so uninstall must clean it for symmetry.
|
|
333
|
+
// Harmless no-op when nothing was installed there.
|
|
334
|
+
// One failing target (e.g. an unreadable install directory)
|
|
335
|
+
// must not abort the whole uninstall: collect the failure and keep planning.
|
|
336
|
+
const planFailures = [];
|
|
337
|
+
for (const editor of [...editors, SHARED_AGENTS_FALLBACK]) {
|
|
338
|
+
try {
|
|
339
|
+
await buildPlanForEditor(editor, roots, plan);
|
|
340
|
+
}
|
|
341
|
+
catch (err) {
|
|
342
|
+
planFailures.push(`${editorDisplayName(editor.name)}: ${err.message}`);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
// Global npm CLIs installed by `ht init` (homegraph + arkanalyzer, homecheck) — detected
|
|
346
|
+
// the same way init decides whether to (re)install them.
|
|
347
|
+
plan.npmDeps = await detectNodeDepsToRemove();
|
|
348
|
+
plan.envVars = plannedEnvVarNames();
|
|
349
|
+
const legacyDir = legacyHomeTransDir();
|
|
350
|
+
if (await dirExists(legacyDir)) {
|
|
351
|
+
plan.configDir = legacyDir;
|
|
352
|
+
}
|
|
353
|
+
renderPlan(plan);
|
|
354
|
+
// Surface plan failures before the empty-plan early return, otherwise an
|
|
355
|
+
// all-targets-failed run would silently look like "nothing to uninstall".
|
|
356
|
+
if (planFailures.length > 0) {
|
|
357
|
+
console.log(chalk.yellow(' Some install targets could not be inspected:'));
|
|
358
|
+
for (const line of planFailures)
|
|
359
|
+
console.log(chalk.yellow(` ! ${line}`));
|
|
360
|
+
console.log('');
|
|
361
|
+
}
|
|
362
|
+
if (isPlanEmpty(plan)) {
|
|
363
|
+
if (planFailures.length > 0)
|
|
364
|
+
process.exitCode = 1;
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
let proceed;
|
|
368
|
+
try {
|
|
369
|
+
({ proceed } = await inquirer.prompt([
|
|
370
|
+
{
|
|
371
|
+
type: 'confirm',
|
|
372
|
+
name: 'proceed',
|
|
373
|
+
message: 'Continue?',
|
|
374
|
+
default: false,
|
|
375
|
+
},
|
|
376
|
+
]));
|
|
377
|
+
}
|
|
378
|
+
catch (err) {
|
|
379
|
+
// Ctrl-C / Esc at the confirm prompt — cancel cleanly, no stack trace.
|
|
380
|
+
if (isPromptAbort(err)) {
|
|
381
|
+
console.log(chalk.yellow('\n Uninstall cancelled. No files modified.\n'));
|
|
382
|
+
process.exit(130);
|
|
383
|
+
}
|
|
384
|
+
throw err;
|
|
385
|
+
}
|
|
386
|
+
if (!proceed) {
|
|
387
|
+
console.log(chalk.yellow('\n Uninstall cancelled. No files modified.\n'));
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const summary = await executePlan(plan);
|
|
391
|
+
console.log('');
|
|
392
|
+
console.log(chalk.green(` Uninstalled: ${summary.deleted} entries deleted, ${summary.modified} files modified.`));
|
|
393
|
+
if (summary.npmNote) {
|
|
394
|
+
console.log(chalk.green(` Global npm packages: ${summary.npmNote}`));
|
|
395
|
+
}
|
|
396
|
+
if (summary.envNote) {
|
|
397
|
+
console.log(chalk.green(` Environment variables: ${summary.envNote}`));
|
|
398
|
+
}
|
|
399
|
+
if (plan.configDir) {
|
|
400
|
+
console.log(chalk.green(` Removed leftover directory: ${prettyHome(plan.configDir)}/`));
|
|
401
|
+
}
|
|
402
|
+
// A partial uninstall (some targets uninspected) must not look like success.
|
|
403
|
+
if (planFailures.length > 0)
|
|
404
|
+
process.exitCode = 1;
|
|
405
|
+
console.log('');
|
|
406
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform-specific document composition (frontmatter + body assembly).
|
|
3
|
+
*
|
|
4
|
+
* Ported from `devecocli-plugin/common.ts`. Differences from the plugin:
|
|
5
|
+
* - the overlay root (`platforms/`) is an explicit, nullable argument; when it
|
|
6
|
+
* is null or missing every document falls back to the legacy transform, so
|
|
7
|
+
* a published package that excludes `platforms/` still installs correctly;
|
|
8
|
+
* - the CLI keeps its own copy instead of importing the plugin (the plugin is
|
|
9
|
+
* read-only for us). `src/cli/platform-parity.test.ts` locks the two
|
|
10
|
+
* implementations together.
|
|
11
|
+
*/
|
|
12
|
+
import fs from 'node:fs/promises';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { FRONTMATTER_BLOCK, parseFrontmatter } from './frontmatter.js';
|
|
15
|
+
import { commandToSkillMd, mapAgentColorToHex, tomlMultiline, transformAgentMd, transformCommandMd, transformSkillMd, } from './transform.js';
|
|
16
|
+
function normalizeText(raw) {
|
|
17
|
+
const noBom = raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw;
|
|
18
|
+
return noBom.replace(/\r\n/g, '\n');
|
|
19
|
+
}
|
|
20
|
+
/** Pull out a `---\n…\n---` block (including delimiters, without the trailing newline). */
|
|
21
|
+
function extractYamlHeader(raw) {
|
|
22
|
+
const m = FRONTMATTER_BLOCK.exec(raw);
|
|
23
|
+
return m
|
|
24
|
+
? m[0].replace(/\r\n/g, '\n').replace(/\n+$/, '').replace(/[ \t]+$/, '')
|
|
25
|
+
: null;
|
|
26
|
+
}
|
|
27
|
+
function trimmedBody(raw) {
|
|
28
|
+
const { body, hasFrontmatter } = parseFrontmatter(raw);
|
|
29
|
+
return (hasFrontmatter ? body : raw).replace(/^(?:\r?\n)+/, '').replace(/\s+$/, '');
|
|
30
|
+
}
|
|
31
|
+
function platformPaths(platformsRoot, platformName, kind, name) {
|
|
32
|
+
const base = path.join(platformsRoot, platformName);
|
|
33
|
+
if (kind === 'skill') {
|
|
34
|
+
const dir = path.join(base, 'skills', name);
|
|
35
|
+
return {
|
|
36
|
+
headerMd: path.join(dir, 'SKILL.frontmatter.md'),
|
|
37
|
+
headerToml: null,
|
|
38
|
+
unique: path.join(dir, 'SKILL.unique.md'),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const dir = path.join(base, kind === 'agent' ? 'agents' : 'commands');
|
|
42
|
+
const stem = name.replace(/\.md$/, '');
|
|
43
|
+
return {
|
|
44
|
+
headerMd: path.join(dir, `${stem}.frontmatter.md`),
|
|
45
|
+
headerToml: kind === 'agent' ? path.join(dir, `${stem}.frontmatter.toml`) : null,
|
|
46
|
+
unique: path.join(dir, `${stem}.unique.md`),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async function readOptional(p) {
|
|
50
|
+
try {
|
|
51
|
+
return normalizeText(await fs.readFile(p, 'utf-8'));
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
// A platform without this overlay is the normal case (most platforms ship
|
|
55
|
+
// none), so a missing file is null. Anything else — permissions, a
|
|
56
|
+
// directory at the path, an I/O error — must surface: silently falling back
|
|
57
|
+
// to the legacy transform would install a document different from the one
|
|
58
|
+
// the overlay was meant to produce.
|
|
59
|
+
if (err.code === 'ENOENT')
|
|
60
|
+
return null;
|
|
61
|
+
throw err;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function joinYamlBody(header, body) {
|
|
65
|
+
const h = header.replace(/\n+$/, '');
|
|
66
|
+
return body ? `${h}\n\n${body}\n` : `${h}\n`;
|
|
67
|
+
}
|
|
68
|
+
async function composeDoc(platform, kind, name, rootRaw, platformsRoot) {
|
|
69
|
+
const rootText = normalizeText(rootRaw);
|
|
70
|
+
const rootHeader = extractYamlHeader(rootText);
|
|
71
|
+
const rootBody = trimmedBody(rootText);
|
|
72
|
+
const [headerFile, tomlFile, uniqueFile] = platformsRoot
|
|
73
|
+
? await (async () => {
|
|
74
|
+
const paths = platformPaths(platformsRoot, platform.name, kind, name);
|
|
75
|
+
return Promise.all([
|
|
76
|
+
readOptional(paths.headerMd),
|
|
77
|
+
paths.headerToml ? readOptional(paths.headerToml) : Promise.resolve(null),
|
|
78
|
+
readOptional(paths.unique),
|
|
79
|
+
]);
|
|
80
|
+
})()
|
|
81
|
+
: [null, null, null];
|
|
82
|
+
const uniqueText = uniqueFile?.trimEnd() ?? null;
|
|
83
|
+
const body = [uniqueText, rootBody].filter((s) => !!s).join('\n\n');
|
|
84
|
+
return {
|
|
85
|
+
header: headerFile ? extractYamlHeader(headerFile) : null,
|
|
86
|
+
headerToml: tomlFile ? tomlFile.replace(/\s+$/, '') : null,
|
|
87
|
+
headerRaw: headerFile ? headerFile.replace(/\s+$/, '') : null,
|
|
88
|
+
unique: uniqueText,
|
|
89
|
+
rootHeader,
|
|
90
|
+
body,
|
|
91
|
+
legacyRaw: rootHeader ? joinYamlBody(rootHeader, body) : body ? `${body}\n` : '',
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function sourceNotes(c) {
|
|
95
|
+
return [
|
|
96
|
+
c.header || c.headerToml ? 'frontmatter: platforms definition' : 'frontmatter: root fallback',
|
|
97
|
+
c.unique ? 'body: unique patch + root body' : 'body: root body',
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
function composeYamlDoc(c) {
|
|
101
|
+
return {
|
|
102
|
+
content: joinYamlBody(c.header, c.body),
|
|
103
|
+
rawCopy: false,
|
|
104
|
+
notes: sourceNotes(c),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/* ------------------------------------------------------------------ */
|
|
108
|
+
/* Content builders */
|
|
109
|
+
/* ------------------------------------------------------------------ */
|
|
110
|
+
export async function buildSkillContent(platform, skillName, rootRaw, platformsRoot) {
|
|
111
|
+
// Verbatim platforms (shared ~/.agents) copy the bundled source untouched: no
|
|
112
|
+
// composition, no overlay, no frontmatter relocation.
|
|
113
|
+
if (platform.verbatim)
|
|
114
|
+
return { content: rootRaw, rawCopy: true, notes: [] };
|
|
115
|
+
const c = await composeDoc(platform, 'skill', skillName, rootRaw, platformsRoot);
|
|
116
|
+
if (c.header)
|
|
117
|
+
return composeYamlDoc(c);
|
|
118
|
+
const t = transformSkillMd(platform, c.legacyRaw);
|
|
119
|
+
return {
|
|
120
|
+
content: t.content,
|
|
121
|
+
// A unique patch changed the body (content != source) → never claim rawCopy,
|
|
122
|
+
// otherwise the installer would copy the source verbatim and drop the patch.
|
|
123
|
+
rawCopy: t.rawCopy && !c.unique,
|
|
124
|
+
notes: [...sourceNotes(c), 'platform frontmatter absent: legacy transform applied', ...t.notes],
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/** Codex: TOML header + body injected as developer_instructions (unless already present). */
|
|
128
|
+
function injectCodexInstructions(toml, body) {
|
|
129
|
+
const notes = [];
|
|
130
|
+
const text = toml
|
|
131
|
+
.split('\n')
|
|
132
|
+
.filter((line) => !(/^\s*#/.test(line) && /developer_instructions\s*=/.test(line)))
|
|
133
|
+
.join('\n')
|
|
134
|
+
.replace(/\s+$/, '');
|
|
135
|
+
if (/^developer_instructions\s*=/m.test(text)) {
|
|
136
|
+
notes.push('developer_instructions already defined in platform TOML: body not injected');
|
|
137
|
+
return { content: text + '\n', notes };
|
|
138
|
+
}
|
|
139
|
+
const key = `developer_instructions = ${tomlMultiline(body)}`;
|
|
140
|
+
const lines = text.split('\n');
|
|
141
|
+
const firstTable = lines.findIndex((line) => /^\s*\[/.test(line));
|
|
142
|
+
if (firstTable >= 0) {
|
|
143
|
+
lines.splice(firstTable, 0, key, '');
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
lines.push('', key);
|
|
147
|
+
}
|
|
148
|
+
notes.push('developer_instructions injected from composed body');
|
|
149
|
+
return { content: lines.join('\n').replace(/\n{3,}$/, '\n\n') + '\n', notes };
|
|
150
|
+
}
|
|
151
|
+
export async function buildAgentContent(platform, fileName, rootRaw, platformsRoot) {
|
|
152
|
+
if (platform.agentFormat === 'none')
|
|
153
|
+
return null;
|
|
154
|
+
// Verbatim: keep the file name and bytes exactly as bundled (no color mapping).
|
|
155
|
+
if (platform.verbatim)
|
|
156
|
+
return { fileName, content: rootRaw, rawCopy: true, notes: [] };
|
|
157
|
+
const c = await composeDoc(platform, 'agent', fileName, rootRaw, platformsRoot);
|
|
158
|
+
const tomlHeader = c.headerToml ?? (platform.agentFormat === 'codex-toml' && !c.header ? c.headerRaw : null);
|
|
159
|
+
if (platform.agentFormat === 'codex-toml' && tomlHeader) {
|
|
160
|
+
const out = injectCodexInstructions(tomlHeader, c.body);
|
|
161
|
+
const notes = sourceNotes(c);
|
|
162
|
+
if (!c.headerToml && !c.header) {
|
|
163
|
+
notes[0] = 'frontmatter: platforms definition (TOML in .frontmatter.md, legacy name)';
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
fileName: fileName.replace(/\.md$/, '.toml'),
|
|
167
|
+
content: out.content,
|
|
168
|
+
rawCopy: false,
|
|
169
|
+
notes: [...notes, ...out.notes],
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (c.header)
|
|
173
|
+
return { fileName, ...composeYamlDoc(c) };
|
|
174
|
+
const t = transformAgentMd(platform, fileName, c.legacyRaw);
|
|
175
|
+
if (!t)
|
|
176
|
+
return null;
|
|
177
|
+
const content = t.rawCopy && !c.unique && platform.agentFormat === 'passthrough'
|
|
178
|
+
? mapAgentColorToHex(t.content)
|
|
179
|
+
: t.content;
|
|
180
|
+
return {
|
|
181
|
+
fileName: t.fileName,
|
|
182
|
+
content,
|
|
183
|
+
rawCopy: t.rawCopy && !c.unique,
|
|
184
|
+
notes: [...sourceNotes(c), 'platform frontmatter absent: legacy transform applied', ...t.notes],
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
export async function buildCommandContent(platform, fileName, rootRaw, platformsRoot) {
|
|
188
|
+
if (platform.verbatim)
|
|
189
|
+
return { content: rootRaw, rawCopy: true, notes: [] };
|
|
190
|
+
const c = await composeDoc(platform, 'command', fileName, rootRaw, platformsRoot);
|
|
191
|
+
if (c.header)
|
|
192
|
+
return composeYamlDoc(c);
|
|
193
|
+
const t = transformCommandMd(platform, c.legacyRaw);
|
|
194
|
+
return {
|
|
195
|
+
content: t.content,
|
|
196
|
+
rawCopy: t.rawCopy && !c.unique,
|
|
197
|
+
notes: [...sourceNotes(c), ...t.notes],
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
export async function buildCommandSkill(platform, fileName, rootRaw, platformsRoot) {
|
|
201
|
+
if (platform.verbatim) {
|
|
202
|
+
const t = commandToSkillMd(platform, fileName, rootRaw);
|
|
203
|
+
return { ...t, notes: [...t.notes] };
|
|
204
|
+
}
|
|
205
|
+
const c = await composeDoc(platform, 'command', fileName, rootRaw, platformsRoot);
|
|
206
|
+
const t = commandToSkillMd(platform, fileName, c.legacyRaw);
|
|
207
|
+
return { ...t, notes: [...sourceNotes(c), ...t.notes] };
|
|
208
|
+
}
|
|
209
|
+
/* ------------------------------------------------------------------ */
|
|
210
|
+
/* Overlay discovery */
|
|
211
|
+
/* ------------------------------------------------------------------ */
|
|
212
|
+
/**
|
|
213
|
+
* Same-name source preference: `platforms/<platform>/<kind>/<name>` wins over the
|
|
214
|
+
* bundled root file (it becomes the composition source body).
|
|
215
|
+
*/
|
|
216
|
+
export async function platformSourcePath(platformsRoot, platformName, kind, packagedRoot, name) {
|
|
217
|
+
if (platformsRoot) {
|
|
218
|
+
const overlay = path.join(platformsRoot, platformName, kind, name);
|
|
219
|
+
if (await pathExists(overlay))
|
|
220
|
+
return { src: overlay, overlay: true };
|
|
221
|
+
}
|
|
222
|
+
return { src: path.join(packagedRoot, name), overlay: false };
|
|
223
|
+
}
|
|
224
|
+
export async function pathExists(p) {
|
|
225
|
+
try {
|
|
226
|
+
await fs.access(p);
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
}
|