@ng-native/migrate 0.17.0 → 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.
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Turns off `declarationMap` in each `tsconfig.typecheck.json` `@ng-native/nx:library` wrote.
3
+ *
4
+ * That config sets `composite: false` so `ngc` can run with `noEmit`, and Nx's TypeScript preset
5
+ * turns `declarationMap` on in `tsconfig.base.json`. With neither `composite` nor `declaration`,
6
+ * TypeScript stops with TS5069 before it checks anything, so `nx typecheck` fails. A config that
7
+ * sets `declarationMap` itself, either way, is left as it is.
8
+ */
9
+ const { files } = require('./host.cjs');
10
+ const { formatted } = require('./sync-app-versions.cjs');
11
+
12
+ /**
13
+ * @param {import('./host.cjs').Host} host
14
+ * @returns {string[]} the configs that are not plain JSON, which are left for the developer
15
+ */
16
+ function libraryTypecheckDeclarationMap(host) {
17
+ const notes = [];
18
+ for (const file of files(host)) {
19
+ if (file !== 'tsconfig.typecheck.json' && !file.endsWith('/tsconfig.typecheck.json')) continue;
20
+ const original = host.read(file) ?? '';
21
+ let config;
22
+ try {
23
+ config = JSON.parse(original);
24
+ } catch {
25
+ notes.push(
26
+ `${file}: not plain JSON, so it was left as it is. If ngc reports TS5069 for it, add ` +
27
+ '"declarationMap": false to its compilerOptions.',
28
+ );
29
+ continue;
30
+ }
31
+ // The generator's: ngc's options, over the library's own config. An app's extends its own.
32
+ const generated = config.extends === './tsconfig.lib.json' && config.angularCompilerOptions;
33
+ if (!generated || 'declarationMap' in (config.compilerOptions ?? {})) continue;
34
+ config.compilerOptions = { ...config.compilerOptions, declarationMap: false };
35
+ host.write(file, formatted(config, original));
36
+ }
37
+ return notes;
38
+ }
39
+
40
+ module.exports = { libraryTypecheckDeclarationMap };
package/migrations.cjs CHANGED
@@ -20,7 +20,10 @@
20
20
  const { deviceReactNativeImport } = require('./device-react-native-import.cjs');
21
21
  const { engineNodeFields } = require('./engine-node-fields.cjs');
22
22
  const { engineNodeTag } = require('./engine-node-tag.cjs');
23
+ const { libraryTypecheckDeclarationMap } = require('./library-typecheck-declaration-map.cjs');
24
+ const { nativeTypecheckTarget } = require('./native-typecheck-target.cjs');
23
25
  const { splitStoreAndPlayer } = require('./split-store-and-player.cjs');
26
+ const { stopWatchingConditions } = require('./stop-watching-conditions.cjs');
24
27
  const { syncAppVersions } = require('./sync-app-versions.cjs');
25
28
  const { trackingAvailableGetter } = require('./tracking-available-getter.cjs');
26
29
  const { version } = require('./package.json');
@@ -66,6 +69,26 @@ const migrations = [
66
69
  description: "Read an engine node's children, text, kind, host and sheet by their new names.",
67
70
  run: engineNodeFields,
68
71
  },
72
+ {
73
+ name: 'library-typecheck-declaration-map',
74
+ version: '0.17.1',
75
+ description:
76
+ "Turn off declarationMap in each Nx library's tsconfig.typecheck.json, which TypeScript refuses beside composite: false.",
77
+ run: libraryTypecheckDeclarationMap,
78
+ },
79
+ {
80
+ name: 'native-typecheck-target',
81
+ version: '0.17.1',
82
+ description: 'Give each Angular Native project in angular.json a typecheck target, run by ngc.',
83
+ run: nativeTypecheckTarget,
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
+ },
69
92
  ];
70
93
 
71
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,
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Gives each Angular Native project in `angular.json` a `typecheck` target, as `ng add` now writes.
3
+ *
4
+ * `ng run <project>:typecheck` runs `ngc` on the project, which checks its templates as well as its
5
+ * code; without it a binding to an input a component does not have goes unchecked. A project is
6
+ * Angular Native's when one of its targets runs one of `@ng-native/schematics`' builders. A
7
+ * `typecheck` target the project already has, whatever runs it, stays.
8
+ */
9
+ const { formatted } = require('./sync-app-versions.cjs');
10
+
11
+ const TARGET = { builder: '@ng-native/schematics:typecheck', options: {} };
12
+
13
+ /** @param {Record<string, { builder?: string }> | undefined} targets */
14
+ const isNative = (targets) =>
15
+ Object.values(targets ?? {}).some((target) =>
16
+ String(target?.builder).startsWith('@ng-native/schematics:'),
17
+ );
18
+
19
+ /**
20
+ * @param {import('./host.cjs').Host} host
21
+ * @returns {string[]} what is left to do by hand: the target, when `angular.json` has comments or
22
+ * trailing commas, which the Angular CLI allows and rewriting it here would lose
23
+ */
24
+ function nativeTypecheckTarget(host) {
25
+ const original = host.read('angular.json');
26
+ if (original === null) return [];
27
+ let workspace;
28
+ try {
29
+ workspace = JSON.parse(original);
30
+ } catch {
31
+ return [
32
+ 'angular.json: not plain JSON, so no typecheck target was added. Give each Angular Native ' +
33
+ 'project one by hand: "typecheck": { "builder": "@ng-native/schematics:typecheck" } in its ' +
34
+ 'architect.',
35
+ ];
36
+ }
37
+ let changed = false;
38
+ for (const project of Object.values(workspace.projects ?? {})) {
39
+ if (!project || typeof project !== 'object') continue;
40
+ const targets = project.architect ?? project.targets;
41
+ if (!isNative(targets) || targets.typecheck) continue;
42
+ targets.typecheck = { ...TARGET };
43
+ changed = true;
44
+ }
45
+ if (changed) host.write('angular.json', formatted(workspace, original));
46
+ return [];
47
+ }
48
+
49
+ module.exports = { nativeTypecheckTarget };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ng-native/migrate",
3
- "version": "0.17.0",
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 };
@@ -82,4 +82,4 @@ function syncAppVersions(host) {
82
82
  ];
83
83
  }
84
84
 
85
- module.exports = { syncAppVersions };
85
+ module.exports = { syncAppVersions, formatted };