@ng-native/migrate 0.17.1 → 0.18.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/migrations.cjs CHANGED
@@ -23,6 +23,7 @@ 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');
28
29
  const { version } = require('./package.json');
@@ -81,6 +82,13 @@ const migrations = [
81
82
  description: 'Give each Angular Native project in angular.json a typecheck target, run by ngc.',
82
83
  run: nativeTypecheckTarget,
83
84
  },
85
+ {
86
+ name: 'stop-watching-conditions',
87
+ version: '0.17.2',
88
+ description:
89
+ "Keep what watchConditions returns in each app's entry point and stop it as the app is destroyed.",
90
+ run: stopWatchingConditions,
91
+ },
84
92
  ];
85
93
 
86
94
  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: '~57.0.26',
35
- 'expo-status-bar': '~57.0.1',
36
- 'expo-system-ui': '~57.0.4',
37
- react: '19.2.3',
38
- 'react-native': '0.86.3',
39
- 'react-native-safe-area-context': '~5.7.0',
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.2.2',
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
- * The range here is that package's `vitest` peer, and a test holds the two together.
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
- for (const [name, installed] of Object.entries({ ...dependencies, ...devDependencies })) {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ng-native/migrate",
3
- "version": "0.17.1",
3
+ "version": "0.18.0",
4
4
  "description": "The migrations that update an Angular Native app to a new release, for nx migrate, ng update and npx.",
5
5
  "keywords": [
6
6
  "angular",
@@ -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 };