@latticecms/cli 0.0.0 → 0.1.1
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 +138 -2
- package/dist/bin.d.ts +3 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +12 -0
- package/dist/bin.js.map +1 -0
- package/dist/changes.d.ts +22 -0
- package/dist/changes.d.ts.map +1 -0
- package/dist/changes.js +118 -0
- package/dist/changes.js.map +1 -0
- package/dist/collection.d.ts +5 -0
- package/dist/collection.d.ts.map +1 -0
- package/dist/collection.js +166 -0
- package/dist/collection.js.map +1 -0
- package/dist/config.d.ts +37 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +111 -0
- package/dist/config.js.map +1 -0
- package/dist/diff.d.ts +6 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +83 -0
- package/dist/diff.js.map +1 -0
- package/dist/edit.d.ts +105 -0
- package/dist/edit.d.ts.map +1 -0
- package/dist/edit.js +571 -0
- package/dist/edit.js.map +1 -0
- package/dist/exit.d.ts +13 -0
- package/dist/exit.d.ts.map +1 -0
- package/dist/exit.js +19 -0
- package/dist/exit.js.map +1 -0
- package/dist/layout.d.ts +20 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +59 -0
- package/dist/layout.js.map +1 -0
- package/dist/main.d.ts +7 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +58 -0
- package/dist/main.js.map +1 -0
- package/dist/migrate.d.ts +9 -0
- package/dist/migrate.d.ts.map +1 -0
- package/dist/migrate.js +120 -0
- package/dist/migrate.js.map +1 -0
- package/dist/migration.d.ts +5 -0
- package/dist/migration.d.ts.map +1 -0
- package/dist/migration.js +170 -0
- package/dist/migration.js.map +1 -0
- package/dist/names.d.ts +15 -0
- package/dist/names.d.ts.map +1 -0
- package/dist/names.js +32 -0
- package/dist/names.js.map +1 -0
- package/dist/templates.d.ts +21 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +73 -0
- package/dist/templates.js.map +1 -0
- package/dist/typescript.d.ts +9 -0
- package/dist/typescript.d.ts.map +1 -0
- package/dist/typescript.js +60 -0
- package/dist/typescript.js.map +1 -0
- package/package.json +61 -2
- package/src/bin.ts +12 -0
- package/src/changes.ts +170 -0
- package/src/collection.ts +228 -0
- package/src/config.ts +142 -0
- package/src/diff.ts +90 -0
- package/src/edit.ts +812 -0
- package/src/exit.ts +19 -0
- package/src/layout.ts +91 -0
- package/src/main.ts +71 -0
- package/src/migrate.ts +155 -0
- package/src/migration.ts +239 -0
- package/src/names.ts +48 -0
- package/src/templates.ts +104 -0
- package/src/typescript.ts +79 -0
package/src/edit.ts
ADDED
|
@@ -0,0 +1,812 @@
|
|
|
1
|
+
import type * as TS from 'typescript';
|
|
2
|
+
import { Exit } from './exit.js';
|
|
3
|
+
import { call, entry } from './layout.js';
|
|
4
|
+
import type { Names } from './names.js';
|
|
5
|
+
import type { TypeScript } from './typescript.js';
|
|
6
|
+
|
|
7
|
+
/** Text to insert at an offset of a file, in place of `remove` characters there. */
|
|
8
|
+
export type Insert = {
|
|
9
|
+
readonly offset: number;
|
|
10
|
+
readonly text: string;
|
|
11
|
+
readonly remove?: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Lines a command adds to a file, where they go in words, and the inserts
|
|
16
|
+
* that put them there. A refused file has no inserts.
|
|
17
|
+
*/
|
|
18
|
+
export type Addition = {
|
|
19
|
+
readonly where: string;
|
|
20
|
+
readonly lines: string;
|
|
21
|
+
readonly inserts: readonly Insert[];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** The additions to one file, or why its shape gives them no place. */
|
|
25
|
+
export type FileEdit = {
|
|
26
|
+
readonly path: string;
|
|
27
|
+
readonly before: string;
|
|
28
|
+
readonly additions: readonly Addition[];
|
|
29
|
+
readonly refused: string | undefined;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* `text` with each insert applied. Inserts never overlap; two at one offset
|
|
34
|
+
* land in the order given.
|
|
35
|
+
*/
|
|
36
|
+
export function applyInserts(text: string, inserts: readonly Insert[]): string {
|
|
37
|
+
return inserts
|
|
38
|
+
.map((insert, order) => ({ insert, order }))
|
|
39
|
+
.sort((a, b) => b.insert.offset - a.insert.offset || b.order - a.order)
|
|
40
|
+
.reduce(
|
|
41
|
+
(out, { insert }) =>
|
|
42
|
+
`${out.slice(0, insert.offset)}${insert.text}${out.slice(insert.offset + (insert.remove ?? 0))}`,
|
|
43
|
+
text,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function parse(ts: TypeScript, path: string, text: string) {
|
|
48
|
+
return ts.createSourceFile(
|
|
49
|
+
path,
|
|
50
|
+
text,
|
|
51
|
+
ts.ScriptTarget.Latest,
|
|
52
|
+
true,
|
|
53
|
+
/\.[cm]?tsx$/.test(path) ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** How a file writes line breaks and one level of indent. */
|
|
58
|
+
export type Style = { readonly newline: string; readonly unit: string };
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The file's line break, and its indent unit: a tab, or the fewest leading
|
|
62
|
+
* spaces on a code line. JSDoc's ` * ` lines do not count.
|
|
63
|
+
*/
|
|
64
|
+
export function styleOf(text: string): Style {
|
|
65
|
+
const lines = text.split(/\r?\n/);
|
|
66
|
+
const spaces = lines.flatMap((line) => {
|
|
67
|
+
const lead = /^( +)[^ *]/.exec(line)?.[1];
|
|
68
|
+
return lead === undefined ? [] : [lead.length];
|
|
69
|
+
});
|
|
70
|
+
const unit = lines.some((line) => /^\t+\S/.test(line))
|
|
71
|
+
? '\t'
|
|
72
|
+
: ' '.repeat(spaces.length === 0 ? 2 : Math.min(...spaces));
|
|
73
|
+
return { newline: text.includes('\r\n') ? '\r\n' : '\n', unit };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The additions with their inserted text in the file's line breaks. */
|
|
77
|
+
export function inStyle(additions: readonly Addition[], { newline }: Style) {
|
|
78
|
+
return additions.map((addition) => ({
|
|
79
|
+
...addition,
|
|
80
|
+
inserts: addition.inserts.map((insert) => ({
|
|
81
|
+
...insert,
|
|
82
|
+
text: insert.text.replaceAll('\n', newline),
|
|
83
|
+
})),
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The whitespace that starts the line holding `position`. */
|
|
88
|
+
export function indentAt(source: TS.SourceFile, position: number): string {
|
|
89
|
+
const { line } = source.getLineAndCharacterOfPosition(position);
|
|
90
|
+
const start = source.getPositionOfLineAndCharacter(line, 0);
|
|
91
|
+
return /^[ \t]*/.exec(source.text.slice(start))?.[0] ?? '';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const lineOf = (source: TS.SourceFile, position: number) =>
|
|
95
|
+
source.getLineAndCharacterOfPosition(position).line;
|
|
96
|
+
|
|
97
|
+
/** `position`, moved past the comments that end its line, so they stay where they are. */
|
|
98
|
+
export function pastComments(
|
|
99
|
+
ts: TypeScript,
|
|
100
|
+
text: string,
|
|
101
|
+
position: number,
|
|
102
|
+
): number {
|
|
103
|
+
return ts.getTrailingCommentRanges(text, position)?.at(-1)?.end ?? position;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The end of the comma token at `position`, skipping trivia, if there is one. */
|
|
107
|
+
export function commaAfter(
|
|
108
|
+
ts: TypeScript,
|
|
109
|
+
text: string,
|
|
110
|
+
position: number,
|
|
111
|
+
): number | undefined {
|
|
112
|
+
const scanner = ts.createScanner(
|
|
113
|
+
ts.ScriptTarget.Latest,
|
|
114
|
+
true,
|
|
115
|
+
ts.LanguageVariant.Standard,
|
|
116
|
+
text,
|
|
117
|
+
);
|
|
118
|
+
scanner.setText(text, position);
|
|
119
|
+
return scanner.scan() === ts.SyntaxKind.CommaToken
|
|
120
|
+
? scanner.getTokenEnd()
|
|
121
|
+
: undefined;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Every call of the identifier `name` in the file. */
|
|
125
|
+
function callsOf(
|
|
126
|
+
ts: TypeScript,
|
|
127
|
+
source: TS.SourceFile,
|
|
128
|
+
name: string,
|
|
129
|
+
): TS.CallExpression[] {
|
|
130
|
+
const calls: TS.CallExpression[] = [];
|
|
131
|
+
const visit = (node: TS.Node): void => {
|
|
132
|
+
if (
|
|
133
|
+
ts.isCallExpression(node) &&
|
|
134
|
+
ts.isIdentifier(node.expression) &&
|
|
135
|
+
node.expression.text === name
|
|
136
|
+
) {
|
|
137
|
+
calls.push(node);
|
|
138
|
+
}
|
|
139
|
+
ts.forEachChild(node, visit);
|
|
140
|
+
};
|
|
141
|
+
visit(source);
|
|
142
|
+
return calls;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The local name the file imports `name` from `module` as. */
|
|
146
|
+
export function importedAs(
|
|
147
|
+
ts: TypeScript,
|
|
148
|
+
source: TS.SourceFile,
|
|
149
|
+
name: string,
|
|
150
|
+
module: string,
|
|
151
|
+
): string | undefined {
|
|
152
|
+
for (const statement of source.statements) {
|
|
153
|
+
if (
|
|
154
|
+
!ts.isImportDeclaration(statement) ||
|
|
155
|
+
!ts.isStringLiteral(statement.moduleSpecifier) ||
|
|
156
|
+
statement.moduleSpecifier.text !== module
|
|
157
|
+
) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const bindings = statement.importClause?.namedBindings;
|
|
161
|
+
if (bindings === undefined || !ts.isNamedImports(bindings)) continue;
|
|
162
|
+
for (const element of bindings.elements) {
|
|
163
|
+
if ((element.propertyName ?? element.name).text === name) {
|
|
164
|
+
return element.name.text;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The names a binding declares: `a`, `{ a, b: c }` and `[a, ...b]`. */
|
|
172
|
+
function bindingNames(ts: TypeScript, name: TS.BindingName): string[] {
|
|
173
|
+
if (ts.isIdentifier(name)) return [name.text];
|
|
174
|
+
return name.elements.flatMap((element) =>
|
|
175
|
+
ts.isBindingElement(element) ? bindingNames(ts, element.name) : [],
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** The names the file imports, and the names it declares at the top level. */
|
|
180
|
+
function topLevelNames(ts: TypeScript, source: TS.SourceFile) {
|
|
181
|
+
const imported = new Set<string>();
|
|
182
|
+
const declared = new Set<string>();
|
|
183
|
+
for (const statement of source.statements) {
|
|
184
|
+
if (ts.isImportDeclaration(statement)) {
|
|
185
|
+
const clause = statement.importClause;
|
|
186
|
+
const bindings = clause?.namedBindings;
|
|
187
|
+
if (clause?.name !== undefined) imported.add(clause.name.text);
|
|
188
|
+
if (bindings !== undefined && ts.isNamespaceImport(bindings)) {
|
|
189
|
+
imported.add(bindings.name.text);
|
|
190
|
+
}
|
|
191
|
+
if (bindings !== undefined && ts.isNamedImports(bindings)) {
|
|
192
|
+
for (const element of bindings.elements) {
|
|
193
|
+
imported.add(element.name.text);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
} else if (ts.isImportEqualsDeclaration(statement)) {
|
|
197
|
+
imported.add(statement.name.text);
|
|
198
|
+
} else if (ts.isVariableStatement(statement)) {
|
|
199
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
200
|
+
for (const name of bindingNames(ts, declaration.name)) {
|
|
201
|
+
declared.add(name);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
} else if (
|
|
205
|
+
(ts.isFunctionDeclaration(statement) ||
|
|
206
|
+
ts.isClassDeclaration(statement) ||
|
|
207
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
208
|
+
ts.isTypeAliasDeclaration(statement) ||
|
|
209
|
+
ts.isEnumDeclaration(statement) ||
|
|
210
|
+
ts.isModuleDeclaration(statement)) &&
|
|
211
|
+
statement.name !== undefined &&
|
|
212
|
+
ts.isIdentifier(statement.name)
|
|
213
|
+
) {
|
|
214
|
+
declared.add(statement.name.text);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return { imported, declared };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** A relative specifier's distance up, then its path, compared as numbers where digits run. */
|
|
221
|
+
export function compareSpecifiers(a: string, b: string): number {
|
|
222
|
+
const depth = (s: string) => s.match(/\.\.\//g)?.length ?? 0;
|
|
223
|
+
if (depth(a) !== depth(b)) return depth(b) - depth(a);
|
|
224
|
+
const rest = (s: string) => s.replace(/^(\.\.?\/)+/, '').split(/(\d+)/);
|
|
225
|
+
const left = rest(a);
|
|
226
|
+
const right = rest(b);
|
|
227
|
+
for (let i = 0; i < Math.min(left.length, right.length); i++) {
|
|
228
|
+
const x = left[i] ?? '';
|
|
229
|
+
const y = right[i] ?? '';
|
|
230
|
+
if (x === y) continue;
|
|
231
|
+
if (i % 2 === 1) return Number(x) - Number(y);
|
|
232
|
+
return x < y ? -1 : 1;
|
|
233
|
+
}
|
|
234
|
+
return left.length - right.length;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* An import of `name` from the relative `specifier`, among the file's
|
|
239
|
+
* imports in Biome's order: farther paths first, then by path. `clause` is
|
|
240
|
+
* what the import binds, such as `{ Notes }` or `m0002`. A file that
|
|
241
|
+
* already imports or declares `name` is refused.
|
|
242
|
+
*/
|
|
243
|
+
export function importAddition(
|
|
244
|
+
ts: TypeScript,
|
|
245
|
+
source: TS.SourceFile,
|
|
246
|
+
file: string,
|
|
247
|
+
name: string,
|
|
248
|
+
clause: string,
|
|
249
|
+
specifier: string,
|
|
250
|
+
): Addition {
|
|
251
|
+
const { imported, declared } = topLevelNames(ts, source);
|
|
252
|
+
if (imported.has(name)) throw new Exit(`${file} already imports ${name}`, 1);
|
|
253
|
+
if (declared.has(name)) throw new Exit(`${file} already declares ${name}`, 1);
|
|
254
|
+
const line = `import ${clause} from '${specifier}';`;
|
|
255
|
+
const where = 'with the imports';
|
|
256
|
+
const imports = source.statements.filter(ts.isImportDeclaration);
|
|
257
|
+
const next = imports.findIndex(
|
|
258
|
+
(statement) =>
|
|
259
|
+
ts.isStringLiteral(statement.moduleSpecifier) &&
|
|
260
|
+
statement.moduleSpecifier.text.startsWith('.') &&
|
|
261
|
+
compareSpecifiers(statement.moduleSpecifier.text, specifier) > 0,
|
|
262
|
+
);
|
|
263
|
+
const previous = imports[next === -1 ? imports.length - 1 : next - 1];
|
|
264
|
+
const first = imports[0];
|
|
265
|
+
const insert: Insert | undefined =
|
|
266
|
+
previous !== undefined
|
|
267
|
+
? {
|
|
268
|
+
offset: pastComments(ts, source.text, previous.getEnd()),
|
|
269
|
+
text: `\n${line}`,
|
|
270
|
+
}
|
|
271
|
+
: first !== undefined
|
|
272
|
+
? { offset: first.getStart(source), text: `${line}\n` }
|
|
273
|
+
: // A file with no imports has no framework() or EventCatalog()
|
|
274
|
+
// from a package, so it is refused and this addition is printed.
|
|
275
|
+
undefined;
|
|
276
|
+
return { where, lines: line, inserts: insert === undefined ? [] : [insert] };
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Whether the file declares `anyone` at the top level. */
|
|
280
|
+
function declaresAnyone(ts: TypeScript, source: TS.SourceFile): boolean {
|
|
281
|
+
return topLevelNames(ts, source).declared.has('anyone');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* The composition's edit: an import, and a `.collection()` call at the end
|
|
286
|
+
* of the one `framework()` chain, open to any signed-in caller. Its shape
|
|
287
|
+
* must be `export const app = framework(...).collection(...)...`; `app` is
|
|
288
|
+
* the export a test reads the contract from.
|
|
289
|
+
*/
|
|
290
|
+
export function compositionEdit(
|
|
291
|
+
ts: TypeScript,
|
|
292
|
+
path: string,
|
|
293
|
+
file: string,
|
|
294
|
+
before: string,
|
|
295
|
+
names: Names,
|
|
296
|
+
specifier: string,
|
|
297
|
+
): FileEdit & { readonly app: string | undefined } {
|
|
298
|
+
const source = parse(ts, path, before);
|
|
299
|
+
const style = styleOf(before);
|
|
300
|
+
const { unit } = style;
|
|
301
|
+
const imported = importAddition(
|
|
302
|
+
ts,
|
|
303
|
+
source,
|
|
304
|
+
file,
|
|
305
|
+
names.exportName,
|
|
306
|
+
`{ ${names.exportName} }`,
|
|
307
|
+
specifier,
|
|
308
|
+
);
|
|
309
|
+
const open = declaresAnyone(ts, source);
|
|
310
|
+
const callLines = (indent: string) =>
|
|
311
|
+
call(
|
|
312
|
+
indent,
|
|
313
|
+
'.collection',
|
|
314
|
+
[
|
|
315
|
+
names.exportName,
|
|
316
|
+
{
|
|
317
|
+
expanded: true,
|
|
318
|
+
lines: (inner) =>
|
|
319
|
+
open
|
|
320
|
+
? [
|
|
321
|
+
`${inner}access: { read: anyone, create: anyone, update: anyone, delete: anyone },`,
|
|
322
|
+
]
|
|
323
|
+
: [
|
|
324
|
+
`${inner}access: {`,
|
|
325
|
+
...['read', 'create', 'update', 'delete'].map(
|
|
326
|
+
(action) => `${inner}${unit}${action}: () => true,`,
|
|
327
|
+
),
|
|
328
|
+
`${inner}},`,
|
|
329
|
+
],
|
|
330
|
+
},
|
|
331
|
+
],
|
|
332
|
+
'',
|
|
333
|
+
unit,
|
|
334
|
+
).join('\n');
|
|
335
|
+
const where = 'at the end of the framework() chain';
|
|
336
|
+
const refuse = (why: string) => ({
|
|
337
|
+
path,
|
|
338
|
+
before,
|
|
339
|
+
additions: [
|
|
340
|
+
{ ...imported, inserts: [] },
|
|
341
|
+
{ where, lines: callLines(unit), inserts: [] },
|
|
342
|
+
],
|
|
343
|
+
refused: why,
|
|
344
|
+
app: undefined,
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
const local = importedAs(ts, source, 'framework', '@latticecms/core');
|
|
348
|
+
const calls = local === undefined ? [] : callsOf(ts, source, local);
|
|
349
|
+
const [call0] = calls;
|
|
350
|
+
if (call0 === undefined || calls.length > 1) {
|
|
351
|
+
return refuse(
|
|
352
|
+
calls.length === 0
|
|
353
|
+
? `${file} has no framework() chain`
|
|
354
|
+
: `${file} has ${calls.length} framework() chains`,
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
let top: TS.Expression = call0;
|
|
358
|
+
let lastLink: TS.MemberName | undefined;
|
|
359
|
+
for (
|
|
360
|
+
let link = top.parent;
|
|
361
|
+
ts.isPropertyAccessExpression(link) &&
|
|
362
|
+
link.expression === top &&
|
|
363
|
+
ts.isCallExpression(link.parent) &&
|
|
364
|
+
link.parent.expression === link;
|
|
365
|
+
link = top.parent
|
|
366
|
+
) {
|
|
367
|
+
if (link.name.text !== 'collection') {
|
|
368
|
+
return refuse(
|
|
369
|
+
`the framework() chain in ${file} calls .${link.name.text}(), so its end is not where collections go`,
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
const [registered] = link.parent.arguments;
|
|
373
|
+
if (
|
|
374
|
+
registered !== undefined &&
|
|
375
|
+
ts.isIdentifier(registered) &&
|
|
376
|
+
registered.text === names.exportName
|
|
377
|
+
) {
|
|
378
|
+
throw new Exit(`${file} already registers ${names.exportName}`, 1);
|
|
379
|
+
}
|
|
380
|
+
lastLink = link.name;
|
|
381
|
+
top = link.parent;
|
|
382
|
+
}
|
|
383
|
+
const notExported = refuse(
|
|
384
|
+
`the framework() chain in ${file} is not an exported const`,
|
|
385
|
+
);
|
|
386
|
+
const declaration = top.parent;
|
|
387
|
+
if (
|
|
388
|
+
!ts.isVariableDeclaration(declaration) ||
|
|
389
|
+
!ts.isIdentifier(declaration.name)
|
|
390
|
+
) {
|
|
391
|
+
return notExported;
|
|
392
|
+
}
|
|
393
|
+
const statement = declaration.parent.parent;
|
|
394
|
+
if (
|
|
395
|
+
!ts.isVariableStatement(statement) ||
|
|
396
|
+
statement.parent !== source ||
|
|
397
|
+
(statement.declarationList.flags & ts.NodeFlags.Const) === 0 ||
|
|
398
|
+
!statement.modifiers?.some(
|
|
399
|
+
(modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword,
|
|
400
|
+
)
|
|
401
|
+
) {
|
|
402
|
+
return notExported;
|
|
403
|
+
}
|
|
404
|
+
// A link on a line of its own sets the indent; a chain on the statement's
|
|
405
|
+
// line gets the statement's indent and one unit.
|
|
406
|
+
const start = statement.getStart(source);
|
|
407
|
+
const indent =
|
|
408
|
+
lastLink !== undefined &&
|
|
409
|
+
lineOf(source, lastLink.getStart(source)) !== lineOf(source, start)
|
|
410
|
+
? indentAt(source, lastLink.getStart(source))
|
|
411
|
+
: `${indentAt(source, start)}${unit}`;
|
|
412
|
+
const lines = callLines(indent);
|
|
413
|
+
return {
|
|
414
|
+
path,
|
|
415
|
+
before,
|
|
416
|
+
additions: inStyle(
|
|
417
|
+
[
|
|
418
|
+
imported,
|
|
419
|
+
{
|
|
420
|
+
where,
|
|
421
|
+
lines,
|
|
422
|
+
inserts: [
|
|
423
|
+
{
|
|
424
|
+
offset: pastComments(ts, before, top.getEnd()),
|
|
425
|
+
text: `\n${lines}`,
|
|
426
|
+
},
|
|
427
|
+
],
|
|
428
|
+
},
|
|
429
|
+
],
|
|
430
|
+
style,
|
|
431
|
+
),
|
|
432
|
+
refused: undefined,
|
|
433
|
+
app: declaration.name.text,
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const events = ['created', 'updated', 'deleted'];
|
|
438
|
+
|
|
439
|
+
/** The name of a property, when it is an identifier or a string. */
|
|
440
|
+
function nameOf(ts: TypeScript, property: TS.ObjectLiteralElementLike) {
|
|
441
|
+
const { name } = property;
|
|
442
|
+
return name !== undefined &&
|
|
443
|
+
(ts.isIdentifier(name) || ts.isStringLiteral(name))
|
|
444
|
+
? name.text
|
|
445
|
+
: undefined;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** Whether a property is `X.events.created`, `.updated` or `.deleted` of a collection. */
|
|
449
|
+
function isCollectionEvent(
|
|
450
|
+
ts: TypeScript,
|
|
451
|
+
property: TS.ObjectLiteralElementLike,
|
|
452
|
+
): boolean {
|
|
453
|
+
if (!ts.isPropertyAssignment(property)) return false;
|
|
454
|
+
const value = property.initializer;
|
|
455
|
+
return (
|
|
456
|
+
ts.isPropertyAccessExpression(value) &&
|
|
457
|
+
events.includes(value.name.text) &&
|
|
458
|
+
ts.isPropertyAccessExpression(value.expression) &&
|
|
459
|
+
value.expression.name.text === 'events' &&
|
|
460
|
+
ts.isIdentifier(value.expression.expression)
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* The events catalog's edit: an import, and the collection's three events
|
|
466
|
+
* after the last collection's, in the one `EventCatalog()` literal.
|
|
467
|
+
*/
|
|
468
|
+
export function eventsEdit(
|
|
469
|
+
ts: TypeScript,
|
|
470
|
+
path: string,
|
|
471
|
+
file: string,
|
|
472
|
+
before: string,
|
|
473
|
+
names: Names,
|
|
474
|
+
specifier: string,
|
|
475
|
+
): FileEdit {
|
|
476
|
+
const source = parse(ts, path, before);
|
|
477
|
+
const style = styleOf(before);
|
|
478
|
+
const { unit } = style;
|
|
479
|
+
const imported = importAddition(
|
|
480
|
+
ts,
|
|
481
|
+
source,
|
|
482
|
+
file,
|
|
483
|
+
names.exportName,
|
|
484
|
+
`{ ${names.exportName} }`,
|
|
485
|
+
specifier,
|
|
486
|
+
);
|
|
487
|
+
const keys = events.map(
|
|
488
|
+
(event) => `${names.camel}${event[0]?.toUpperCase()}${event.slice(1)}`,
|
|
489
|
+
);
|
|
490
|
+
const entries = (indent: string) =>
|
|
491
|
+
events
|
|
492
|
+
.flatMap((event, index) =>
|
|
493
|
+
entry(
|
|
494
|
+
indent,
|
|
495
|
+
keys[index] ?? event,
|
|
496
|
+
`${names.exportName}.events.${event}`,
|
|
497
|
+
unit,
|
|
498
|
+
),
|
|
499
|
+
)
|
|
500
|
+
.join('\n');
|
|
501
|
+
const where = "after the last collection's events";
|
|
502
|
+
const local = importedAs(ts, source, 'EventCatalog', 'hexok');
|
|
503
|
+
const calls = local === undefined ? [] : callsOf(ts, source, local);
|
|
504
|
+
const literal = calls[0]?.arguments[1];
|
|
505
|
+
if (
|
|
506
|
+
calls.length !== 1 ||
|
|
507
|
+
literal === undefined ||
|
|
508
|
+
!ts.isObjectLiteralExpression(literal)
|
|
509
|
+
) {
|
|
510
|
+
return {
|
|
511
|
+
path,
|
|
512
|
+
before,
|
|
513
|
+
additions: [
|
|
514
|
+
{ ...imported, inserts: [] },
|
|
515
|
+
{ where, lines: entries(unit), inserts: [] },
|
|
516
|
+
],
|
|
517
|
+
refused:
|
|
518
|
+
calls.length === 0
|
|
519
|
+
? `${file} has no EventCatalog()`
|
|
520
|
+
: calls.length > 1
|
|
521
|
+
? `${file} has ${calls.length} EventCatalog() calls`
|
|
522
|
+
: `the EventCatalog() in ${file} has no object literal of events`,
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
const { properties } = literal;
|
|
526
|
+
for (const property of properties) {
|
|
527
|
+
const name = nameOf(ts, property);
|
|
528
|
+
if (name !== undefined && keys.includes(name)) {
|
|
529
|
+
throw new Exit(`${file} already lists ${name}`, 1);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
const open = literal.getStart(source);
|
|
533
|
+
const after =
|
|
534
|
+
properties.findLast((property) => isCollectionEvent(ts, property)) ??
|
|
535
|
+
properties.at(-1);
|
|
536
|
+
// Entries on a line of their own set the indent; a literal on one line
|
|
537
|
+
// gets its line's indent and one unit.
|
|
538
|
+
const indent =
|
|
539
|
+
after !== undefined &&
|
|
540
|
+
lineOf(source, after.getStart(source)) !== lineOf(source, open)
|
|
541
|
+
? indentAt(source, after.getStart(source))
|
|
542
|
+
: `${indentAt(source, open)}${unit}`;
|
|
543
|
+
const lines = entries(indent);
|
|
544
|
+
let inserts: Insert[];
|
|
545
|
+
if (after === undefined) {
|
|
546
|
+
inserts = [
|
|
547
|
+
{
|
|
548
|
+
offset: pastComments(ts, before, open + 1),
|
|
549
|
+
text: `\n${lines}\n${indentAt(source, open)}`,
|
|
550
|
+
},
|
|
551
|
+
];
|
|
552
|
+
} else {
|
|
553
|
+
const followed =
|
|
554
|
+
properties.indexOf(after) < properties.length - 1 ||
|
|
555
|
+
properties.hasTrailingComma;
|
|
556
|
+
const comma = followed ? commaAfter(ts, before, after.getEnd()) : undefined;
|
|
557
|
+
inserts =
|
|
558
|
+
comma !== undefined
|
|
559
|
+
? [{ offset: pastComments(ts, before, comma), text: `\n${lines}` }]
|
|
560
|
+
: [
|
|
561
|
+
// The comma goes before any comment that ends the line, and
|
|
562
|
+
// the entries after it.
|
|
563
|
+
{ offset: after.getEnd(), text: ',' },
|
|
564
|
+
{
|
|
565
|
+
offset: pastComments(ts, before, after.getEnd()),
|
|
566
|
+
text: `\n${lines.slice(0, -1)}`,
|
|
567
|
+
},
|
|
568
|
+
];
|
|
569
|
+
}
|
|
570
|
+
return {
|
|
571
|
+
path,
|
|
572
|
+
before,
|
|
573
|
+
additions: inStyle([imported, { where, lines, inserts }], style),
|
|
574
|
+
refused: undefined,
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/** The list's elements: the local name each binds, and the specifier of its default import. */
|
|
579
|
+
export type ListElement = {
|
|
580
|
+
readonly local: string;
|
|
581
|
+
readonly specifier: string;
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
/** The one exported array literal in a migrations index, with its elements. */
|
|
585
|
+
export type MigrationList = {
|
|
586
|
+
readonly literal: TS.ArrayLiteralExpression;
|
|
587
|
+
readonly elements: readonly ListElement[];
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* The migrations list: the one top-level `export const x = [...]`, each of
|
|
592
|
+
* whose elements is a migration's default import. A file with no such
|
|
593
|
+
* list, or two, gives the reason instead.
|
|
594
|
+
*/
|
|
595
|
+
export function migrationList(
|
|
596
|
+
ts: TypeScript,
|
|
597
|
+
source: TS.SourceFile,
|
|
598
|
+
file: string,
|
|
599
|
+
): MigrationList | string {
|
|
600
|
+
const literals = source.statements.flatMap((statement) =>
|
|
601
|
+
ts.isVariableStatement(statement) &&
|
|
602
|
+
statement.modifiers?.some(
|
|
603
|
+
(modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword,
|
|
604
|
+
)
|
|
605
|
+
? statement.declarationList.declarations.flatMap((declaration) =>
|
|
606
|
+
declaration.initializer !== undefined &&
|
|
607
|
+
ts.isArrayLiteralExpression(declaration.initializer)
|
|
608
|
+
? [declaration.initializer]
|
|
609
|
+
: [],
|
|
610
|
+
)
|
|
611
|
+
: [],
|
|
612
|
+
);
|
|
613
|
+
const [literal] = literals;
|
|
614
|
+
if (literal === undefined || literals.length > 1) {
|
|
615
|
+
return literals.length === 0
|
|
616
|
+
? `${file} has no exported array of migrations`
|
|
617
|
+
: `${file} has ${literals.length} exported arrays`;
|
|
618
|
+
}
|
|
619
|
+
const defaults = new Map<string, string>();
|
|
620
|
+
for (const statement of source.statements) {
|
|
621
|
+
const local = ts.isImportDeclaration(statement)
|
|
622
|
+
? statement.importClause?.name?.text
|
|
623
|
+
: undefined;
|
|
624
|
+
if (
|
|
625
|
+
local !== undefined &&
|
|
626
|
+
ts.isImportDeclaration(statement) &&
|
|
627
|
+
ts.isStringLiteral(statement.moduleSpecifier) &&
|
|
628
|
+
statement.moduleSpecifier.text.startsWith('.')
|
|
629
|
+
) {
|
|
630
|
+
defaults.set(local, statement.moduleSpecifier.text);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
const elements = literal.elements.map((element): ListElement => {
|
|
634
|
+
const specifier = ts.isIdentifier(element)
|
|
635
|
+
? defaults.get(element.text)
|
|
636
|
+
: undefined;
|
|
637
|
+
if (!ts.isIdentifier(element) || specifier === undefined) {
|
|
638
|
+
throw new Exit(
|
|
639
|
+
`the list in ${file} has ${element.getText(source)}, which is not a migration's default import`,
|
|
640
|
+
1,
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
return { local: element.text, specifier };
|
|
644
|
+
});
|
|
645
|
+
return { literal, elements };
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/** Whether the text from `start` to `end` holds a comment. */
|
|
649
|
+
function hasComments(
|
|
650
|
+
ts: TypeScript,
|
|
651
|
+
text: string,
|
|
652
|
+
start: number,
|
|
653
|
+
end: number,
|
|
654
|
+
): boolean {
|
|
655
|
+
const scanner = ts.createScanner(
|
|
656
|
+
ts.ScriptTarget.Latest,
|
|
657
|
+
false,
|
|
658
|
+
ts.LanguageVariant.Standard,
|
|
659
|
+
text,
|
|
660
|
+
);
|
|
661
|
+
scanner.setText(text, start, end - start);
|
|
662
|
+
for (
|
|
663
|
+
let token = scanner.scan();
|
|
664
|
+
token !== ts.SyntaxKind.EndOfFileToken;
|
|
665
|
+
token = scanner.scan()
|
|
666
|
+
) {
|
|
667
|
+
if (
|
|
668
|
+
token === ts.SyntaxKind.SingleLineCommentTrivia ||
|
|
669
|
+
token === ts.SyntaxKind.MultiLineCommentTrivia
|
|
670
|
+
) {
|
|
671
|
+
return true;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
return false;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/** The inserts that put `local` at the end of an array literal. */
|
|
678
|
+
function elementInserts(
|
|
679
|
+
ts: TypeScript,
|
|
680
|
+
source: TS.SourceFile,
|
|
681
|
+
literal: TS.ArrayLiteralExpression,
|
|
682
|
+
local: string,
|
|
683
|
+
unit: string,
|
|
684
|
+
): Insert[] | string {
|
|
685
|
+
const text = source.text;
|
|
686
|
+
const open = literal.getStart(source);
|
|
687
|
+
const close = literal.getEnd() - 1;
|
|
688
|
+
const { elements } = literal;
|
|
689
|
+
const last = elements.at(-1);
|
|
690
|
+
const lastComma =
|
|
691
|
+
last !== undefined && elements.hasTrailingComma
|
|
692
|
+
? commaAfter(ts, text, last.getEnd())
|
|
693
|
+
: undefined;
|
|
694
|
+
if (lineOf(source, open) !== lineOf(source, close)) {
|
|
695
|
+
// One element per line: the new one goes on a line of its own.
|
|
696
|
+
const indent =
|
|
697
|
+
last !== undefined &&
|
|
698
|
+
lineOf(source, last.getStart(source)) !== lineOf(source, open)
|
|
699
|
+
? indentAt(source, last.getStart(source))
|
|
700
|
+
: `${indentAt(source, open)}${unit}`;
|
|
701
|
+
if (last === undefined) {
|
|
702
|
+
return [
|
|
703
|
+
{
|
|
704
|
+
offset: pastComments(ts, text, open + 1),
|
|
705
|
+
text: `\n${indent}${local},`,
|
|
706
|
+
},
|
|
707
|
+
];
|
|
708
|
+
}
|
|
709
|
+
return lastComma !== undefined
|
|
710
|
+
? [
|
|
711
|
+
{
|
|
712
|
+
offset: pastComments(ts, text, lastComma),
|
|
713
|
+
text: `\n${indent}${local},`,
|
|
714
|
+
},
|
|
715
|
+
]
|
|
716
|
+
: [
|
|
717
|
+
{ offset: last.getEnd(), text: ',' },
|
|
718
|
+
{
|
|
719
|
+
offset: pastComments(ts, text, last.getEnd()),
|
|
720
|
+
text: `\n${indent}${local},`,
|
|
721
|
+
},
|
|
722
|
+
];
|
|
723
|
+
}
|
|
724
|
+
// On one line: it stays there while the line fits, as Biome keeps it.
|
|
725
|
+
const added =
|
|
726
|
+
last === undefined
|
|
727
|
+
? { offset: open + 1, text: local }
|
|
728
|
+
: lastComma !== undefined
|
|
729
|
+
? { offset: lastComma, text: ` ${local},` }
|
|
730
|
+
: { offset: last.getEnd(), text: `, ${local}` };
|
|
731
|
+
const lineStart = source.getPositionOfLineAndCharacter(
|
|
732
|
+
lineOf(source, close),
|
|
733
|
+
0,
|
|
734
|
+
);
|
|
735
|
+
const lineLength =
|
|
736
|
+
source.getLineEndOfPosition(close) - lineStart + added.text.length;
|
|
737
|
+
if (lineLength <= 80) return [added];
|
|
738
|
+
// Too long for one line: one element per line, as Biome breaks it.
|
|
739
|
+
if (hasComments(ts, text, open, close)) {
|
|
740
|
+
return 'the list is on one line with comments in it, so it cannot be broken into lines';
|
|
741
|
+
}
|
|
742
|
+
const indent = indentAt(source, open);
|
|
743
|
+
const inner = `${indent}${unit}`;
|
|
744
|
+
const all = [...elements];
|
|
745
|
+
const inserts: Insert[] = [];
|
|
746
|
+
const first = all[0];
|
|
747
|
+
if (first !== undefined) {
|
|
748
|
+
inserts.push({
|
|
749
|
+
offset: open + 1,
|
|
750
|
+
text: `\n${inner}`,
|
|
751
|
+
remove: first.getStart(source) - open - 1,
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
all.forEach((element, index) => {
|
|
755
|
+
const next = all[index + 1];
|
|
756
|
+
const comma = commaAfter(ts, text, element.getEnd()) ?? element.getEnd();
|
|
757
|
+
if (next !== undefined) {
|
|
758
|
+
inserts.push({
|
|
759
|
+
offset: comma,
|
|
760
|
+
text: `\n${inner}`,
|
|
761
|
+
remove: next.getStart(source) - comma,
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
const end = lastComma ?? last?.getEnd() ?? open + 1;
|
|
766
|
+
inserts.push({
|
|
767
|
+
offset: end,
|
|
768
|
+
text: `${lastComma === undefined && last !== undefined ? ',' : ''}\n${inner}${local},\n${indent}`,
|
|
769
|
+
remove: close - end,
|
|
770
|
+
});
|
|
771
|
+
return inserts;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
* The migrations index's edit: a default import of the new migration, and
|
|
776
|
+
* its local name at the end of the list.
|
|
777
|
+
*/
|
|
778
|
+
export function migrationsEdit(
|
|
779
|
+
ts: TypeScript,
|
|
780
|
+
path: string,
|
|
781
|
+
file: string,
|
|
782
|
+
before: string,
|
|
783
|
+
list: MigrationList,
|
|
784
|
+
local: string,
|
|
785
|
+
specifier: string,
|
|
786
|
+
): FileEdit {
|
|
787
|
+
const source = parse(ts, path, before);
|
|
788
|
+
const style = styleOf(before);
|
|
789
|
+
const imported = importAddition(ts, source, file, local, local, specifier);
|
|
790
|
+
const where = 'at the end of the migrations list';
|
|
791
|
+
const inserts = elementInserts(ts, source, list.literal, local, style.unit);
|
|
792
|
+
if (typeof inserts === 'string') {
|
|
793
|
+
return {
|
|
794
|
+
path,
|
|
795
|
+
before,
|
|
796
|
+
additions: [
|
|
797
|
+
{ ...imported, inserts: [] },
|
|
798
|
+
{ where, lines: `${style.unit}${local},`, inserts: [] },
|
|
799
|
+
],
|
|
800
|
+
refused: `${file}: ${inserts}`,
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
return {
|
|
804
|
+
path,
|
|
805
|
+
before,
|
|
806
|
+
additions: inStyle(
|
|
807
|
+
[imported, { where, lines: `${style.unit}${local},`, inserts }],
|
|
808
|
+
style,
|
|
809
|
+
),
|
|
810
|
+
refused: undefined,
|
|
811
|
+
};
|
|
812
|
+
}
|