@ng-native/migrate 0.22.2 → 0.24.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/available-signal.cjs +639 -0
- package/migrations.cjs +16 -0
- package/native-app.cjs +138 -7
- package/os-comparisons-on-macos.cjs +149 -0
- package/package.json +1 -1
|
@@ -0,0 +1,639 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.23.0 makes every service's `available` a `Signal<boolean>`, read as `available()`. It used to
|
|
3
|
+
* come in five shapes:
|
|
4
|
+
*
|
|
5
|
+
* - a property, read as `x.available`: `DevMenu`, `Haptics`, `SplashScreen`, `Tracking`, `Fonts` and
|
|
6
|
+
* `Watch`. Each read is rewritten to `x.available()`.
|
|
7
|
+
* - a method that resolved, read as `await x.available()`: `Biometrics`, `AppleSignIn`,
|
|
8
|
+
* `ScreenCapture` and `StoreReview`. Each `await x.available()` is rewritten to `x.available()`
|
|
9
|
+
* and noted, since the signal is false until the platform answers, where the await waited for
|
|
10
|
+
* it; any other use of the promise, such as `.then(...)`, is noted.
|
|
11
|
+
* - a signal of `boolean | null`, the sensors': a comparison of `x.available()` with `null` or
|
|
12
|
+
* `undefined`, or a `??` after it, is noted, since it is never null now.
|
|
13
|
+
*
|
|
14
|
+
* What a member is read from is followed as `tracking-available-getter` follows it: `inject(X)`
|
|
15
|
+
* itself, `this.` a member of the enclosing class that holds one, or a name whose nearest
|
|
16
|
+
* declaration holds one, where holding one means initialised with `inject(X)`, `X` being the
|
|
17
|
+
* import rather than a name that shadows it, or typed `X`. `splashScreen`, the instance the splash
|
|
18
|
+
* screen entry exports, is one too. A component's template, inline or in the file its
|
|
19
|
+
* `templateUrl` names, reads that component's own members, so a property read there is rewritten
|
|
20
|
+
* for those alone, and not where the name is read off something else (`model.haptics`). A template
|
|
21
|
+
* file the components using it hold different things in is noted instead. A namespace import is
|
|
22
|
+
* noted.
|
|
23
|
+
*/
|
|
24
|
+
const { applied, scriptKind, SOURCE, typescript } = require('./move-imports.cjs');
|
|
25
|
+
const { files } = require('./host.cjs');
|
|
26
|
+
const { ELSE, isStatic, nearestBinding, thisOf } = require('./scope.cjs');
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Each service by its entry point: the names it is injected or typed as, instances an entry
|
|
30
|
+
* exports, and the shape its `available` had.
|
|
31
|
+
*/
|
|
32
|
+
const SERVICES = [
|
|
33
|
+
{ entry: '@ng-native/device', names: ['DevMenu'], shape: 'property' },
|
|
34
|
+
{ entry: '@ng-native/expo/haptics', names: ['Haptics'], shape: 'property' },
|
|
35
|
+
{ entry: '@ng-native/expo/tracking', names: ['Tracking'], shape: 'property' },
|
|
36
|
+
{ entry: '@ng-native/expo/fonts', names: ['Fonts', 'FontRegistry'], shape: 'property' },
|
|
37
|
+
{
|
|
38
|
+
entry: '@ng-native/expo/splash-screen',
|
|
39
|
+
names: ['SplashScreen', 'Splash'],
|
|
40
|
+
instances: ['splashScreen'],
|
|
41
|
+
shape: 'property',
|
|
42
|
+
},
|
|
43
|
+
{ entry: '@ng-native/expo/watch', names: ['Watch'], shape: 'property' },
|
|
44
|
+
{ entry: '@ng-native/expo/biometrics', names: ['Biometrics'], shape: 'promise' },
|
|
45
|
+
{ entry: '@ng-native/expo/apple-sign-in', names: ['AppleSignIn'], shape: 'promise' },
|
|
46
|
+
{ entry: '@ng-native/expo/screen-capture', names: ['ScreenCapture'], shape: 'promise' },
|
|
47
|
+
{ entry: '@ng-native/expo/store-review', names: ['StoreReview'], shape: 'promise' },
|
|
48
|
+
{
|
|
49
|
+
entry: '@ng-native/expo/sensors',
|
|
50
|
+
names: [
|
|
51
|
+
'Accelerometer',
|
|
52
|
+
'Gyroscope',
|
|
53
|
+
'Magnetometer',
|
|
54
|
+
'DeviceMotion',
|
|
55
|
+
'Barometer',
|
|
56
|
+
'LightSensor',
|
|
57
|
+
'Sensor',
|
|
58
|
+
],
|
|
59
|
+
shape: 'nullable',
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
const ENTRIES = new Set(SERVICES.map((service) => service.entry));
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The local names each shape's services are imported as, the instances imported, and the
|
|
67
|
+
* namespace imports of an entry point that holds one.
|
|
68
|
+
*/
|
|
69
|
+
function imported(ts, source) {
|
|
70
|
+
const found = {
|
|
71
|
+
types: { property: new Set(), promise: new Set(), nullable: new Set() },
|
|
72
|
+
instances: new Set(),
|
|
73
|
+
namespaces: [],
|
|
74
|
+
};
|
|
75
|
+
for (const statement of source.statements) {
|
|
76
|
+
if (!ts.isImportDeclaration(statement)) continue;
|
|
77
|
+
const entry = statement.moduleSpecifier.text;
|
|
78
|
+
const services = SERVICES.filter((service) => service.entry === entry);
|
|
79
|
+
if (!services.length) continue;
|
|
80
|
+
const bindings = statement.importClause?.namedBindings;
|
|
81
|
+
if (bindings && ts.isNamespaceImport(bindings)) found.namespaces.push([statement, entry]);
|
|
82
|
+
for (const element of bindings && ts.isNamedImports(bindings) ? bindings.elements : []) {
|
|
83
|
+
importedName(element, services, found);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return found;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Files one named import under each service it names, by shape, or as an instance. */
|
|
90
|
+
function importedName(element, services, { types, instances }) {
|
|
91
|
+
const name = (element.propertyName ?? element.name).text;
|
|
92
|
+
for (const service of services) {
|
|
93
|
+
if (service.names.includes(name)) types[service.shape].add(element.name.text);
|
|
94
|
+
if (service.instances?.includes(name)) instances.add(element.name.text);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Whether `node` is `inject(X)` for an `X` in `types`: the import, not a parameter or variable of
|
|
100
|
+
* the same name that shadows it.
|
|
101
|
+
*/
|
|
102
|
+
const isInjected = (ts, node, types) =>
|
|
103
|
+
ts.isCallExpression(node) &&
|
|
104
|
+
ts.isIdentifier(node.expression) &&
|
|
105
|
+
node.expression.text === 'inject' &&
|
|
106
|
+
node.arguments.length >= 1 &&
|
|
107
|
+
ts.isIdentifier(node.arguments[0]) &&
|
|
108
|
+
types.has(node.arguments[0].text) &&
|
|
109
|
+
nearestBinding(ts, node.arguments[0]) === undefined;
|
|
110
|
+
|
|
111
|
+
/** Whether a variable, property or parameter holds one of `types`. */
|
|
112
|
+
function holds(ts, declaration, types) {
|
|
113
|
+
const { type, initializer } = declaration;
|
|
114
|
+
if (type && ts.isTypeReferenceNode(type) && types.has(type.typeName.getText())) return true;
|
|
115
|
+
return Boolean(initializer && isInjected(ts, initializer, types));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The members of a class that hold one of `types`, apart by instance and static. */
|
|
119
|
+
function classHolders(ts, cls, types) {
|
|
120
|
+
const names = { instance: new Set(), static: new Set() };
|
|
121
|
+
for (const member of cls.members) {
|
|
122
|
+
if (
|
|
123
|
+
ts.isPropertyDeclaration(member) &&
|
|
124
|
+
ts.isIdentifier(member.name) &&
|
|
125
|
+
holds(ts, member, types)
|
|
126
|
+
) {
|
|
127
|
+
names[isStatic(ts, member) ? 'static' : 'instance'].add(member.name.text);
|
|
128
|
+
}
|
|
129
|
+
if (!ts.isConstructorDeclaration(member)) continue;
|
|
130
|
+
for (const parameter of member.parameters) {
|
|
131
|
+
const property = ts.getModifiers(parameter)?.length && ts.isIdentifier(parameter.name);
|
|
132
|
+
if (property && holds(ts, parameter, types)) names.instance.add(parameter.name.text);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return names;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Whether `receiver` is one of `types`, or one of `instances`, as this file can tell. */
|
|
139
|
+
function isHolder(ts, receiver, types, instances) {
|
|
140
|
+
if (isInjected(ts, receiver, types)) return true;
|
|
141
|
+
if (ts.isIdentifier(receiver)) {
|
|
142
|
+
const declaration = nearestBinding(ts, receiver);
|
|
143
|
+
if (!declaration) return instances.has(receiver.text);
|
|
144
|
+
return declaration !== ELSE && holds(ts, declaration, types);
|
|
145
|
+
}
|
|
146
|
+
if (!ts.isPropertyAccessExpression(receiver)) return false;
|
|
147
|
+
if (receiver.expression.kind !== ts.SyntaxKind.ThisKeyword) return false;
|
|
148
|
+
const self = thisOf(ts, receiver);
|
|
149
|
+
if (!self) return false;
|
|
150
|
+
const names = classHolders(ts, self.cls, types);
|
|
151
|
+
return (self.static ? names.static : names.instance).has(receiver.name.text);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Whether `node` reads a member named `available`, as `x.available` or `x['available']`. */
|
|
155
|
+
const readsAvailable = (ts, node) =>
|
|
156
|
+
(ts.isPropertyAccessExpression(node) && node.name.text === 'available') ||
|
|
157
|
+
(ts.isElementAccessExpression(node) &&
|
|
158
|
+
ts.isStringLiteralLike(node.argumentExpression) &&
|
|
159
|
+
node.argumentExpression.text === 'available');
|
|
160
|
+
|
|
161
|
+
/** The expression a member is read from, for either way of reading it. */
|
|
162
|
+
const receiverOf = (node) => node.expression;
|
|
163
|
+
|
|
164
|
+
/** Whether `call` is used as a promise still: chained with then, catch or finally, or in Promise.all. */
|
|
165
|
+
function usedAsPromise(ts, call) {
|
|
166
|
+
const around = outside(ts, call);
|
|
167
|
+
const parent = around.parent;
|
|
168
|
+
if (
|
|
169
|
+
ts.isPropertyAccessExpression(parent) &&
|
|
170
|
+
parent.expression === around &&
|
|
171
|
+
['then', 'catch', 'finally'].includes(parent.name.text)
|
|
172
|
+
) {
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
const list = ts.isArrayLiteralExpression(parent) ? parent.parent : parent;
|
|
176
|
+
return (
|
|
177
|
+
ts.isCallExpression(list) &&
|
|
178
|
+
ts.isPropertyAccessExpression(list.expression) &&
|
|
179
|
+
ts.isIdentifier(list.expression.expression) &&
|
|
180
|
+
list.expression.expression.text === 'Promise' &&
|
|
181
|
+
['all', 'allSettled', 'race', 'any'].includes(list.expression.name.text)
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Skips the parentheses around an expression, outwards. */
|
|
186
|
+
function outside(ts, node) {
|
|
187
|
+
let at = node;
|
|
188
|
+
while (ts.isParenthesizedExpression(at.parent)) at = at.parent;
|
|
189
|
+
return at;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Whether `node` is `null` or `undefined`. */
|
|
193
|
+
const isNullish = (ts, node) =>
|
|
194
|
+
node.kind === ts.SyntaxKind.NullKeyword || (ts.isIdentifier(node) && node.text === 'undefined');
|
|
195
|
+
|
|
196
|
+
const NULL_CHECKS = new Set(['===', '==', '!==', '!=', '??']);
|
|
197
|
+
|
|
198
|
+
/** The edits to a file's code, and the notes for what it leaves to the developer. */
|
|
199
|
+
function codeEdits(ts, source, file, { types, instances }) {
|
|
200
|
+
const edits = [];
|
|
201
|
+
const notes = [];
|
|
202
|
+
const line = (node) => source.getLineAndCharacterOfPosition(node.getStart()).line + 1;
|
|
203
|
+
const visit = (node) => {
|
|
204
|
+
if (readsAvailable(ts, node)) {
|
|
205
|
+
const call = ts.isCallExpression(node.parent) && node.parent.expression === node;
|
|
206
|
+
if (!call && isHolder(ts, receiverOf(node), types.property, instances)) {
|
|
207
|
+
edits.push({ start: node.getEnd(), end: node.getEnd(), text: '()' });
|
|
208
|
+
}
|
|
209
|
+
if (call && node.parent.arguments.length === 0) {
|
|
210
|
+
promised(node.parent, receiverOf(node));
|
|
211
|
+
nullable(node.parent, receiverOf(node));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
ts.forEachChild(node, visit);
|
|
215
|
+
};
|
|
216
|
+
const promised = (call, receiver) => {
|
|
217
|
+
if (!isHolder(ts, receiver, types.promise, instances)) return;
|
|
218
|
+
const around = outside(ts, call);
|
|
219
|
+
if (ts.isAwaitExpression(around.parent)) {
|
|
220
|
+
const awaited = around.parent;
|
|
221
|
+
edits.push({ start: awaited.getStart(), end: awaited.expression.getStart(), text: '' });
|
|
222
|
+
notes.push(
|
|
223
|
+
`${file}:${line(call)}: available() is a signal now, false until the platform answers, ` +
|
|
224
|
+
'so without the await this reads false at first. Read it where a change shows, in a ' +
|
|
225
|
+
'template or a computed(), rather than once.',
|
|
226
|
+
);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
// Only a call still used as a promise: one that is read as a signal is already migrated.
|
|
230
|
+
if (!usedAsPromise(ts, call)) return;
|
|
231
|
+
notes.push(
|
|
232
|
+
`${file}:${line(call)}: available() is a signal now, not a promise. ` +
|
|
233
|
+
'Read it as available(), without awaiting it or calling then().',
|
|
234
|
+
);
|
|
235
|
+
};
|
|
236
|
+
const nullable = (call, receiver) => {
|
|
237
|
+
if (!isHolder(ts, receiver, types.nullable, instances)) return;
|
|
238
|
+
const around = outside(ts, call);
|
|
239
|
+
const parent = around.parent;
|
|
240
|
+
if (!ts.isBinaryExpression(parent)) return;
|
|
241
|
+
const operator = parent.operatorToken.getText();
|
|
242
|
+
const other = parent.left === around ? parent.right : parent.left;
|
|
243
|
+
if (operator === '??' || (NULL_CHECKS.has(operator) && isNullish(ts, other))) {
|
|
244
|
+
notes.push(
|
|
245
|
+
`${file}:${line(call)}: A sensor's available() is never null now: it is false until ` +
|
|
246
|
+
'the platform answers.',
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
visit(source);
|
|
251
|
+
return { edits, notes };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* A template declaring `name` itself: `@let`, a `@for` or `*ngFor` item or `let` alias, a `#`
|
|
256
|
+
* reference, an `as` alias, or an `ng-template`'s `let-`.
|
|
257
|
+
*/
|
|
258
|
+
const declaration = (name) =>
|
|
259
|
+
new RegExp(
|
|
260
|
+
`@let\\s+${name}\\b|\\blet[\\s-]+${name}\\b|#${name}\\b|\\bas\\s+${name}\\b|@for\\s*\\(\\s*${name}\\s+of\\b`,
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* A property read of `name.available`, not already called, where `name` is the component's own:
|
|
265
|
+
* read bare or as `this.name`, and not as a member of something else, as in
|
|
266
|
+
* `model.haptics.available`.
|
|
267
|
+
*/
|
|
268
|
+
const propertyRead = (name) =>
|
|
269
|
+
new RegExp(
|
|
270
|
+
`(?<![\\w$])(?<!(?<!\\bthis\\s*)\\s*\\.\\s*)${name}(?:\\.available\\b|\\[\\s*['"]available['"]\\s*\\])(?!\\s*\\()`,
|
|
271
|
+
'g',
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
/** A bound attribute, `[x]`, `(x)`, `[(x)]` or `*x`, and where its quoted value starts. */
|
|
275
|
+
const BOUND = /(?:\[\(?[^\s=\]]+\)?\]|\([^\s=)]+\)|\*[\w-]+)\s*=\s*(["'])/g;
|
|
276
|
+
|
|
277
|
+
/** A block whose parameters are an expression: `@if (`, `@else if (`, `@for (` and the rest. */
|
|
278
|
+
const BLOCK = /@(?:if|else\s+if|for|switch|case|defer)\s*\(/g;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* An expression from `from` to where `ends` says it ends, outside any quoted string in it: where it
|
|
282
|
+
* ends, and the pieces of it that are code rather than a string's contents, which are text.
|
|
283
|
+
*/
|
|
284
|
+
function scan(text, from, ends) {
|
|
285
|
+
const code = [];
|
|
286
|
+
let piece = from;
|
|
287
|
+
let quote = null;
|
|
288
|
+
for (let at = from; at < text.length; at++) {
|
|
289
|
+
const char = text[at];
|
|
290
|
+
if (quote === '`' && text.startsWith('${', at)) {
|
|
291
|
+
// A substitution in a template string is code again, to its closing brace.
|
|
292
|
+
const inner = scan(text, at + 2, closingBrace(text));
|
|
293
|
+
code.push(...inner.code);
|
|
294
|
+
at = inner.end;
|
|
295
|
+
} else if (quote) {
|
|
296
|
+
if (char === '\\') at++;
|
|
297
|
+
else if (char === quote) [quote, piece] = [null, at + 1];
|
|
298
|
+
} else if (ends(at)) {
|
|
299
|
+
code.push([piece, at]);
|
|
300
|
+
return { end: at, code };
|
|
301
|
+
} else if (char === "'" || char === '"' || char === '`') {
|
|
302
|
+
code.push([piece, at]);
|
|
303
|
+
quote = char;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (!quote) code.push([piece, text.length]);
|
|
307
|
+
return { end: text.length, code };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** For `scan`: the end is where the parenthesis open before it closes, past the ones inside it. */
|
|
311
|
+
function closing(text) {
|
|
312
|
+
let depth = 1;
|
|
313
|
+
return (at) => {
|
|
314
|
+
if (text[at] === '(') depth++;
|
|
315
|
+
else if (text[at] === ')') depth--;
|
|
316
|
+
return depth === 0;
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** Each `{{ }}` interpolation's span. */
|
|
321
|
+
function interpolationsOf(text) {
|
|
322
|
+
const spans = [];
|
|
323
|
+
for (let at = text.indexOf('{{'); at !== -1;) {
|
|
324
|
+
const { end } = scan(text, at + 2, (here) => text.startsWith('}}', here));
|
|
325
|
+
spans.push([at, end]);
|
|
326
|
+
// Past the whole span, so a `{{` in a string inside it starts no second one.
|
|
327
|
+
at = text.indexOf('{{', end + 2);
|
|
328
|
+
}
|
|
329
|
+
return spans;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Each tag's span, from its `<` to its `>`, past a `>` in a quoted attribute value, and in
|
|
334
|
+
* `values` each quoted attribute value's span.
|
|
335
|
+
*/
|
|
336
|
+
function tagsOf(text, values = []) {
|
|
337
|
+
const tags = [];
|
|
338
|
+
// A `<` in an interpolation is a comparison, `{{ a <b }}`, not a tag.
|
|
339
|
+
const interpolations = interpolationsOf(text);
|
|
340
|
+
for (const match of text.matchAll(/<[a-zA-Z/]/g)) {
|
|
341
|
+
if (tags.length && match.index < tags[tags.length - 1][1]) continue;
|
|
342
|
+
if (interpolations.some(([start, end]) => match.index > start && match.index < end)) continue;
|
|
343
|
+
let quote = null;
|
|
344
|
+
let end = match.index + 1;
|
|
345
|
+
for (; end < text.length; end++) {
|
|
346
|
+
const char = text[end];
|
|
347
|
+
if (quote) {
|
|
348
|
+
if (char === quote) {
|
|
349
|
+
quote = null;
|
|
350
|
+
values[values.length - 1][1] = end;
|
|
351
|
+
}
|
|
352
|
+
} else if (char === '"' || char === "'") {
|
|
353
|
+
quote = char;
|
|
354
|
+
values.push([end, text.length]);
|
|
355
|
+
} else if (char === '>') break;
|
|
356
|
+
}
|
|
357
|
+
tags.push([match.index, end]);
|
|
358
|
+
}
|
|
359
|
+
return tags;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** For `scan`: the end is where the brace open before it closes, past the ones inside it. */
|
|
363
|
+
function closingBrace(text) {
|
|
364
|
+
let depth = 1;
|
|
365
|
+
return (at) => {
|
|
366
|
+
if (text[at] === '{') depth++;
|
|
367
|
+
else if (text[at] === '}') depth--;
|
|
368
|
+
return depth === 0;
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* The spans of a template Angular reads as code: `{{ }}` interpolations, bound attribute values,
|
|
374
|
+
* block parameters and `@let` values, less the strings in them. Text, plain attribute values and
|
|
375
|
+
* a string constant are shown as they are, so a `name.available` there is left alone.
|
|
376
|
+
*/
|
|
377
|
+
function expressions(text) {
|
|
378
|
+
const spans = [];
|
|
379
|
+
for (let at = text.indexOf('{{'); at !== -1; at = text.indexOf('{{', at)) {
|
|
380
|
+
const { end, code } = scan(text, at + 2, (here) => text.startsWith('}}', here));
|
|
381
|
+
spans.push(...code);
|
|
382
|
+
at = end + 2;
|
|
383
|
+
}
|
|
384
|
+
// A binding is one only as an attribute of a tag, not inside another attribute's quoted text.
|
|
385
|
+
const values = [];
|
|
386
|
+
const tags = tagsOf(text, values);
|
|
387
|
+
const within = (index, ranges) => ranges.some(([start, end]) => index > start && index < end);
|
|
388
|
+
for (const match of text.matchAll(BOUND)) {
|
|
389
|
+
if (!within(match.index, tags) || within(match.index, values)) continue;
|
|
390
|
+
const quote = match[1];
|
|
391
|
+
spans.push(...scan(text, match.index + match[0].length, (at) => text[at] === quote).code);
|
|
392
|
+
}
|
|
393
|
+
// A block is one only outside a tag: `label="@if (x)"` is an attribute's text.
|
|
394
|
+
const outside = (match) => !tags.some(([start, end]) => match.index > start && match.index < end);
|
|
395
|
+
for (const match of [...text.matchAll(BLOCK)].filter(outside)) {
|
|
396
|
+
spans.push(...scan(text, match.index + match[0].length, closing(text)).code);
|
|
397
|
+
}
|
|
398
|
+
for (const match of [...text.matchAll(/@let\s+[\w$]+\s*=/g)].filter(outside)) {
|
|
399
|
+
spans.push(...scan(text, match.index + match[0].length, (at) => text[at] === ';').code);
|
|
400
|
+
}
|
|
401
|
+
return spans;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Where in `text` an expression reads `name.available` as a property. */
|
|
405
|
+
function readsOf(text, name) {
|
|
406
|
+
const spans = expressions(text);
|
|
407
|
+
return [...text.matchAll(propertyRead(name))]
|
|
408
|
+
.map((match) => match.index)
|
|
409
|
+
.filter((index) => spans.some(([start, end]) => index >= start && index < end));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** A template with each holder's `.available` called, and the names it left as declared there. */
|
|
413
|
+
function inTemplate(text, names) {
|
|
414
|
+
const calls = [];
|
|
415
|
+
const left = [];
|
|
416
|
+
for (const name of new Set(names)) {
|
|
417
|
+
const reads = new Set(readsOf(text, name));
|
|
418
|
+
if (declaration(name).test(text)) {
|
|
419
|
+
if (reads.size) left.push(name);
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
for (const match of text.matchAll(propertyRead(name))) {
|
|
423
|
+
if (reads.has(match.index)) calls.push(match.index + match[0].length);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return { calls, left };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** `text` with `()` written at each of `calls`. */
|
|
430
|
+
const withCalls = (text, calls) =>
|
|
431
|
+
applied(
|
|
432
|
+
text,
|
|
433
|
+
calls.map((at) => ({ start: at, end: at, text: '()' })),
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* A string literal's text as it decodes, which is the template Angular reads, and for each of its
|
|
438
|
+
* characters, and its end, where it is in the literal's source: `\'` is one character, two long.
|
|
439
|
+
*/
|
|
440
|
+
function decode(raw) {
|
|
441
|
+
let text = '';
|
|
442
|
+
const at = [];
|
|
443
|
+
for (let index = 1; index < raw.length - 1; index++) {
|
|
444
|
+
at.push(index);
|
|
445
|
+
if (raw[index] !== '\\') {
|
|
446
|
+
text += raw[index];
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
const [char, length] = escaped(raw, index + 1);
|
|
450
|
+
if (char === '') at.pop();
|
|
451
|
+
// One place for each UTF-16 unit, as an escape past U+FFFF decodes to two.
|
|
452
|
+
for (let unit = 1; unit < char.length; unit++) at.push(index);
|
|
453
|
+
text += char;
|
|
454
|
+
index += length;
|
|
455
|
+
}
|
|
456
|
+
at.push(raw.length - 1);
|
|
457
|
+
return { text, at };
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const ESCAPES = { n: '\n', t: '\t', r: '\r', b: '\b', f: '\f', v: '\v', 0: '\0' };
|
|
461
|
+
|
|
462
|
+
/** The character an escape after a backslash at `index` stands for, and how long it is. */
|
|
463
|
+
function escaped(raw, index) {
|
|
464
|
+
const char = raw[index];
|
|
465
|
+
const hex = /^(?:x([\da-fA-F]{2})|u([\da-fA-F]{4})|u\{([\da-fA-F]+)\})/.exec(raw.slice(index));
|
|
466
|
+
if (hex) return [String.fromCodePoint(parseInt(hex[1] ?? hex[2] ?? hex[3], 16)), hex[0].length];
|
|
467
|
+
if (char === '\r' && raw[index + 1] === '\n') return ['', 2];
|
|
468
|
+
if (char === '\n' || char === '\r') return ['', 1];
|
|
469
|
+
return [ESCAPES[char] ?? char, 1];
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const declarationLine = (text, name) =>
|
|
473
|
+
text.slice(0, text.search(declaration(name))).split('\n').length;
|
|
474
|
+
|
|
475
|
+
const readLine = (text, name) => text.slice(0, readsOf(text, name)[0]).split('\n').length;
|
|
476
|
+
|
|
477
|
+
const sharedNote = (where, name) =>
|
|
478
|
+
`${where}: Not every component that uses this template holds a service as ${name}, so it was ` +
|
|
479
|
+
`left as it is. Where it reads the service, call ${name}.available().`;
|
|
480
|
+
|
|
481
|
+
const localNote = (where, name) =>
|
|
482
|
+
`${where}: The template declares a ${name} of its own, so it was left as it is. ` +
|
|
483
|
+
`Where it reads the component's service, call ${name}.available().`;
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* The names `Component` is read by when imported from `@angular/core`: by name, under an alias,
|
|
487
|
+
* or off a namespace import, as `ng.Component`.
|
|
488
|
+
*/
|
|
489
|
+
function componentNames(ts, source) {
|
|
490
|
+
const names = new Set();
|
|
491
|
+
for (const statement of source.statements) {
|
|
492
|
+
if (!ts.isImportDeclaration(statement) || statement.moduleSpecifier.text !== '@angular/core') {
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
const bindings = statement.importClause?.namedBindings;
|
|
496
|
+
if (bindings && ts.isNamespaceImport(bindings)) names.add(`${bindings.name.text}.Component`);
|
|
497
|
+
for (const element of bindings && ts.isNamedImports(bindings) ? bindings.elements : []) {
|
|
498
|
+
if ((element.propertyName ?? element.name).text === 'Component') names.add(element.name.text);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return names;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/** The object literal a class's `@Component(...)` decorator is given, if it has one. */
|
|
505
|
+
function componentMetadata(ts, cls, components) {
|
|
506
|
+
for (const decorator of ts.getDecorators(cls) ?? []) {
|
|
507
|
+
const call = decorator.expression;
|
|
508
|
+
if (!ts.isCallExpression(call) || !components.has(call.expression.getText())) continue;
|
|
509
|
+
const [metadata] = call.arguments;
|
|
510
|
+
if (metadata && ts.isObjectLiteralExpression(metadata)) return metadata;
|
|
511
|
+
}
|
|
512
|
+
return null;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** A workspace path with `./` and `../` resolved. */
|
|
516
|
+
function normalise(path) {
|
|
517
|
+
const parts = [];
|
|
518
|
+
for (const part of path.split('/')) {
|
|
519
|
+
if (part === '..') parts.pop();
|
|
520
|
+
else if (part !== '.' && part !== '') parts.push(part);
|
|
521
|
+
}
|
|
522
|
+
return parts.join('/');
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** One component's inline template as an edit, and the template file it names, with its holders. */
|
|
526
|
+
function componentTemplate(ts, source, file, metadata, names, dir, found) {
|
|
527
|
+
for (const property of metadata.properties) {
|
|
528
|
+
if (!ts.isPropertyAssignment(property) || !ts.isIdentifier(property.name)) continue;
|
|
529
|
+
const value = property.initializer;
|
|
530
|
+
if (!ts.isStringLiteralLike(value)) continue;
|
|
531
|
+
if (property.name.text === 'templateUrl') found.urls.push([normalise(dir + value.text), names]);
|
|
532
|
+
if (property.name.text !== 'template') continue;
|
|
533
|
+
// Read as it decodes, so an escaped quote is the quote Angular sees, and each call written
|
|
534
|
+
// back where that character is in the source.
|
|
535
|
+
const decoded = decode(value.getText());
|
|
536
|
+
const { calls, left } = inTemplate(decoded.text, names);
|
|
537
|
+
const line = source.getLineAndCharacterOfPosition(property.getStart()).line + 1;
|
|
538
|
+
for (const name of left) found.notes.push(localNote(`${file}:${line}`, name));
|
|
539
|
+
for (const call of calls) {
|
|
540
|
+
const start = value.getStart() + decoded.at[call];
|
|
541
|
+
found.edits.push({ start, end: start, text: '()' });
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/** Each component's inline template edits, and the template files it names, with its holders. */
|
|
547
|
+
function templates(ts, source, file, types) {
|
|
548
|
+
const found = { edits: [], urls: [], notes: [] };
|
|
549
|
+
const dir = file.includes('/') ? file.slice(0, file.lastIndexOf('/') + 1) : '';
|
|
550
|
+
const components = componentNames(ts, source);
|
|
551
|
+
const visit = (node) => {
|
|
552
|
+
const cls = ts.isClassDeclaration(node) || ts.isClassExpression(node);
|
|
553
|
+
const metadata = cls && componentMetadata(ts, node, components);
|
|
554
|
+
// A template reads the instance's members.
|
|
555
|
+
// Every component is recorded, holding a service or not, so a template file several of them
|
|
556
|
+
// share is rewritten only when each holds the same.
|
|
557
|
+
if (metadata) {
|
|
558
|
+
const names = classHolders(ts, node, types).instance;
|
|
559
|
+
componentTemplate(ts, source, file, metadata, names, dir, found);
|
|
560
|
+
}
|
|
561
|
+
ts.forEachChild(node, visit);
|
|
562
|
+
};
|
|
563
|
+
visit(source);
|
|
564
|
+
return found;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const parse = (ts, file, text) =>
|
|
568
|
+
ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, scriptKind(ts, file));
|
|
569
|
+
|
|
570
|
+
/** Rewrites one source file, and answers the template files it names, each with its holders. */
|
|
571
|
+
function rewriteSource(ts, host, file, text, notes) {
|
|
572
|
+
const source = parse(ts, file, text);
|
|
573
|
+
const found = imported(ts, source);
|
|
574
|
+
for (const [statement, entry] of found.namespaces) {
|
|
575
|
+
const at = source.getLineAndCharacterOfPosition(statement.getStart()).line + 1;
|
|
576
|
+
notes.push(
|
|
577
|
+
`${file}:${at}: A namespace import of '${entry}' cannot be followed automatically. ` +
|
|
578
|
+
"A service's available is a signal now: read it as available(), and do not await it.",
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
const { types } = found;
|
|
582
|
+
if (
|
|
583
|
+
!types.property.size &&
|
|
584
|
+
!types.promise.size &&
|
|
585
|
+
!types.nullable.size &&
|
|
586
|
+
!found.instances.size
|
|
587
|
+
) {
|
|
588
|
+
return templates(ts, source, file, new Set()).urls;
|
|
589
|
+
}
|
|
590
|
+
const code = codeEdits(ts, source, file, found);
|
|
591
|
+
const view = templates(ts, source, file, types.property);
|
|
592
|
+
notes.push(...view.notes, ...code.notes);
|
|
593
|
+
const edits = [...view.edits, ...code.edits];
|
|
594
|
+
if (edits.length) host.write(file, applied(text, edits));
|
|
595
|
+
return view.urls;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** @param {import('./host.cjs').Host} host */
|
|
599
|
+
function availableSignal(host) {
|
|
600
|
+
const ts = typescript();
|
|
601
|
+
const notes = [];
|
|
602
|
+
// Each template file, with the holders of each component that uses it.
|
|
603
|
+
const users = new Map();
|
|
604
|
+
for (const file of files(host)) {
|
|
605
|
+
const text = SOURCE.test(file) ? host.read(file) : null;
|
|
606
|
+
if (text === null) continue;
|
|
607
|
+
let urls = [];
|
|
608
|
+
if ([...ENTRIES].some((entry) => text.includes(entry))) {
|
|
609
|
+
urls = rewriteSource(ts, host, file, text, notes);
|
|
610
|
+
} else if (text.includes('templateUrl')) {
|
|
611
|
+
urls = templates(ts, parse(ts, file, text), file, new Set()).urls;
|
|
612
|
+
}
|
|
613
|
+
for (const [url, names] of urls) users.set(url, [...(users.get(url) ?? []), new Set(names)]);
|
|
614
|
+
}
|
|
615
|
+
for (const [url, held] of users) rewriteTemplate(host, url, held, notes);
|
|
616
|
+
return notes;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Rewrites a template file for the holders of the components that use it, unless they differ on
|
|
621
|
+
* a name it reads `available` from: one component's service is another's plain object.
|
|
622
|
+
*/
|
|
623
|
+
function rewriteTemplate(host, url, held, notes) {
|
|
624
|
+
const text = host.read(url);
|
|
625
|
+
if (text === null) return;
|
|
626
|
+
const names = new Set(held.flatMap((each) => [...each]));
|
|
627
|
+
const split = [...names].filter(
|
|
628
|
+
(name) => held.some((each) => !each.has(name)) && readsOf(text, name).length > 0,
|
|
629
|
+
);
|
|
630
|
+
if (split.length) {
|
|
631
|
+
for (const name of split) notes.push(sharedNote(`${url}:${readLine(text, name)}`, name));
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
const { calls, left } = inTemplate(text, names);
|
|
635
|
+
for (const name of left) notes.push(localNote(`${url}:${declarationLine(text, name)}`, name));
|
|
636
|
+
if (calls.length) host.write(url, withCalls(text, calls));
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
module.exports = { availableSignal };
|
package/migrations.cjs
CHANGED
|
@@ -17,11 +17,13 @@
|
|
|
17
17
|
* @property {(host: import('./host.cjs').Host) => string[]} run Changes the files, and answers
|
|
18
18
|
* what is left for the developer to do, one line each.
|
|
19
19
|
*/
|
|
20
|
+
const { availableSignal } = require('./available-signal.cjs');
|
|
20
21
|
const { deviceReactNativeImport } = require('./device-react-native-import.cjs');
|
|
21
22
|
const { engineNodeFields } = require('./engine-node-fields.cjs');
|
|
22
23
|
const { engineNodeTag } = require('./engine-node-tag.cjs');
|
|
23
24
|
const { libraryTypecheckDeclarationMap } = require('./library-typecheck-declaration-map.cjs');
|
|
24
25
|
const { nativeTypecheckTarget } = require('./native-typecheck-target.cjs');
|
|
26
|
+
const { osComparisonsOnMacos } = require('./os-comparisons-on-macos.cjs');
|
|
25
27
|
const { splitStoreAndPlayer } = require('./split-store-and-player.cjs');
|
|
26
28
|
const { stopWatchingConditions } = require('./stop-watching-conditions.cjs');
|
|
27
29
|
const { syncAppVersions } = require('./sync-app-versions.cjs');
|
|
@@ -97,6 +99,20 @@ const migrations = [
|
|
|
97
99
|
"Resolve the workspace's path aliases in Nx Vitest configs with vite-tsconfig-paths, as nxViteTsPaths does nothing from Nx 24.",
|
|
98
100
|
run: vitestTsconfigPaths,
|
|
99
101
|
},
|
|
102
|
+
{
|
|
103
|
+
name: 'os-comparisons-on-macos',
|
|
104
|
+
version: '0.23.0',
|
|
105
|
+
description:
|
|
106
|
+
"Note each comparison with 'ios', which answers differently now that macOS is reported as 'macos', and each !== 'android' and OS_VERSION comparison, which a Mac passes into a branch that may be written for iOS.",
|
|
107
|
+
run: osComparisonsOnMacos,
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: 'available-signal',
|
|
111
|
+
version: '0.23.0',
|
|
112
|
+
description:
|
|
113
|
+
"Call each service's available, now a Signal<boolean> on every service, and stop awaiting it.",
|
|
114
|
+
run: availableSignal,
|
|
115
|
+
},
|
|
100
116
|
];
|
|
101
117
|
|
|
102
118
|
module.exports = { migrations };
|
package/native-app.cjs
CHANGED
|
@@ -155,22 +155,122 @@ function bundleIdentifier(name, scope) {
|
|
|
155
155
|
return ['com', ...(valid(org) ? [org] : []), valid(app) ? app : `app${app}`].join('.');
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
+
/** The package that builds and packages the macOS app, and its config plugin's name. */
|
|
159
|
+
const DESKTOP_PACKAGE = '@ng-native/desktop';
|
|
160
|
+
|
|
161
|
+
/** What a desktop app adds to its dependencies: the host and the `ng-native-desktop` command. */
|
|
162
|
+
const desktopDependencies = { [DESKTOP_PACKAGE]: version };
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* A desktop-only app's dependencies, the desktop template's: the mobile app's, less the two that
|
|
166
|
+
* only set up a phone's status bar and system UI, with the desktop host and `@ng-native/expo`, whose
|
|
167
|
+
* services the desktop starter uses. A host library is pinned to the prebuilt host's version.
|
|
168
|
+
*
|
|
169
|
+
* @type {Record<string, string>}
|
|
170
|
+
*/
|
|
171
|
+
const desktopOnlyDependencies = Object.fromEntries(
|
|
172
|
+
Object.entries({
|
|
173
|
+
...dependencies,
|
|
174
|
+
...desktopDependencies,
|
|
175
|
+
'@ng-native/expo': version,
|
|
176
|
+
// Exactly the version the prebuilt desktop host is built with, so the app runs on it.
|
|
177
|
+
'react-native-safe-area-context': '5.10.1',
|
|
178
|
+
})
|
|
179
|
+
.filter(([name]) => name !== 'expo-status-bar' && name !== 'expo-system-ui')
|
|
180
|
+
.sort(([a], [b]) => (a < b ? -1 : 1)),
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* What a desktop-only app lacks of a mobile app's dependencies, which adding iOS and Android back
|
|
185
|
+
* adds: the phone's status bar and system UI packages.
|
|
186
|
+
*
|
|
187
|
+
* @type {Record<string, string>}
|
|
188
|
+
*/
|
|
189
|
+
const mobileDependencies = Object.fromEntries(
|
|
190
|
+
Object.entries(dependencies).filter(([name]) => !(name in desktopOnlyDependencies)),
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The scripts a desktop app adds: `npm run macos` builds the host and opens the app from the dev
|
|
195
|
+
* server `npm start` runs, and `npm run package` makes the `.app`, `.dmg` or Mac App Store package.
|
|
196
|
+
* The Expo CLI has no `run:macos`: its `run` commands are a fixed list.
|
|
197
|
+
*/
|
|
198
|
+
const DESKTOP_SCRIPTS = { macos: 'ng-native-desktop run', package: 'ng-native-desktop package' };
|
|
199
|
+
|
|
200
|
+
/** What `ng-native-desktop package` writes into the app's directory, for its `.gitignore`. */
|
|
201
|
+
const DESKTOP_GITIGNORE = ['# Built by ng-native-desktop package', '/dist/macos', ''].join('\n');
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* An Expo config with macOS added: `macos` in `platforms` (with iOS and Android when the config
|
|
205
|
+
* named none, which is Expo's default), Expo's `experiments.outOfTreePlatforms`, without which Expo
|
|
206
|
+
* drops `macos` from every bundle and export without a word, and the desktop config plugin. Adding
|
|
207
|
+
* twice changes nothing.
|
|
208
|
+
*
|
|
209
|
+
* @param {Record<string, any>} expo
|
|
210
|
+
*/
|
|
211
|
+
function withDesktop(expo) {
|
|
212
|
+
const platforms = expo.platforms ?? ['ios', 'android'];
|
|
213
|
+
const plugins = expo.plugins ?? [];
|
|
214
|
+
const named = (plugin) => (Array.isArray(plugin) ? plugin[0] : plugin);
|
|
215
|
+
return {
|
|
216
|
+
...expo,
|
|
217
|
+
platforms: platforms.includes('macos') ? platforms : [...platforms, 'macos'],
|
|
218
|
+
experiments: { ...expo.experiments, outOfTreePlatforms: true },
|
|
219
|
+
plugins: plugins.some((plugin) => named(plugin) === DESKTOP_PACKAGE)
|
|
220
|
+
? plugins
|
|
221
|
+
: [...plugins, DESKTOP_PACKAGE],
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* An Expo config with iOS and Android added to a desktop-only one: `ios` and `android` in
|
|
227
|
+
* `platforms`, ahead of what it lists, and the phone settings the desktop template's `app.json`
|
|
228
|
+
* leaves out, as `appJson` writes them: portrait orientation, tablets on iOS, and the Android
|
|
229
|
+
* package, which is the iOS bundle identifier. A setting the config has is kept, and a config with
|
|
230
|
+
* no `platforms` has Expo's default, iOS and Android, already. Adding twice changes nothing.
|
|
231
|
+
*
|
|
232
|
+
* @param {Record<string, any>} expo
|
|
233
|
+
*/
|
|
234
|
+
function withMobile(expo) {
|
|
235
|
+
const platforms = expo.platforms;
|
|
236
|
+
const missing = ['ios', 'android'].filter((os) => platforms && !platforms.includes(os));
|
|
237
|
+
const bundle = expo.ios?.bundleIdentifier;
|
|
238
|
+
return {
|
|
239
|
+
...expo,
|
|
240
|
+
...(missing.length && { platforms: [...missing, ...platforms] }),
|
|
241
|
+
orientation: expo.orientation ?? 'portrait',
|
|
242
|
+
ios: { supportsTablet: true, ...expo.ios },
|
|
243
|
+
android: {
|
|
244
|
+
...(bundle && { package: bundle }),
|
|
245
|
+
predictiveBackGestureEnabled: false,
|
|
246
|
+
...expo.android,
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
158
251
|
/**
|
|
159
252
|
* The template's `app.json`, named for this app, without the template's icons: they are the
|
|
160
253
|
* template's branding, and Expo draws its own default until `icon` is set.
|
|
161
254
|
*
|
|
162
255
|
* @param {string} name the project name, which is also the Expo slug once any scope is removed
|
|
163
256
|
* @param {string} [id] the iOS bundle identifier and Android package, by default one from `name`
|
|
257
|
+
* @param {readonly string[]} [platforms] the OSes the app runs on, iOS and Android by default. With
|
|
258
|
+
* `macos`, Expo's `experiments.outOfTreePlatforms` is set too: without it Expo drops `macos` from
|
|
259
|
+
* every bundle and export without a word.
|
|
164
260
|
*/
|
|
165
|
-
function appJson(name, id) {
|
|
261
|
+
function appJson(name, id, platforms = ['ios', 'android']) {
|
|
166
262
|
const slug = name.replace(/^@[^/]+\//, '');
|
|
167
263
|
const bundle = id ?? bundleIdentifier(slug, name.match(/^@[^/]+/)?.[0]);
|
|
264
|
+
if (platforms.length === 1 && platforms[0] === 'macos') {
|
|
265
|
+
return JSON.stringify({ expo: desktopOnlyExpo(slug, bundle) }, null, 2) + '\n';
|
|
266
|
+
}
|
|
168
267
|
const expo = {
|
|
169
268
|
name: slug,
|
|
170
269
|
slug,
|
|
171
270
|
version: '1.0.0',
|
|
172
271
|
// Not left to Expo, which adds `web` whenever `react-dom` resolves, and @nx/react puts one there.
|
|
173
|
-
platforms: [
|
|
272
|
+
platforms: [...platforms],
|
|
273
|
+
...(platforms.includes('macos') ? { experiments: { outOfTreePlatforms: true } } : {}),
|
|
174
274
|
orientation: 'portrait',
|
|
175
275
|
userInterfaceStyle: 'automatic',
|
|
176
276
|
ios: { supportsTablet: true, bundleIdentifier: bundle },
|
|
@@ -178,11 +278,34 @@ function appJson(name, id) {
|
|
|
178
278
|
scheme: slug.replace(/[^a-z0-9]/gi, '').toLowerCase(),
|
|
179
279
|
// The template's: Expo otherwise guesses a router root from src/app and says so on every start.
|
|
180
280
|
extra: { router: { root: 'src/app' } },
|
|
181
|
-
plugins: ['@ng-native/metro'],
|
|
281
|
+
plugins: ['@ng-native/metro', ...(platforms.includes('macos') ? [DESKTOP_PACKAGE] : [])],
|
|
182
282
|
};
|
|
183
283
|
return JSON.stringify({ expo }, null, 2) + '\n';
|
|
184
284
|
}
|
|
185
285
|
|
|
286
|
+
/**
|
|
287
|
+
* The desktop template's `app.json`, named for this app: macOS alone, with no phone settings. The
|
|
288
|
+
* bundle identifier stays in `ios.bundleIdentifier`, the key a Mac app's is read from, since Apple
|
|
289
|
+
* shares one identifier across its OSes.
|
|
290
|
+
*
|
|
291
|
+
* @param {string} slug
|
|
292
|
+
* @param {string} bundle
|
|
293
|
+
*/
|
|
294
|
+
function desktopOnlyExpo(slug, bundle) {
|
|
295
|
+
return {
|
|
296
|
+
name: slug,
|
|
297
|
+
slug,
|
|
298
|
+
version: '1.0.0',
|
|
299
|
+
platforms: ['macos'],
|
|
300
|
+
experiments: { outOfTreePlatforms: true },
|
|
301
|
+
userInterfaceStyle: 'automatic',
|
|
302
|
+
ios: { bundleIdentifier: bundle },
|
|
303
|
+
scheme: slug.replace(/[^a-z0-9]/gi, '').toLowerCase(),
|
|
304
|
+
extra: { router: { root: 'src/app' } },
|
|
305
|
+
plugins: ['@ng-native/metro', DESKTOP_PACKAGE],
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
186
309
|
/**
|
|
187
310
|
* The dependencies an app lists in its own `package.json`, at the ranges the workspace root
|
|
188
311
|
* installs, though nothing installs from it.
|
|
@@ -194,12 +317,12 @@ function appJson(name, id) {
|
|
|
194
317
|
* second copy of React Native beside the root's.
|
|
195
318
|
*
|
|
196
319
|
* @param {{ dependencies?: Record<string, string>, devDependencies?: Record<string, string> }} root
|
|
320
|
+
* @param {Record<string, string>} [wanted] the app's dependencies, a desktop-only app's in place of
|
|
321
|
+
* the mobile app's
|
|
197
322
|
*/
|
|
198
|
-
function prebuildPins(root) {
|
|
323
|
+
function prebuildPins(root, wanted = dependencies) {
|
|
199
324
|
const installed = { ...root.devDependencies, ...root.dependencies };
|
|
200
|
-
return Object.fromEntries(
|
|
201
|
-
Object.keys(dependencies).map((pkg) => [pkg, installed[pkg] ?? dependencies[pkg]]),
|
|
202
|
-
);
|
|
325
|
+
return Object.fromEntries(Object.keys(wanted).map((pkg) => [pkg, installed[pkg] ?? wanted[pkg]]));
|
|
203
326
|
}
|
|
204
327
|
|
|
205
328
|
/**
|
|
@@ -251,6 +374,14 @@ module.exports = {
|
|
|
251
374
|
lintDevDependencies,
|
|
252
375
|
ESLINT_CONFIG,
|
|
253
376
|
GITIGNORE,
|
|
377
|
+
DESKTOP_PACKAGE,
|
|
378
|
+
DESKTOP_SCRIPTS,
|
|
379
|
+
DESKTOP_GITIGNORE,
|
|
380
|
+
desktopDependencies,
|
|
381
|
+
desktopOnlyDependencies,
|
|
382
|
+
mobileDependencies,
|
|
383
|
+
withDesktop,
|
|
384
|
+
withMobile,
|
|
254
385
|
appJson,
|
|
255
386
|
bundleIdentifier,
|
|
256
387
|
isBundleIdentifier,
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.23.0 reports macOS as `'macos'`: the `OS` token, `Platform.OS` and `nativePlatform()` all say
|
|
3
|
+
* so, where `OS` used to fold every OS but Android and the web into `'ios'`. And `OS_VERSION` is the
|
|
4
|
+
* macOS major on a Mac. So a check an app wrote for iOS can be wrong there:
|
|
5
|
+
*
|
|
6
|
+
* - `=== 'ios'` is false on a Mac, though iOS and macOS share most of what such a branch is for;
|
|
7
|
+
* - `!== 'android'` is true on a Mac, as it was, which then takes a branch that may be written for
|
|
8
|
+
* iOS;
|
|
9
|
+
* - `OS_VERSION >= 26` matches macOS 26 too, where it may be meant for iOS 26;
|
|
10
|
+
*
|
|
11
|
+
* Which of these an app wants is its own to say, so nothing is rewritten: each comparison in the
|
|
12
|
+
* app's TypeScript, JavaScript, templates and inline templates is noted with its file and line.
|
|
13
|
+
*/
|
|
14
|
+
const { scriptKind, SOURCE, typescript } = require('./move-imports.cjs');
|
|
15
|
+
const { files } = require('./host.cjs');
|
|
16
|
+
|
|
17
|
+
const TEMPLATE = /\.html$/;
|
|
18
|
+
const EQUALITY = new Set(['===', '==', '!==', '!=']);
|
|
19
|
+
const ORDER = new Set(['<', '<=', '>', '>=', '===', '==', '!==', '!=']);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Whether a file can hold a comparison worth a note at all, before it is parsed. A quote may be
|
|
23
|
+
* escaped, as one in a single-quoted inline template is.
|
|
24
|
+
*/
|
|
25
|
+
const mentions = (text) =>
|
|
26
|
+
text !== null && (/['"`](ios|android)\\?['"`]/.test(text) || text.includes('OS_VERSION'));
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A comparison in a template binding: with a quoted OS on either side, or of `OS_VERSION` with a
|
|
30
|
+
* number on either side.
|
|
31
|
+
*/
|
|
32
|
+
const COMPARISON = new RegExp(
|
|
33
|
+
[
|
|
34
|
+
/(?<os>[\w.$?!]+(?:\(\))?)\s*(?<osOperator>===|!==|==|!=)\s*(?<quote>['"`])(?<literal>ios|android)\k<quote>/,
|
|
35
|
+
/(?<firstQuote>['"`])(?<firstLiteral>ios|android)\k<firstQuote>\s*(?<firstOperator>===|!==|==|!=)\s*[\w$][\w.$?!]*(?:\(\))?/,
|
|
36
|
+
/\bOS_VERSION\b(?:\(\))?\s*(?<versionOperator><=|>=|<|>|===|!==|==|!=)\s*\d+/,
|
|
37
|
+
/\b\d+\s*(?<reversedOperator><=|>=|<|>|===|!==|==|!=)\s*OS_VERSION\b(?:\(\))?/,
|
|
38
|
+
]
|
|
39
|
+
.map((pattern) => pattern.source)
|
|
40
|
+
.join('|'),
|
|
41
|
+
'g',
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Why a comparison answers differently on a Mac, or null when it does not.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} operator
|
|
48
|
+
* @param {string} literal the string compared with, when one side is a string
|
|
49
|
+
* @param {boolean} version whether either side reads `OS_VERSION`
|
|
50
|
+
*/
|
|
51
|
+
function why(operator, literal, version) {
|
|
52
|
+
const negated = operator.startsWith('!');
|
|
53
|
+
if (EQUALITY.has(operator) && literal === 'ios') {
|
|
54
|
+
return negated
|
|
55
|
+
? "is true on macOS, where the OS is now 'macos'. If the branch is for every OS but Apple's, check for 'macos' too."
|
|
56
|
+
: "is false on macOS, where the OS is now 'macos'. If the branch is for every Apple OS, check for 'macos' too.";
|
|
57
|
+
}
|
|
58
|
+
if (EQUALITY.has(operator) && negated && literal === 'android') {
|
|
59
|
+
return 'is true on macOS too, so a Mac takes this branch. Check that it is right there.';
|
|
60
|
+
}
|
|
61
|
+
if (ORDER.has(operator) && version) {
|
|
62
|
+
return (
|
|
63
|
+
'also compares the macOS version on a Mac, where macOS 26 matches too. ' +
|
|
64
|
+
'Check the OS as well if it is about iOS.'
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Notes for the comparisons in a TypeScript or JavaScript file. */
|
|
71
|
+
function scriptNotes(ts, file, text) {
|
|
72
|
+
const source = ts.createSourceFile(
|
|
73
|
+
file,
|
|
74
|
+
text,
|
|
75
|
+
ts.ScriptTarget.Latest,
|
|
76
|
+
true,
|
|
77
|
+
scriptKind(ts, file),
|
|
78
|
+
);
|
|
79
|
+
const notes = [];
|
|
80
|
+
const visit = (node) => {
|
|
81
|
+
if (isInlineTemplate(ts, node)) {
|
|
82
|
+
const line = source.getLineAndCharacterOfPosition(node.initializer.getStart()).line + 1;
|
|
83
|
+
notes.push(...templateNotes(file, node.initializer.text, line));
|
|
84
|
+
}
|
|
85
|
+
if (ts.isBinaryExpression(node)) {
|
|
86
|
+
const operator = ts.tokenToString(node.operatorToken.kind) ?? '';
|
|
87
|
+
const literal = [node.left, node.right].find((side) => ts.isStringLiteralLike(side));
|
|
88
|
+
const version = /\bOS_VERSION\b/.test(node.getText());
|
|
89
|
+
const reason = why(operator, literal?.text ?? '', version);
|
|
90
|
+
if (reason) {
|
|
91
|
+
const line = source.getLineAndCharacterOfPosition(node.getStart()).line + 1;
|
|
92
|
+
notes.push(`${file}:${line}: ${node.getText()} ${reason}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
ts.forEachChild(node, visit);
|
|
96
|
+
};
|
|
97
|
+
visit(source);
|
|
98
|
+
return notes;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Whether `node` is a component's inline template: a `template` property in an object literal,
|
|
103
|
+
* given as a string with no substitutions.
|
|
104
|
+
*/
|
|
105
|
+
const isInlineTemplate = (ts, node) =>
|
|
106
|
+
ts.isPropertyAssignment(node) &&
|
|
107
|
+
(ts.isIdentifier(node.name) || ts.isStringLiteral(node.name)) &&
|
|
108
|
+
node.name.text === 'template' &&
|
|
109
|
+
ts.isStringLiteralLike(node.initializer);
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Notes for the comparisons in a template's bindings, `@if` and `[class]` alike.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} file
|
|
115
|
+
* @param {string} text
|
|
116
|
+
* @param {number} [first] the file's line the template starts on, past 1 for an inline one
|
|
117
|
+
*/
|
|
118
|
+
function templateNotes(file, text, first = 1) {
|
|
119
|
+
const notes = [];
|
|
120
|
+
// The whole template, since a comparison may break over lines; noted at the line it starts on.
|
|
121
|
+
for (const match of text.matchAll(COMPARISON)) {
|
|
122
|
+
const { osOperator, literal, firstOperator, firstLiteral, versionOperator, reversedOperator } =
|
|
123
|
+
match.groups ?? {};
|
|
124
|
+
const reason = osOperator
|
|
125
|
+
? why(osOperator, literal, false)
|
|
126
|
+
: firstOperator
|
|
127
|
+
? why(firstOperator, firstLiteral, false)
|
|
128
|
+
: why(versionOperator ?? reversedOperator, '', true);
|
|
129
|
+
const line = first + (text.slice(0, match.index).match(/\n/g)?.length ?? 0);
|
|
130
|
+
if (reason) notes.push(`${file}:${line}: ${match[0].replace(/\s+/g, ' ')} ${reason}`);
|
|
131
|
+
}
|
|
132
|
+
return notes;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** @param {import('./host.cjs').Host} host */
|
|
136
|
+
function osComparisonsOnMacos(host) {
|
|
137
|
+
const ts = typescript();
|
|
138
|
+
const notes = [];
|
|
139
|
+
for (const file of files(host)) {
|
|
140
|
+
const script = SOURCE.test(file) && !file.endsWith('.d.ts');
|
|
141
|
+
if (!script && !TEMPLATE.test(file)) continue;
|
|
142
|
+
const text = host.read(file);
|
|
143
|
+
if (!mentions(text)) continue;
|
|
144
|
+
notes.push(...(script ? scriptNotes(ts, file, text) : templateNotes(file, text)));
|
|
145
|
+
}
|
|
146
|
+
return notes;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
module.exports = { osComparisonsOnMacos };
|