@ethlete/cdk 5.0.0-next.23 ā 5.0.0-next.25
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/CHANGELOG.md +36 -6
- package/fesm2022/ethlete-cdk.mjs +386 -32
- package/fesm2022/ethlete-cdk.mjs.map +1 -1
- package/generators/generators.json +5 -0
- package/generators/migrate-from-cdk/imports.js +235 -0
- package/generators/migrate-from-cdk/installed-version.js +86 -0
- package/generators/migrate-from-cdk/migration-map.js +21 -0
- package/generators/migrate-from-cdk/migration-scope.js +30 -0
- package/generators/migrate-from-cdk/migration.js +118 -0
- package/generators/migrate-from-cdk/report.js +279 -0
- package/generators/migrate-from-cdk/schema.json +30 -0
- package/generators/migrate-from-cdk/templates.js +71 -0
- package/migration-map.json +3626 -0
- package/package.json +18 -1
- package/types/ethlete-cdk.d.ts +132 -226
|
@@ -4,6 +4,11 @@
|
|
|
4
4
|
"factory": "./migrate-to-v5/migration",
|
|
5
5
|
"schema": "./migrate-to-v5/schema.json",
|
|
6
6
|
"description": "A migration to update the cdk to version 5"
|
|
7
|
+
},
|
|
8
|
+
"migrate-from-cdk": {
|
|
9
|
+
"factory": "./migrate-from-cdk/migration",
|
|
10
|
+
"schema": "./migrate-from-cdk/schema.json",
|
|
11
|
+
"description": "Rewrite the mechanical parts of a cdk ā components migration and report what needs a decision"
|
|
7
12
|
}
|
|
8
13
|
}
|
|
9
14
|
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import { CDK_PACKAGE } from './migration-map.js';
|
|
3
|
+
export const createSourceFile = (content, filePath = 'temp.ts') => ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
|
4
|
+
export const applyReplacements = (content, replacements) => {
|
|
5
|
+
let result = content;
|
|
6
|
+
[...replacements]
|
|
7
|
+
.sort((left, right) => right.start - left.start)
|
|
8
|
+
.forEach(({ start, end, replacement }) => {
|
|
9
|
+
result = result.slice(0, start) + replacement + result.slice(end);
|
|
10
|
+
});
|
|
11
|
+
return result;
|
|
12
|
+
};
|
|
13
|
+
const namedImportsOf = (declaration) => {
|
|
14
|
+
const bindings = declaration.importClause?.namedBindings;
|
|
15
|
+
return bindings && ts.isNamedImports(bindings) && bindings.elements.length > 0 ? bindings : null;
|
|
16
|
+
};
|
|
17
|
+
const importDeclarations = (sourceFile) => sourceFile.statements.filter((statement) => ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier));
|
|
18
|
+
const moduleOf = (declaration) => declaration.moduleSpecifier.text;
|
|
19
|
+
const renderImport = (specifiers, moduleSpecifier, typeOnly = false) => `import ${typeOnly ? 'type ' : ''}{ ${specifiers.join(', ')} } from '${moduleSpecifier}';`;
|
|
20
|
+
/** The cdk symbols a file imports, in source order, keyed by the name the module exports. */
|
|
21
|
+
export const readCdkImportedSymbols = (content, filePath) => {
|
|
22
|
+
const sourceFile = createSourceFile(content, filePath);
|
|
23
|
+
const symbols = [];
|
|
24
|
+
for (const declaration of importDeclarations(sourceFile)) {
|
|
25
|
+
if (moduleOf(declaration) !== CDK_PACKAGE)
|
|
26
|
+
continue;
|
|
27
|
+
const named = namedImportsOf(declaration);
|
|
28
|
+
if (!named)
|
|
29
|
+
continue;
|
|
30
|
+
for (const element of named.elements) {
|
|
31
|
+
symbols.push({
|
|
32
|
+
name: (element.propertyName ?? element.name).text,
|
|
33
|
+
line: sourceFile.getLineAndCharacterOfPosition(element.getStart(sourceFile)).line + 1,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return symbols;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Moves the given cdk specifiers to their successor packages, merging into an existing import from that
|
|
41
|
+
* package when the file already has one. Returns the renames the reference pass still has to apply -
|
|
42
|
+
* a moved specifier only carries its new name inside the import statement.
|
|
43
|
+
*/
|
|
44
|
+
export const rewriteCdkImports = (content, filePath, rewrites) => {
|
|
45
|
+
const sourceFile = createSourceFile(content, filePath);
|
|
46
|
+
const declarations = importDeclarations(sourceFile);
|
|
47
|
+
const cdkDeclarations = declarations.filter((declaration) => moduleOf(declaration) === CDK_PACKAGE);
|
|
48
|
+
if (cdkDeclarations.length === 0) {
|
|
49
|
+
return { content, renames: new Map(), moved: [] };
|
|
50
|
+
}
|
|
51
|
+
const mergeTargets = new Map();
|
|
52
|
+
const importedNames = new Map();
|
|
53
|
+
for (const declaration of declarations) {
|
|
54
|
+
const module = moduleOf(declaration);
|
|
55
|
+
const named = namedImportsOf(declaration);
|
|
56
|
+
if (!named)
|
|
57
|
+
continue;
|
|
58
|
+
const names = importedNames.get(module) ?? new Set();
|
|
59
|
+
named.elements.forEach((element) => names.add(element.name.text));
|
|
60
|
+
importedNames.set(module, names);
|
|
61
|
+
if (module !== CDK_PACKAGE && !declaration.importClause?.isTypeOnly && !mergeTargets.has(module)) {
|
|
62
|
+
mergeTargets.set(module, named);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const renames = new Map();
|
|
66
|
+
const moved = [];
|
|
67
|
+
const additions = new Map();
|
|
68
|
+
const declarationEdits = [];
|
|
69
|
+
for (const declaration of cdkDeclarations) {
|
|
70
|
+
const named = namedImportsOf(declaration);
|
|
71
|
+
if (!named)
|
|
72
|
+
continue;
|
|
73
|
+
const kept = [];
|
|
74
|
+
let changed = false;
|
|
75
|
+
for (const element of named.elements) {
|
|
76
|
+
const importedName = (element.propertyName ?? element.name).text;
|
|
77
|
+
const localName = element.name.text;
|
|
78
|
+
const rewrite = rewrites.get(importedName);
|
|
79
|
+
if (!rewrite) {
|
|
80
|
+
kept.push(element.getText(sourceFile));
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
changed = true;
|
|
84
|
+
moved.push(importedName);
|
|
85
|
+
const typePrefix = declaration.importClause?.isTypeOnly || element.isTypeOnly ? 'type ' : '';
|
|
86
|
+
const isAliased = !!element.propertyName;
|
|
87
|
+
const specifier = isAliased ? `${typePrefix}${rewrite.to} as ${localName}` : `${typePrefix}${rewrite.to}`;
|
|
88
|
+
const finalLocalName = isAliased ? localName : rewrite.to;
|
|
89
|
+
if (!isAliased && rewrite.to !== localName) {
|
|
90
|
+
renames.set(localName, rewrite.to);
|
|
91
|
+
}
|
|
92
|
+
const alreadyImported = importedNames.get(rewrite.package)?.has(finalLocalName);
|
|
93
|
+
const pending = additions.get(rewrite.package) ?? [];
|
|
94
|
+
if (!alreadyImported && !pending.includes(specifier)) {
|
|
95
|
+
additions.set(rewrite.package, [...pending, specifier]);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (changed) {
|
|
99
|
+
declarationEdits.push({ declaration, kept });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (declarationEdits.length === 0) {
|
|
103
|
+
return { content, renames: new Map(), moved: [] };
|
|
104
|
+
}
|
|
105
|
+
const replacements = [];
|
|
106
|
+
const freshImports = [];
|
|
107
|
+
for (const [module, specifiers] of additions) {
|
|
108
|
+
const target = mergeTargets.get(module);
|
|
109
|
+
if (target) {
|
|
110
|
+
const lastElement = target.elements[target.elements.length - 1];
|
|
111
|
+
replacements.push({
|
|
112
|
+
start: lastElement.getEnd(),
|
|
113
|
+
end: lastElement.getEnd(),
|
|
114
|
+
replacement: `, ${specifiers.join(', ')}`,
|
|
115
|
+
});
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
freshImports.push(renderImport(specifiers, module));
|
|
119
|
+
}
|
|
120
|
+
declarationEdits.forEach(({ declaration, kept }, index) => {
|
|
121
|
+
const isAnchor = index === 0;
|
|
122
|
+
const anchoredImports = isAnchor ? freshImports : [];
|
|
123
|
+
const start = declaration.getStart(sourceFile);
|
|
124
|
+
const keptImport = kept.length > 0 ? [renderImport(kept, CDK_PACKAGE, declaration.importClause?.isTypeOnly)] : [];
|
|
125
|
+
const lines = [...keptImport, ...anchoredImports];
|
|
126
|
+
if (lines.length > 0) {
|
|
127
|
+
replacements.push({ start, end: declaration.getEnd(), replacement: lines.join('\n') });
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const end = content[declaration.getEnd()] === '\n' ? declaration.getEnd() + 1 : declaration.getEnd();
|
|
131
|
+
replacements.push({ start, end, replacement: '' });
|
|
132
|
+
});
|
|
133
|
+
return { content: applyReplacements(content, replacements), renames, moved };
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Whether an identifier is a *name* rather than a *reference* - a declaration, a property key, the right
|
|
137
|
+
* hand side of a member access. Renaming those would hit same-named properties and locals.
|
|
138
|
+
*/
|
|
139
|
+
const isNamePosition = (node) => {
|
|
140
|
+
const parent = node.parent;
|
|
141
|
+
if (!parent)
|
|
142
|
+
return false;
|
|
143
|
+
if (ts.isPropertyAccessExpression(parent) && parent.name === node)
|
|
144
|
+
return true;
|
|
145
|
+
if (ts.isQualifiedName(parent) && parent.right === node)
|
|
146
|
+
return true;
|
|
147
|
+
if (ts.isPropertyAssignment(parent) && parent.name === node)
|
|
148
|
+
return true;
|
|
149
|
+
if (ts.isShorthandPropertyAssignment(parent) && parent.name === node)
|
|
150
|
+
return true;
|
|
151
|
+
if (ts.isPropertySignature(parent) && parent.name === node)
|
|
152
|
+
return true;
|
|
153
|
+
if (ts.isMethodSignature(parent) && parent.name === node)
|
|
154
|
+
return true;
|
|
155
|
+
if (ts.isEnumMember(parent) && parent.name === node)
|
|
156
|
+
return true;
|
|
157
|
+
if (ts.isBindingElement(parent) && parent.propertyName === node)
|
|
158
|
+
return true;
|
|
159
|
+
if (ts.isVariableDeclaration(parent) && parent.name === node)
|
|
160
|
+
return true;
|
|
161
|
+
if (ts.isFunctionDeclaration(parent) && parent.name === node)
|
|
162
|
+
return true;
|
|
163
|
+
if (ts.isClassDeclaration(parent) && parent.name === node)
|
|
164
|
+
return true;
|
|
165
|
+
if (ts.isInterfaceDeclaration(parent) && parent.name === node)
|
|
166
|
+
return true;
|
|
167
|
+
if (ts.isTypeAliasDeclaration(parent) && parent.name === node)
|
|
168
|
+
return true;
|
|
169
|
+
if (ts.isEnumDeclaration(parent) && parent.name === node)
|
|
170
|
+
return true;
|
|
171
|
+
if (ts.isModuleDeclaration(parent) && parent.name === node)
|
|
172
|
+
return true;
|
|
173
|
+
if (ts.isMethodDeclaration(parent) && parent.name === node)
|
|
174
|
+
return true;
|
|
175
|
+
if (ts.isPropertyDeclaration(parent) && parent.name === node)
|
|
176
|
+
return true;
|
|
177
|
+
if (ts.isGetAccessorDeclaration(parent) && parent.name === node)
|
|
178
|
+
return true;
|
|
179
|
+
if (ts.isSetAccessorDeclaration(parent) && parent.name === node)
|
|
180
|
+
return true;
|
|
181
|
+
if (ts.isParameter(parent) && parent.name === node)
|
|
182
|
+
return true;
|
|
183
|
+
if (ts.isTypeParameterDeclaration(parent) && parent.name === node)
|
|
184
|
+
return true;
|
|
185
|
+
// Import and export clauses are rewritten by `rewriteCdkImports`, never by the reference pass.
|
|
186
|
+
if (ts.isImportSpecifier(parent) || ts.isExportSpecifier(parent))
|
|
187
|
+
return true;
|
|
188
|
+
if (ts.isImportClause(parent) || ts.isNamespaceImport(parent))
|
|
189
|
+
return true;
|
|
190
|
+
return false;
|
|
191
|
+
};
|
|
192
|
+
const collectLocalDeclarations = (sourceFile) => {
|
|
193
|
+
const declared = new Set();
|
|
194
|
+
const add = (name) => {
|
|
195
|
+
if (name && ts.isIdentifier(name))
|
|
196
|
+
declared.add(name.text);
|
|
197
|
+
};
|
|
198
|
+
const visit = (node) => {
|
|
199
|
+
if (ts.isVariableDeclaration(node))
|
|
200
|
+
add(node.name);
|
|
201
|
+
if (ts.isFunctionDeclaration(node))
|
|
202
|
+
add(node.name);
|
|
203
|
+
if (ts.isClassDeclaration(node))
|
|
204
|
+
add(node.name);
|
|
205
|
+
if (ts.isInterfaceDeclaration(node))
|
|
206
|
+
add(node.name);
|
|
207
|
+
if (ts.isTypeAliasDeclaration(node))
|
|
208
|
+
add(node.name);
|
|
209
|
+
if (ts.isEnumDeclaration(node))
|
|
210
|
+
add(node.name);
|
|
211
|
+
ts.forEachChild(node, visit);
|
|
212
|
+
};
|
|
213
|
+
visit(sourceFile);
|
|
214
|
+
return declared;
|
|
215
|
+
};
|
|
216
|
+
/** Renames references to the moved bindings. A name the file also declares itself is left alone. */
|
|
217
|
+
export const renameReferences = (content, filePath, renames) => {
|
|
218
|
+
if (renames.size === 0)
|
|
219
|
+
return content;
|
|
220
|
+
const sourceFile = createSourceFile(content, filePath);
|
|
221
|
+
const declaredLocally = collectLocalDeclarations(sourceFile);
|
|
222
|
+
const replacements = [];
|
|
223
|
+
const visit = (node) => {
|
|
224
|
+
if (ts.isIdentifier(node)) {
|
|
225
|
+
const nextName = renames.get(node.text);
|
|
226
|
+
if (nextName && !declaredLocally.has(node.text) && !isNamePosition(node)) {
|
|
227
|
+
replacements.push({ start: node.getStart(sourceFile), end: node.getEnd(), replacement: nextName });
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
ts.forEachChild(node, visit);
|
|
231
|
+
};
|
|
232
|
+
visit(sourceFile);
|
|
233
|
+
return applyReplacements(content, replacements);
|
|
234
|
+
};
|
|
235
|
+
//# sourceMappingURL=imports.js.map
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const DEPENDENCY_FIELDS = ['dependencies', 'devDependencies', 'peerDependencies'];
|
|
2
|
+
/** `^1.0.0-next.34` / `>=1.2.3 <2` / `~1.2.3` ā the version the comparison runs against, or `null`. */
|
|
3
|
+
const versionFromRange = (range) => {
|
|
4
|
+
const first = range.split('||')[0]?.trim() ?? '';
|
|
5
|
+
const cleaned = first.split(/\s+/)[0]?.replace(/^[\^~=v]+|^>=?|^<=?/, '') ?? '';
|
|
6
|
+
return /^\d+\.\d+/.test(cleaned) ? cleaned : null;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* The version of `packageName` the workspace declares, or `null` when it declares none (or a range no
|
|
10
|
+
* comparison can be made against, like `workspace:*`).
|
|
11
|
+
*/
|
|
12
|
+
export const readInstalledVersion = (tree, packageName) => {
|
|
13
|
+
const raw = tree.read('package.json', 'utf-8');
|
|
14
|
+
if (!raw) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
let parsed;
|
|
18
|
+
try {
|
|
19
|
+
parsed = JSON.parse(raw);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
for (const field of DEPENDENCY_FIELDS) {
|
|
25
|
+
const range = parsed[field]?.[packageName];
|
|
26
|
+
if (range) {
|
|
27
|
+
const version = versionFromRange(range);
|
|
28
|
+
if (version) {
|
|
29
|
+
return version;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
};
|
|
35
|
+
const parseVersion = (version) => {
|
|
36
|
+
const [core = '', ...prereleaseParts] = version.split('-');
|
|
37
|
+
return {
|
|
38
|
+
numbers: core.split('.').map((part) => Number.parseInt(part, 10) || 0),
|
|
39
|
+
prerelease: prereleaseParts.join('-'),
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
const comparePrerelease = (left, right) => {
|
|
43
|
+
// Semver: a version without a prerelease outranks the same version with one.
|
|
44
|
+
if (left === right)
|
|
45
|
+
return 0;
|
|
46
|
+
if (left === '')
|
|
47
|
+
return 1;
|
|
48
|
+
if (right === '')
|
|
49
|
+
return -1;
|
|
50
|
+
const leftParts = left.split('.');
|
|
51
|
+
const rightParts = right.split('.');
|
|
52
|
+
for (let index = 0; index < Math.max(leftParts.length, rightParts.length); index += 1) {
|
|
53
|
+
const leftPart = leftParts[index];
|
|
54
|
+
const rightPart = rightParts[index];
|
|
55
|
+
if (leftPart === undefined)
|
|
56
|
+
return -1;
|
|
57
|
+
if (rightPart === undefined)
|
|
58
|
+
return 1;
|
|
59
|
+
const leftNumber = Number.parseInt(leftPart, 10);
|
|
60
|
+
const rightNumber = Number.parseInt(rightPart, 10);
|
|
61
|
+
const bothNumeric = /^\d+$/.test(leftPart) && /^\d+$/.test(rightPart);
|
|
62
|
+
if (bothNumeric) {
|
|
63
|
+
if (leftNumber !== rightNumber)
|
|
64
|
+
return leftNumber < rightNumber ? -1 : 1;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (leftPart !== rightPart)
|
|
68
|
+
return leftPart < rightPart ? -1 : 1;
|
|
69
|
+
}
|
|
70
|
+
return 0;
|
|
71
|
+
};
|
|
72
|
+
/** `-1` / `0` / `1`, prerelease-aware enough for the `x.y.z-next.N` versions this repo publishes. */
|
|
73
|
+
export const compareVersions = (left, right) => {
|
|
74
|
+
const parsedLeft = parseVersion(left);
|
|
75
|
+
const parsedRight = parseVersion(right);
|
|
76
|
+
for (let index = 0; index < Math.max(parsedLeft.numbers.length, parsedRight.numbers.length); index += 1) {
|
|
77
|
+
const leftNumber = parsedLeft.numbers[index] ?? 0;
|
|
78
|
+
const rightNumber = parsedRight.numbers[index] ?? 0;
|
|
79
|
+
if (leftNumber !== rightNumber)
|
|
80
|
+
return leftNumber < rightNumber ? -1 : 1;
|
|
81
|
+
}
|
|
82
|
+
return comparePrerelease(parsedLeft.prerelease, parsedRight.prerelease);
|
|
83
|
+
};
|
|
84
|
+
/** An unknown installed version counts as satisfied - the caller reports the assumption instead. */
|
|
85
|
+
export const isSinceSatisfied = (installed, since) => installed === null || compareVersions(installed, since) >= 0;
|
|
86
|
+
//# sourceMappingURL=installed-version.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { workspaceRoot } from '@nx/devkit';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { isAbsolute, join } from 'node:path';
|
|
4
|
+
export const MIGRATION_KINDS = ['move', 'rename', 'reshape', 'rename+reshape', 'replaced-by', 'removed'];
|
|
5
|
+
/** Kinds whose successor is reachable by editing the import statement alone. */
|
|
6
|
+
export const MECHANICAL_KINDS = ['move', 'rename'];
|
|
7
|
+
/** Kinds whose successor needs a human: the contract changed, or several symbols replace one. */
|
|
8
|
+
export const JUDGMENT_KINDS = ['reshape', 'rename+reshape', 'replaced-by'];
|
|
9
|
+
export const CDK_PACKAGE = '@ethlete/cdk';
|
|
10
|
+
const MAP_CANDIDATES = [`node_modules/${CDK_PACKAGE}/migration-map.json`, 'libs/cdk/migration-map.json'];
|
|
11
|
+
export const loadMigrationMap = (mapPath) => {
|
|
12
|
+
const candidates = mapPath
|
|
13
|
+
? [isAbsolute(mapPath) ? mapPath : join(workspaceRoot, mapPath)]
|
|
14
|
+
: MAP_CANDIDATES.map((candidate) => join(workspaceRoot, candidate));
|
|
15
|
+
const found = candidates.find((candidate) => existsSync(candidate));
|
|
16
|
+
if (!found) {
|
|
17
|
+
throw new Error(`Could not read the cdk migration map. Looked in ${candidates.join(', ')}. Install ${CDK_PACKAGE} or pass --mapPath.`);
|
|
18
|
+
}
|
|
19
|
+
return JSON.parse(readFileSync(found, 'utf-8'));
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=migration-map.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { getProjects, visitNotIgnoredFiles } from '@nx/devkit';
|
|
2
|
+
const stripTrailingSlash = (value) => (value.endsWith('/') ? value.slice(0, -1) : value);
|
|
3
|
+
export const createMigrationScope = (tree, options) => {
|
|
4
|
+
const roots = [];
|
|
5
|
+
for (const projectName of options.projects ?? []) {
|
|
6
|
+
const project = getProjects(tree).get(projectName);
|
|
7
|
+
if (!project) {
|
|
8
|
+
throw new Error(`Unknown project "${projectName}". Pass names as they appear in \`nx show projects\`, or use --include with a path prefix.`);
|
|
9
|
+
}
|
|
10
|
+
roots.push(stripTrailingSlash(project.root));
|
|
11
|
+
}
|
|
12
|
+
for (const includePath of options.include ?? []) {
|
|
13
|
+
roots.push(stripTrailingSlash(includePath));
|
|
14
|
+
}
|
|
15
|
+
if (roots.length === 0) {
|
|
16
|
+
return {
|
|
17
|
+
visit: (targetTree, callback) => visitNotIgnoredFiles(targetTree, '', callback),
|
|
18
|
+
describe: () => 'the whole workspace',
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
visit: (targetTree, callback) => {
|
|
23
|
+
for (const root of roots) {
|
|
24
|
+
visitNotIgnoredFiles(targetTree, root, callback);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
describe: () => roots.join(', '),
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=migration-scope.js.map
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { formatFiles } from '@nx/devkit';
|
|
2
|
+
import { readCdkImportedSymbols, renameReferences, rewriteCdkImports } from './imports.js';
|
|
3
|
+
import { isSinceSatisfied, readInstalledVersion } from './installed-version.js';
|
|
4
|
+
import { JUDGMENT_KINDS, loadMigrationMap, MECHANICAL_KINDS } from './migration-map.js';
|
|
5
|
+
import { createMigrationScope } from './migration-scope.js';
|
|
6
|
+
import { addSymbolSite, createEmptyReport, isReportEmpty, renderReport, scanStyleSheet, scanTemplate, } from './report.js';
|
|
7
|
+
import { findInlineStyleSpans, findInlineTemplateSpans, lineOfIndex, rewriteStyleSheet, rewriteTemplate, transformInlineSpans, } from './templates.js';
|
|
8
|
+
export const MIGRATE_FROM_CDK_REPORT_PATH = 'migrate-from-cdk-tasks.md';
|
|
9
|
+
const TARGET_PACKAGES = ['@ethlete/components', '@ethlete/core'];
|
|
10
|
+
/**
|
|
11
|
+
* The one `rename+reshape` row that is still rewritten: the generator handles the whole reshape
|
|
12
|
+
* (`mode`, `renderBackground`, `multiColor`, the colour variable) itself, so only the colour default is
|
|
13
|
+
* left for the report.
|
|
14
|
+
*/
|
|
15
|
+
const SPINNER_SYMBOL = 'ProgressSpinnerComponent';
|
|
16
|
+
const isTypeScript = (filePath) => filePath.endsWith('.ts') && !filePath.endsWith('.d.ts');
|
|
17
|
+
const isTemplate = (filePath) => filePath.endsWith('.html');
|
|
18
|
+
const isStyleSheet = (filePath) => /\.(css|scss)$/.test(filePath);
|
|
19
|
+
const isScannable = (filePath) => isTypeScript(filePath) || isTemplate(filePath) || isStyleSheet(filePath);
|
|
20
|
+
const collectRewrites = (map, isAvailable) => {
|
|
21
|
+
const rewrites = new Map();
|
|
22
|
+
for (const [name, entry] of Object.entries(map)) {
|
|
23
|
+
if (!entry.to || !entry.package)
|
|
24
|
+
continue;
|
|
25
|
+
const isMechanical = MECHANICAL_KINDS.includes(entry.kind) || name === SPINNER_SYMBOL;
|
|
26
|
+
if (!isMechanical || !isAvailable(entry))
|
|
27
|
+
continue;
|
|
28
|
+
rewrites.set(name, { to: entry.to, package: entry.package });
|
|
29
|
+
}
|
|
30
|
+
return rewrites;
|
|
31
|
+
};
|
|
32
|
+
const scanFile = (report, filePath, content, map, isAvailable) => {
|
|
33
|
+
if (isTemplate(filePath)) {
|
|
34
|
+
scanTemplate(report, content, { file: filePath, startLine: 1 });
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (isStyleSheet(filePath)) {
|
|
38
|
+
scanStyleSheet(report, content, { file: filePath, startLine: 1 });
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
for (const span of findInlineTemplateSpans(content, filePath)) {
|
|
42
|
+
scanTemplate(report, span.text, { file: filePath, startLine: lineOfIndex(content, span.start) });
|
|
43
|
+
}
|
|
44
|
+
for (const span of findInlineStyleSpans(content, filePath)) {
|
|
45
|
+
scanStyleSheet(report, span.text, { file: filePath, startLine: lineOfIndex(content, span.start) });
|
|
46
|
+
}
|
|
47
|
+
for (const { name, line } of readCdkImportedSymbols(content, filePath)) {
|
|
48
|
+
const entry = map[name];
|
|
49
|
+
if (!entry)
|
|
50
|
+
continue;
|
|
51
|
+
const site = { file: filePath, line };
|
|
52
|
+
if (entry.since && !isAvailable(entry)) {
|
|
53
|
+
addSymbolSite(report.gatedSymbols, name, entry, site);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (JUDGMENT_KINDS.includes(entry.kind) || name === SPINNER_SYMBOL) {
|
|
57
|
+
addSymbolSite(report.judgmentSymbols, name, entry, site);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const rewriteFile = (filePath, content, rewrites) => {
|
|
62
|
+
if (isTemplate(filePath))
|
|
63
|
+
return rewriteTemplate(content);
|
|
64
|
+
if (isStyleSheet(filePath))
|
|
65
|
+
return rewriteStyleSheet(content);
|
|
66
|
+
const { content: withImports, renames } = rewriteCdkImports(content, filePath, rewrites);
|
|
67
|
+
const withReferences = renameReferences(withImports, filePath, renames);
|
|
68
|
+
const withTemplates = transformInlineSpans(withReferences, findInlineTemplateSpans(withReferences, filePath), rewriteTemplate);
|
|
69
|
+
return transformInlineSpans(withTemplates, findInlineStyleSpans(withTemplates, filePath), rewriteStyleSheet);
|
|
70
|
+
};
|
|
71
|
+
export default async function migrateFromCdk(tree, schema) {
|
|
72
|
+
console.log('\nš Migrating off @ethlete/cdk...');
|
|
73
|
+
const map = loadMigrationMap(schema.mapPath);
|
|
74
|
+
const scope = createMigrationScope(tree, schema);
|
|
75
|
+
console.log(` Scope: ${scope.describe()}`);
|
|
76
|
+
const installedVersions = Object.fromEntries(TARGET_PACKAGES.map((packageName) => [packageName, readInstalledVersion(tree, packageName)]));
|
|
77
|
+
const isAvailable = (entry) => !entry.since || isSinceSatisfied(installedVersions[entry.package ?? '@ethlete/components'] ?? null, entry.since);
|
|
78
|
+
const rewrites = collectRewrites(map, isAvailable);
|
|
79
|
+
const report = createEmptyReport();
|
|
80
|
+
const files = [];
|
|
81
|
+
scope.visit(tree, (filePath) => {
|
|
82
|
+
if (!isScannable(filePath) || filePath === MIGRATE_FROM_CDK_REPORT_PATH)
|
|
83
|
+
return;
|
|
84
|
+
files.push(filePath);
|
|
85
|
+
});
|
|
86
|
+
for (const filePath of files) {
|
|
87
|
+
const content = tree.read(filePath, 'utf-8');
|
|
88
|
+
if (!content)
|
|
89
|
+
continue;
|
|
90
|
+
scanFile(report, filePath, content, map, isAvailable);
|
|
91
|
+
}
|
|
92
|
+
let rewrittenFiles = 0;
|
|
93
|
+
for (const filePath of files) {
|
|
94
|
+
const content = tree.read(filePath, 'utf-8');
|
|
95
|
+
if (!content)
|
|
96
|
+
continue;
|
|
97
|
+
const next = rewriteFile(filePath, content, rewrites);
|
|
98
|
+
if (next !== content) {
|
|
99
|
+
tree.write(filePath, next);
|
|
100
|
+
rewrittenFiles += 1;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const hasTasks = !isReportEmpty(report);
|
|
104
|
+
if (hasTasks) {
|
|
105
|
+
tree.write(MIGRATE_FROM_CDK_REPORT_PATH, renderReport(report, { installedVersions, rewrittenFiles }));
|
|
106
|
+
}
|
|
107
|
+
if (!schema.skipFormat) {
|
|
108
|
+
await formatFiles(tree);
|
|
109
|
+
}
|
|
110
|
+
console.log(` ā Rewrote ${rewrittenFiles} file(s)`);
|
|
111
|
+
if (hasTasks) {
|
|
112
|
+
console.log(`\nā ļø Some changes need a decision - see ${MIGRATE_FROM_CDK_REPORT_PATH}.`);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
console.log('\nā
Nothing left that needs a decision.');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=migration.js.map
|