bmad-method-test-architecture-enterprise 3.6.0 → 4.0.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/.claude-plugin/marketplace.json +1 -1
- package/README.md +10 -3
- package/cli/lib/automate-prompt.js +3 -7
- package/cli/lib/framework-setup.js +1 -2
- package/cli/lib/parse-report.js +1 -1
- package/cli/lib/resolve-tea-config.js +76 -10
- package/cli/lib/trace-command.js +12 -4
- package/cli/migrate-customizations.js +678 -71
- package/cli/teach.js +0 -1
- package/cli/test-review.js +1 -1
- package/package.json +3 -2
- package/skills/bmad-tea/SKILL.md +2 -2
- package/skills/bmad-tea-automate/SKILL.md +12 -11
- package/skills/bmad-tea-automate/customize.toml +12 -0
- package/skills/bmad-tea-automate/red/atdd-checklist-template.md +4 -4
- package/skills/bmad-tea-automate/red/checklist.md +1 -0
- package/skills/bmad-tea-automate/red/instructions.md +1 -1
- package/skills/bmad-tea-automate/red/steps-c/step-04a-subagent-api-failing.md +5 -0
- package/skills/bmad-tea-automate/red/steps-c/step-04b-subagent-e2e-failing.md +4 -0
- package/skills/bmad-tea-automate/red/steps-c/step-04c-aggregate.md +3 -2
- package/skills/bmad-tea-automate/red/steps-c/step-05-validate-and-complete.md +5 -4
- package/skills/bmad-tea-automate/red/steps-e/step-02-apply-edit.md +4 -4
- package/skills/bmad-tea-automate/red/steps-v/step-01-validate.md +4 -4
- package/skills/bmad-tea-automate/red/workflow.yaml +0 -1
- package/skills/bmad-tea-automate/resources/test-generation-routing.md +4 -7
- package/skills/bmad-tea-automate/steps-c/step-04-validate-and-summarize.md +4 -4
- package/skills/bmad-tea-automate/steps-e/step-02-apply-edit.md +4 -4
- package/skills/bmad-tea-automate/steps-v/step-01-validate.md +4 -4
- package/skills/bmad-tea-automate/workflow.yaml +0 -1
- package/skills/bmad-tea-evaluate/SKILL.md +1 -1
- package/skills/bmad-tea-framework/SKILL.md +3 -3
- package/skills/bmad-tea-framework/customize.toml +11 -2
- package/skills/bmad-tea-framework/resources/hooks/tea-enforce.cjs +55 -24
- package/skills/bmad-tea-framework/resources/setup-routing.md +6 -6
- package/skills/bmad-tea-framework/steps-c/step-04-docs-and-scripts.md +16 -12
- package/skills/bmad-tea-framework/workflow.yaml +0 -1
- package/skills/bmad-tea-nfr/SKILL.md +2 -2
- package/skills/bmad-tea-nfr/workflow.yaml +0 -1
- package/skills/bmad-tea-teach-me-testing/SKILL.md +2 -2
- package/skills/bmad-tea-test-design/SKILL.md +2 -2
- package/skills/bmad-tea-test-design/workflow.yaml +0 -1
- package/skills/bmad-tea-test-review/SKILL.md +2 -2
- package/skills/bmad-tea-test-review/workflow.yaml +0 -1
- package/skills/bmad-tea-trace/SKILL.md +2 -2
- package/skills/bmad-tea-trace/steps-c/step-04-analyze-gaps.md +1 -0
- package/skills/bmad-tea-trace/steps-c/step-05-gate-decision.md +29 -12
- package/skills/bmad-tea-trace/workflow.yaml +0 -1
- package/skills/bmod-tea/bmod.toml +2 -2
- package/skills/bmod-tea/help/help.md +10 -9
- package/skills/bmod-tea/knowledge/maestro-flows.md +1 -1
- package/skills/bmod-tea/migration-1.toml +7 -7
- package/skills/bmod-tea/migration-3.toml +50 -0
- package/skills/bmod-tea/retired.toml +13 -0
- package/skills/bmad-tea-automate/red/customize.toml +0 -38
- package/skills/bmad-tea-framework/ci/customize.toml +0 -10
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Move legacy TEA customizations to their current homes: pre-3.0 files to canonical names, the retired red-mode and CI
|
|
4
|
+
* stems into [workflow.red] and [workflow.ci] of their owning skills, and agent menu references to current skill ids.
|
|
5
|
+
*/
|
|
3
6
|
const fs = require('node:fs');
|
|
4
7
|
const path = require('node:path');
|
|
5
8
|
const { isDeepStrictEqual } = require('node:util');
|
|
6
9
|
const TOML = require('smol-toml');
|
|
7
10
|
|
|
8
|
-
const NAMES = ['automate', 'evaluate', 'framework', 'nfr', 'test-design', 'test-review', 'trace'
|
|
11
|
+
const NAMES = ['automate', 'evaluate', 'framework', 'nfr', 'test-design', 'test-review', 'trace'];
|
|
9
12
|
const PAIRS = [
|
|
10
13
|
...NAMES.map((suffix) => [`bmad-testarch-${suffix}`, `bmad-tea-${suffix}`]),
|
|
11
14
|
['bmad-teach-me-testing', 'bmad-tea-teach-me-testing'],
|
|
@@ -13,6 +16,25 @@ const PAIRS = [
|
|
|
13
16
|
const MENU_NAMES = new Map(PAIRS);
|
|
14
17
|
MENU_NAMES.set('bmad-testarch-atdd', 'bmad-tea-automate');
|
|
15
18
|
MENU_NAMES.set('bmad-testarch-ci', 'bmad-tea-framework');
|
|
19
|
+
/** Retired customization stems whose [workflow] table now lives as a sub-table of the owning skill. */
|
|
20
|
+
const SURFACES = [
|
|
21
|
+
{ target: 'bmad-tea-automate', table: 'red', sources: ['bmad-tea-atdd', 'bmad-testarch-atdd'] },
|
|
22
|
+
{ target: 'bmad-tea-framework', table: 'ci', sources: ['bmad-tea-ci', 'bmad-testarch-ci'], moveCiPlatform: true },
|
|
23
|
+
];
|
|
24
|
+
const LAYERS = ['.toml', '.user.toml'];
|
|
25
|
+
const BACKUP_PREFIX = '.v6-v7-migration-backup-';
|
|
26
|
+
/** Which backup values this command already restored or deliberately kept, so reruns never repeat or question them. */
|
|
27
|
+
const RECORD_NAME = '.tea-customization-migration.json';
|
|
28
|
+
const RECORD_VERSION = 1;
|
|
29
|
+
/** The pre-v7 personal config layer BMad core no longer merges, and the layers TEA reads `[modules.tea]` from. */
|
|
30
|
+
const LEGACY_USER_CONFIG = ['_bmad', 'config.user.toml'];
|
|
31
|
+
const CONFIG_LAYERS = [
|
|
32
|
+
['_bmad', 'config.toml'],
|
|
33
|
+
['_bmad', 'custom', 'config.toml'],
|
|
34
|
+
['_bmad', 'custom', 'config.user.toml'],
|
|
35
|
+
];
|
|
36
|
+
const HEADER = /^\s*(\[\[?)\s*([^[\]]*?)\s*(\]\]?)\s*(?:#.*)?$/;
|
|
37
|
+
const CI_PLATFORM_LINE = /^\s*(?:ci_platform|"ci_platform"|'ci_platform')\s*=/;
|
|
16
38
|
|
|
17
39
|
function isTable(value) {
|
|
18
40
|
return value !== null && typeof value === 'object' && !Array.isArray(value) && !(value instanceof Date);
|
|
@@ -38,6 +60,14 @@ function parse(file, source) {
|
|
|
38
60
|
}
|
|
39
61
|
}
|
|
40
62
|
|
|
63
|
+
function tryParse(source) {
|
|
64
|
+
try {
|
|
65
|
+
return TOML.parse(source);
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
41
71
|
/** Reject links and non-files before any content is read or changed. */
|
|
42
72
|
function readFile(file) {
|
|
43
73
|
let stat;
|
|
@@ -65,6 +95,418 @@ function checkDirectory(directory, required = false) {
|
|
|
65
95
|
return true;
|
|
66
96
|
}
|
|
67
97
|
|
|
98
|
+
/** Split text into lines that each keep their own line ending, so joining them restores every byte. */
|
|
99
|
+
function toLines(text) {
|
|
100
|
+
const parts = text.split(/(\r?\n)/);
|
|
101
|
+
const lines = [];
|
|
102
|
+
for (let index = 0; index < parts.length; index += 2) lines.push({ text: parts[index], eol: parts[index + 1] ?? '' });
|
|
103
|
+
return lines;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function joinLines(lines) {
|
|
107
|
+
return lines.map((line) => line.text + line.eol).join('');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function lineEnding(text) {
|
|
111
|
+
return text.includes('\r\n') ? '\r\n' : '\n';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function headerPath(line) {
|
|
115
|
+
const match = HEADER.exec(line);
|
|
116
|
+
if (!match || match[1].length !== match[3].length) return null;
|
|
117
|
+
const unquote = (part) => part.trim().replace(/^"(.*)"$|^'(.*)'$/, '$1$2');
|
|
118
|
+
return { array: match[1] === '[[', path: match[2].split('.').map((part) => unquote(part)) };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Locate a `[a.b]` table header and the lines up to the next header. */
|
|
122
|
+
function findSection(lines, tablePath) {
|
|
123
|
+
for (let start = 0; start < lines.length; start++) {
|
|
124
|
+
const header = headerPath(lines[start].text);
|
|
125
|
+
if (!header || header.array || header.path.join('.') !== tablePath) continue;
|
|
126
|
+
let end = start + 1;
|
|
127
|
+
while (end < lines.length && headerPath(lines[end].text) === null) end++;
|
|
128
|
+
return { start, end };
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Edit only the files whose structure is provably understood; anything else needs a person. */
|
|
134
|
+
function manualEdit(file, reason, table) {
|
|
135
|
+
return new Error(`Cannot safely edit ${file}: ${reason}. Add the settings under ${table} by hand, then rerun.`);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Add lines at the end of a table section, before any trailing comments that introduce the next table. */
|
|
139
|
+
function insertIntoSection(file, text, tablePath, newLines) {
|
|
140
|
+
const lines = toLines(text);
|
|
141
|
+
const section = findSection(lines, tablePath);
|
|
142
|
+
if (!section) throw manualEdit(file, `no [${tablePath}] header to add keys under`, `[${tablePath}]`);
|
|
143
|
+
let at = section.start;
|
|
144
|
+
for (let index = section.start + 1; index < section.end; index++) {
|
|
145
|
+
const trimmed = lines[index].text.trim();
|
|
146
|
+
if (trimmed !== '' && !trimmed.startsWith('#')) at = index;
|
|
147
|
+
}
|
|
148
|
+
const eol = lineEnding(text);
|
|
149
|
+
const inserted = newLines.map((line) => ({ text: line, eol }));
|
|
150
|
+
if (lines[at].eol === '') {
|
|
151
|
+
lines[at].eol = eol;
|
|
152
|
+
inserted.at(-1).eol = '';
|
|
153
|
+
}
|
|
154
|
+
lines.splice(at + 1, 0, ...inserted);
|
|
155
|
+
return joinLines(lines);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Append a block after existing content, separated by one blank line, leaving every existing byte in place. */
|
|
159
|
+
function appendBlock(text, block) {
|
|
160
|
+
if (text === '') return block;
|
|
161
|
+
const eol = lineEnding(text);
|
|
162
|
+
let result = text.endsWith('\n') ? text : text + eol;
|
|
163
|
+
if (result.trim() !== '' && !/\r?\n\r?\n$/.test(result)) result += eol;
|
|
164
|
+
return result + block;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Comment lines of a legacy file, which no parsed value carries. */
|
|
168
|
+
function commentLines(text) {
|
|
169
|
+
return toLines(text)
|
|
170
|
+
.map((line) => line.text.trim())
|
|
171
|
+
.filter((line) => line.startsWith('#'));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Keep a legacy file's comments (notes and commented-out settings) when its values merge into an existing table. */
|
|
175
|
+
function withComments(text, candidate) {
|
|
176
|
+
const comments = commentLines(candidate.text);
|
|
177
|
+
if (comments.length === 0) return text;
|
|
178
|
+
const eol = lineEnding(text === '' ? candidate.text : text);
|
|
179
|
+
return appendBlock(text, [`# Comments from ${candidate.name}`, ...comments].join(eol) + eol);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Turn a legacy file's [workflow] table into [workflow.<table>], keeping its comments and layout when that is provably exact.
|
|
184
|
+
* Bare and quoted spellings of `workflow` are rewritten; anything else falls back to a fresh rendering plus the file's comments.
|
|
185
|
+
*/
|
|
186
|
+
function nestedSource(candidate, table) {
|
|
187
|
+
const lines = toLines(candidate.text);
|
|
188
|
+
for (const line of lines) {
|
|
189
|
+
const match = /^(\s*\[\[?\s*(?:workflow|"workflow"|'workflow'))(\s*(?:\.[^\]]*)?\]\]?\s*(?:#.*)?)$/.exec(line.text);
|
|
190
|
+
if (match) line.text = `${match[1]}.${table}${match[2]}`;
|
|
191
|
+
}
|
|
192
|
+
const rewritten = joinLines(lines);
|
|
193
|
+
const expected = { workflow: { [table]: candidate.table } };
|
|
194
|
+
if (isDeepStrictEqual(tryParse(rewritten), expected)) return rewritten;
|
|
195
|
+
return withComments(TOML.stringify(expected), candidate);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function renderKey(file, key, value, table) {
|
|
199
|
+
const rendered = TOML.stringify({ [key]: value }).trimEnd();
|
|
200
|
+
if (/^\s*\[/m.test(rendered)) throw manualEdit(file, `${key} is a table and cannot be inserted inside ${table}`, table);
|
|
201
|
+
return rendered.split(/\r?\n/);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function provenance(candidate) {
|
|
205
|
+
if (candidate.topLevel) return `${candidate.file} [workflow] (live)`;
|
|
206
|
+
return candidate.live ? `${candidate.file} (live)` : `${candidate.file} (from ${candidate.provenance})`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Folders a v6 to v7 upgrade left behind, newest first by name. */
|
|
210
|
+
function listBackups(root) {
|
|
211
|
+
return fs
|
|
212
|
+
.readdirSync(root)
|
|
213
|
+
.filter((name) => name.startsWith(BACKUP_PREFIX))
|
|
214
|
+
.toSorted()
|
|
215
|
+
.toReversed();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** The newest backup that actually holds the file wins; a newer backup without it never hides an older one. */
|
|
219
|
+
function findInBackups(root, backups, segments) {
|
|
220
|
+
search: for (const folder of backups) {
|
|
221
|
+
let directory = path.join(root, folder);
|
|
222
|
+
if (!checkDirectory(directory)) continue;
|
|
223
|
+
for (const segment of segments.slice(0, -1)) {
|
|
224
|
+
directory = path.join(directory, segment);
|
|
225
|
+
if (!checkDirectory(directory)) continue search;
|
|
226
|
+
}
|
|
227
|
+
const found = readFile(path.join(directory, segments.at(-1)));
|
|
228
|
+
if (found) return { ...found, provenance: folder };
|
|
229
|
+
}
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function emptyRecord() {
|
|
234
|
+
return { version: RECORD_VERSION, restored: {}, kept: {} };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Read the migration record; a damaged record stops the run so no decision recorded in it is lost. */
|
|
238
|
+
function readRecord(customRoot) {
|
|
239
|
+
const file = path.join(customRoot, RECORD_NAME);
|
|
240
|
+
const found = readFile(file);
|
|
241
|
+
if (!found) return emptyRecord();
|
|
242
|
+
let value;
|
|
243
|
+
try {
|
|
244
|
+
value = JSON.parse(found.text);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
throw new Error(`Cannot read migration record ${file}: ${error.message}. Fix or remove it, then rerun.`);
|
|
247
|
+
}
|
|
248
|
+
const isNameMap = (map) => isTable(map) && Object.values(map).every((folder) => typeof folder === 'string');
|
|
249
|
+
if (!isTable(value) || value.version !== RECORD_VERSION || !isNameMap(value.restored) || !isNameMap(value.kept)) {
|
|
250
|
+
throw new Error(
|
|
251
|
+
`Cannot read migration record ${file}: expected {"version": ${RECORD_VERSION}, "restored": {...}, "kept": {...}}. Fix or remove it, then rerun.`,
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
return { version: RECORD_VERSION, restored: { ...value.restored }, kept: { ...value.kept } };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function sortedNames(map) {
|
|
258
|
+
return Object.fromEntries(Object.entries(map).toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Replace the record in one rename; a failure here is reported but never undoes the finished migration. */
|
|
262
|
+
function writeRecord(customRoot, record) {
|
|
263
|
+
const file = path.join(customRoot, RECORD_NAME);
|
|
264
|
+
const temp = path.join(customRoot, `${RECORD_NAME}.${process.pid}.tmp`);
|
|
265
|
+
const text = `${JSON.stringify({ version: RECORD_VERSION, restored: sortedNames(record.restored), kept: sortedNames(record.kept) }, null, 2)}\n`;
|
|
266
|
+
try {
|
|
267
|
+
readFile(file);
|
|
268
|
+
fs.writeFileSync(temp, text, { flag: 'wx', mode: 0o644 });
|
|
269
|
+
fs.renameSync(temp, file);
|
|
270
|
+
} catch (error) {
|
|
271
|
+
if (fs.existsSync(temp)) fs.unlinkSync(temp);
|
|
272
|
+
throw new Error(
|
|
273
|
+
`The customizations were migrated, but the migration record ${file} could not be written: ${error.message}. Fix the cause and rerun; a rerun may ask about backup values again.`,
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function recordKey(targetName, table, key) {
|
|
279
|
+
return `${targetName}|${table}|${key}`;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function readOptional(file) {
|
|
283
|
+
try {
|
|
284
|
+
return fs.readFileSync(file, 'utf8');
|
|
285
|
+
} catch (error) {
|
|
286
|
+
if (error.code === 'ENOENT') return null;
|
|
287
|
+
throw new Error(`Cannot read ${file}: ${error.message}`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Name `[modules.tea]` keys a pre-v7 personal config holds that TEA no longer reads; this command leaves that file alone. */
|
|
292
|
+
function legacyConfigNotice(root, backups) {
|
|
293
|
+
const liveFile = path.join(root, ...LEGACY_USER_CONFIG);
|
|
294
|
+
const liveText = readOptional(liveFile);
|
|
295
|
+
const legacy = liveText === null ? findInBackups(root, backups, LEGACY_USER_CONFIG) : { file: liveFile, text: liveText };
|
|
296
|
+
if (!legacy) return null;
|
|
297
|
+
const tea = tryParse(legacy.text)?.modules?.tea;
|
|
298
|
+
if (!isTable(tea)) return null;
|
|
299
|
+
const held = new Set();
|
|
300
|
+
for (const segments of CONFIG_LAYERS) {
|
|
301
|
+
const file = path.join(root, ...segments);
|
|
302
|
+
const text = readOptional(file);
|
|
303
|
+
if (text === null) continue;
|
|
304
|
+
const layer = parse(file, text).modules?.tea;
|
|
305
|
+
if (isTable(layer)) for (const key of Object.keys(layer)) held.add(key);
|
|
306
|
+
}
|
|
307
|
+
const ignored = Object.keys(tea).filter((key) => !held.has(key));
|
|
308
|
+
if (ignored.length === 0) return null;
|
|
309
|
+
const line = `Not migrated by this command: ${legacy.file} [modules.tea] keys ${ignored.join(', ')} are ignored by TEA. Run \`bmad migrate tea\` to move them.`;
|
|
310
|
+
return { kind: 'notice', messages: [{ done: line, check: line }] };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function collectCandidates(context, surface, suffix, targetName) {
|
|
314
|
+
const candidates = [];
|
|
315
|
+
for (const stem of surface.sources) {
|
|
316
|
+
const name = stem + suffix;
|
|
317
|
+
const live = readFile(path.join(context.customRoot, name));
|
|
318
|
+
const backupSegments = ['_bmad', 'custom', name];
|
|
319
|
+
let found = null;
|
|
320
|
+
if (live) {
|
|
321
|
+
// A backup copy of a file migrated from the live tree must never resurface on a later run.
|
|
322
|
+
found = { ...live, provenance: 'live', shadowed: findInBackups(context.root, context.backups, backupSegments) !== null };
|
|
323
|
+
} else if (!Object.hasOwn(context.record.restored, name)) {
|
|
324
|
+
found = findInBackups(context.root, context.backups, backupSegments);
|
|
325
|
+
}
|
|
326
|
+
if (!found) continue;
|
|
327
|
+
const value = parse(found.file, found.text);
|
|
328
|
+
for (const key of Object.keys(value)) {
|
|
329
|
+
if (key !== 'workflow')
|
|
330
|
+
throw new Error(`${found.file} has ${key} outside [workflow]; move it manually into ${targetName}, then rerun.`);
|
|
331
|
+
}
|
|
332
|
+
if (value.workflow !== undefined && !isTable(value.workflow))
|
|
333
|
+
throw new Error(`Expected a [workflow] table in ${found.file}; move its settings manually into ${targetName}, then rerun.`);
|
|
334
|
+
const isLive = found.provenance === 'live';
|
|
335
|
+
const table = { ...value.workflow };
|
|
336
|
+
if (!isLive) {
|
|
337
|
+
for (const key of Object.keys(table)) {
|
|
338
|
+
const recorded = recordKey(targetName, surface.table, key);
|
|
339
|
+
if (Object.hasOwn(context.record.restored, recorded) || Object.hasOwn(context.record.kept, recorded)) delete table[key];
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
candidates.push({ name, ...found, live: isLive, table });
|
|
343
|
+
}
|
|
344
|
+
return candidates;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** Find value conflicts and kept backup values against the target as it stands before this run. */
|
|
348
|
+
function analyse(candidates, targetTable, targetFile, tableLabel, keepExisting) {
|
|
349
|
+
const conflicts = [];
|
|
350
|
+
const kept = [];
|
|
351
|
+
const keys = [...new Set(candidates.flatMap((candidate) => Object.keys(candidate.table)))];
|
|
352
|
+
for (const key of keys) {
|
|
353
|
+
const holders = candidates.filter((candidate) => Object.hasOwn(candidate.table, key));
|
|
354
|
+
if (Object.hasOwn(targetTable, key)) {
|
|
355
|
+
for (const candidate of holders) {
|
|
356
|
+
if (isDeepStrictEqual(candidate.table[key], targetTable[key])) continue;
|
|
357
|
+
const both = `${JSON.stringify(candidate.table[key])} in ${provenance(candidate)} and ${JSON.stringify(targetTable[key])} in ${targetFile} ${tableLabel}`;
|
|
358
|
+
if (candidate.live) conflicts.push(`Conflicting ${key}: ${both}. Make them agree, then rerun.`);
|
|
359
|
+
else if (keepExisting) kept.push({ key, candidate, value: targetTable[key] });
|
|
360
|
+
else conflicts.push(`Conflicting ${key}: ${both}. Make them agree, or rerun with --keep-existing to keep the existing value.`);
|
|
361
|
+
}
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
const [first, ...rest] = holders;
|
|
365
|
+
for (const candidate of rest) {
|
|
366
|
+
if (isDeepStrictEqual(candidate.table[key], first.table[key])) continue;
|
|
367
|
+
conflicts.push(
|
|
368
|
+
`Conflicting ${key}: ${JSON.stringify(first.table[key])} in ${provenance(first)} and ${JSON.stringify(candidate.table[key])} in ${provenance(candidate)}. Make them agree, or set ${key} under ${tableLabel} in ${targetFile} to the value you want, then rerun.`,
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return { conflicts, kept };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** Move `ci_platform` from [workflow] into [workflow.ci] of the same file, line and all. */
|
|
376
|
+
function moveCiPlatform(file, text, current) {
|
|
377
|
+
const lines = toLines(text);
|
|
378
|
+
const section = findSection(lines, 'workflow');
|
|
379
|
+
const hits = [];
|
|
380
|
+
for (let index = (section?.start ?? 0) + 1; section && index < section.end; index++) {
|
|
381
|
+
if (CI_PLATFORM_LINE.test(lines[index].text)) hits.push(index);
|
|
382
|
+
}
|
|
383
|
+
if (hits.length !== 1) throw manualEdit(file, 'ci_platform is not a single line under a [workflow] header', '[workflow.ci]');
|
|
384
|
+
const [moved] = lines.splice(hits[0], 1);
|
|
385
|
+
const removed = joinLines(lines);
|
|
386
|
+
const expected = parse(file, text);
|
|
387
|
+
delete expected.workflow.ci_platform;
|
|
388
|
+
if (!isDeepStrictEqual(tryParse(removed), expected))
|
|
389
|
+
throw manualEdit(file, 'ci_platform could not be removed from [workflow] exactly', '[workflow.ci]');
|
|
390
|
+
const ci = current.workflow?.ci;
|
|
391
|
+
if (ci && Object.hasOwn(ci, 'ci_platform')) return removed;
|
|
392
|
+
const line = moved.text.trim();
|
|
393
|
+
if (ci) return insertIntoSection(file, removed, 'workflow.ci', [line]);
|
|
394
|
+
return appendBlock(removed, `[workflow.ci]${lineEnding(text)}${line}${lineEnding(text)}`);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Plan the merge of one retired surface into one layer of its owning skill. */
|
|
398
|
+
function planSurface(context, surface, suffix) {
|
|
399
|
+
const targetName = surface.target + suffix;
|
|
400
|
+
const targetFile = path.join(context.customRoot, targetName);
|
|
401
|
+
const tableLabel = `[workflow.${surface.table}]`;
|
|
402
|
+
const base = context.staged.get(targetFile) ?? readFile(targetFile);
|
|
403
|
+
const baseLabel = base?.source ?? targetFile;
|
|
404
|
+
const baseValue = base ? parse(baseLabel, base.text) : {};
|
|
405
|
+
if (baseValue.workflow !== undefined && !isTable(baseValue.workflow))
|
|
406
|
+
throw manualEdit(baseLabel, '[workflow] is not a table', tableLabel);
|
|
407
|
+
const existing = baseValue.workflow?.[surface.table];
|
|
408
|
+
if (existing !== undefined && !isTable(existing)) throw manualEdit(baseLabel, `${tableLabel} is not a table`, tableLabel);
|
|
409
|
+
const candidates = collectCandidates(context, surface, suffix, targetName);
|
|
410
|
+
if (surface.moveCiPlatform && baseValue.workflow && Object.hasOwn(baseValue.workflow, 'ci_platform')) {
|
|
411
|
+
candidates.push({
|
|
412
|
+
name: targetName,
|
|
413
|
+
file: targetFile,
|
|
414
|
+
provenance: 'live',
|
|
415
|
+
live: true,
|
|
416
|
+
topLevel: true,
|
|
417
|
+
table: { ci_platform: baseValue.workflow.ci_platform },
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
if (candidates.length === 0) return;
|
|
421
|
+
const { conflicts, kept } = analyse(candidates, existing ?? {}, targetFile, tableLabel, context.keepExisting);
|
|
422
|
+
if (conflicts.length > 0) {
|
|
423
|
+
context.conflicts.push(...conflicts);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const record = context.nextRecord;
|
|
428
|
+
const baseText = base?.text ?? null;
|
|
429
|
+
let text = baseText;
|
|
430
|
+
let mode;
|
|
431
|
+
const messages = [];
|
|
432
|
+
const expected = parse(baseLabel, baseText ?? '');
|
|
433
|
+
for (const candidate of candidates) {
|
|
434
|
+
const current = text === null ? {} : parse(targetFile, text);
|
|
435
|
+
const have = current.workflow?.[surface.table];
|
|
436
|
+
const missing = Object.keys(candidate.table).filter((key) => !have || !Object.hasOwn(have, key));
|
|
437
|
+
const from = candidate.live ? '' : ` (from ${candidate.provenance})`;
|
|
438
|
+
if (candidate.topLevel) {
|
|
439
|
+
text = moveCiPlatform(targetFile, text, current);
|
|
440
|
+
delete expected.workflow.ci_platform;
|
|
441
|
+
expected.workflow.ci ??= {};
|
|
442
|
+
expected.workflow.ci.ci_platform ??= candidate.table.ci_platform;
|
|
443
|
+
const move = `${targetName} [workflow].ci_platform -> ${targetName} ${tableLabel}`;
|
|
444
|
+
messages.push({ done: move, check: `Would move ${move}` });
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
if (!candidate.live || candidate.shadowed) record.restored[candidate.name] = candidate.provenance;
|
|
448
|
+
if (missing.length > 0) {
|
|
449
|
+
mode ??= candidate.stat.mode & 0o777;
|
|
450
|
+
const additions = Object.fromEntries(missing.map((key) => [key, candidate.table[key]]));
|
|
451
|
+
expected.workflow ??= {};
|
|
452
|
+
expected.workflow[surface.table] = { ...expected.workflow[surface.table], ...additions };
|
|
453
|
+
let next = have
|
|
454
|
+
? insertIntoSection(
|
|
455
|
+
targetFile,
|
|
456
|
+
text,
|
|
457
|
+
`workflow.${surface.table}`,
|
|
458
|
+
missing.flatMap((key) => renderKey(targetFile, key, candidate.table[key], tableLabel)),
|
|
459
|
+
)
|
|
460
|
+
: appendBlock(text ?? '', nestedSource(candidate, surface.table));
|
|
461
|
+
if (have) next = withComments(next, candidate);
|
|
462
|
+
if (!isDeepStrictEqual(tryParse(next), expected))
|
|
463
|
+
throw manualEdit(targetFile, `the merged ${tableLabel} could not be verified`, tableLabel);
|
|
464
|
+
text = next;
|
|
465
|
+
if (!candidate.live) for (const key of missing) record.restored[recordKey(targetName, surface.table, key)] = candidate.provenance;
|
|
466
|
+
}
|
|
467
|
+
if (missing.length === 0 && candidate.live) {
|
|
468
|
+
const withNote = withComments(text ?? '', candidate);
|
|
469
|
+
if (withNote !== text) text = withNote;
|
|
470
|
+
}
|
|
471
|
+
if (missing.length > 0 || candidate.live) {
|
|
472
|
+
const move = `${candidate.name} [workflow] -> ${targetName} ${tableLabel}${from}${missing.length === 0 ? ' (values already present)' : ''}`;
|
|
473
|
+
messages.push({ done: move, check: `Would move ${move}` });
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
for (const { key, candidate, value } of kept) {
|
|
477
|
+
record.kept[recordKey(targetName, surface.table, key)] = candidate.provenance;
|
|
478
|
+
const notice = `${key} = ${JSON.stringify(value)} in ${targetName} ${tableLabel}; ignoring ${JSON.stringify(candidate.table[key])} from ${candidate.name} (from ${candidate.provenance})`;
|
|
479
|
+
messages.push({ done: `Kept ${notice}`, check: `Would keep ${notice}` });
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const group = [];
|
|
483
|
+
let writer = null;
|
|
484
|
+
if (text !== baseText) {
|
|
485
|
+
if (!isDeepStrictEqual(tryParse(text), expected))
|
|
486
|
+
throw manualEdit(targetFile, `the merged ${tableLabel} could not be verified`, tableLabel);
|
|
487
|
+
if (base?.pending) {
|
|
488
|
+
// The file is itself being restored from a backup in this run: fold the merge into that creation.
|
|
489
|
+
writer = base.pending;
|
|
490
|
+
writer.text = text;
|
|
491
|
+
writer.expected = expected;
|
|
492
|
+
} else {
|
|
493
|
+
writer = base
|
|
494
|
+
? { kind: 'update', oldFile: targetFile, newFile: targetFile, old: base, text, expected }
|
|
495
|
+
: { kind: 'create', oldFile: null, newFile: targetFile, text, mode: mode ?? 0o644, expected };
|
|
496
|
+
group.push(writer);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
for (const candidate of candidates) {
|
|
500
|
+
if (!candidate.live || candidate.topLevel) continue;
|
|
501
|
+
const old = { file: candidate.file, stat: candidate.stat, text: candidate.text };
|
|
502
|
+
group.push({ kind: 'remove', oldFile: candidate.file, newFile: targetFile, old, requires: text });
|
|
503
|
+
}
|
|
504
|
+
const carrier = writer ?? group[0];
|
|
505
|
+
if (carrier) carrier.messages = [...(carrier.messages ?? []), ...messages];
|
|
506
|
+
else if (messages.length > 0) context.actions.push({ kind: 'notice', messages });
|
|
507
|
+
context.actions.push(...group);
|
|
508
|
+
}
|
|
509
|
+
|
|
68
510
|
/** Edit only skill assignments inside agent menu entries, retaining every other byte. */
|
|
69
511
|
function rewriteMenu(file, source) {
|
|
70
512
|
const original = parse(file, source);
|
|
@@ -99,35 +541,79 @@ function rewriteMenu(file, source) {
|
|
|
99
541
|
return rewritten;
|
|
100
542
|
}
|
|
101
543
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
544
|
+
/**
|
|
545
|
+
* Stage a pre-3.0 rename, or the restore of an owning-skill file a v6 to v7 cleanup removed, so later merges land on the
|
|
546
|
+
* file that will exist after this run.
|
|
547
|
+
*/
|
|
548
|
+
function planRename(context, oldName, newName, suffix) {
|
|
549
|
+
const { customRoot, staged, actions } = context;
|
|
550
|
+
const oldFile = path.join(customRoot, oldName + suffix);
|
|
551
|
+
const newFile = path.join(customRoot, newName + suffix);
|
|
552
|
+
const old = readFile(oldFile);
|
|
553
|
+
const current = readFile(newFile);
|
|
554
|
+
if (old) {
|
|
555
|
+
const oldValue = parse(oldFile, old.text);
|
|
556
|
+
if (current) {
|
|
557
|
+
const newValue = parse(newFile, current.text);
|
|
558
|
+
const conflict = conflictAt(oldValue, newValue);
|
|
559
|
+
if (conflict) throw new Error(`Conflicting ${conflict} in ${oldFile} and ${newFile}`);
|
|
560
|
+
throw new Error(
|
|
561
|
+
`Both ${oldFile} and ${newFile} exist. Merge their values and comments manually into ${newFile}, then remove ${oldFile} and rerun.`,
|
|
562
|
+
);
|
|
121
563
|
}
|
|
564
|
+
actions.push({ kind: 'move', oldFile, newFile, old });
|
|
565
|
+
staged.set(newFile, { file: newFile, stat: old.stat, text: old.text, source: oldFile });
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
if (current || Object.hasOwn(context.record.restored, newName + suffix)) return;
|
|
569
|
+
const found =
|
|
570
|
+
findInBackups(context.root, context.backups, ['_bmad', 'custom', oldName + suffix]) ??
|
|
571
|
+
findInBackups(context.root, context.backups, ['_bmad', 'custom', newName + suffix]);
|
|
572
|
+
if (!found) return;
|
|
573
|
+
const move = `${path.basename(found.file)} -> ${newName + suffix} (from ${found.provenance})`;
|
|
574
|
+
const pending = {
|
|
575
|
+
kind: 'create',
|
|
576
|
+
oldFile: null,
|
|
577
|
+
newFile,
|
|
578
|
+
text: found.text,
|
|
579
|
+
mode: found.stat.mode & 0o777,
|
|
580
|
+
expected: parse(found.file, found.text),
|
|
581
|
+
messages: [{ done: move, check: `Would move ${move}` }],
|
|
582
|
+
};
|
|
583
|
+
actions.push(pending);
|
|
584
|
+
staged.set(newFile, { file: newFile, text: found.text, source: found.file, pending });
|
|
585
|
+
context.nextRecord.restored[newName + suffix] = found.provenance;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/** Read every input, stage every change and surface every conflict before anything is written. */
|
|
589
|
+
function preflight(root, customRoot, { keepExisting = false } = {}) {
|
|
590
|
+
const record = readRecord(customRoot);
|
|
591
|
+
const context = {
|
|
592
|
+
root,
|
|
593
|
+
customRoot,
|
|
594
|
+
backups: listBackups(root),
|
|
595
|
+
staged: new Map(),
|
|
596
|
+
actions: [],
|
|
597
|
+
conflicts: [],
|
|
598
|
+
keepExisting,
|
|
599
|
+
record,
|
|
600
|
+
nextRecord: structuredClone(record),
|
|
601
|
+
};
|
|
602
|
+
for (const [oldName, newName] of PAIRS) for (const suffix of LAYERS) planRename(context, oldName, newName, suffix);
|
|
603
|
+
for (const surface of SURFACES) for (const suffix of LAYERS) planSurface(context, surface, suffix);
|
|
604
|
+
if (context.conflicts.length > 0) {
|
|
605
|
+
throw new Error(`Customization conflicts found; nothing was changed.\n${context.conflicts.map((line) => `- ${line}`).join('\n')}`);
|
|
122
606
|
}
|
|
123
|
-
for (const suffix of
|
|
607
|
+
for (const suffix of LAYERS) {
|
|
124
608
|
const file = path.join(customRoot, 'bmad-tea' + suffix);
|
|
125
609
|
const old = readFile(file);
|
|
126
610
|
if (!old) continue;
|
|
127
611
|
const rewritten = rewriteMenu(file, old.text);
|
|
128
|
-
if (rewritten !== null) actions.push({ kind: 'rewrite', oldFile: file, newFile: file, old, text: rewritten });
|
|
612
|
+
if (rewritten !== null) context.actions.push({ kind: 'rewrite', oldFile: file, newFile: file, old, text: rewritten });
|
|
129
613
|
}
|
|
130
|
-
|
|
614
|
+
const notice = legacyConfigNotice(root, context.backups);
|
|
615
|
+
if (notice) context.actions.push(notice);
|
|
616
|
+
return { actions: context.actions, record, nextRecord: context.nextRecord };
|
|
131
617
|
}
|
|
132
618
|
|
|
133
619
|
function unchanged(snapshot) {
|
|
@@ -141,68 +627,165 @@ function unchanged(snapshot) {
|
|
|
141
627
|
);
|
|
142
628
|
}
|
|
143
629
|
|
|
630
|
+
/** Re-read a written customization and confirm both its bytes and its parsed values. */
|
|
631
|
+
function verifyWrite(action) {
|
|
632
|
+
const current = readFile(action.newFile);
|
|
633
|
+
if (current?.text !== action.text || !isDeepStrictEqual(tryParse(current.text), action.expected)) {
|
|
634
|
+
throw new Error(`Could not verify ${action.newFile} after writing it`);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/** Perform one staged action; `completed` records it as soon as there is something to undo. */
|
|
639
|
+
function applyAction(action, index, customRoot, completed) {
|
|
640
|
+
const backup = path.join(customRoot, `.tea-migration-${process.pid}-${index}.bak`);
|
|
641
|
+
switch (action.kind) {
|
|
642
|
+
case 'move': {
|
|
643
|
+
if (readFile(action.newFile) !== null) throw new Error(`Customization appeared during migration: ${action.newFile}`);
|
|
644
|
+
fs.linkSync(action.oldFile, action.newFile);
|
|
645
|
+
completed.push(action);
|
|
646
|
+
fs.unlinkSync(action.oldFile);
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
case 'create': {
|
|
650
|
+
if (readFile(action.newFile) !== null) throw new Error(`Customization appeared during migration: ${action.newFile}`);
|
|
651
|
+
fs.linkSync(action.temp, action.newFile);
|
|
652
|
+
completed.push(action);
|
|
653
|
+
verifyWrite(action);
|
|
654
|
+
break;
|
|
655
|
+
}
|
|
656
|
+
case 'remove': {
|
|
657
|
+
if ((readFile(action.newFile)?.text ?? null) !== action.requires)
|
|
658
|
+
throw new Error(`Customization changed during migration: ${action.newFile}`);
|
|
659
|
+
fs.linkSync(action.oldFile, backup);
|
|
660
|
+
action.backup = backup;
|
|
661
|
+
completed.push(action);
|
|
662
|
+
fs.unlinkSync(action.oldFile);
|
|
663
|
+
break;
|
|
664
|
+
}
|
|
665
|
+
default: {
|
|
666
|
+
fs.linkSync(action.oldFile, backup);
|
|
667
|
+
action.backup = backup;
|
|
668
|
+
completed.push(action);
|
|
669
|
+
fs.renameSync(action.temp, action.oldFile);
|
|
670
|
+
if (action.kind === 'update') verifyWrite(action);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function currentText(file) {
|
|
676
|
+
try {
|
|
677
|
+
return fs.readFileSync(file, 'utf8');
|
|
678
|
+
} catch (error) {
|
|
679
|
+
if (error.code === 'ENOENT') return null;
|
|
680
|
+
throw error;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Undo one completed action unless someone changed the file since this run touched it; then the newer content stays,
|
|
686
|
+
* the recovery copy stays, and the returned line says where it is.
|
|
687
|
+
*/
|
|
688
|
+
function rollbackAction(action) {
|
|
689
|
+
switch (action.kind) {
|
|
690
|
+
case 'move': {
|
|
691
|
+
if (fs.existsSync(action.oldFile))
|
|
692
|
+
return `rollback skipped for ${action.newFile}: ${action.oldFile} reappeared after the migration moved it`;
|
|
693
|
+
fs.linkSync(action.newFile, action.oldFile);
|
|
694
|
+
fs.unlinkSync(action.newFile);
|
|
695
|
+
return null;
|
|
696
|
+
}
|
|
697
|
+
case 'create': {
|
|
698
|
+
const current = currentText(action.newFile);
|
|
699
|
+
if (current === null) return null;
|
|
700
|
+
if (current !== action.text)
|
|
701
|
+
return `rollback skipped for ${action.newFile}: it changed after the migration wrote it; it did not exist before the migration`;
|
|
702
|
+
fs.unlinkSync(action.newFile);
|
|
703
|
+
return null;
|
|
704
|
+
}
|
|
705
|
+
case 'remove': {
|
|
706
|
+
const current = currentText(action.oldFile);
|
|
707
|
+
if (current === null) fs.linkSync(action.backup, action.oldFile);
|
|
708
|
+
else if (current !== action.old.text) {
|
|
709
|
+
action.keepBackup = true;
|
|
710
|
+
return `rollback skipped for ${action.oldFile}: it changed after the migration removed it; the previous content is in ${action.backup}`;
|
|
711
|
+
}
|
|
712
|
+
fs.unlinkSync(action.backup);
|
|
713
|
+
return null;
|
|
714
|
+
}
|
|
715
|
+
default: {
|
|
716
|
+
const current = currentText(action.oldFile);
|
|
717
|
+
if (current === action.old.text) {
|
|
718
|
+
fs.unlinkSync(action.backup);
|
|
719
|
+
return null;
|
|
720
|
+
}
|
|
721
|
+
if (current !== null && current !== action.text) {
|
|
722
|
+
action.keepBackup = true;
|
|
723
|
+
return `rollback skipped for ${action.oldFile}: it changed after the migration wrote it; the previous content is in ${action.backup}`;
|
|
724
|
+
}
|
|
725
|
+
if (current !== null) fs.unlinkSync(action.oldFile);
|
|
726
|
+
fs.linkSync(action.backup, action.oldFile);
|
|
727
|
+
fs.unlinkSync(action.backup);
|
|
728
|
+
return null;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
144
733
|
/** Apply staged changes under one exclusive lock and undo completed actions on failure. */
|
|
145
|
-
function applyActions(
|
|
734
|
+
function applyActions(allActions, customRoot, beforeAction) {
|
|
735
|
+
const actions = allActions.filter((action) => action.kind !== 'notice');
|
|
146
736
|
const completed = [];
|
|
147
737
|
const temporary = [];
|
|
148
738
|
let rollbackSafe = true;
|
|
149
739
|
try {
|
|
150
740
|
for (const [index, action] of actions.entries()) {
|
|
151
|
-
if (
|
|
741
|
+
if (!['rewrite', 'update', 'create'].includes(action.kind)) continue;
|
|
742
|
+
const mode = action.kind === 'create' ? action.mode : action.old.stat.mode & 0o777;
|
|
152
743
|
const temp = path.join(customRoot, `.tea-migration-${process.pid}-${index}.tmp`);
|
|
153
|
-
fs.writeFileSync(temp, action.text, { flag: 'wx', mode
|
|
154
|
-
fs.chmodSync(temp,
|
|
744
|
+
fs.writeFileSync(temp, action.text, { flag: 'wx', mode });
|
|
745
|
+
fs.chmodSync(temp, mode);
|
|
155
746
|
action.temp = temp;
|
|
156
747
|
temporary.push(temp);
|
|
157
748
|
}
|
|
158
749
|
for (const [index, action] of actions.entries()) {
|
|
159
750
|
beforeAction?.(action, index);
|
|
160
|
-
if (!unchanged(action.old)) throw new Error(`Customization changed during migration: ${action.oldFile}`);
|
|
161
|
-
|
|
162
|
-
if (readFile(action.newFile) !== null) throw new Error(`Customization appeared during migration: ${action.newFile}`);
|
|
163
|
-
fs.linkSync(action.oldFile, action.newFile);
|
|
164
|
-
completed.push(action);
|
|
165
|
-
fs.unlinkSync(action.oldFile);
|
|
166
|
-
} else {
|
|
167
|
-
const backup = path.join(customRoot, `.tea-migration-${process.pid}-${index}.bak`);
|
|
168
|
-
fs.linkSync(action.oldFile, backup);
|
|
169
|
-
action.backup = backup;
|
|
170
|
-
completed.push(action);
|
|
171
|
-
fs.renameSync(action.temp, action.oldFile);
|
|
172
|
-
}
|
|
751
|
+
if (action.old && !unchanged(action.old)) throw new Error(`Customization changed during migration: ${action.oldFile}`);
|
|
752
|
+
applyAction(action, index, customRoot, completed);
|
|
173
753
|
}
|
|
174
754
|
} catch (error) {
|
|
175
755
|
const rollbackErrors = [];
|
|
756
|
+
const skipped = [];
|
|
176
757
|
for (const action of completed.toReversed()) {
|
|
177
758
|
try {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
fs.unlinkSync(action.newFile);
|
|
181
|
-
} else {
|
|
182
|
-
if (fs.existsSync(action.oldFile)) fs.unlinkSync(action.oldFile);
|
|
183
|
-
fs.linkSync(action.backup, action.oldFile);
|
|
184
|
-
fs.unlinkSync(action.backup);
|
|
185
|
-
}
|
|
759
|
+
const skip = rollbackAction(action);
|
|
760
|
+
if (skip) skipped.push(skip);
|
|
186
761
|
} catch (rollbackError) {
|
|
187
|
-
rollbackErrors.push(`${action.oldFile}: ${rollbackError.message}`);
|
|
762
|
+
rollbackErrors.push(`${action.oldFile ?? action.newFile}: ${rollbackError.message}`);
|
|
188
763
|
}
|
|
189
764
|
}
|
|
190
765
|
rollbackSafe = rollbackErrors.length === 0;
|
|
191
|
-
|
|
766
|
+
const notes = [...skipped, ...(rollbackErrors.length > 0 ? [`rollback needs manual repair: ${rollbackErrors.join('; ')}`] : [])];
|
|
767
|
+
throw new Error([error.message, ...notes].join('; '));
|
|
192
768
|
} finally {
|
|
193
|
-
if (rollbackSafe)
|
|
769
|
+
if (rollbackSafe) {
|
|
770
|
+
for (const action of actions) if (action.backup && !action.keepBackup && fs.existsSync(action.backup)) fs.unlinkSync(action.backup);
|
|
771
|
+
}
|
|
194
772
|
for (const temp of temporary) if (fs.existsSync(temp)) fs.unlinkSync(temp);
|
|
195
773
|
}
|
|
196
774
|
}
|
|
197
775
|
|
|
198
|
-
function migrate(projectRoot, { check = false, beforeAction } = {}) {
|
|
776
|
+
function migrate(projectRoot, { check = false, keepExisting = false, beforeAction } = {}) {
|
|
199
777
|
const root = path.resolve(projectRoot);
|
|
200
778
|
checkDirectory(root, true);
|
|
201
779
|
const bmadRoot = path.join(root, '_bmad');
|
|
202
780
|
if (!checkDirectory(bmadRoot)) return [];
|
|
203
781
|
const customRoot = path.join(bmadRoot, 'custom');
|
|
204
|
-
if (
|
|
205
|
-
if (
|
|
782
|
+
if (check) return preflight(root, customRoot, { keepExisting }).actions;
|
|
783
|
+
if (!checkDirectory(customRoot)) {
|
|
784
|
+
// A cleanup may have removed the whole folder while a backup still holds settings to restore.
|
|
785
|
+
const planned = preflight(root, customRoot, { keepExisting }).actions;
|
|
786
|
+
if (planned.every((action) => action.kind === 'notice')) return planned;
|
|
787
|
+
fs.mkdirSync(customRoot);
|
|
788
|
+
}
|
|
206
789
|
const lock = path.join(customRoot, '.tea-migration.lock');
|
|
207
790
|
try {
|
|
208
791
|
fs.mkdirSync(lock, { mode: 0o700 });
|
|
@@ -215,29 +798,53 @@ function migrate(projectRoot, { check = false, beforeAction } = {}) {
|
|
|
215
798
|
throw new Error(`Cannot lock ${customRoot} for migration: ${error.message}`);
|
|
216
799
|
}
|
|
217
800
|
try {
|
|
218
|
-
const
|
|
219
|
-
applyActions(actions, customRoot, beforeAction);
|
|
220
|
-
|
|
801
|
+
const plan = preflight(root, customRoot, { keepExisting });
|
|
802
|
+
applyActions(plan.actions, customRoot, beforeAction);
|
|
803
|
+
if (!isDeepStrictEqual(plan.nextRecord, plan.record)) writeRecord(customRoot, plan.nextRecord);
|
|
804
|
+
return plan.actions;
|
|
221
805
|
} finally {
|
|
222
806
|
fs.rmdirSync(lock);
|
|
223
807
|
}
|
|
224
808
|
}
|
|
225
809
|
|
|
810
|
+
function describe(action, check) {
|
|
811
|
+
const lines = (action.messages ?? []).map((message) => (check ? message.check : message.done));
|
|
812
|
+
if (action.kind === 'rewrite') lines.push(`${check ? 'Would update' : 'Updated'} menu references in ${action.oldFile}`);
|
|
813
|
+
if (action.kind === 'move') lines.push(`${check ? 'Would move ' : ''}${action.oldFile} -> ${action.newFile}`);
|
|
814
|
+
return lines;
|
|
815
|
+
}
|
|
816
|
+
|
|
226
817
|
function main(argv = process.argv.slice(2)) {
|
|
227
|
-
const usage =
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
for (
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
818
|
+
const usage =
|
|
819
|
+
'Usage: tea-migrate-customizations [--project-root PATH] [--check] [--keep-existing] [--help]\nFor a directory beginning with a dash, use --project-root ./-name.';
|
|
820
|
+
let projectRoot = process.cwd();
|
|
821
|
+
let check = false;
|
|
822
|
+
let keepExisting = false;
|
|
823
|
+
let help = false;
|
|
824
|
+
const seen = new Set();
|
|
825
|
+
for (let index = 0; index < argv.length; index++) {
|
|
826
|
+
const arg = argv[index];
|
|
827
|
+
if (arg === '--help' || arg === '-h') {
|
|
828
|
+
help = true;
|
|
829
|
+
} else if (arg === '--check' && !seen.has(arg)) {
|
|
830
|
+
check = true;
|
|
831
|
+
} else if (arg === '--keep-existing' && !seen.has(arg)) {
|
|
832
|
+
keepExisting = true;
|
|
833
|
+
} else if (arg === '--project-root' && !seen.has(arg)) {
|
|
834
|
+
const value = argv[++index];
|
|
835
|
+
if (!value || value.startsWith('-')) throw new Error('--project-root requires a path');
|
|
836
|
+
projectRoot = path.resolve(value);
|
|
837
|
+
} else {
|
|
838
|
+
throw new Error(usage);
|
|
839
|
+
}
|
|
840
|
+
seen.add(arg);
|
|
841
|
+
}
|
|
842
|
+
if (help) {
|
|
843
|
+
process.stdout.write(`${usage}\n`);
|
|
844
|
+
return 0;
|
|
240
845
|
}
|
|
846
|
+
const actions = migrate(projectRoot, { check, keepExisting });
|
|
847
|
+
for (const action of actions) for (const line of describe(action, check)) process.stdout.write(`${line}\n`);
|
|
241
848
|
if (actions.length === 0) process.stdout.write('No legacy TEA customizations found.\n');
|
|
242
849
|
return 0;
|
|
243
850
|
}
|