@ontrails/trails 1.0.0-beta.13 → 1.0.0-beta.14
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/.turbo/turbo-lint.log +1 -1
- package/CHANGELOG.md +16 -0
- package/__tests__/examples.test.ts +14 -0
- package/dist/src/app.d.ts.map +1 -1
- package/dist/src/app.js +13 -2
- package/dist/src/app.js.map +1 -1
- package/dist/src/clack.d.ts +1 -1
- package/dist/src/clack.js +1 -1
- package/dist/src/cli.js +2 -2
- package/dist/src/cli.js.map +1 -1
- package/dist/src/trails/add-trail.js +13 -13
- package/dist/src/trails/add-trail.js.map +1 -1
- package/dist/src/trails/add-trailhead.d.ts +13 -0
- package/dist/src/trails/add-trailhead.d.ts.map +1 -0
- package/dist/src/trails/add-trailhead.js +88 -0
- package/dist/src/trails/add-trailhead.js.map +1 -0
- package/dist/src/trails/add-verify.js +10 -10
- package/dist/src/trails/add-verify.js.map +1 -1
- package/dist/src/trails/create-scaffold.js +26 -26
- package/dist/src/trails/create-scaffold.js.map +1 -1
- package/dist/src/trails/create.d.ts +6 -6
- package/dist/src/trails/create.d.ts.map +1 -1
- package/dist/src/trails/create.js +29 -29
- package/dist/src/trails/create.js.map +1 -1
- package/dist/src/trails/dev-clean.d.ts +9 -0
- package/dist/src/trails/dev-clean.d.ts.map +1 -0
- package/dist/src/trails/dev-clean.js +65 -0
- package/dist/src/trails/dev-clean.js.map +1 -0
- package/dist/src/trails/dev-reset.d.ts +6 -0
- package/dist/src/trails/dev-reset.d.ts.map +1 -0
- package/dist/src/trails/dev-reset.js +38 -0
- package/dist/src/trails/dev-reset.js.map +1 -0
- package/dist/src/trails/dev-stats.d.ts +7 -0
- package/dist/src/trails/dev-stats.d.ts.map +1 -0
- package/dist/src/trails/dev-stats.js +61 -0
- package/dist/src/trails/dev-stats.js.map +1 -0
- package/dist/src/trails/dev-support.d.ts +64 -0
- package/dist/src/trails/dev-support.d.ts.map +1 -0
- package/dist/src/trails/dev-support.js +178 -0
- package/dist/src/trails/dev-support.js.map +1 -0
- package/dist/src/trails/draft-promote.d.ts +18 -0
- package/dist/src/trails/draft-promote.d.ts.map +1 -0
- package/dist/src/trails/draft-promote.js +386 -0
- package/dist/src/trails/draft-promote.js.map +1 -0
- package/dist/src/trails/guide.d.ts +13 -3
- package/dist/src/trails/guide.d.ts.map +1 -1
- package/dist/src/trails/guide.js +21 -37
- package/dist/src/trails/guide.js.map +1 -1
- package/dist/src/trails/load-app.d.ts +3 -1
- package/dist/src/trails/load-app.d.ts.map +1 -1
- package/dist/src/trails/load-app.js +53 -10
- package/dist/src/trails/load-app.js.map +1 -1
- package/dist/src/trails/project.d.ts.map +1 -1
- package/dist/src/trails/project.js +14 -3
- package/dist/src/trails/project.js.map +1 -1
- package/dist/src/trails/survey.d.ts +4 -58
- package/dist/src/trails/survey.d.ts.map +1 -1
- package/dist/src/trails/survey.js +52 -173
- package/dist/src/trails/survey.js.map +1 -1
- package/dist/src/trails/topo-constants.d.ts +3 -0
- package/dist/src/trails/topo-constants.d.ts.map +1 -0
- package/dist/src/trails/topo-constants.js +3 -0
- package/dist/src/trails/topo-constants.js.map +1 -0
- package/dist/src/trails/topo-export.d.ts +18 -0
- package/dist/src/trails/topo-export.d.ts.map +1 -0
- package/dist/src/trails/topo-export.js +34 -0
- package/dist/src/trails/topo-export.js.map +1 -0
- package/dist/src/trails/topo-history.d.ts +24 -0
- package/dist/src/trails/topo-history.d.ts.map +1 -0
- package/dist/src/trails/topo-history.js +33 -0
- package/dist/src/trails/topo-history.js.map +1 -0
- package/dist/src/trails/topo-pin.d.ts +21 -0
- package/dist/src/trails/topo-pin.d.ts.map +1 -0
- package/dist/src/trails/topo-pin.js +35 -0
- package/dist/src/trails/topo-pin.js.map +1 -0
- package/dist/src/trails/topo-read-support.d.ts +54 -0
- package/dist/src/trails/topo-read-support.d.ts.map +1 -0
- package/dist/src/trails/topo-read-support.js +178 -0
- package/dist/src/trails/topo-read-support.js.map +1 -0
- package/dist/src/trails/topo-reports.d.ts +50 -0
- package/dist/src/trails/topo-reports.d.ts.map +1 -0
- package/dist/src/trails/topo-reports.js +122 -0
- package/dist/src/trails/topo-reports.js.map +1 -0
- package/dist/src/trails/topo-show.d.ts +23 -0
- package/dist/src/trails/topo-show.d.ts.map +1 -0
- package/dist/src/trails/topo-show.js +53 -0
- package/dist/src/trails/topo-show.js.map +1 -0
- package/dist/src/trails/topo-store-support.d.ts +13 -0
- package/dist/src/trails/topo-store-support.d.ts.map +1 -0
- package/dist/src/trails/topo-store-support.js +55 -0
- package/dist/src/trails/topo-store-support.js.map +1 -0
- package/dist/src/trails/topo-support.d.ts +87 -0
- package/dist/src/trails/topo-support.d.ts.map +1 -0
- package/dist/src/trails/topo-support.js +165 -0
- package/dist/src/trails/topo-support.js.map +1 -0
- package/dist/src/trails/topo-unpin.d.ts +15 -0
- package/dist/src/trails/topo-unpin.d.ts.map +1 -0
- package/dist/src/trails/topo-unpin.js +39 -0
- package/dist/src/trails/topo-unpin.js.map +1 -0
- package/dist/src/trails/topo-verify.d.ts +5 -0
- package/dist/src/trails/topo-verify.d.ts.map +1 -0
- package/dist/src/trails/topo-verify.js +28 -0
- package/dist/src/trails/topo-verify.js.map +1 -0
- package/dist/src/trails/topo.d.ts +5 -0
- package/dist/src/trails/topo.d.ts.map +1 -0
- package/dist/src/trails/topo.js +67 -0
- package/dist/src/trails/topo.js.map +1 -0
- package/dist/src/trails/warden.d.ts +1 -1
- package/dist/src/trails/warden.d.ts.map +1 -1
- package/dist/src/trails/warden.js +28 -27
- package/dist/src/trails/warden.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -7
- package/src/__tests__/draft-promote.test.ts +144 -0
- package/src/__tests__/load-app.test.ts +43 -0
- package/src/__tests__/survey.test.ts +85 -0
- package/src/__tests__/topo-dev.test.ts +424 -0
- package/src/app.ts +22 -0
- package/src/trails/dev-clean.ts +73 -0
- package/src/trails/dev-reset.ts +44 -0
- package/src/trails/dev-stats.ts +64 -0
- package/src/trails/dev-support.ts +326 -0
- package/src/trails/draft-promote.ts +704 -0
- package/src/trails/guide.ts +22 -37
- package/src/trails/load-app.ts +76 -13
- package/src/trails/project.ts +17 -3
- package/src/trails/survey.ts +56 -256
- package/src/trails/topo-constants.ts +2 -0
- package/src/trails/topo-export.ts +39 -0
- package/src/trails/topo-history.ts +40 -0
- package/src/trails/topo-pin.ts +42 -0
- package/src/trails/topo-read-support.ts +332 -0
- package/src/trails/topo-reports.ts +221 -0
- package/src/trails/topo-show.ts +58 -0
- package/src/trails/topo-store-support.ts +96 -0
- package/src/trails/topo-support.ts +274 -0
- package/src/trails/topo-unpin.ts +51 -0
- package/src/trails/topo-verify.ts +29 -0
- package/src/trails/topo.ts +73 -0
- package/src/trails/warden.ts +1 -0
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
import { existsSync, renameSync, statSync } from 'node:fs';
|
|
2
|
+
import { basename, dirname, join, relative } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
Result,
|
|
6
|
+
ValidationError,
|
|
7
|
+
analyzeDraftState,
|
|
8
|
+
isDraftId,
|
|
9
|
+
trail,
|
|
10
|
+
} from '@ontrails/core';
|
|
11
|
+
import {
|
|
12
|
+
DRAFT_FILE_PREFIX,
|
|
13
|
+
findStringLiterals,
|
|
14
|
+
isDraftMarkedFile,
|
|
15
|
+
parse,
|
|
16
|
+
stripDraftFileMarkers,
|
|
17
|
+
} from '@ontrails/warden';
|
|
18
|
+
import { z } from 'zod';
|
|
19
|
+
|
|
20
|
+
import { loadApp } from './load-app.js';
|
|
21
|
+
import { findTopoPath } from './project.js';
|
|
22
|
+
|
|
23
|
+
interface PromotionEdit {
|
|
24
|
+
readonly end: number;
|
|
25
|
+
readonly replacement: string;
|
|
26
|
+
readonly start: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface FileRename {
|
|
30
|
+
readonly from: string;
|
|
31
|
+
readonly to: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const isManagedSourceFile = (match: string): boolean =>
|
|
35
|
+
!match.endsWith('.d.ts') &&
|
|
36
|
+
!match.startsWith('node_modules/') &&
|
|
37
|
+
!match.startsWith('dist/') &&
|
|
38
|
+
!match.startsWith('.git/');
|
|
39
|
+
|
|
40
|
+
const collectTsFiles = (rootDir: string): string[] => {
|
|
41
|
+
const files: string[] = [];
|
|
42
|
+
for (const match of new Bun.Glob('**/*.ts').scanSync({
|
|
43
|
+
cwd: rootDir,
|
|
44
|
+
dot: false,
|
|
45
|
+
onlyFiles: true,
|
|
46
|
+
})) {
|
|
47
|
+
if (isManagedSourceFile(match)) {
|
|
48
|
+
files.push(join(rootDir, match));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return files.toSorted();
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const applyEdits = (
|
|
55
|
+
sourceCode: string,
|
|
56
|
+
edits: readonly PromotionEdit[]
|
|
57
|
+
): string => {
|
|
58
|
+
let updated = sourceCode;
|
|
59
|
+
for (const edit of [...edits].toSorted((a, b) => b.start - a.start)) {
|
|
60
|
+
updated =
|
|
61
|
+
updated.slice(0, edit.start) + edit.replacement + updated.slice(edit.end);
|
|
62
|
+
}
|
|
63
|
+
return updated;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const literalQuote = (raw: string): '"' | "'" | '`' => {
|
|
67
|
+
const [first] = raw;
|
|
68
|
+
return first === '"' || first === '`' ? first : "'";
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const replaceQuotedLiteral = (
|
|
72
|
+
sourceCode: string,
|
|
73
|
+
start: number,
|
|
74
|
+
end: number,
|
|
75
|
+
nextValue: string
|
|
76
|
+
): string => {
|
|
77
|
+
const quote = literalQuote(sourceCode.slice(start, end));
|
|
78
|
+
return `${quote}${nextValue}${quote}`;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const replaceIdLiterals = (
|
|
82
|
+
sourceCode: string,
|
|
83
|
+
filePath: string,
|
|
84
|
+
fromId: string,
|
|
85
|
+
toId: string
|
|
86
|
+
): { readonly changed: boolean; readonly nextSource: string } => {
|
|
87
|
+
const ast = parse(filePath, sourceCode);
|
|
88
|
+
if (!ast) {
|
|
89
|
+
return { changed: false, nextSource: sourceCode };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const edits = findStringLiterals(ast, (value) => value === fromId).map(
|
|
93
|
+
(match) => ({
|
|
94
|
+
end: match.end,
|
|
95
|
+
replacement: replaceQuotedLiteral(
|
|
96
|
+
sourceCode,
|
|
97
|
+
match.start,
|
|
98
|
+
match.end,
|
|
99
|
+
toId
|
|
100
|
+
),
|
|
101
|
+
start: match.start,
|
|
102
|
+
})
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
if (edits.length === 0) {
|
|
106
|
+
return { changed: false, nextSource: sourceCode };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
changed: true,
|
|
111
|
+
nextSource: applyEdits(sourceCode, edits),
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const hasDraftIds = (sourceCode: string, filePath: string): boolean => {
|
|
116
|
+
const ast = parse(filePath, sourceCode);
|
|
117
|
+
if (!ast) {
|
|
118
|
+
return sourceCode.includes(DRAFT_FILE_PREFIX);
|
|
119
|
+
}
|
|
120
|
+
return findStringLiterals(ast, (value) => isDraftId(value)).length > 0;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const toJsPath = (filePath: string): string => filePath.replace(/\.ts$/, '.js');
|
|
124
|
+
|
|
125
|
+
const toRelativeModulePath = (fromFile: string, toFile: string): string => {
|
|
126
|
+
const rel = relative(dirname(fromFile), toJsPath(toFile)).replaceAll(
|
|
127
|
+
'\\',
|
|
128
|
+
'/'
|
|
129
|
+
);
|
|
130
|
+
return rel.startsWith('.') ? rel : `./${rel}`;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const replaceLiteralValue = (
|
|
134
|
+
sourceCode: string,
|
|
135
|
+
filePath: string,
|
|
136
|
+
currentValue: string,
|
|
137
|
+
nextValue: string
|
|
138
|
+
): { readonly changed: boolean; readonly nextSource: string } => {
|
|
139
|
+
const ast = parse(filePath, sourceCode);
|
|
140
|
+
if (!ast) {
|
|
141
|
+
return { changed: false, nextSource: sourceCode };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const edits = findStringLiterals(ast, (value) => value === currentValue).map(
|
|
145
|
+
(match) => ({
|
|
146
|
+
end: match.end,
|
|
147
|
+
replacement: replaceQuotedLiteral(
|
|
148
|
+
sourceCode,
|
|
149
|
+
match.start,
|
|
150
|
+
match.end,
|
|
151
|
+
nextValue
|
|
152
|
+
),
|
|
153
|
+
start: match.start,
|
|
154
|
+
})
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
if (edits.length === 0) {
|
|
158
|
+
return { changed: false, nextSource: sourceCode };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
changed: true,
|
|
163
|
+
nextSource: applyEdits(sourceCode, edits),
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const collectOutputId = (
|
|
168
|
+
app: Awaited<ReturnType<typeof loadApp>>,
|
|
169
|
+
id: string
|
|
170
|
+
) => app.get(id) ?? app.signals.get(id) ?? app.getProvision(id);
|
|
171
|
+
|
|
172
|
+
const toRelativeOutputPath = (rootDir: string, filePath: string): string =>
|
|
173
|
+
relative(rootDir, filePath).replaceAll('\\', '/');
|
|
174
|
+
|
|
175
|
+
const toProjectModulePath = (sourceImport: string): string =>
|
|
176
|
+
sourceImport.startsWith('./')
|
|
177
|
+
? `./src/${sourceImport.slice(2)}`
|
|
178
|
+
: sourceImport;
|
|
179
|
+
|
|
180
|
+
interface PromotionRewriteState {
|
|
181
|
+
readonly renames: FileRename[];
|
|
182
|
+
readonly updatedSourceFiles: Set<string>;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
interface PromotionLoadState {
|
|
186
|
+
readonly appModule: string | null;
|
|
187
|
+
readonly loadError: string | null;
|
|
188
|
+
readonly loadedApp: Awaited<ReturnType<typeof loadApp>> | undefined;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const validatePromotionInput = (input: {
|
|
192
|
+
readonly fromId: string;
|
|
193
|
+
readonly toId: string;
|
|
194
|
+
}): Result<void, ValidationError> => {
|
|
195
|
+
if (!isDraftId(input.fromId)) {
|
|
196
|
+
return Result.err(
|
|
197
|
+
new ValidationError(
|
|
198
|
+
`fromId must use the reserved draft prefix: "${input.fromId}"`
|
|
199
|
+
)
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (isDraftId(input.toId)) {
|
|
204
|
+
return Result.err(
|
|
205
|
+
new ValidationError(
|
|
206
|
+
`toId must be established, not draft: "${input.toId}"`
|
|
207
|
+
)
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return Result.ok();
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const validatePromotionRoot = (
|
|
215
|
+
rootDir: string
|
|
216
|
+
): Result<void, ValidationError> => {
|
|
217
|
+
if (!existsSync(rootDir)) {
|
|
218
|
+
return Result.err(
|
|
219
|
+
new ValidationError(`rootDir does not exist: "${rootDir}"`)
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (!statSync(rootDir).isDirectory()) {
|
|
224
|
+
return Result.err(
|
|
225
|
+
new ValidationError(`rootDir must be a directory: "${rootDir}"`)
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return Result.ok();
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const resolveValidatedPromotionRoot = (
|
|
233
|
+
input: {
|
|
234
|
+
readonly fromId: string;
|
|
235
|
+
readonly rootDir?: string | undefined;
|
|
236
|
+
readonly toId: string;
|
|
237
|
+
},
|
|
238
|
+
ctx: { readonly cwd?: string | undefined }
|
|
239
|
+
): Result<string, ValidationError> => {
|
|
240
|
+
const validation = validatePromotionInput(input);
|
|
241
|
+
if (validation.isErr()) {
|
|
242
|
+
return validation;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const rootDir = input.rootDir ?? ctx.cwd ?? process.cwd();
|
|
246
|
+
const rootValidation = validatePromotionRoot(rootDir);
|
|
247
|
+
if (rootValidation.isErr()) {
|
|
248
|
+
return rootValidation;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return Result.ok(rootDir);
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const rewritePromotedSourceFiles = async (
|
|
255
|
+
filePaths: readonly string[],
|
|
256
|
+
fromId: string,
|
|
257
|
+
toId: string,
|
|
258
|
+
updatedSourceFiles: Set<string>
|
|
259
|
+
): Promise<void> => {
|
|
260
|
+
for (const filePath of filePaths) {
|
|
261
|
+
const sourceCode = await Bun.file(filePath).text();
|
|
262
|
+
const replaced = replaceIdLiterals(sourceCode, filePath, fromId, toId);
|
|
263
|
+
if (!replaced.changed) {
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
await Bun.write(filePath, replaced.nextSource);
|
|
268
|
+
updatedSourceFiles.add(filePath);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const hasDraftIdsInFile = async (filePath: string): Promise<boolean> => {
|
|
273
|
+
const sourceCode = await Bun.file(filePath).text();
|
|
274
|
+
return hasDraftIds(sourceCode, filePath);
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const buildPromotableFileRename = (
|
|
278
|
+
filePath: string,
|
|
279
|
+
fileName: string
|
|
280
|
+
): Result<FileRename | null, Error> => {
|
|
281
|
+
const nextFileName = stripDraftFileMarkers(fileName);
|
|
282
|
+
if (nextFileName === fileName) {
|
|
283
|
+
return Result.ok(null);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const nextPath = join(dirname(filePath), nextFileName);
|
|
287
|
+
if (nextPath !== filePath && existsSync(nextPath)) {
|
|
288
|
+
return Result.err(
|
|
289
|
+
new ValidationError(
|
|
290
|
+
`Cannot promote draft file "${filePath}" because "${nextPath}" already exists.`
|
|
291
|
+
)
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return Result.ok({ from: filePath, to: nextPath });
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
const collectPromotableFileRename = async (
|
|
299
|
+
filePath: string
|
|
300
|
+
): Promise<Result<FileRename | null, Error>> => {
|
|
301
|
+
if (!isDraftMarkedFile(filePath)) {
|
|
302
|
+
return Result.ok(null);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const fileName = basename(filePath);
|
|
306
|
+
if (!fileName) {
|
|
307
|
+
return Result.ok(null);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (await hasDraftIdsInFile(filePath)) {
|
|
311
|
+
return Result.ok(null);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return buildPromotableFileRename(filePath, fileName);
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
/** Validate that no two renames target the same path and no target already exists. */
|
|
318
|
+
const validateRenameTargets = (
|
|
319
|
+
renames: readonly FileRename[]
|
|
320
|
+
): Result<void, Error> => {
|
|
321
|
+
const targets = new Set<string>();
|
|
322
|
+
for (const r of renames) {
|
|
323
|
+
if (targets.has(r.to)) {
|
|
324
|
+
return Result.err(
|
|
325
|
+
new ValidationError(
|
|
326
|
+
`Duplicate rename target "${r.to}" — multiple draft files would be renamed to the same path`
|
|
327
|
+
)
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
if (existsSync(r.to)) {
|
|
331
|
+
return Result.err(
|
|
332
|
+
new ValidationError(
|
|
333
|
+
`Rename target "${r.to}" already exists — cannot overwrite`
|
|
334
|
+
)
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
targets.add(r.to);
|
|
338
|
+
}
|
|
339
|
+
return Result.ok();
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
const collectFileRenames = async (
|
|
343
|
+
filePaths: readonly string[]
|
|
344
|
+
): Promise<Result<FileRename[], Error>> => {
|
|
345
|
+
const renames: FileRename[] = [];
|
|
346
|
+
for (const filePath of filePaths) {
|
|
347
|
+
const renameResult = await collectPromotableFileRename(filePath);
|
|
348
|
+
if (renameResult.isErr()) {
|
|
349
|
+
return renameResult;
|
|
350
|
+
}
|
|
351
|
+
if (renameResult.value !== null) {
|
|
352
|
+
renames.push(renameResult.value);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return Result.ok(renames);
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const collectAndApplyFileRenames = async (
|
|
359
|
+
filePaths: readonly string[]
|
|
360
|
+
): Promise<Result<FileRename[], Error>> => {
|
|
361
|
+
const collected = await collectFileRenames(filePaths);
|
|
362
|
+
if (collected.isErr()) {
|
|
363
|
+
return collected;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const renames = collected.value;
|
|
367
|
+
const valid = validateRenameTargets(renames);
|
|
368
|
+
if (valid.isErr()) {
|
|
369
|
+
return valid;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
for (const r of renames) {
|
|
373
|
+
renameSync(r.from, r.to);
|
|
374
|
+
}
|
|
375
|
+
return Result.ok(renames);
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const applyRenameEffects = (
|
|
379
|
+
updatedSourceFiles: Set<string>,
|
|
380
|
+
renames: readonly FileRename[]
|
|
381
|
+
): void => {
|
|
382
|
+
for (const rename of renames) {
|
|
383
|
+
if (updatedSourceFiles.delete(rename.from)) {
|
|
384
|
+
updatedSourceFiles.add(rename.to);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const applyRelativeImportRename = (
|
|
390
|
+
sourceCode: string,
|
|
391
|
+
filePath: string,
|
|
392
|
+
rename: FileRename
|
|
393
|
+
): { readonly changed: boolean; readonly sourceCode: string } => {
|
|
394
|
+
const currentValue = toRelativeModulePath(filePath, rename.from);
|
|
395
|
+
const nextValue = toRelativeModulePath(filePath, rename.to);
|
|
396
|
+
if (currentValue === nextValue) {
|
|
397
|
+
return { changed: false, sourceCode };
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const replaced = replaceLiteralValue(
|
|
401
|
+
sourceCode,
|
|
402
|
+
filePath,
|
|
403
|
+
currentValue,
|
|
404
|
+
nextValue
|
|
405
|
+
);
|
|
406
|
+
if (!replaced.changed) {
|
|
407
|
+
return { changed: false, sourceCode };
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
return { changed: true, sourceCode: replaced.nextSource };
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
const rewriteRelativeImportsForFile = (
|
|
414
|
+
filePath: string,
|
|
415
|
+
renames: readonly FileRename[],
|
|
416
|
+
sourceCode: string
|
|
417
|
+
): { readonly changed: boolean; readonly sourceCode: string } => {
|
|
418
|
+
let nextSourceCode = sourceCode;
|
|
419
|
+
let changed = false;
|
|
420
|
+
|
|
421
|
+
for (const rename of renames) {
|
|
422
|
+
const updated = applyRelativeImportRename(nextSourceCode, filePath, rename);
|
|
423
|
+
if (!updated.changed) {
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
nextSourceCode = updated.sourceCode;
|
|
428
|
+
changed = true;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
return { changed, sourceCode: nextSourceCode };
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
const updateRelativeImportsForFile = async (
|
|
435
|
+
filePath: string,
|
|
436
|
+
renames: readonly FileRename[]
|
|
437
|
+
): Promise<boolean> => {
|
|
438
|
+
const sourceCode = await Bun.file(filePath).text();
|
|
439
|
+
const updated = rewriteRelativeImportsForFile(filePath, renames, sourceCode);
|
|
440
|
+
if (updated.changed) {
|
|
441
|
+
await Bun.write(filePath, updated.sourceCode);
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
return false;
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
const updateRelativeImports = async (
|
|
449
|
+
filePaths: readonly string[],
|
|
450
|
+
renames: readonly FileRename[]
|
|
451
|
+
): Promise<string[]> => {
|
|
452
|
+
const updatedFiles = new Set<string>();
|
|
453
|
+
|
|
454
|
+
for (const filePath of filePaths) {
|
|
455
|
+
const changed = await updateRelativeImportsForFile(filePath, renames);
|
|
456
|
+
if (changed) {
|
|
457
|
+
updatedFiles.add(filePath);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return [...updatedFiles].toSorted();
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
const rewritePromotionState = async (
|
|
465
|
+
rootDir: string,
|
|
466
|
+
input: {
|
|
467
|
+
readonly fromId: string;
|
|
468
|
+
readonly renameFiles: boolean;
|
|
469
|
+
readonly toId: string;
|
|
470
|
+
}
|
|
471
|
+
): Promise<Result<PromotionRewriteState, Error>> => {
|
|
472
|
+
const initialFiles = collectTsFiles(rootDir);
|
|
473
|
+
const updatedSourceFiles = new Set<string>();
|
|
474
|
+
|
|
475
|
+
await rewritePromotedSourceFiles(
|
|
476
|
+
initialFiles,
|
|
477
|
+
input.fromId,
|
|
478
|
+
input.toId,
|
|
479
|
+
updatedSourceFiles
|
|
480
|
+
);
|
|
481
|
+
|
|
482
|
+
const renamesResult = input.renameFiles
|
|
483
|
+
? await collectAndApplyFileRenames(initialFiles)
|
|
484
|
+
: Result.ok([] as FileRename[]);
|
|
485
|
+
if (renamesResult.isErr()) {
|
|
486
|
+
return Result.err(renamesResult.error);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
applyRenameEffects(updatedSourceFiles, renamesResult.value);
|
|
490
|
+
for (const f of await updateRelativeImports(
|
|
491
|
+
collectTsFiles(rootDir),
|
|
492
|
+
renamesResult.value
|
|
493
|
+
)) {
|
|
494
|
+
updatedSourceFiles.add(f);
|
|
495
|
+
}
|
|
496
|
+
return Result.ok({ renames: renamesResult.value, updatedSourceFiles });
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
const resolvePromotionAppModule = async (
|
|
500
|
+
input: {
|
|
501
|
+
readonly appModule?: string | undefined;
|
|
502
|
+
},
|
|
503
|
+
rootDir: string
|
|
504
|
+
): Promise<string | null> => {
|
|
505
|
+
const discoveredAppModule = await findTopoPath(rootDir);
|
|
506
|
+
return (
|
|
507
|
+
input.appModule ??
|
|
508
|
+
(discoveredAppModule === null
|
|
509
|
+
? null
|
|
510
|
+
: toProjectModulePath(discoveredAppModule))
|
|
511
|
+
);
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
const loadVerifiedApp = async (
|
|
515
|
+
appModule: string | null,
|
|
516
|
+
rootDir: string
|
|
517
|
+
): Promise<PromotionLoadState> => {
|
|
518
|
+
if (appModule === null) {
|
|
519
|
+
return { appModule, loadError: null, loadedApp: undefined };
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
let loadError: string | null = null;
|
|
523
|
+
const loadedApp = await loadApp(appModule, rootDir, { fresh: true }).catch(
|
|
524
|
+
(error: unknown): undefined => {
|
|
525
|
+
loadError = error instanceof Error ? error.message : String(error);
|
|
526
|
+
return undefined;
|
|
527
|
+
}
|
|
528
|
+
);
|
|
529
|
+
|
|
530
|
+
return { appModule, loadError, loadedApp };
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
const toRenamedFiles = (rootDir: string, renames: readonly FileRename[]) =>
|
|
534
|
+
renames.map((rename) => ({
|
|
535
|
+
from: toRelativeOutputPath(rootDir, rename.from),
|
|
536
|
+
to: toRelativeOutputPath(rootDir, rename.to),
|
|
537
|
+
}));
|
|
538
|
+
|
|
539
|
+
const toUpdatedFiles = (rootDir: string, updatedSourceFiles: Set<string>) =>
|
|
540
|
+
[...updatedSourceFiles]
|
|
541
|
+
.toSorted()
|
|
542
|
+
.map((filePath) => toRelativeOutputPath(rootDir, filePath));
|
|
543
|
+
|
|
544
|
+
const buildUnverifiedPromotionResult = (
|
|
545
|
+
rootDir: string,
|
|
546
|
+
loadError: string | null,
|
|
547
|
+
renames: readonly FileRename[],
|
|
548
|
+
updatedSourceFiles: Set<string>,
|
|
549
|
+
appModule: string | null
|
|
550
|
+
) =>
|
|
551
|
+
Result.ok({
|
|
552
|
+
appModule,
|
|
553
|
+
message:
|
|
554
|
+
loadError === null
|
|
555
|
+
? 'Promotion rewrote source files, but no topo entrypoint could be loaded for verification.'
|
|
556
|
+
: `Promotion rewrote source files, but verification failed: ${loadError}`,
|
|
557
|
+
promotedEstablished: false,
|
|
558
|
+
remainingDraftIds: [],
|
|
559
|
+
renamedFiles: toRenamedFiles(rootDir, renames),
|
|
560
|
+
updatedFiles: toUpdatedFiles(rootDir, updatedSourceFiles),
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
const buildVerifiedPromotionResult = (
|
|
564
|
+
rootDir: string,
|
|
565
|
+
analysis: ReturnType<typeof analyzeDraftState>,
|
|
566
|
+
promotedEstablished: boolean,
|
|
567
|
+
renames: readonly FileRename[],
|
|
568
|
+
updatedSourceFiles: Set<string>,
|
|
569
|
+
appModule: string | null,
|
|
570
|
+
toId: string
|
|
571
|
+
) => {
|
|
572
|
+
const blockingFinding = analysis.findings.find(
|
|
573
|
+
(finding) => finding.id === toId
|
|
574
|
+
);
|
|
575
|
+
|
|
576
|
+
return Result.ok({
|
|
577
|
+
appModule,
|
|
578
|
+
message:
|
|
579
|
+
blockingFinding?.message ??
|
|
580
|
+
(promotedEstablished
|
|
581
|
+
? `Promoted "${toId}" is now established.`
|
|
582
|
+
: `Promoted "${toId}" could not be verified as established.`),
|
|
583
|
+
promotedEstablished,
|
|
584
|
+
remainingDraftIds: [...analysis.declaredDraftIds].toSorted(),
|
|
585
|
+
renamedFiles: toRenamedFiles(rootDir, renames),
|
|
586
|
+
updatedFiles: toUpdatedFiles(rootDir, updatedSourceFiles),
|
|
587
|
+
});
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
const buildVerifiedPromotionResultFromApp = (
|
|
591
|
+
rootDir: string,
|
|
592
|
+
loadedApp: Awaited<ReturnType<typeof loadApp>>,
|
|
593
|
+
renames: readonly FileRename[],
|
|
594
|
+
updatedSourceFiles: Set<string>,
|
|
595
|
+
appModule: string | null,
|
|
596
|
+
toId: string
|
|
597
|
+
) => {
|
|
598
|
+
const analysis = analyzeDraftState(loadedApp);
|
|
599
|
+
const promotedNode = collectOutputId(loadedApp, toId);
|
|
600
|
+
const promotedEstablished =
|
|
601
|
+
promotedNode !== undefined && !analysis.contaminatedIds.has(toId);
|
|
602
|
+
|
|
603
|
+
return buildVerifiedPromotionResult(
|
|
604
|
+
rootDir,
|
|
605
|
+
analysis,
|
|
606
|
+
promotedEstablished,
|
|
607
|
+
renames,
|
|
608
|
+
updatedSourceFiles,
|
|
609
|
+
appModule,
|
|
610
|
+
toId
|
|
611
|
+
);
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
const promoteDraftState = async (
|
|
615
|
+
input: {
|
|
616
|
+
readonly appModule?: string | undefined;
|
|
617
|
+
readonly fromId: string;
|
|
618
|
+
readonly renameFiles: boolean;
|
|
619
|
+
readonly rootDir?: string | undefined;
|
|
620
|
+
readonly toId: string;
|
|
621
|
+
},
|
|
622
|
+
ctx: { readonly cwd?: string | undefined }
|
|
623
|
+
) => {
|
|
624
|
+
const rootDirResult = resolveValidatedPromotionRoot(input, ctx);
|
|
625
|
+
if (rootDirResult.isErr()) {
|
|
626
|
+
return rootDirResult;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
const rewriteResult = await rewritePromotionState(rootDirResult.value, input);
|
|
630
|
+
if (rewriteResult.isErr()) {
|
|
631
|
+
return Result.err(rewriteResult.error);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
const { renames, updatedSourceFiles } = rewriteResult.value;
|
|
635
|
+
const appModule = await resolvePromotionAppModule(input, rootDirResult.value);
|
|
636
|
+
const { loadError, loadedApp } = await loadVerifiedApp(
|
|
637
|
+
appModule,
|
|
638
|
+
rootDirResult.value
|
|
639
|
+
);
|
|
640
|
+
|
|
641
|
+
return loadedApp
|
|
642
|
+
? buildVerifiedPromotionResultFromApp(
|
|
643
|
+
rootDirResult.value,
|
|
644
|
+
loadedApp,
|
|
645
|
+
renames,
|
|
646
|
+
updatedSourceFiles,
|
|
647
|
+
appModule,
|
|
648
|
+
input.toId
|
|
649
|
+
)
|
|
650
|
+
: buildUnverifiedPromotionResult(
|
|
651
|
+
rootDirResult.value,
|
|
652
|
+
loadError,
|
|
653
|
+
renames,
|
|
654
|
+
updatedSourceFiles,
|
|
655
|
+
appModule
|
|
656
|
+
);
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
export const draftPromoteTrail = trail('draft.promote', {
|
|
660
|
+
blaze: promoteDraftState,
|
|
661
|
+
description:
|
|
662
|
+
'Promote a draft id to an established id, rewrite inbound references, and verify the result against a fresh topo load.',
|
|
663
|
+
examples: [
|
|
664
|
+
{
|
|
665
|
+
error: 'ValidationError',
|
|
666
|
+
input: {
|
|
667
|
+
fromId: '_draft.entity.prepare',
|
|
668
|
+
renameFiles: true,
|
|
669
|
+
rootDir: './__does_not_exist__/draft-promote-example',
|
|
670
|
+
toId: 'entity.prepare',
|
|
671
|
+
},
|
|
672
|
+
name: 'Rejects a missing project root before any rewrite begins',
|
|
673
|
+
},
|
|
674
|
+
],
|
|
675
|
+
input: z.object({
|
|
676
|
+
appModule: z
|
|
677
|
+
.string()
|
|
678
|
+
.optional()
|
|
679
|
+
.describe('Optional app module to verify after promotion'),
|
|
680
|
+
fromId: z.string().describe('Draft id to promote'),
|
|
681
|
+
renameFiles: z
|
|
682
|
+
.boolean()
|
|
683
|
+
.default(true)
|
|
684
|
+
.describe('Rename draft-marked files that no longer contain draft ids'),
|
|
685
|
+
rootDir: z.string().optional().describe('Project root directory'),
|
|
686
|
+
toId: z
|
|
687
|
+
.string()
|
|
688
|
+
.describe('Established id to write in place of the draft id'),
|
|
689
|
+
}),
|
|
690
|
+
intent: 'write',
|
|
691
|
+
output: z.object({
|
|
692
|
+
appModule: z.string().nullable(),
|
|
693
|
+
message: z.string(),
|
|
694
|
+
promotedEstablished: z.boolean(),
|
|
695
|
+
remainingDraftIds: z.array(z.string()),
|
|
696
|
+
renamedFiles: z.array(
|
|
697
|
+
z.object({
|
|
698
|
+
from: z.string(),
|
|
699
|
+
to: z.string(),
|
|
700
|
+
})
|
|
701
|
+
),
|
|
702
|
+
updatedFiles: z.array(z.string()),
|
|
703
|
+
}),
|
|
704
|
+
});
|