@ng-native/migrate 0.17.1 → 0.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/migrations.cjs +16 -0
- package/native-app.cjs +45 -12
- package/package.json +1 -1
- package/stop-watching-conditions.cjs +101 -0
- package/vitest-tsconfig-paths.cjs +51 -0
package/migrations.cjs
CHANGED
|
@@ -23,8 +23,10 @@ const { engineNodeTag } = require('./engine-node-tag.cjs');
|
|
|
23
23
|
const { libraryTypecheckDeclarationMap } = require('./library-typecheck-declaration-map.cjs');
|
|
24
24
|
const { nativeTypecheckTarget } = require('./native-typecheck-target.cjs');
|
|
25
25
|
const { splitStoreAndPlayer } = require('./split-store-and-player.cjs');
|
|
26
|
+
const { stopWatchingConditions } = require('./stop-watching-conditions.cjs');
|
|
26
27
|
const { syncAppVersions } = require('./sync-app-versions.cjs');
|
|
27
28
|
const { trackingAvailableGetter } = require('./tracking-available-getter.cjs');
|
|
29
|
+
const { vitestTsconfigPaths } = require('./vitest-tsconfig-paths.cjs');
|
|
28
30
|
const { version } = require('./package.json');
|
|
29
31
|
|
|
30
32
|
/** @type {Migration[]} */
|
|
@@ -81,6 +83,20 @@ const migrations = [
|
|
|
81
83
|
description: 'Give each Angular Native project in angular.json a typecheck target, run by ngc.',
|
|
82
84
|
run: nativeTypecheckTarget,
|
|
83
85
|
},
|
|
86
|
+
{
|
|
87
|
+
name: 'stop-watching-conditions',
|
|
88
|
+
version: '0.17.2',
|
|
89
|
+
description:
|
|
90
|
+
"Keep what watchConditions returns in each app's entry point and stop it as the app is destroyed.",
|
|
91
|
+
run: stopWatchingConditions,
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
name: 'vitest-tsconfig-paths',
|
|
95
|
+
version: '0.18.1',
|
|
96
|
+
description:
|
|
97
|
+
"Resolve the workspace's path aliases in Nx Vitest configs with vite-tsconfig-paths, as nxViteTsPaths does nothing from Nx 24.",
|
|
98
|
+
run: vitestTsconfigPaths,
|
|
99
|
+
},
|
|
84
100
|
];
|
|
85
101
|
|
|
86
102
|
module.exports = { migrations };
|
package/native-app.cjs
CHANGED
|
@@ -31,12 +31,12 @@ const dependencies = {
|
|
|
31
31
|
...Object.fromEntries(FRAMEWORK.map((name) => [name, version])),
|
|
32
32
|
'@angular/common': '^22.0.0',
|
|
33
33
|
'@angular/core': '^22.0.0',
|
|
34
|
-
expo: '~
|
|
35
|
-
'expo-status-bar': '~
|
|
36
|
-
'expo-system-ui': '~
|
|
37
|
-
react: '19.
|
|
38
|
-
'react-native': '0.
|
|
39
|
-
'react-native-safe-area-context': '~5.
|
|
34
|
+
expo: '~58.0.16',
|
|
35
|
+
'expo-status-bar': '~58.0.3',
|
|
36
|
+
'expo-system-ui': '~58.0.6',
|
|
37
|
+
react: '19.3.0',
|
|
38
|
+
'react-native': '0.88.0',
|
|
39
|
+
'react-native-safe-area-context': '~5.10.1',
|
|
40
40
|
};
|
|
41
41
|
|
|
42
42
|
/** @type {Record<string, string>} */
|
|
@@ -47,11 +47,35 @@ const devDependencies = {
|
|
|
47
47
|
'@angular/compiler-cli': '^22.0.0',
|
|
48
48
|
'@babel/core': '^7.29.7',
|
|
49
49
|
'@ng-native/testing': version,
|
|
50
|
-
'@types/react': '~19.
|
|
50
|
+
'@types/react': '~19.3.0',
|
|
51
51
|
typescript: '~6.0.3',
|
|
52
52
|
vitest: '^5.0.0',
|
|
53
53
|
};
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* What `--linter=eslint` adds beside `devDependencies`: `@ng-native/eslint-plugin` and what it runs
|
|
57
|
+
* on, angular-eslint for the templates and typescript-eslint for the code.
|
|
58
|
+
*
|
|
59
|
+
* @type {Record<string, string>}
|
|
60
|
+
*/
|
|
61
|
+
const lintDevDependencies = {
|
|
62
|
+
'@ng-native/eslint-plugin': version,
|
|
63
|
+
'angular-eslint': '^22.0.0',
|
|
64
|
+
eslint: '^10.0.0',
|
|
65
|
+
'typescript-eslint': '^8.0.0',
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** The app's `eslint.config.mjs`, with `--linter=eslint`. */
|
|
69
|
+
const ESLINT_CONFIG = `import ngNative from '@ng-native/eslint-plugin';
|
|
70
|
+
|
|
71
|
+
// The DOM events, HTML controls and browser APIs that compile but do nothing on a device. See
|
|
72
|
+
// https://ng-native.com/packages/eslint-plugin for each rule.
|
|
73
|
+
export default [
|
|
74
|
+
{ ignores: ['.expo/', 'ios/', 'android/', 'dist/'] },
|
|
75
|
+
...ngNative.configs.recommended,
|
|
76
|
+
];
|
|
77
|
+
`;
|
|
78
|
+
|
|
55
79
|
/**
|
|
56
80
|
* The app's own `.gitignore`: the lines of the template's that are about the app's directory. The
|
|
57
81
|
* rest, `node_modules/` and the like, are the workspace's business.
|
|
@@ -180,10 +204,11 @@ function prebuildPins(root) {
|
|
|
180
204
|
|
|
181
205
|
/**
|
|
182
206
|
* Where what a workspace already has can be older than what a new app is given: `@ng-native/testing`
|
|
183
|
-
* installs Vitest 5 but its plugin runs on 4, which `ng new` 22.1 and the Nx preset still start on
|
|
184
|
-
*
|
|
207
|
+
* installs Vitest 5 but its plugin runs on 4, which `ng new` 22.1 and the Nx preset still start on,
|
|
208
|
+
* and `@ng-native/eslint-plugin` runs on the ESLint 9 the Nx preset still starts on. Each range here
|
|
209
|
+
* is that package's peer, and a test holds the two together.
|
|
185
210
|
*/
|
|
186
|
-
const accepted = { vitest: '^4.0.8 || ^5.0.0' };
|
|
211
|
+
const accepted = { vitest: '^4.0.8 || ^5.0.0', eslint: '^9.0.0 || ^10.0.0' };
|
|
187
212
|
|
|
188
213
|
/**
|
|
189
214
|
* Why an existing dependency would stop this app installing or running, as a sentence each.
|
|
@@ -195,12 +220,18 @@ const accepted = { vitest: '^4.0.8 || ^5.0.0' };
|
|
|
195
220
|
* generator.
|
|
196
221
|
*
|
|
197
222
|
* @param {{ dependencies?: Record<string, string>, devDependencies?: Record<string, string> }} manifest
|
|
223
|
+
* @param {{ linter?: 'eslint' | 'none' }} [options] with `eslint`, the lint packages are judged too
|
|
198
224
|
* @returns {string[]}
|
|
199
225
|
*/
|
|
200
|
-
function conflicts(manifest) {
|
|
226
|
+
function conflicts(manifest, { linter = 'none' } = {}) {
|
|
201
227
|
const existing = { ...manifest.dependencies, ...manifest.devDependencies };
|
|
202
228
|
const problems = [];
|
|
203
|
-
|
|
229
|
+
const lint = linter === 'eslint' ? lintDevDependencies : {};
|
|
230
|
+
for (const [name, installed] of Object.entries({
|
|
231
|
+
...dependencies,
|
|
232
|
+
...devDependencies,
|
|
233
|
+
...lint,
|
|
234
|
+
})) {
|
|
204
235
|
const range = existing[name];
|
|
205
236
|
const wanted = accepted[name] ?? installed;
|
|
206
237
|
if (!range || name.startsWith('@ng-native/')) continue;
|
|
@@ -217,6 +248,8 @@ function conflicts(manifest) {
|
|
|
217
248
|
module.exports = {
|
|
218
249
|
dependencies,
|
|
219
250
|
devDependencies,
|
|
251
|
+
lintDevDependencies,
|
|
252
|
+
ESLINT_CONFIG,
|
|
220
253
|
GITIGNORE,
|
|
221
254
|
appJson,
|
|
222
255
|
bundleIdentifier,
|
package/package.json
CHANGED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `watchConditions(app.engine)` returns a function that stops watching the device's size, colour
|
|
3
|
+
* scheme and other conditions, and an app's `main.ts` dropped it: when Android recreates the
|
|
4
|
+
* activity, the old app's watch kept running beside the new one's. The template and generators
|
|
5
|
+
* now keep it and stop it as the app is destroyed.
|
|
6
|
+
*
|
|
7
|
+
* In each file that imports `watchConditions` from `@ng-native/device`, a call to it that is a
|
|
8
|
+
* statement of its own, whose engine is read off a named app (`app.engine`), becomes the same two
|
|
9
|
+
* lines: the call kept in `stopWatching`, and `app.applicationRef.onDestroy(stopWatching)`. A call
|
|
10
|
+
* whose result is already used is left alone. One given an engine with no app to read it off, or
|
|
11
|
+
* in a scope that already has a `stopWatching`, is noted rather than changed.
|
|
12
|
+
*/
|
|
13
|
+
const { applied, scriptKind, SOURCE, typescript } = require('./move-imports.cjs');
|
|
14
|
+
const { files } = require('./host.cjs');
|
|
15
|
+
|
|
16
|
+
const ENTRY = '@ng-native/device';
|
|
17
|
+
const NAME = 'stopWatching';
|
|
18
|
+
|
|
19
|
+
/** The local names `watchConditions` is imported as. */
|
|
20
|
+
function imported(ts, source) {
|
|
21
|
+
const names = new Set();
|
|
22
|
+
for (const statement of source.statements) {
|
|
23
|
+
if (!ts.isImportDeclaration(statement) || statement.moduleSpecifier.text !== ENTRY) continue;
|
|
24
|
+
const bindings = statement.importClause?.namedBindings;
|
|
25
|
+
for (const element of bindings && ts.isNamedImports(bindings) ? bindings.elements : []) {
|
|
26
|
+
const name = (element.propertyName ?? element.name).text;
|
|
27
|
+
if (name === 'watchConditions') names.add(element.name.text);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return names;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The note for a call this migration cannot rewrite. */
|
|
34
|
+
function note(file, source, call) {
|
|
35
|
+
const line = source.getLineAndCharacterOfPosition(call.getStart()).line + 1;
|
|
36
|
+
return (
|
|
37
|
+
`${file}:${line}: ${call.getText()} returns a function that stops watching. ` +
|
|
38
|
+
'Keep it and call it as the app is destroyed: app.applicationRef.onDestroy(stop).'
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Each bare `watchConditions(x.engine, ...)` statement as an edit, and a note for the rest. */
|
|
43
|
+
function rewrite(ts, file, source, names, notes) {
|
|
44
|
+
const text = source.text;
|
|
45
|
+
const edits = [];
|
|
46
|
+
const visit = (node) => {
|
|
47
|
+
const call = ts.isExpressionStatement(node) && node.expression;
|
|
48
|
+
if (
|
|
49
|
+
call &&
|
|
50
|
+
ts.isCallExpression(call) &&
|
|
51
|
+
ts.isIdentifier(call.expression) &&
|
|
52
|
+
names.has(call.expression.text)
|
|
53
|
+
) {
|
|
54
|
+
const [engine] = call.arguments;
|
|
55
|
+
const app =
|
|
56
|
+
engine &&
|
|
57
|
+
ts.isPropertyAccessExpression(engine) &&
|
|
58
|
+
engine.name.text === 'engine' &&
|
|
59
|
+
ts.isIdentifier(engine.expression) &&
|
|
60
|
+
engine.expression.text;
|
|
61
|
+
// ponytail: a stopWatching anywhere in the file counts as taken, rather than resolving scope.
|
|
62
|
+
if (!app || new RegExp(`\\b${NAME}\\b`).test(text)) notes.push(note(file, source, call));
|
|
63
|
+
else {
|
|
64
|
+
const line = text.lastIndexOf('\n', node.getStart()) + 1;
|
|
65
|
+
const indent = text.slice(line, node.getStart());
|
|
66
|
+
edits.push({
|
|
67
|
+
start: node.getStart(),
|
|
68
|
+
end: node.getEnd(),
|
|
69
|
+
text: `const ${NAME} = ${call.getText()};\n${indent}${app}.applicationRef.onDestroy(${NAME});`,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
ts.forEachChild(node, visit);
|
|
74
|
+
};
|
|
75
|
+
visit(source);
|
|
76
|
+
return edits;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** @param {import('./host.cjs').Host} host */
|
|
80
|
+
function stopWatchingConditions(host) {
|
|
81
|
+
const ts = typescript();
|
|
82
|
+
const notes = [];
|
|
83
|
+
for (const file of files(host)) {
|
|
84
|
+
const text = SOURCE.test(file) ? host.read(file) : null;
|
|
85
|
+
if (text === null || !text.includes(ENTRY) || !text.includes('watchConditions')) continue;
|
|
86
|
+
const source = ts.createSourceFile(
|
|
87
|
+
file,
|
|
88
|
+
text,
|
|
89
|
+
ts.ScriptTarget.Latest,
|
|
90
|
+
true,
|
|
91
|
+
scriptKind(ts, file),
|
|
92
|
+
);
|
|
93
|
+
const names = imported(ts, source);
|
|
94
|
+
if (!names.size) continue;
|
|
95
|
+
const edits = rewrite(ts, file, source, names, notes);
|
|
96
|
+
if (edits.length) host.write(file, applied(text, edits));
|
|
97
|
+
}
|
|
98
|
+
return notes;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
module.exports = { stopWatchingConditions };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@ng-native/nx` wrote `nxViteTsPaths()` from `@nx/vite` into the Vitest config of an app or
|
|
3
|
+
* library in a workspace with tsconfig path aliases. From Nx 24 that export is an inert stub, so a
|
|
4
|
+
* test importing one of the workspace's libraries failed with "Cannot find package '@org/ui'". The
|
|
5
|
+
* generators now write `vite-tsconfig-paths`, as Nx 24 does in its own Vite configs.
|
|
6
|
+
*
|
|
7
|
+
* Each `vitest.config.mts` that runs `ngNative()` and imports `nxViteTsPaths` from `@nx/vite`
|
|
8
|
+
* imports `tsconfigPaths` from `vite-tsconfig-paths` in its place, and the root `package.json`
|
|
9
|
+
* gains `vite-tsconfig-paths` when it lists neither. A config the generator did not write is left
|
|
10
|
+
* as it is.
|
|
11
|
+
*/
|
|
12
|
+
const { files } = require('./host.cjs');
|
|
13
|
+
const { formatted } = require('./sync-app-versions.cjs');
|
|
14
|
+
|
|
15
|
+
// Either quote, as the workspace's Prettier formats it.
|
|
16
|
+
const FROM =
|
|
17
|
+
/import \{ nxViteTsPaths \} from (['"])@nx\/vite\/plugins\/nx-tsconfig-paths\.plugin\1;/;
|
|
18
|
+
const TO = 'import tsconfigPaths from $1vite-tsconfig-paths$1;';
|
|
19
|
+
const DEPENDENCY = { name: 'vite-tsconfig-paths', range: '^5.1.4' };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Rewrites each config the generators wrote, and answers whether there was one.
|
|
23
|
+
*
|
|
24
|
+
* @param {import('./host.cjs').Host} host
|
|
25
|
+
*/
|
|
26
|
+
function rewriteConfigs(host) {
|
|
27
|
+
let changed = false;
|
|
28
|
+
for (const file of files(host)) {
|
|
29
|
+
if (file !== 'vitest.config.mts' && !file.endsWith('/vitest.config.mts')) continue;
|
|
30
|
+
const text = host.read(file) ?? '';
|
|
31
|
+
if (!FROM.test(text) || !text.includes('@ng-native/testing/vitest')) continue;
|
|
32
|
+
host.write(file, text.replace(FROM, TO).replace(/\bnxViteTsPaths\(\)/g, 'tsconfigPaths()'));
|
|
33
|
+
changed = true;
|
|
34
|
+
}
|
|
35
|
+
return changed;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** @param {import('./host.cjs').Host} host */
|
|
39
|
+
function vitestTsconfigPaths(host) {
|
|
40
|
+
const original = rewriteConfigs(host) ? host.read('package.json') : null;
|
|
41
|
+
if (original === null) return [];
|
|
42
|
+
const manifest = JSON.parse(original);
|
|
43
|
+
if (manifest.dependencies?.[DEPENDENCY.name] || manifest.devDependencies?.[DEPENDENCY.name]) {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
manifest.devDependencies = { ...manifest.devDependencies, [DEPENDENCY.name]: DEPENDENCY.range };
|
|
47
|
+
host.write('package.json', formatted(manifest, original));
|
|
48
|
+
return ['Install vite-tsconfig-paths, which the root package.json now lists.'];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = { vitestTsconfigPaths };
|