@mouryaamit/nx-polystack 1.14.0 → 1.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/cli/commands/ionic-standalone.d.ts +67 -0
- package/dist/src/cli/commands/ionic-standalone.js +694 -0
- package/dist/src/cli/commands/ionic-standalone.js.map +1 -0
- package/dist/src/cli/commands/upgrade-standalone.d.ts +8 -1
- package/dist/src/cli/commands/upgrade-standalone.js +42 -10
- package/dist/src/cli/commands/upgrade-standalone.js.map +1 -1
- package/dist/src/cli/help.js +7 -1
- package/dist/src/cli/help.js.map +1 -1
- package/dist/src/cli/launcher.js +1 -0
- package/dist/src/cli/launcher.js.map +1 -1
- package/dist/src/lib/utils/ionic-tags.d.ts +22 -0
- package/dist/src/lib/utils/ionic-tags.js +221 -0
- package/dist/src/lib/utils/ionic-tags.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runIonicStep = exports.convertIonicApp = exports.scanTags = exports.isIonicApp = exports.ionicContext = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
6
|
+
const path = tslib_1.__importStar(require("path"));
|
|
7
|
+
const angular_deps_1 = require("../../lib/utils/angular-deps");
|
|
8
|
+
const ionic_tags_1 = require("../../lib/utils/ionic-tags");
|
|
9
|
+
const standalone_providers_1 = require("./standalone-providers");
|
|
10
|
+
const upgrade_standalone_1 = require("./upgrade-standalone");
|
|
11
|
+
const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
12
|
+
/** The context for an Angular major, with the installed Ionic and ionicons exports when present. */
|
|
13
|
+
function ionicContext(ts, root, major) {
|
|
14
|
+
const standaloneEntry = (0, angular_deps_1.ionicStandaloneImport)(major);
|
|
15
|
+
const moving = new Set([(0, angular_deps_1.ionicAngularImport)(major)]);
|
|
16
|
+
// Ionic 9 has no /standalone entry; @ionic/migrate moves those imports, this catches leftovers.
|
|
17
|
+
if (standaloneEntry !== '@ionic/angular/standalone')
|
|
18
|
+
moving.add('@ionic/angular/standalone');
|
|
19
|
+
moving.delete(standaloneEntry);
|
|
20
|
+
const exports = (0, ionic_tags_1.moduleExports)(ts, root, standaloneEntry);
|
|
21
|
+
const installed = exports ? [...exports].filter((n) => /^Ion[A-Z]/.test(n)) : [];
|
|
22
|
+
return {
|
|
23
|
+
ts,
|
|
24
|
+
root,
|
|
25
|
+
major,
|
|
26
|
+
standaloneEntry,
|
|
27
|
+
moving,
|
|
28
|
+
classes: installed.length > 0 ? new Set(installed) : ionic_tags_1.STATIC_ION_CLASSES,
|
|
29
|
+
exports,
|
|
30
|
+
icons: (0, ionic_tags_1.moduleExports)(ts, root, 'ionicons/icons'),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
exports.ionicContext = ionicContext;
|
|
34
|
+
/** An app whose sources still import IonicModule from @ionic/angular (spec files aside). */
|
|
35
|
+
function isIonicApp(root, project) {
|
|
36
|
+
if (!project.app)
|
|
37
|
+
return false;
|
|
38
|
+
return (0, upgrade_standalone_1.tsFiles)(path.join(root, project.root, 'src')).some((f) => {
|
|
39
|
+
const text = fs.readFileSync(f, 'utf8');
|
|
40
|
+
return /\bIonicModule\b/.test(text) && /['"]@ionic\/angular(\/lazy)?['"]/.test(text);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
exports.isIonicApp = isIonicApp;
|
|
44
|
+
/** Opening tags of an HTML template with their attributes (quoted values may hold `>`). */
|
|
45
|
+
function scanTags(html) {
|
|
46
|
+
const out = [];
|
|
47
|
+
let i = 0;
|
|
48
|
+
while ((i = html.indexOf('<', i)) >= 0) {
|
|
49
|
+
const m = /^<([a-zA-Z][\w-]*)/.exec(html.slice(i, i + 200));
|
|
50
|
+
if (!m) {
|
|
51
|
+
i++;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
let j = i + m[0].length;
|
|
55
|
+
const attrs = [];
|
|
56
|
+
while (j < html.length) {
|
|
57
|
+
while (j < html.length && /\s/.test(html[j]))
|
|
58
|
+
j++;
|
|
59
|
+
if (html[j] === '>') {
|
|
60
|
+
j++;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
if (html.startsWith('/>', j)) {
|
|
64
|
+
j += 2;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
const a = /^[^\s=>"'/]+/.exec(html.slice(j));
|
|
68
|
+
if (!a) {
|
|
69
|
+
j++;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
j += a[0].length;
|
|
73
|
+
let k = j;
|
|
74
|
+
while (k < html.length && /\s/.test(html[k]))
|
|
75
|
+
k++;
|
|
76
|
+
let value;
|
|
77
|
+
if (html[k] === '=') {
|
|
78
|
+
k++;
|
|
79
|
+
while (k < html.length && /\s/.test(html[k]))
|
|
80
|
+
k++;
|
|
81
|
+
const q = html[k];
|
|
82
|
+
if (q === '"' || q === "'") {
|
|
83
|
+
const end = html.indexOf(q, k + 1);
|
|
84
|
+
value = html.slice(k + 1, end < 0 ? html.length : end);
|
|
85
|
+
j = end < 0 ? html.length : end + 1;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const u = /^[^\s>]+/.exec(html.slice(k));
|
|
89
|
+
value = u ? u[0] : '';
|
|
90
|
+
j = k + value.length;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
attrs.push({ name: a[0], value });
|
|
94
|
+
}
|
|
95
|
+
out.push({ name: m[1], attrs });
|
|
96
|
+
i = j;
|
|
97
|
+
}
|
|
98
|
+
return out;
|
|
99
|
+
}
|
|
100
|
+
exports.scanTags = scanTags;
|
|
101
|
+
const ICON_NAME = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
102
|
+
/** The Ion* classes and icons a template needs; problems go to reasons, loose icon names to loose. */
|
|
103
|
+
function templateNeeds(c, html, where, reasons, loose) {
|
|
104
|
+
const needs = { classes: new Set(), icons: new Set() };
|
|
105
|
+
const clean = html.replace(/<!--[\s\S]*?-->/g, '');
|
|
106
|
+
const found = [...clean.matchAll(/<(ion-[a-z][a-z0-9-]*)/g)].map((m) => m[1]);
|
|
107
|
+
const tags = scanTags(clean);
|
|
108
|
+
const scanned = tags.filter((t) => t.name.startsWith('ion-'));
|
|
109
|
+
if (scanned.length !== found.length) {
|
|
110
|
+
reasons.push(`${where}: the template's ion-* tags could not be read reliably`);
|
|
111
|
+
return needs;
|
|
112
|
+
}
|
|
113
|
+
const need = (cls, what) => {
|
|
114
|
+
if (c.classes.has(cls))
|
|
115
|
+
needs.classes.add(cls);
|
|
116
|
+
else
|
|
117
|
+
reasons.push(`${where}: ${what} has no standalone ${cls} in ${c.standaloneEntry}`);
|
|
118
|
+
};
|
|
119
|
+
for (const tag of tags) {
|
|
120
|
+
const ion = tag.name.startsWith('ion-');
|
|
121
|
+
if (ion)
|
|
122
|
+
need((0, ionic_tags_1.ionTagClass)(tag.name), `<${tag.name}>`);
|
|
123
|
+
const bare = (n) => n.replace(/^\[(attr\.)?/, '').replace(/\]$/, '');
|
|
124
|
+
if (tag.attrs.some((a) => bare(a.name) === 'routerLink')) {
|
|
125
|
+
if (ion)
|
|
126
|
+
need('IonRouterLink', `routerLink on <${tag.name}>`);
|
|
127
|
+
else if (tag.name === 'a')
|
|
128
|
+
need('IonRouterLinkWithHref', 'routerLink on <a>');
|
|
129
|
+
}
|
|
130
|
+
if (!ion)
|
|
131
|
+
continue;
|
|
132
|
+
for (const a of tag.attrs) {
|
|
133
|
+
const name = bare(a.name);
|
|
134
|
+
const shown = `${where}: <${tag.name} ${a.name}${a.value !== undefined ? `="${a.value}"` : ''}>`;
|
|
135
|
+
if (tag.name === 'ion-icon' && ['name', 'ios', 'md'].includes(name)) {
|
|
136
|
+
let literal;
|
|
137
|
+
if (a.name.startsWith('[')) {
|
|
138
|
+
literal = /^\s*'([^']*)'\s*$/.exec(a.value ?? '')?.[1];
|
|
139
|
+
}
|
|
140
|
+
else if (a.value !== undefined && !a.value.includes('{{')) {
|
|
141
|
+
literal = a.value;
|
|
142
|
+
}
|
|
143
|
+
if (literal === undefined)
|
|
144
|
+
loose.push(`${shown} (bound name)`);
|
|
145
|
+
else if (!ICON_NAME.test(literal))
|
|
146
|
+
loose.push(`${shown} (not an icon name)`);
|
|
147
|
+
else if (c.icons && !c.icons.has((0, ionic_tags_1.iconExportName)(literal)))
|
|
148
|
+
loose.push(`${shown} (not in ionicons/icons)`);
|
|
149
|
+
else
|
|
150
|
+
needs.icons.add(literal);
|
|
151
|
+
}
|
|
152
|
+
else if ((tag.name === 'ion-icon' && name === 'icon') ||
|
|
153
|
+
(tag.name !== 'ion-icon' && /icon$/i.test(name))) {
|
|
154
|
+
loose.push(`${shown} (icon name outside ion-icon name)`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return needs;
|
|
159
|
+
}
|
|
160
|
+
/** The decorator call (`Component`, `NgModule`, …) a node sits in, if any. */
|
|
161
|
+
function enclosingDecorator(ts, node) {
|
|
162
|
+
for (let n = node; n; n = n.parent) {
|
|
163
|
+
if (ts.isDecorator(n)) {
|
|
164
|
+
const e = n.expression;
|
|
165
|
+
return ts.isCallExpression(e) && ts.isIdentifier(e.expression) ? e.expression.text : '?';
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
const propName = (ts, p) => p.name && (ts.isIdentifier(p.name) || ts.isStringLiteral(p.name)) ? p.name.text : undefined;
|
|
171
|
+
/** Leading whitespace of the line a node starts on. */
|
|
172
|
+
function indentOf(text, at) {
|
|
173
|
+
const lineStart = text.lastIndexOf('\n', at - 1) + 1;
|
|
174
|
+
return /^[ \t]*/.exec(text.slice(lineStart))?.[0] ?? '';
|
|
175
|
+
}
|
|
176
|
+
/** The edit that registers icons in a class constructor; a string is why it cannot. */
|
|
177
|
+
function iconEdit(ts, sf, cls, icons) {
|
|
178
|
+
const text = sf.text;
|
|
179
|
+
const fmt = ([key, local]) => (key === local ? key : `${key}: ${local}`);
|
|
180
|
+
let existing;
|
|
181
|
+
const find = (n) => {
|
|
182
|
+
if (ts.isCallExpression(n) &&
|
|
183
|
+
ts.isIdentifier(n.expression) &&
|
|
184
|
+
n.expression.text === 'addIcons') {
|
|
185
|
+
existing = n;
|
|
186
|
+
}
|
|
187
|
+
if (!existing)
|
|
188
|
+
ts.forEachChild(n, find);
|
|
189
|
+
};
|
|
190
|
+
find(cls);
|
|
191
|
+
if (existing) {
|
|
192
|
+
const obj = existing.arguments[0];
|
|
193
|
+
if (existing.arguments.length !== 1 || !ts.isObjectLiteralExpression(obj)) {
|
|
194
|
+
return 'its addIcons call does not take an object literal';
|
|
195
|
+
}
|
|
196
|
+
const have = new Set(obj.properties.map((p) => propName(ts, p)));
|
|
197
|
+
const add = icons.filter(([key]) => !have.has(key));
|
|
198
|
+
if (add.length === 0)
|
|
199
|
+
return undefined;
|
|
200
|
+
const props = [...obj.properties.map((p) => p.getText(sf)), ...add.map(fmt)];
|
|
201
|
+
return { start: obj.getStart(sf), end: obj.getEnd(), text: `{ ${props.join(', ')} }` };
|
|
202
|
+
}
|
|
203
|
+
const line = `addIcons({ ${icons.map(fmt).join(', ')} });`;
|
|
204
|
+
const memberIndent = cls.members.length > 0
|
|
205
|
+
? indentOf(text, cls.members[0].getStart(sf))
|
|
206
|
+
: `${indentOf(text, cls.getStart(sf))} `;
|
|
207
|
+
const ctor = cls.members.find((m) => ts.isConstructorDeclaration(m) && m.body !== undefined);
|
|
208
|
+
if (ctor?.body) {
|
|
209
|
+
const first = ctor.body.statements[0];
|
|
210
|
+
const superFirst = first !== undefined &&
|
|
211
|
+
ts.isExpressionStatement(first) &&
|
|
212
|
+
ts.isCallExpression(first.expression) &&
|
|
213
|
+
first.expression.expression.kind === ts.SyntaxKind.SuperKeyword;
|
|
214
|
+
if (superFirst) {
|
|
215
|
+
const at = first.getEnd();
|
|
216
|
+
return { start: at, end: at, text: `\n${indentOf(text, first.getStart(sf))}${line}` };
|
|
217
|
+
}
|
|
218
|
+
if (first) {
|
|
219
|
+
const at = first.getStart(sf);
|
|
220
|
+
return { start: at, end: at, text: `${line}\n${indentOf(text, at)}` };
|
|
221
|
+
}
|
|
222
|
+
const ci = indentOf(text, ctor.getStart(sf));
|
|
223
|
+
return {
|
|
224
|
+
start: ctor.body.getStart(sf),
|
|
225
|
+
end: ctor.body.getEnd(),
|
|
226
|
+
text: `{\n${ci} ${line}\n${ci}}`,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
if (cls.heritageClauses?.some((h) => h.token === ts.SyntaxKind.ExtendsKeyword)) {
|
|
230
|
+
return 'it extends another class and has no constructor to register icons in';
|
|
231
|
+
}
|
|
232
|
+
const ctorText = `constructor() {\n${memberIndent} ${line}\n${memberIndent}}`;
|
|
233
|
+
let anchor;
|
|
234
|
+
for (const m of cls.members) {
|
|
235
|
+
if (!ts.isPropertyDeclaration(m))
|
|
236
|
+
break;
|
|
237
|
+
anchor = m;
|
|
238
|
+
}
|
|
239
|
+
if (anchor) {
|
|
240
|
+
return { start: anchor.getEnd(), end: anchor.getEnd(), text: `\n\n${memberIndent}${ctorText}` };
|
|
241
|
+
}
|
|
242
|
+
const at = cls.members.pos;
|
|
243
|
+
const after = cls.members.length > 0 ? '\n' : '';
|
|
244
|
+
return { start: at, end: at, text: `\n${memberIndent}${ctorText}\n${after}` };
|
|
245
|
+
}
|
|
246
|
+
/** Converts one source file of an app into app (writes, done, reasons). */
|
|
247
|
+
function convertFile(c, file, app) {
|
|
248
|
+
const { ts } = c;
|
|
249
|
+
const rel = path.relative(c.root, file).split(path.sep).join('/');
|
|
250
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
251
|
+
// Icon names in code (overlay buttons, toasts, …) only work through the svg asset.
|
|
252
|
+
for (const m of text.matchAll(/\b\w*[iI]con\s*:\s*['"`]([^'"`]*)['"`]/g)) {
|
|
253
|
+
app.loose.push(`${rel}: ${m[0]} (icon name in code)`);
|
|
254
|
+
}
|
|
255
|
+
if (/\bprovideIonicAngular\s*\(/.test(text))
|
|
256
|
+
app.providers = true;
|
|
257
|
+
if (!/@ionic\/angular|@Component\s*\(/.test(text))
|
|
258
|
+
return;
|
|
259
|
+
const sf = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true);
|
|
260
|
+
const reasons = [];
|
|
261
|
+
const edits = [];
|
|
262
|
+
const what = [];
|
|
263
|
+
// Imports from Ionic's entries.
|
|
264
|
+
const merged = new Map();
|
|
265
|
+
const standaloneDecls = [];
|
|
266
|
+
const movingDecls = [];
|
|
267
|
+
const moved = [];
|
|
268
|
+
let ionicModule;
|
|
269
|
+
let lastImport;
|
|
270
|
+
for (const st of sf.statements) {
|
|
271
|
+
if (!ts.isImportDeclaration(st) || !ts.isStringLiteral(st.moduleSpecifier))
|
|
272
|
+
continue;
|
|
273
|
+
lastImport = st;
|
|
274
|
+
const spec = st.moduleSpecifier.text;
|
|
275
|
+
const moving = c.moving.has(spec);
|
|
276
|
+
if (!moving && spec !== c.standaloneEntry)
|
|
277
|
+
continue;
|
|
278
|
+
const clause = st.importClause;
|
|
279
|
+
if (!clause ||
|
|
280
|
+
clause.name ||
|
|
281
|
+
!clause.namedBindings ||
|
|
282
|
+
!ts.isNamedImports(clause.namedBindings)) {
|
|
283
|
+
if (moving)
|
|
284
|
+
reasons.push(`${rel}: the import from '${spec}' is not a named import`);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
for (const el of clause.namedBindings.elements) {
|
|
288
|
+
const imported = (el.propertyName ?? el.name).text;
|
|
289
|
+
if (imported === 'IonicModule') {
|
|
290
|
+
if (el.propertyName)
|
|
291
|
+
reasons.push(`${rel}: IonicModule is imported under another name`);
|
|
292
|
+
ionicModule = el.name.text;
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
if (moving && c.exports && !c.exports.has(imported)) {
|
|
296
|
+
reasons.push(`${rel}: ${imported} is not exported by ${c.standaloneEntry}`);
|
|
297
|
+
}
|
|
298
|
+
if (moving)
|
|
299
|
+
moved.push(el.name.text);
|
|
300
|
+
const elText = clause.isTypeOnly ? `type ${el.getText(sf)}` : el.getText(sf);
|
|
301
|
+
if (!merged.has(el.name.text))
|
|
302
|
+
merged.set(el.name.text, elText);
|
|
303
|
+
}
|
|
304
|
+
(moving ? movingDecls : standaloneDecls).push(st);
|
|
305
|
+
}
|
|
306
|
+
// Where IonicModule is used.
|
|
307
|
+
const componentRefs = new Map();
|
|
308
|
+
const forRoots = [];
|
|
309
|
+
if (ionicModule) {
|
|
310
|
+
const local = ionicModule;
|
|
311
|
+
const visit = (n) => {
|
|
312
|
+
if (ts.isImportDeclaration(n))
|
|
313
|
+
return;
|
|
314
|
+
if (ts.isIdentifier(n) && n.text === local) {
|
|
315
|
+
const p = n.parent;
|
|
316
|
+
const decorator = enclosingDecorator(ts, n);
|
|
317
|
+
const isImportsArray = ts.isArrayLiteralExpression(p) &&
|
|
318
|
+
ts.isPropertyAssignment(p.parent) &&
|
|
319
|
+
propName(ts, p.parent) === 'imports';
|
|
320
|
+
if (isImportsArray && decorator === 'Component') {
|
|
321
|
+
let cls = p;
|
|
322
|
+
while (cls && !ts.isClassDeclaration(cls))
|
|
323
|
+
cls = cls.parent;
|
|
324
|
+
if (cls)
|
|
325
|
+
componentRefs.set(cls, p);
|
|
326
|
+
}
|
|
327
|
+
else if (ts.isPropertyAccessExpression(p) &&
|
|
328
|
+
p.name.text === 'forRoot' &&
|
|
329
|
+
ts.isCallExpression(p.parent) &&
|
|
330
|
+
p.parent.expression === p &&
|
|
331
|
+
ts.isCallExpression(p.parent.parent) &&
|
|
332
|
+
ts.isIdentifier(p.parent.parent.expression) &&
|
|
333
|
+
p.parent.parent.expression.text === 'importProvidersFrom' &&
|
|
334
|
+
ts.isArrayLiteralExpression(p.parent.parent.parent) &&
|
|
335
|
+
['main.ts', 'app.config.ts'].includes(path.basename(file))) {
|
|
336
|
+
forRoots.push(p.parent);
|
|
337
|
+
}
|
|
338
|
+
else if (decorator === 'NgModule') {
|
|
339
|
+
reasons.push(`${rel}: an NgModule still imports IonicModule`);
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
reasons.push(`${rel}: IonicModule is used outside a component's imports or importProvidersFrom`);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
ts.forEachChild(n, visit);
|
|
346
|
+
};
|
|
347
|
+
visit(sf);
|
|
348
|
+
}
|
|
349
|
+
// provideIonicAngular instead of IonicModule.forRoot.
|
|
350
|
+
const added = new Set();
|
|
351
|
+
const swaps = new Map();
|
|
352
|
+
for (const f of forRoots) {
|
|
353
|
+
const call = f.parent;
|
|
354
|
+
swaps.set(call, [...(swaps.get(call) ?? []), f]);
|
|
355
|
+
}
|
|
356
|
+
for (const [call, roots] of swaps) {
|
|
357
|
+
const keep = call.arguments.filter((a) => !roots.includes(a));
|
|
358
|
+
const provides = roots.map((r) => `provideIonicAngular(${r.arguments.map((a) => a.getText(sf)).join(', ')})`);
|
|
359
|
+
const parts = [
|
|
360
|
+
...(keep.length > 0
|
|
361
|
+
? [`importProvidersFrom(${keep.map((a) => a.getText(sf)).join(', ')})`]
|
|
362
|
+
: []),
|
|
363
|
+
...provides,
|
|
364
|
+
];
|
|
365
|
+
edits.push({ start: call.getStart(sf), end: call.getEnd(), text: parts.join(', ') });
|
|
366
|
+
added.add('provideIonicAngular');
|
|
367
|
+
app.providers = true;
|
|
368
|
+
what.push('provideIonicAngular() instead of IonicModule.forRoot()');
|
|
369
|
+
}
|
|
370
|
+
// Components.
|
|
371
|
+
const icons = [];
|
|
372
|
+
const iconImports = new Set();
|
|
373
|
+
for (const cls of sf.statements.filter(ts.isClassDeclaration)) {
|
|
374
|
+
const decorators = (ts.canHaveDecorators?.(cls) ? ts.getDecorators?.(cls) : undefined) ?? [];
|
|
375
|
+
const dec = decorators.find((d) => ts.isCallExpression(d.expression) &&
|
|
376
|
+
ts.isIdentifier(d.expression.expression) &&
|
|
377
|
+
d.expression.expression.text === 'Component');
|
|
378
|
+
if (!dec)
|
|
379
|
+
continue;
|
|
380
|
+
const name = cls.name?.text ?? 'a component';
|
|
381
|
+
const where = `${rel} (${name})`;
|
|
382
|
+
const meta = dec.expression.arguments[0];
|
|
383
|
+
if (!meta || !ts.isObjectLiteralExpression(meta)) {
|
|
384
|
+
reasons.push(`${where}: @Component metadata is not an object literal`);
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
const props = new Map();
|
|
388
|
+
for (const p of meta.properties) {
|
|
389
|
+
const key = propName(ts, p);
|
|
390
|
+
if (key)
|
|
391
|
+
props.set(key, p);
|
|
392
|
+
}
|
|
393
|
+
const value = (key) => {
|
|
394
|
+
const p = props.get(key);
|
|
395
|
+
return p && ts.isPropertyAssignment(p) ? p.initializer : p ? null : undefined;
|
|
396
|
+
};
|
|
397
|
+
const literal = (e) => e && (ts.isStringLiteral(e) || ts.isNoSubstitutionTemplateLiteral(e)) ? e.text : undefined;
|
|
398
|
+
let html;
|
|
399
|
+
let templateRange;
|
|
400
|
+
const tpl = value('template');
|
|
401
|
+
const url = value('templateUrl');
|
|
402
|
+
if (tpl !== undefined) {
|
|
403
|
+
html = literal(tpl);
|
|
404
|
+
if (html === undefined) {
|
|
405
|
+
reasons.push(`${where}: the template is built at runtime`);
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
if (tpl)
|
|
409
|
+
templateRange = [tpl.getStart(sf), tpl.getEnd()];
|
|
410
|
+
}
|
|
411
|
+
else if (url !== undefined) {
|
|
412
|
+
const u = literal(url);
|
|
413
|
+
const f = u !== undefined ? path.resolve(path.dirname(file), u) : undefined;
|
|
414
|
+
if (!f || !fs.existsSync(f)) {
|
|
415
|
+
reasons.push(`${where}: template ${u ?? url?.getText(sf) ?? '?'} not found`);
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
html = fs.readFileSync(f, 'utf8');
|
|
419
|
+
}
|
|
420
|
+
const needs = html
|
|
421
|
+
? templateNeeds(c, html, where, reasons, app.loose)
|
|
422
|
+
: { classes: new Set(), icons: new Set() };
|
|
423
|
+
const ionicArray = componentRefs.get(cls);
|
|
424
|
+
if (needs.classes.size === 0 && needs.icons.size === 0 && !ionicArray)
|
|
425
|
+
continue;
|
|
426
|
+
const standalone = value('standalone');
|
|
427
|
+
const isStandalone = standalone?.kind === ts.SyntaxKind.TrueKeyword || (standalone === undefined && c.major >= 19);
|
|
428
|
+
if (!isStandalone) {
|
|
429
|
+
reasons.push(`${where}: not a standalone component`);
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
const importsValue = value('imports');
|
|
433
|
+
if (importsValue === null || (importsValue && !ts.isArrayLiteralExpression(importsValue))) {
|
|
434
|
+
reasons.push(`${where}: imports is not an array literal`);
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
const importsInit = importsValue;
|
|
438
|
+
const existing = importsInit ? importsInit.elements.map((e) => e.getText(sf)) : [];
|
|
439
|
+
const ionClasses = [...needs.classes].sort();
|
|
440
|
+
const next = [
|
|
441
|
+
...existing.filter((e) => e !== ionicModule),
|
|
442
|
+
...ionClasses.filter((n) => !existing.includes(n)),
|
|
443
|
+
];
|
|
444
|
+
if (importsInit) {
|
|
445
|
+
if (next.join() !== existing.join()) {
|
|
446
|
+
edits.push({
|
|
447
|
+
start: importsInit.getStart(sf),
|
|
448
|
+
end: importsInit.getEnd(),
|
|
449
|
+
text: `[${next.join(', ')}]`,
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
else if (next.length > 0) {
|
|
454
|
+
const after = props.get('standalone') ?? props.get('selector');
|
|
455
|
+
const at = after ? after.getEnd() : meta.getStart(sf) + 1;
|
|
456
|
+
const ind = indentOf(text, (after ?? meta.properties[0] ?? meta).getStart(sf));
|
|
457
|
+
edits.push({
|
|
458
|
+
start: at,
|
|
459
|
+
end: at,
|
|
460
|
+
text: after ? `,\n${ind}imports: [${next.join(', ')}]` : ` imports: [${next.join(', ')}],`,
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
ionClasses.forEach((n) => added.add(n));
|
|
464
|
+
const parts = [
|
|
465
|
+
...(ionicArray ? ['IonicModule out'] : []),
|
|
466
|
+
...(ionClasses.length > 0 ? [ionClasses.join(', ')] : []),
|
|
467
|
+
];
|
|
468
|
+
if (needs.icons.size > 0) {
|
|
469
|
+
// A local name an icon import would shadow gets an alias.
|
|
470
|
+
const outside = templateRange
|
|
471
|
+
? text.slice(0, templateRange[0]) + text.slice(templateRange[1])
|
|
472
|
+
: text;
|
|
473
|
+
const mine = [];
|
|
474
|
+
for (const kebab of [...needs.icons].sort()) {
|
|
475
|
+
const key = (0, ionic_tags_1.iconExportName)(kebab);
|
|
476
|
+
let local = key;
|
|
477
|
+
if (new RegExp(`\\b${key}\\b`).test(outside))
|
|
478
|
+
local = `${key}Icon`;
|
|
479
|
+
if (local !== key && new RegExp(`\\b${local}\\b`).test(outside)) {
|
|
480
|
+
reasons.push(`${where}: icon ${kebab} clashes with names in the file`);
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
mine.push([key, local]);
|
|
484
|
+
}
|
|
485
|
+
const e = iconEdit(ts, sf, cls, mine);
|
|
486
|
+
if (typeof e === 'string') {
|
|
487
|
+
reasons.push(`${where}: ${e}`);
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
if (e) {
|
|
491
|
+
edits.push(e);
|
|
492
|
+
icons.push(...mine);
|
|
493
|
+
mine.forEach(([key, local]) => iconImports.add(key === local ? key : `${key} as ${local}`));
|
|
494
|
+
parts.push(`addIcons(${mine.map(([k]) => k).join(', ')})`);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
what.push(`${name}: ${parts.join('; ')}`);
|
|
498
|
+
}
|
|
499
|
+
if (reasons.length > 0) {
|
|
500
|
+
app.reasons.push(...reasons);
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
if (edits.length === 0 && movingDecls.length === 0)
|
|
504
|
+
return;
|
|
505
|
+
if (moved.length > 0)
|
|
506
|
+
what.push(`${moved.join(', ')} from ${c.standaloneEntry}`);
|
|
507
|
+
// One import from the standalone entry, where the first Ionic import was.
|
|
508
|
+
for (const n of added)
|
|
509
|
+
if (!merged.has(n))
|
|
510
|
+
merged.set(n, n);
|
|
511
|
+
const decls = [...standaloneDecls, ...movingDecls].sort((a, b) => a.pos - b.pos);
|
|
512
|
+
const typeOnly = [...merged.values()].every((v) => v.startsWith('type '));
|
|
513
|
+
const line = merged.size === 0
|
|
514
|
+
? ''
|
|
515
|
+
: typeOnly
|
|
516
|
+
? `import type { ${[...merged.values()].map((v) => v.slice(5)).join(', ')} } from '${c.standaloneEntry}';`
|
|
517
|
+
: `import { ${[...merged.values()].join(', ')} } from '${c.standaloneEntry}';`;
|
|
518
|
+
decls.forEach((d, i) => {
|
|
519
|
+
let end = d.getEnd();
|
|
520
|
+
if (i > 0 || !line)
|
|
521
|
+
end += /^\r?\n/.exec(text.slice(end))?.[0].length ?? 0;
|
|
522
|
+
edits.push({ start: d.getStart(sf), end, text: i === 0 ? line : '' });
|
|
523
|
+
});
|
|
524
|
+
if (decls.length === 0 && line) {
|
|
525
|
+
const at = lastImport ? lastImport.getEnd() : 0;
|
|
526
|
+
edits.push({ start: at, end: at, text: lastImport ? `\n${line}` : `${line}\n` });
|
|
527
|
+
}
|
|
528
|
+
let out = text;
|
|
529
|
+
for (const e of edits.sort((a, b) => b.start - a.start)) {
|
|
530
|
+
out = out.slice(0, e.start) + e.text + out.slice(e.end);
|
|
531
|
+
}
|
|
532
|
+
if (icons.length > 0) {
|
|
533
|
+
out = (0, standalone_providers_1.addNamedImports)(out, ['addIcons'], 'ionicons');
|
|
534
|
+
out = (0, standalone_providers_1.addNamedImports)(out, [...iconImports], 'ionicons/icons');
|
|
535
|
+
}
|
|
536
|
+
if (swaps.size > 0)
|
|
537
|
+
out = (0, upgrade_standalone_1.dropUnusedNamedImport)(out, 'importProvidersFrom', '@angular/core');
|
|
538
|
+
if (out === text)
|
|
539
|
+
return;
|
|
540
|
+
app.writes.set(file, out);
|
|
541
|
+
app.done.push(`${rel}: ${what.join('; ')}`);
|
|
542
|
+
}
|
|
543
|
+
/** Spec files of a directory tree (not converted; reported when they use IonicModule). */
|
|
544
|
+
function specFiles(dir) {
|
|
545
|
+
const out = [];
|
|
546
|
+
const walk = (d) => {
|
|
547
|
+
if (!fs.existsSync(d))
|
|
548
|
+
return;
|
|
549
|
+
for (const e of fs.readdirSync(d, { withFileTypes: true })) {
|
|
550
|
+
if (e.name === 'node_modules' || e.name.startsWith('.'))
|
|
551
|
+
continue;
|
|
552
|
+
const p = path.join(d, e.name);
|
|
553
|
+
if (e.isDirectory())
|
|
554
|
+
walk(p);
|
|
555
|
+
else if (e.name.endsWith('.spec.ts'))
|
|
556
|
+
out.push(p);
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
walk(dir);
|
|
560
|
+
return out;
|
|
561
|
+
}
|
|
562
|
+
/** Import aliases (tsconfig paths) of libraries whose sources still use IonicModule. */
|
|
563
|
+
function ionicModuleLibAliases(root, libs) {
|
|
564
|
+
const withModule = libs.filter((l) => (0, upgrade_standalone_1.tsFiles)(path.join(root, l.root)).some((f) => /\bIonicModule\b/.test(fs.readFileSync(f, 'utf8'))));
|
|
565
|
+
if (withModule.length === 0)
|
|
566
|
+
return [];
|
|
567
|
+
let paths = {};
|
|
568
|
+
for (const name of ['tsconfig.base.json', 'tsconfig.json']) {
|
|
569
|
+
try {
|
|
570
|
+
const json = JSON.parse(fs.readFileSync(path.join(root, name), 'utf8'));
|
|
571
|
+
const co = json['compilerOptions'];
|
|
572
|
+
if (isObject(co) && isObject(co['paths'])) {
|
|
573
|
+
paths = co['paths'];
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
catch {
|
|
578
|
+
// no such tsconfig, or not plain JSON
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
return Object.entries(paths)
|
|
582
|
+
.filter(([, targets]) => Array.isArray(targets) &&
|
|
583
|
+
targets.some((t) => typeof t === 'string' &&
|
|
584
|
+
withModule.some((l) => t.replace(/^\.\//, '').startsWith(`${l.root}/`))))
|
|
585
|
+
.map(([alias]) => alias.replace(/\/\*$/, ''));
|
|
586
|
+
}
|
|
587
|
+
/** Converts one Ionic app (nothing is written here; see IonicAppResult). */
|
|
588
|
+
function convertIonicApp(c, project, libs = []) {
|
|
589
|
+
const src = path.join(c.root, project.root, 'src');
|
|
590
|
+
const app = {
|
|
591
|
+
writes: new Map(),
|
|
592
|
+
done: [],
|
|
593
|
+
notes: [],
|
|
594
|
+
reasons: [],
|
|
595
|
+
loose: [],
|
|
596
|
+
providers: false,
|
|
597
|
+
};
|
|
598
|
+
const files = (0, upgrade_standalone_1.tsFiles)(src);
|
|
599
|
+
for (const f of files)
|
|
600
|
+
convertFile(c, f, app);
|
|
601
|
+
if (!app.providers) {
|
|
602
|
+
app.reasons.push(`${project.root}/src: no importProvidersFrom(IonicModule.forRoot()) in main.ts or app/app.config.ts to replace`);
|
|
603
|
+
}
|
|
604
|
+
for (const alias of ionicModuleLibAliases(c.root, libs)) {
|
|
605
|
+
const esc = alias.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&');
|
|
606
|
+
const re = new RegExp(`from\\s*['"]${esc}(/[^'"]*)?['"]`);
|
|
607
|
+
if (files.some((f) => re.test(fs.readFileSync(f, 'utf8')))) {
|
|
608
|
+
app.reasons.push(`${project.root}: imports ${alias}, a library that still uses IonicModule`);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
if (app.reasons.length > 0)
|
|
612
|
+
return { ...app, writes: new Map(), done: [] };
|
|
613
|
+
const specs = specFiles(src).filter((f) => /\bIonicModule\b/.test(fs.readFileSync(f, 'utf8')));
|
|
614
|
+
if (specs.length > 0) {
|
|
615
|
+
app.notes.push(`${specs
|
|
616
|
+
.map((f) => path.relative(c.root, f).split(path.sep).join('/'))
|
|
617
|
+
.join(', ')}: spec files still use IonicModule (left as they are; ` +
|
|
618
|
+
'TestBed can use provideIonicAngular() and the components instead)');
|
|
619
|
+
}
|
|
620
|
+
// The ionicons svg asset: only needed for icon names addIcons does not register.
|
|
621
|
+
const pjFile = path.join(c.root, project.root, 'project.json');
|
|
622
|
+
let pj;
|
|
623
|
+
try {
|
|
624
|
+
pj = JSON.parse(fs.readFileSync(pjFile, 'utf8'));
|
|
625
|
+
}
|
|
626
|
+
catch {
|
|
627
|
+
pj = undefined;
|
|
628
|
+
}
|
|
629
|
+
const targets = pj && isObject(pj['targets']) ? pj['targets'] : {};
|
|
630
|
+
const build = isObject(targets['build']) ? targets['build'] : {};
|
|
631
|
+
const options = isObject(build['options']) ? build['options'] : {};
|
|
632
|
+
const assets = Array.isArray(options['assets']) ? options['assets'] : [];
|
|
633
|
+
const isIconAsset = (a) => (typeof a === 'string' ? a : isObject(a) ? String(a['input'] ?? '') : '').includes('ionicons/dist/ionicons/svg');
|
|
634
|
+
if (assets.some(isIconAsset)) {
|
|
635
|
+
if (app.loose.length > 0) {
|
|
636
|
+
app.notes.push(`${project.root}/project.json: ionicons svg asset kept, for icon names addIcons does not ` +
|
|
637
|
+
`register: ${app.loose.join('; ')}`);
|
|
638
|
+
}
|
|
639
|
+
else {
|
|
640
|
+
options['assets'] = assets.filter((a) => !isIconAsset(a));
|
|
641
|
+
app.writes.set(pjFile, JSON.stringify(pj, null, 2) + '\n');
|
|
642
|
+
app.done.push(`${project.root}/project.json: ionicons svg asset removed (icons come from addIcons)`);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
else if (app.loose.length > 0) {
|
|
646
|
+
app.notes.push(`icon names addIcons does not register: ${app.loose.join('; ')}`);
|
|
647
|
+
}
|
|
648
|
+
return app;
|
|
649
|
+
}
|
|
650
|
+
exports.convertIonicApp = convertIonicApp;
|
|
651
|
+
/**
|
|
652
|
+
* The ionic-standalone step on these apps: writes the changes of every app that converts safely,
|
|
653
|
+
* reports the others, and returns the files written (workspace-relative).
|
|
654
|
+
*/
|
|
655
|
+
function runIonicStep(root, apps, libs, angularMajor, say, log) {
|
|
656
|
+
const ts = (0, standalone_providers_1.loadTypescript)(root);
|
|
657
|
+
if (!ts) {
|
|
658
|
+
log(' typescript is not installed in the workspace; ionic-standalone skipped.');
|
|
659
|
+
return [];
|
|
660
|
+
}
|
|
661
|
+
const c = ionicContext(ts, root, angularMajor);
|
|
662
|
+
if (!c.exports) {
|
|
663
|
+
log(` ${c.standaloneEntry} is not installed; the built-in list of Ionic components is used.`);
|
|
664
|
+
}
|
|
665
|
+
const written = [];
|
|
666
|
+
for (const p of apps) {
|
|
667
|
+
if (!(0, upgrade_standalone_1.standaloneMain)(root, p)) {
|
|
668
|
+
log(` ${p.name}: still bootstraps an NgModule; ionic-standalone skipped.`);
|
|
669
|
+
continue;
|
|
670
|
+
}
|
|
671
|
+
if (!isIonicApp(root, p))
|
|
672
|
+
continue;
|
|
673
|
+
const r = convertIonicApp(c, p, libs);
|
|
674
|
+
if (r.reasons.length > 0) {
|
|
675
|
+
log(` ${p.name}: left on IonicModule (no file changed):`);
|
|
676
|
+
for (const reason of r.reasons)
|
|
677
|
+
log(` - ${reason}`);
|
|
678
|
+
continue;
|
|
679
|
+
}
|
|
680
|
+
for (const [file, text] of r.writes) {
|
|
681
|
+
fs.writeFileSync(file, text);
|
|
682
|
+
written.push(path.relative(root, file).split(path.sep).join('/'));
|
|
683
|
+
}
|
|
684
|
+
for (const d of r.done)
|
|
685
|
+
say(d);
|
|
686
|
+
for (const n of r.notes)
|
|
687
|
+
log(` ${n}`);
|
|
688
|
+
}
|
|
689
|
+
if (written.length === 0)
|
|
690
|
+
say('nothing to change');
|
|
691
|
+
return written;
|
|
692
|
+
}
|
|
693
|
+
exports.runIonicStep = runIonicStep;
|
|
694
|
+
//# sourceMappingURL=ionic-standalone.js.map
|