@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,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal YAML frontmatter subset: parse + serialize.
|
|
3
|
+
*
|
|
4
|
+
* Ported from `devecocli-plugin/common.ts` (the plugin is kept untouched; see
|
|
5
|
+
* `src/cli/platform-parity.test.ts` which asserts the two implementations agree).
|
|
6
|
+
* Supported syntax is exactly what the bundled documents use: top-level
|
|
7
|
+
* `key: value`, one nested map, and block lists.
|
|
8
|
+
*/
|
|
9
|
+
/** Strip quotes and unescape; bare values pass through. */
|
|
10
|
+
function parseScalar(v) {
|
|
11
|
+
if (v.length >= 2 && v.startsWith('"') && v.endsWith('"')) {
|
|
12
|
+
return v.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
|
|
13
|
+
}
|
|
14
|
+
if (v.length >= 2 && v.startsWith("'") && v.endsWith("'")) {
|
|
15
|
+
return v.slice(1, -1).replace(/''/g, "'");
|
|
16
|
+
}
|
|
17
|
+
return v;
|
|
18
|
+
}
|
|
19
|
+
/** Strip a UTF-8 BOM (PowerShell 5.1 / legacy Notepad writes one). */
|
|
20
|
+
export function stripBom(raw) {
|
|
21
|
+
return raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Leading YAML frontmatter block: opening `---`, YAML, closing `---`.
|
|
25
|
+
*
|
|
26
|
+
* Trailing spaces/tabs on the closing delimiter (`--- `) are accepted: editors
|
|
27
|
+
* and hand-edited overlays do write them, and two independently written regexes
|
|
28
|
+
* used to disagree about it, which duplicated the block in the generated file.
|
|
29
|
+
* This is the single source of truth for every frontmatter regex in the package.
|
|
30
|
+
*/
|
|
31
|
+
export const FRONTMATTER_BLOCK = /^---\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/;
|
|
32
|
+
export function parseFrontmatter(raw) {
|
|
33
|
+
// A BOM must not hide a valid block: the delimiter would no longer be at the
|
|
34
|
+
// start of the string. Strip it once and compute the body from the same text.
|
|
35
|
+
const src = stripBom(raw);
|
|
36
|
+
const m = FRONTMATTER_BLOCK.exec(src);
|
|
37
|
+
if (!m)
|
|
38
|
+
return { fm: {}, body: src, hasFrontmatter: false };
|
|
39
|
+
const fm = {};
|
|
40
|
+
let parent = null;
|
|
41
|
+
for (const line of m[1].split(/\r?\n/)) {
|
|
42
|
+
if (!line.trim() || line.trim().startsWith('#'))
|
|
43
|
+
continue;
|
|
44
|
+
const indented = line[0] === ' ' || line[0] === '\t';
|
|
45
|
+
const trimmed = line.trim();
|
|
46
|
+
if (trimmed.startsWith('- ') && parent !== null) {
|
|
47
|
+
const item = parseScalar(trimmed.slice(2).trim());
|
|
48
|
+
const cur = fm[parent];
|
|
49
|
+
fm[parent] = Array.isArray(cur) ? [...cur, item] : [item];
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const idx = line.indexOf(':');
|
|
53
|
+
if (idx < 0)
|
|
54
|
+
continue;
|
|
55
|
+
const key = line.slice(0, idx).trim();
|
|
56
|
+
const value = line.slice(idx + 1).trim();
|
|
57
|
+
if (!indented) {
|
|
58
|
+
parent = key;
|
|
59
|
+
if (value === '') {
|
|
60
|
+
if (!(key in fm))
|
|
61
|
+
fm[key] = {};
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
fm[key] = parseScalar(value);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else if (parent !== null && value !== '') {
|
|
68
|
+
const cur = fm[parent];
|
|
69
|
+
const map = Array.isArray(cur) || typeof cur !== 'object' || cur === undefined ? {} : cur;
|
|
70
|
+
map[key] = parseScalar(value);
|
|
71
|
+
fm[parent] = map;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { fm, body: src.slice(m[0].length), hasFrontmatter: true };
|
|
75
|
+
}
|
|
76
|
+
/** Bare values are restricted to a conservative safe set; boolean-like strings get quoted. */
|
|
77
|
+
const BARE_SAFE = /^[A-Za-z0-9_][A-Za-z0-9_ ./+|=-]*$/;
|
|
78
|
+
const BOOL_LIKE = /^(true|false|yes|no|on|off|null|~)$/i;
|
|
79
|
+
/** Fields whose true/false must stay unquoted so the platform parses a boolean. */
|
|
80
|
+
const NATIVE_BOOLEAN_KEYS = new Set([
|
|
81
|
+
'subtask',
|
|
82
|
+
'disable-model-invocation',
|
|
83
|
+
'user-invocable',
|
|
84
|
+
'disable',
|
|
85
|
+
'hidden',
|
|
86
|
+
'is_background',
|
|
87
|
+
'background',
|
|
88
|
+
'readonly',
|
|
89
|
+
]);
|
|
90
|
+
function formatScalar(key, v) {
|
|
91
|
+
if (v === '')
|
|
92
|
+
return '""';
|
|
93
|
+
if (NATIVE_BOOLEAN_KEYS.has(key) && /^(true|false)$/i.test(v))
|
|
94
|
+
return v.toLowerCase();
|
|
95
|
+
if (!BARE_SAFE.test(v) || BOOL_LIKE.test(v) || v !== v.trim()) {
|
|
96
|
+
return '"' + v.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"';
|
|
97
|
+
}
|
|
98
|
+
return v;
|
|
99
|
+
}
|
|
100
|
+
/** Serialize as a `---\n...\n---\n` block (exactly one trailing newline). */
|
|
101
|
+
export function serializeFrontmatter(fm) {
|
|
102
|
+
const lines = ['---'];
|
|
103
|
+
for (const [key, value] of Object.entries(fm)) {
|
|
104
|
+
if (Array.isArray(value)) {
|
|
105
|
+
if (value.length === 0) {
|
|
106
|
+
lines.push(`${key}: []`);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
lines.push(`${key}:`);
|
|
110
|
+
for (const item of value)
|
|
111
|
+
lines.push(` - ${formatScalar(key, item)}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else if (typeof value === 'boolean') {
|
|
115
|
+
lines.push(`${key}: ${value ? 'true' : 'false'}`);
|
|
116
|
+
}
|
|
117
|
+
else if (typeof value === 'object' && value !== null) {
|
|
118
|
+
const entries = Object.entries(value);
|
|
119
|
+
if (entries.length === 0) {
|
|
120
|
+
lines.push(`${key}: {}`);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
lines.push(`${key}:`);
|
|
124
|
+
for (const [k, v] of entries)
|
|
125
|
+
lines.push(` ${k}: ${formatScalar(k, v)}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
lines.push(`${key}: ${formatScalar(key, value)}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
lines.push('---', '');
|
|
133
|
+
return lines.join('\n');
|
|
134
|
+
}
|
|
135
|
+
export function asString(v) {
|
|
136
|
+
return typeof v === 'string' ? v : undefined;
|
|
137
|
+
}
|
|
138
|
+
export function asRecord(v) {
|
|
139
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v) ? v : {};
|
|
140
|
+
}
|
|
141
|
+
export function fmValueToString(v) {
|
|
142
|
+
if (typeof v === 'string')
|
|
143
|
+
return v;
|
|
144
|
+
if (typeof v === 'boolean')
|
|
145
|
+
return v ? 'true' : 'false';
|
|
146
|
+
if (Array.isArray(v))
|
|
147
|
+
return v.join(', ');
|
|
148
|
+
return Object.entries(v)
|
|
149
|
+
.map(([k, val]) => `${k}: ${val}`)
|
|
150
|
+
.join(', ');
|
|
151
|
+
}
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filesystem installer: turns bundled skills/agents/commands into the target
|
|
3
|
+
* platform's layout, using the composition/transform layer.
|
|
4
|
+
*
|
|
5
|
+
* Semantics are aligned with `devecocli-plugin`'s `installTo()` /
|
|
6
|
+
* `uninstallFrom()` so `ht init` and `devecocli plugin install` converge on the
|
|
7
|
+
* same on-disk result:
|
|
8
|
+
* - same-name entries in the platform's `legacyDirs` are removed first
|
|
9
|
+
* (plugin `cleanLegacyDirs()`), and uninstall covers them too;
|
|
10
|
+
* - a skill directory is cleared before the copy, so files dropped from the
|
|
11
|
+
* package do not linger; a command-as-skill directory is recreated;
|
|
12
|
+
* - validation reads the installed files back from disk.
|
|
13
|
+
* Deliberate CLI-only behaviors:
|
|
14
|
+
* - `node_modules` is never copied (a dev checkout links reqgen deps there and
|
|
15
|
+
* copying a symlink/socket fails with ENOTSUP), and an existing
|
|
16
|
+
* `node_modules` in an installed skill survives the clear, so the linked
|
|
17
|
+
* `node_modules/.hometrans-npm-link` stamp is kept (deleting it forced a
|
|
18
|
+
* fresh `npm link` on every editor, every time);
|
|
19
|
+
* - identical agent/command files are left untouched so repeated `ht init`
|
|
20
|
+
* runs are idempotent (`force` disables the check);
|
|
21
|
+
* - conflicts with existing installs are confirmed interactively by `ht init`
|
|
22
|
+
* (the plugin instead refuses without `--force`).
|
|
23
|
+
*/
|
|
24
|
+
import fs from 'node:fs/promises';
|
|
25
|
+
import path from 'node:path';
|
|
26
|
+
import { buildAgentContent, buildCommandContent, buildCommandSkill, buildSkillContent, pathExists, platformSourcePath, } from './compose.js';
|
|
27
|
+
import { validateAgentFile, validateCommandMd, validateSkillMd } from './validate.js';
|
|
28
|
+
/** Entries never copied into an installed skill. */
|
|
29
|
+
const SKILL_COPY_SKIP_NAMES = new Set(['node_modules']);
|
|
30
|
+
/** Composition inputs (and the SKILL.md output) are not part of file overlays. */
|
|
31
|
+
const SKILL_OVERLAY_EXCLUDE = new Set(['SKILL.frontmatter.md', 'SKILL.unique.md', 'SKILL.md']);
|
|
32
|
+
/* ------------------------------------------------------------------ */
|
|
33
|
+
/* Bundled content discovery */
|
|
34
|
+
/* ------------------------------------------------------------------ */
|
|
35
|
+
/** Bundled skill directory names (those containing a `SKILL.md`). */
|
|
36
|
+
export async function listBundledSkills(skillsRoot) {
|
|
37
|
+
const entries = await fs.readdir(skillsRoot, { withFileTypes: true }).catch(() => []);
|
|
38
|
+
const names = [];
|
|
39
|
+
for (const entry of entries) {
|
|
40
|
+
if (!entry.isDirectory())
|
|
41
|
+
continue;
|
|
42
|
+
if (await pathExists(path.join(skillsRoot, entry.name, 'SKILL.md')))
|
|
43
|
+
names.push(entry.name);
|
|
44
|
+
}
|
|
45
|
+
return names;
|
|
46
|
+
}
|
|
47
|
+
/** Top-level bundled agent entries (files and directories). */
|
|
48
|
+
export async function listBundledAgents(agentsRoot) {
|
|
49
|
+
const entries = await fs.readdir(agentsRoot, { withFileTypes: true }).catch(() => []);
|
|
50
|
+
return entries.filter((e) => e.isFile() || e.isDirectory()).map((e) => e.name);
|
|
51
|
+
}
|
|
52
|
+
/** Bundled slash-command file names (`*.md` directly under commands/). */
|
|
53
|
+
export async function listBundledCommands(commandsRoot) {
|
|
54
|
+
const entries = await fs.readdir(commandsRoot, { withFileTypes: true }).catch(() => []);
|
|
55
|
+
return entries.filter((e) => e.isFile() && e.name.endsWith('.md')).map((e) => e.name);
|
|
56
|
+
}
|
|
57
|
+
/* ------------------------------------------------------------------ */
|
|
58
|
+
/* Small fs helpers */
|
|
59
|
+
/* ------------------------------------------------------------------ */
|
|
60
|
+
async function readIfExists(p) {
|
|
61
|
+
try {
|
|
62
|
+
return await fs.readFile(p, 'utf-8');
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async function sameContent(p, content) {
|
|
69
|
+
return (await readIfExists(p)) === content;
|
|
70
|
+
}
|
|
71
|
+
async function isFile(p) {
|
|
72
|
+
try {
|
|
73
|
+
return (await fs.stat(p)).isFile();
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function isDirectory(p) {
|
|
80
|
+
try {
|
|
81
|
+
return (await fs.stat(p)).isDirectory();
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Copy `src` over `dest` without clearing `dest` first.
|
|
89
|
+
*
|
|
90
|
+
* Files are overwritten in place, so anything the install did not put there
|
|
91
|
+
* (notably a linked `node_modules/`) stays intact. Only type conflicts are
|
|
92
|
+
* resolved destructively: a file where a directory is expected (or vice versa)
|
|
93
|
+
* is removed before the copy.
|
|
94
|
+
*/
|
|
95
|
+
async function copyDirRecursive(src, dest) {
|
|
96
|
+
if (await isFile(dest))
|
|
97
|
+
await fs.rm(dest, { force: true });
|
|
98
|
+
await fs.mkdir(dest, { recursive: true });
|
|
99
|
+
for (const entry of await fs.readdir(src, { withFileTypes: true })) {
|
|
100
|
+
if (SKILL_COPY_SKIP_NAMES.has(entry.name))
|
|
101
|
+
continue;
|
|
102
|
+
const s = path.join(src, entry.name);
|
|
103
|
+
const d = path.join(dest, entry.name);
|
|
104
|
+
if (entry.isDirectory()) {
|
|
105
|
+
await copyDirRecursive(s, d);
|
|
106
|
+
}
|
|
107
|
+
else if (entry.isFile()) {
|
|
108
|
+
// A path that used to be a directory can collide with a new file.
|
|
109
|
+
if (await isDirectory(d))
|
|
110
|
+
await fs.rm(d, { recursive: true, force: true });
|
|
111
|
+
await fs.copyFile(s, d);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Empty `dir` except the names in `keep` (an installed skill's linked
|
|
117
|
+
* `node_modules`). Same effect as the plugin's `rm -rf dest` before the copy,
|
|
118
|
+
* minus the link it would destroy.
|
|
119
|
+
*/
|
|
120
|
+
async function clearDirExcept(dir, keep) {
|
|
121
|
+
if (await isFile(dir)) {
|
|
122
|
+
await fs.rm(dir, { force: true });
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
for (const entry of await fs.readdir(dir, { withFileTypes: true }).catch(() => [])) {
|
|
126
|
+
if (keep.has(entry.name))
|
|
127
|
+
continue;
|
|
128
|
+
await fs.rm(path.join(dir, entry.name), { recursive: true, force: true });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Remove bundled entries of the same name from the platform's legacy install
|
|
133
|
+
* locations (never anything else). Mirrors devecocli-plugin `cleanLegacyDirs()`.
|
|
134
|
+
*/
|
|
135
|
+
async function cleanLegacyDirs(platform, skills, agents, commands) {
|
|
136
|
+
for (const legacy of platform.legacyDirs ?? []) {
|
|
137
|
+
if (legacy.skillsDir) {
|
|
138
|
+
for (const s of skills)
|
|
139
|
+
await fs.rm(path.join(legacy.skillsDir, s), { recursive: true, force: true });
|
|
140
|
+
}
|
|
141
|
+
if (legacy.agentsDir) {
|
|
142
|
+
for (const a of agents)
|
|
143
|
+
await fs.rm(path.join(legacy.agentsDir, a), { recursive: true, force: true });
|
|
144
|
+
}
|
|
145
|
+
if (legacy.commandsDir) {
|
|
146
|
+
for (const c of commands)
|
|
147
|
+
await fs.rm(path.join(legacy.commandsDir, c), { force: true });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** Copy overlay files (same-name overwrites + new files) below `overlayDir`. */
|
|
152
|
+
async function copyOverlayFiles(overlayDir, dest, exclude) {
|
|
153
|
+
if (!overlayDir || !(await pathExists(overlayDir)))
|
|
154
|
+
return 0;
|
|
155
|
+
let count = 0;
|
|
156
|
+
const walk = async (src, out) => {
|
|
157
|
+
for (const entry of await fs.readdir(src, { withFileTypes: true }).catch(() => [])) {
|
|
158
|
+
const s = path.join(src, entry.name);
|
|
159
|
+
const o = path.join(out, entry.name);
|
|
160
|
+
if (entry.isDirectory())
|
|
161
|
+
await walk(s, o);
|
|
162
|
+
else if (entry.isFile() && !exclude.has(entry.name)) {
|
|
163
|
+
count++;
|
|
164
|
+
await fs.mkdir(path.dirname(o), { recursive: true });
|
|
165
|
+
await fs.copyFile(s, o);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
await walk(overlayDir, dest);
|
|
170
|
+
return count;
|
|
171
|
+
}
|
|
172
|
+
/** Agent file names to clean: Codex additionally removes the raw `.md` of older versions. */
|
|
173
|
+
function agentFileCandidates(platform, mdName) {
|
|
174
|
+
return platform.agentFormat === 'codex-toml'
|
|
175
|
+
? [mdName.replace(/\.md$/, '.toml'), mdName]
|
|
176
|
+
: [mdName];
|
|
177
|
+
}
|
|
178
|
+
function addNote(map, note) {
|
|
179
|
+
map.set(note, (map.get(note) ?? 0) + 1);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Install every bundled skill / agent / command into the platform's dirs.
|
|
183
|
+
* `platform` must already carry expanded directories (see `platformForEditor`).
|
|
184
|
+
*/
|
|
185
|
+
export async function installTo(platform, roots, opts = {}) {
|
|
186
|
+
const force = opts.force === true;
|
|
187
|
+
const skipSkills = opts.skipSkills ?? new Set();
|
|
188
|
+
const skipAgents = opts.skipAgents ?? new Set();
|
|
189
|
+
const skipCommands = opts.skipCommands ?? new Set();
|
|
190
|
+
const noteCounts = new Map();
|
|
191
|
+
const issues = [];
|
|
192
|
+
const skipped = [];
|
|
193
|
+
const written = [];
|
|
194
|
+
const unchanged = [];
|
|
195
|
+
const label = platform.displayName ?? platform.name;
|
|
196
|
+
for (const root of [roots.skillsRoot, roots.agentsRoot, roots.commandsRoot]) {
|
|
197
|
+
if (!(await pathExists(root))) {
|
|
198
|
+
throw new Error(`Incomplete install source: ${root} is missing. ` +
|
|
199
|
+
'Reinstall hometrans (the package must ship skills/, agents/ and commands/).');
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const bundledSkills = await listBundledSkills(roots.skillsRoot);
|
|
203
|
+
const bundledAgents = await listBundledAgents(roots.agentsRoot);
|
|
204
|
+
const bundledCommands = await listBundledCommands(roots.commandsRoot);
|
|
205
|
+
// Older releases' wrong install locations: drop the same-name residue so the
|
|
206
|
+
// old copy does not coexist with the one installed below.
|
|
207
|
+
await cleanLegacyDirs(platform, bundledSkills, bundledAgents, bundledCommands);
|
|
208
|
+
/* ------------------------------- skills ------------------------------- */
|
|
209
|
+
let skills = 0;
|
|
210
|
+
if (platform.skillsDir) {
|
|
211
|
+
await fs.mkdir(platform.skillsDir, { recursive: true });
|
|
212
|
+
for (const name of bundledSkills) {
|
|
213
|
+
if (skipSkills.has(name))
|
|
214
|
+
continue;
|
|
215
|
+
const sourceRaw = await fs.readFile(path.join(roots.skillsRoot, name, 'SKILL.md'), 'utf-8');
|
|
216
|
+
const t = await buildSkillContent(platform, name, sourceRaw, roots.platformsRoot);
|
|
217
|
+
const dest = path.join(platform.skillsDir, name);
|
|
218
|
+
const overlayDir = roots.platformsRoot
|
|
219
|
+
? path.join(roots.platformsRoot, platform.name, 'skills', name)
|
|
220
|
+
: null;
|
|
221
|
+
// Clear, then copy: files removed from the package do not linger. The
|
|
222
|
+
// linked `node_modules` (and its `.hometrans-npm-link` stamp) is kept so
|
|
223
|
+
// re-runs do not re-link reqgen.
|
|
224
|
+
await clearDirExcept(dest, SKILL_COPY_SKIP_NAMES);
|
|
225
|
+
await copyDirRecursive(path.join(roots.skillsRoot, name), dest);
|
|
226
|
+
const overlayCount = await copyOverlayFiles(overlayDir, dest, SKILL_OVERLAY_EXCLUDE);
|
|
227
|
+
if (overlayCount > 0) {
|
|
228
|
+
addNote(noteCounts, `file overlay: platforms/${platform.name}/skills/${name} (${overlayCount} files)`);
|
|
229
|
+
}
|
|
230
|
+
await fs.writeFile(path.join(dest, 'SKILL.md'), t.content, 'utf-8');
|
|
231
|
+
for (const n of t.notes)
|
|
232
|
+
addNote(noteCounts, n);
|
|
233
|
+
// Read back what landed on disk: proves the target platform can parse it.
|
|
234
|
+
const installed = await fs.readFile(path.join(dest, 'SKILL.md'), 'utf-8');
|
|
235
|
+
issues.push(...validateSkillMd(name, installed, platform.skillDescriptionLimit));
|
|
236
|
+
written.push(dest);
|
|
237
|
+
skills++;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else if (bundledSkills.length > 0) {
|
|
241
|
+
skipped.push(`${label}: skills skipped - platform has no skills directory`);
|
|
242
|
+
}
|
|
243
|
+
/* ------------------------------- agents ------------------------------- */
|
|
244
|
+
let agents = 0;
|
|
245
|
+
if (platform.agentsDir) {
|
|
246
|
+
await fs.mkdir(platform.agentsDir, { recursive: true });
|
|
247
|
+
for (const name of bundledAgents) {
|
|
248
|
+
if (skipAgents.has(name))
|
|
249
|
+
continue;
|
|
250
|
+
const srcPath = path.join(roots.agentsRoot, name);
|
|
251
|
+
const stat = await fs.stat(srcPath);
|
|
252
|
+
if (stat.isDirectory()) {
|
|
253
|
+
const dest = path.join(platform.agentsDir, name);
|
|
254
|
+
await clearDirExcept(dest, SKILL_COPY_SKIP_NAMES);
|
|
255
|
+
await copyDirRecursive(srcPath, dest);
|
|
256
|
+
const overlayDir = roots.platformsRoot
|
|
257
|
+
? path.join(roots.platformsRoot, platform.name, 'agents', name)
|
|
258
|
+
: null;
|
|
259
|
+
const count = await copyOverlayFiles(overlayDir, dest, new Set());
|
|
260
|
+
if (count > 0) {
|
|
261
|
+
addNote(noteCounts, `file overlay: platforms/${platform.name}/agents/${name} (${count} files)`);
|
|
262
|
+
}
|
|
263
|
+
written.push(dest);
|
|
264
|
+
agents++;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (!name.endsWith('.md'))
|
|
268
|
+
continue;
|
|
269
|
+
const source = await platformSourcePath(roots.platformsRoot, platform.name, 'agents', roots.agentsRoot, name);
|
|
270
|
+
if (source.overlay)
|
|
271
|
+
addNote(noteCounts, `source overlay: platforms/${platform.name}/agents/${name}`);
|
|
272
|
+
const raw = await fs.readFile(source.src, 'utf-8');
|
|
273
|
+
const t = await buildAgentContent(platform, name, raw, roots.platformsRoot);
|
|
274
|
+
if (!t)
|
|
275
|
+
continue;
|
|
276
|
+
const dest = path.join(platform.agentsDir, t.fileName);
|
|
277
|
+
if (!force && (await sameContent(dest, t.content))) {
|
|
278
|
+
unchanged.push(dest);
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
await fs.writeFile(dest, t.content, 'utf-8');
|
|
282
|
+
written.push(dest);
|
|
283
|
+
}
|
|
284
|
+
if (t.fileName !== name)
|
|
285
|
+
await fs.rm(path.join(platform.agentsDir, name), { force: true });
|
|
286
|
+
for (const n of t.notes)
|
|
287
|
+
addNote(noteCounts, n);
|
|
288
|
+
issues.push(...validateAgentFile(platform, t.fileName, await fs.readFile(dest, 'utf-8')));
|
|
289
|
+
agents++;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
else if (bundledAgents.length > 0) {
|
|
293
|
+
skipped.push(`${label}: agents skipped - platform has no file-based subagent`);
|
|
294
|
+
}
|
|
295
|
+
/* ------------------------------ commands ------------------------------ */
|
|
296
|
+
let commands = 0;
|
|
297
|
+
if (platform.commandsAsSkills && platform.skillsDir) {
|
|
298
|
+
if (bundledCommands.length > 0) {
|
|
299
|
+
await fs.mkdir(platform.skillsDir, { recursive: true });
|
|
300
|
+
addNote(noteCounts, `commands installed as skills into ${platform.skillsDir}`);
|
|
301
|
+
}
|
|
302
|
+
for (const name of bundledCommands) {
|
|
303
|
+
if (skipCommands.has(name))
|
|
304
|
+
continue;
|
|
305
|
+
const source = await platformSourcePath(roots.platformsRoot, platform.name, 'commands', roots.commandsRoot, name);
|
|
306
|
+
if (source.overlay)
|
|
307
|
+
addNote(noteCounts, `source overlay: platforms/${platform.name}/commands/${name}`);
|
|
308
|
+
const sourceRaw = await fs.readFile(source.src, 'utf-8');
|
|
309
|
+
const t = await buildCommandSkill(platform, name, sourceRaw, roots.platformsRoot);
|
|
310
|
+
const dest = path.join(platform.skillsDir, t.skillName);
|
|
311
|
+
const skillFile = path.join(dest, 'SKILL.md');
|
|
312
|
+
const openaiFile = path.join(dest, 'agents', 'openai.yaml');
|
|
313
|
+
const sameSkill = !force && (await sameContent(skillFile, t.content));
|
|
314
|
+
// No sidecar expected → a leftover one also counts as a change.
|
|
315
|
+
const sameYaml = t.openaiYaml === null
|
|
316
|
+
? !(await pathExists(openaiFile))
|
|
317
|
+
: await sameContent(openaiFile, t.openaiYaml);
|
|
318
|
+
if (sameSkill && sameYaml) {
|
|
319
|
+
unchanged.push(dest);
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
// Recreate the directory (plugin: rm -rf + mkdir) so nothing stale stays.
|
|
323
|
+
await fs.rm(dest, { recursive: true, force: true });
|
|
324
|
+
await fs.mkdir(dest, { recursive: true });
|
|
325
|
+
await fs.writeFile(skillFile, t.content, 'utf-8');
|
|
326
|
+
if (t.openaiYaml !== null) {
|
|
327
|
+
await fs.mkdir(path.dirname(openaiFile), { recursive: true });
|
|
328
|
+
await fs.writeFile(openaiFile, t.openaiYaml, 'utf-8');
|
|
329
|
+
}
|
|
330
|
+
written.push(dest);
|
|
331
|
+
}
|
|
332
|
+
for (const n of t.notes)
|
|
333
|
+
addNote(noteCounts, n);
|
|
334
|
+
issues.push(...validateSkillMd(t.skillName, await fs.readFile(skillFile, 'utf-8'), platform.skillDescriptionLimit));
|
|
335
|
+
commands++;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
else if (platform.commandsDir && bundledCommands.length > 0) {
|
|
339
|
+
await fs.mkdir(platform.commandsDir, { recursive: true });
|
|
340
|
+
for (const name of bundledCommands) {
|
|
341
|
+
if (skipCommands.has(name))
|
|
342
|
+
continue;
|
|
343
|
+
const source = await platformSourcePath(roots.platformsRoot, platform.name, 'commands', roots.commandsRoot, name);
|
|
344
|
+
if (source.overlay)
|
|
345
|
+
addNote(noteCounts, `source overlay: platforms/${platform.name}/commands/${name}`);
|
|
346
|
+
const sourceRaw = await fs.readFile(source.src, 'utf-8');
|
|
347
|
+
const t = await buildCommandContent(platform, name, sourceRaw, roots.platformsRoot);
|
|
348
|
+
const dest = path.join(platform.commandsDir, name);
|
|
349
|
+
const content = t.rawCopy ? sourceRaw : t.content;
|
|
350
|
+
if (!force && (await sameContent(dest, content)))
|
|
351
|
+
unchanged.push(dest);
|
|
352
|
+
else {
|
|
353
|
+
await fs.writeFile(dest, content, 'utf-8');
|
|
354
|
+
written.push(dest);
|
|
355
|
+
}
|
|
356
|
+
for (const n of t.notes)
|
|
357
|
+
addNote(noteCounts, n);
|
|
358
|
+
issues.push(...validateCommandMd(platform, name, await fs.readFile(dest, 'utf-8')));
|
|
359
|
+
commands++;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
else if (bundledCommands.length > 0) {
|
|
363
|
+
skipped.push(`${label}: commands skipped - platform has no slash-command directory`);
|
|
364
|
+
}
|
|
365
|
+
const notes = [...noteCounts.entries()].map(([note, n]) => n > 1 ? `${note} (${n} files)` : note);
|
|
366
|
+
return { skills, agents, commands, written, unchanged, skipped, notes, issues };
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Enumerate every path `ht init` would have created for this platform, including
|
|
370
|
+
* `commandsAsSkills` skill directories. Missing paths are omitted, so a dry
|
|
371
|
+
* machine yields an empty list.
|
|
372
|
+
*/
|
|
373
|
+
export async function collectUninstallPaths(platform, roots) {
|
|
374
|
+
const entries = [];
|
|
375
|
+
const seen = new Set();
|
|
376
|
+
const add = async (p, isDir) => {
|
|
377
|
+
if (seen.has(p) || !(await pathExists(p)))
|
|
378
|
+
return;
|
|
379
|
+
seen.add(p);
|
|
380
|
+
entries.push({ absPath: p, isDir });
|
|
381
|
+
};
|
|
382
|
+
const skills = await listBundledSkills(roots.skillsRoot);
|
|
383
|
+
const agents = await listBundledAgents(roots.agentsRoot);
|
|
384
|
+
const commands = await listBundledCommands(roots.commandsRoot);
|
|
385
|
+
if (platform.skillsDir) {
|
|
386
|
+
for (const name of skills)
|
|
387
|
+
await add(path.join(platform.skillsDir, name), true);
|
|
388
|
+
}
|
|
389
|
+
if (platform.agentsDir) {
|
|
390
|
+
for (const name of agents) {
|
|
391
|
+
for (const candidate of agentFileCandidates(platform, name)) {
|
|
392
|
+
await add(path.join(platform.agentsDir, candidate), false);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
for (const name of commands) {
|
|
397
|
+
if (platform.commandsDir)
|
|
398
|
+
await add(path.join(platform.commandsDir, name), false);
|
|
399
|
+
if (platform.commandsAsSkills && platform.skillsDir) {
|
|
400
|
+
await add(path.join(platform.skillsDir, name.replace(/\.md$/, '')), true);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
// Residue an older release left in the platform's legacy locations (plugin
|
|
404
|
+
// `uninstallFrom()` removes it too).
|
|
405
|
+
for (const legacy of platform.legacyDirs ?? []) {
|
|
406
|
+
if (legacy.skillsDir)
|
|
407
|
+
for (const s of skills)
|
|
408
|
+
await add(path.join(legacy.skillsDir, s), true);
|
|
409
|
+
if (legacy.agentsDir)
|
|
410
|
+
for (const a of agents)
|
|
411
|
+
await add(path.join(legacy.agentsDir, a), false);
|
|
412
|
+
if (legacy.commandsDir)
|
|
413
|
+
for (const c of commands)
|
|
414
|
+
await add(path.join(legacy.commandsDir, c), false);
|
|
415
|
+
}
|
|
416
|
+
return entries;
|
|
417
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Language selection for `ht init`.
|
|
3
|
+
*
|
|
4
|
+
* The bundled `skills/`, `agents/` and `commands/` are the Chinese originals. An
|
|
5
|
+
* English install stages a copy of them, applies the English copies under `i18n/`
|
|
6
|
+
* and hands the result to the regular installer, so composition, platform
|
|
7
|
+
* overlays and validation stay identical for both languages.
|
|
8
|
+
*/
|
|
9
|
+
import fs from 'node:fs/promises';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { pathExists } from './compose.js';
|
|
12
|
+
export const INSTALL_LANGUAGES = ['zh', 'en'];
|
|
13
|
+
/** Default for runs that do not choose: the behavior of every earlier release. */
|
|
14
|
+
export const DEFAULT_INSTALL_LANGUAGE = 'zh';
|
|
15
|
+
const CONTENT_DIRS = [
|
|
16
|
+
['skills', 'skillsRoot'],
|
|
17
|
+
['agents', 'agentsRoot'],
|
|
18
|
+
['commands', 'commandsRoot'],
|
|
19
|
+
];
|
|
20
|
+
const RENAME_TABLE = 'FILE-NAMES.md';
|
|
21
|
+
const RENAME_ROW = /^\|\s*`([^`]+)`\s*\|\s*`([^`]+)`\s*\|\s*$/gm;
|
|
22
|
+
/** Originals whose English copy has a different name, from `i18n/FILE-NAMES.md`. */
|
|
23
|
+
export function parseOverlayRenames(markdown) {
|
|
24
|
+
return [...markdown.matchAll(RENAME_ROW)].map(([, english, original]) => ({ english, original }));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Read the rename table and check the overlay is complete. Throws rather than
|
|
28
|
+
* letting an "English" install quietly come out in Chinese.
|
|
29
|
+
*/
|
|
30
|
+
export async function loadLanguageOverlay(i18nRoot) {
|
|
31
|
+
for (const [dir] of CONTENT_DIRS) {
|
|
32
|
+
if (!(await pathExists(path.join(i18nRoot, dir)))) {
|
|
33
|
+
throw new Error(`English content is missing: ${path.join(i18nRoot, dir)}. Reinstall hometrans.`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const tablePath = path.join(i18nRoot, RENAME_TABLE);
|
|
37
|
+
let table;
|
|
38
|
+
try {
|
|
39
|
+
table = await fs.readFile(tablePath, 'utf-8');
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
throw new Error(`English content is missing: ${tablePath}. Reinstall hometrans.`);
|
|
43
|
+
}
|
|
44
|
+
const renames = parseOverlayRenames(table);
|
|
45
|
+
for (const { english } of renames) {
|
|
46
|
+
if (!(await pathExists(path.join(i18nRoot, english)))) {
|
|
47
|
+
throw new Error(`English content is missing: ${path.join(i18nRoot, english)}. Reinstall hometrans.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return renames;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Build the English content tree under `stagingDir` and return roots pointing at
|
|
54
|
+
* it. The originals of renamed files are removed so a Chinese-named copy does
|
|
55
|
+
* not ship next to its English replacement.
|
|
56
|
+
*/
|
|
57
|
+
export async function stageEnglishRoots(roots, i18nRoot, stagingDir) {
|
|
58
|
+
const renames = await loadLanguageOverlay(i18nRoot);
|
|
59
|
+
const staged = { ...roots };
|
|
60
|
+
for (const [dir, key] of CONTENT_DIRS) {
|
|
61
|
+
const dest = path.join(stagingDir, dir);
|
|
62
|
+
if (await pathExists(roots[key])) {
|
|
63
|
+
await fs.cp(roots[key], dest, {
|
|
64
|
+
recursive: true,
|
|
65
|
+
filter: (src) => path.basename(src) !== 'node_modules',
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
await fs.cp(path.join(i18nRoot, dir), dest, { recursive: true });
|
|
69
|
+
staged[key] = dest;
|
|
70
|
+
}
|
|
71
|
+
for (const { original } of renames) {
|
|
72
|
+
if (CONTENT_DIRS.some(([dir]) => original.startsWith(`${dir}/`))) {
|
|
73
|
+
await fs.rm(path.join(stagingDir, original), { force: true });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return staged;
|
|
77
|
+
}
|