@ng-native/migrate 0.0.0-stage → 0.3.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/native-app.cjs ADDED
@@ -0,0 +1,228 @@
1
+ /**
2
+ * What an Angular Native app is made of, whichever generator writes it: `@ng-native/nx`'s
3
+ * `application` and `@ng-native/schematics`'s. Both depend on this package already, for the
4
+ * migrations, so the app's shape is written once here rather than once in each.
5
+ *
6
+ * Only what is the same in every workspace lives here: the dependencies and their ranges, the
7
+ * `app.json`, the bundle identifier, the app's `.gitignore`. What a workspace tool changes, and
8
+ * the template files each generator copies, stay with that generator.
9
+ *
10
+ * The source of truth is `template/`, the starter `create-expo-app` uses and the setup this project
11
+ * verifies before every release. The versions below are the template's own, and each generator's
12
+ * `native-app.test.ts` fails the moment they drift from it.
13
+ *
14
+ * The `@ng-native/*` packages are pinned to this package's own version, because every one of them
15
+ * is released in lockstep with it.
16
+ */
17
+ const semver = require('semver');
18
+
19
+ const { version } = require('./package.json');
20
+
21
+ const FRAMEWORK = [
22
+ '@ng-native/components',
23
+ '@ng-native/device',
24
+ '@ng-native/fabric',
25
+ '@ng-native/metro',
26
+ '@ng-native/platform',
27
+ ];
28
+
29
+ /** @type {Record<string, string>} */
30
+ const dependencies = {
31
+ ...Object.fromEntries(FRAMEWORK.map((name) => [name, version])),
32
+ '@angular/common': '^22.0.0',
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',
40
+ };
41
+
42
+ /** @type {Record<string, string>} */
43
+ const devDependencies = {
44
+ // An Angular workspace already has Babel 8 through @angular/compiler-cli, and React Native's
45
+ // Babel peer would otherwise take the newest copy there is.
46
+ // The typecheck script compiles the templates too, which only the Angular compiler can.
47
+ '@angular/compiler-cli': '^22.0.0',
48
+ '@babel/core': '^7.29.7',
49
+ '@ng-native/testing': version,
50
+ '@types/react': '~19.2.2',
51
+ typescript: '~6.0.3',
52
+ vitest: '^5.0.0',
53
+ };
54
+
55
+ /**
56
+ * The app's own `.gitignore`: the lines of the template's that are about the app's directory. The
57
+ * rest, `node_modules/` and the like, are the workspace's business.
58
+ */
59
+ const GITIGNORE = [
60
+ '# generated native folders',
61
+ '/ios',
62
+ '/android',
63
+ '',
64
+ "# Generated by @ng-native/tailwind's withTailwind, rebuilt on every Metro start",
65
+ '.angular-native/',
66
+ '',
67
+ ].join('\n');
68
+
69
+ /** Java's reserved words, which Android refuses as a segment of a package name. */
70
+ const JAVA_KEYWORDS = new Set(
71
+ (
72
+ 'abstract assert boolean break byte case catch char class const continue default do double ' +
73
+ 'else enum extends false final finally float for goto if implements import instanceof int ' +
74
+ 'interface long native new null package private protected public return short static ' +
75
+ 'strictfp super switch synchronized this throw throws transient true try void volatile while'
76
+ ).split(' '),
77
+ );
78
+
79
+ /**
80
+ * Kotlin's hard keywords, which it refuses as a package segment as Java refuses its own: Expo
81
+ * writes the Android package into `MainActivity.kt` and `MainApplication.kt` without escaping one.
82
+ */
83
+ const KOTLIN_KEYWORDS = new Set(
84
+ (
85
+ 'as break class continue do else false for fun if in interface is null object package return ' +
86
+ 'super this throw true try typealias typeof val var when while'
87
+ ).split(' '),
88
+ );
89
+
90
+ /** Whether `segment` is a keyword Java or Kotlin refuses as a package segment. */
91
+ const isKeyword = (segment) => JAVA_KEYWORDS.has(segment) || KOTLIN_KEYWORDS.has(segment);
92
+
93
+ /**
94
+ * Whether iOS takes `id` as a bundle identifier and Android as a package name: two or more
95
+ * segments, each a letter and then letters or digits (iOS refuses `_`, Android `-`), none of them
96
+ * a Java or Kotlin keyword.
97
+ *
98
+ * @param {string} id
99
+ */
100
+ function isBundleIdentifier(id) {
101
+ const segments = id.split('.');
102
+ return segments.length > 1 && segments.every((s) => /^[a-z][a-z0-9]*$/i.test(s) && !isKeyword(s));
103
+ }
104
+
105
+ /**
106
+ * Why `id` cannot be the app's bundle identifier, as a sentence, or `undefined` when it can.
107
+ *
108
+ * @param {string} id
109
+ */
110
+ function bundleIdentifierProblem(id) {
111
+ if (isBundleIdentifier(id)) return undefined;
112
+ return (
113
+ `${id} is not a bundle identifier both iOS and Android accept: ` +
114
+ 'two or more dot-separated segments, each a letter and then letters or digits, none a Java or ' +
115
+ 'Kotlin keyword.'
116
+ );
117
+ }
118
+
119
+ /**
120
+ * `com.<scope>.<name>`, for an app given no `--bundleIdentifier`, where prebuild would otherwise
121
+ * make it `com.anonymous.<name>`. The scope is left out when it cannot be made a segment.
122
+ *
123
+ * @param {string} name the app's name, without a scope
124
+ * @param {string | undefined} scope the workspace's npm scope, `@acme`
125
+ */
126
+ function bundleIdentifier(name, scope) {
127
+ const clean = (text) => text.toLowerCase().replace(/[^a-z0-9]/g, '');
128
+ const valid = (segment) => /^[a-z]/.test(segment) && !isKeyword(segment);
129
+ const org = clean(scope ?? '');
130
+ const app = clean(name);
131
+ return ['com', ...(valid(org) ? [org] : []), valid(app) ? app : `app${app}`].join('.');
132
+ }
133
+
134
+ /**
135
+ * The template's `app.json`, named for this app, without the template's icons: they are the
136
+ * template's branding, and Expo draws its own default until `icon` is set.
137
+ *
138
+ * @param {string} name the project name, which is also the Expo slug once any scope is removed
139
+ * @param {string} [id] the iOS bundle identifier and Android package, by default one from `name`
140
+ */
141
+ function appJson(name, id) {
142
+ const slug = name.replace(/^@[^/]+\//, '');
143
+ const bundle = id ?? bundleIdentifier(slug, name.match(/^@[^/]+/)?.[0]);
144
+ const expo = {
145
+ name: slug,
146
+ slug,
147
+ version: '1.0.0',
148
+ // Not left to Expo, which adds `web` whenever `react-dom` resolves, and @nx/react puts one there.
149
+ platforms: ['ios', 'android'],
150
+ orientation: 'portrait',
151
+ userInterfaceStyle: 'automatic',
152
+ ios: { supportsTablet: true, bundleIdentifier: bundle },
153
+ android: { package: bundle, predictiveBackGestureEnabled: false },
154
+ scheme: slug.replace(/[^a-z0-9]/gi, '').toLowerCase(),
155
+ // The template's: Expo otherwise guesses a router root from src/app and says so on every start.
156
+ extra: { router: { root: 'src/app' } },
157
+ plugins: ['@ng-native/metro'],
158
+ };
159
+ return JSON.stringify({ expo }, null, 2) + '\n';
160
+ }
161
+
162
+ /**
163
+ * The dependencies an app lists in its own `package.json`, at the ranges the workspace root
164
+ * installs, though nothing installs from it.
165
+ *
166
+ * All of them, because Expo's autolinking links the native modules the app's `package.json` names
167
+ * and no others: without `react-native-safe-area-context` here, Android crashed on RNCSafeAreaView.
168
+ * And `expo prebuild`, which `expo run:ios` starts with, adds `expo`, `react` or `react-native` if
169
+ * it does not find them and then offers to install them into the app's directory, which would put a
170
+ * second copy of React Native beside the root's.
171
+ *
172
+ * @param {{ dependencies?: Record<string, string>, devDependencies?: Record<string, string> }} root
173
+ */
174
+ function prebuildPins(root) {
175
+ const installed = { ...root.devDependencies, ...root.dependencies };
176
+ return Object.fromEntries(
177
+ Object.keys(dependencies).map((pkg) => [pkg, installed[pkg] ?? dependencies[pkg]]),
178
+ );
179
+ }
180
+
181
+ /**
182
+ * 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.
185
+ */
186
+ const accepted = { vitest: '^4.0.8 || ^5.0.0' };
187
+
188
+ /**
189
+ * Why an existing dependency would stop this app installing or running, as a sentence each.
190
+ *
191
+ * Only ranges already in the manifest are judged, and only a range that cannot overlap the one
192
+ * wanted is a conflict: `~6.0.2` and `~6.0.3` resolve to the same TypeScript. A missing package is
193
+ * added at the version above. Nothing is rewritten, because moving a workspace's Angular or Vitest
194
+ * is an upgrade of every project in it, and that belongs to `ng update` or `nx migrate`, not to a
195
+ * generator.
196
+ *
197
+ * @param {{ dependencies?: Record<string, string>, devDependencies?: Record<string, string> }} manifest
198
+ * @returns {string[]}
199
+ */
200
+ function conflicts(manifest) {
201
+ const existing = { ...manifest.dependencies, ...manifest.devDependencies };
202
+ const problems = [];
203
+ for (const [name, installed] of Object.entries({ ...dependencies, ...devDependencies })) {
204
+ const range = existing[name];
205
+ const wanted = accepted[name] ?? installed;
206
+ if (!range || name.startsWith('@ng-native/')) continue;
207
+ if (semver.validRange(range) && !semver.intersects(range, wanted)) {
208
+ problems.push(
209
+ `${name} is ${range} here, and Angular Native needs ${wanted}. ` +
210
+ `npm will refuse the install until it is moved.`,
211
+ );
212
+ }
213
+ }
214
+ return problems;
215
+ }
216
+
217
+ module.exports = {
218
+ dependencies,
219
+ devDependencies,
220
+ GITIGNORE,
221
+ appJson,
222
+ bundleIdentifier,
223
+ isBundleIdentifier,
224
+ bundleIdentifierProblem,
225
+ prebuildPins,
226
+ accepted,
227
+ conflicts,
228
+ };
package/package.json CHANGED
@@ -1,6 +1,53 @@
1
1
  {
2
2
  "name": "@ng-native/migrate",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.3.0",
4
+ "description": "The migrations that update an Angular Native app to a new release, for nx migrate, ng update and npx.",
5
+ "keywords": [
6
+ "angular",
7
+ "react-native",
8
+ "angular-native",
9
+ "expo",
10
+ "migrations",
11
+ "codemod",
12
+ "upgrade"
13
+ ],
14
+ "type": "module",
15
+ "main": "index.cjs",
16
+ "bin": {
17
+ "ng-native-migrate": "./cli.cjs"
18
+ },
19
+ "dependencies": {
20
+ "semver": "^7.7.0"
21
+ },
22
+ "peerDependencies": {
23
+ "typescript": ">=5.9.0 <7"
24
+ },
25
+ "devDependencies": {
26
+ "typescript": "~6.0.3"
27
+ },
28
+ "nx": {
29
+ "tags": [
30
+ "scope:migrate",
31
+ "layer:build"
32
+ ]
33
+ },
34
+ "license": "MIT",
35
+ "homepage": "https://ng-native.com/guide/updating",
36
+ "bugs": {
37
+ "url": "https://github.com/ng-native/ng-native/issues"
38
+ },
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/ng-native/ng-native.git",
42
+ "directory": "packages/migrate"
43
+ },
44
+ "files": [
45
+ "*.cjs"
46
+ ],
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "scripts": {
51
+ "typecheck": "tsc -p tsconfig.json --noEmit"
52
+ }
6
53
  }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * 0.3.0 split `@ng-native/expo/store` and `@ng-native/expo/player` into an entry point per native
3
+ * module, so an app installs only the module it uses: each entry point `require`s its module, and
4
+ * Metro fails a build on any `require` it cannot resolve, whether or not the code runs. What both
5
+ * halves share (`Store`, `NativeStore`, `Player`, `PlayerState` and the rest) stays where it was.
6
+ */
7
+ const { moveImports } = require('./move-imports.cjs');
8
+
9
+ const MOVED = {
10
+ '@ng-native/expo/store': {
11
+ Storage: '@ng-native/expo/async-storage',
12
+ SecureStorage: '@ng-native/expo/secure-store',
13
+ },
14
+ '@ng-native/expo/player': {
15
+ audioPlayer: '@ng-native/expo/audio',
16
+ videoPlayer: '@ng-native/expo/video',
17
+ },
18
+ };
19
+
20
+ /** @param {import('./host.cjs').Host} host */
21
+ function splitStoreAndPlayer(host) {
22
+ return moveImports(host, MOVED);
23
+ }
24
+
25
+ module.exports = { splitStoreAndPlayer };
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Moves the `@ng-native/*` packages every `package.json` in the workspace lists to this release.
3
+ *
4
+ * `nx migrate` and `ng update` move the root `package.json`'s, through the package group, and no
5
+ * others. An app's own `package.json` lists the same packages: Expo links the native modules it
6
+ * names, and with package-manager workspaces it is what they are installed from. A plain Expo app
7
+ * has only the one, which `ng-native-migrate` moves through this too.
8
+ *
9
+ * A range keeps its `^` or `~`. Anything that is not a version, such as `workspace:*` or `file:`,
10
+ * stays, and so do peer ranges, which say what a library accepts rather than what it installs.
11
+ */
12
+ const semver = require('semver');
13
+ const { files } = require('./host.cjs');
14
+ const { version } = require('./package.json');
15
+
16
+ /** @param {Record<string, string> | undefined} dependencies */
17
+ function moved(dependencies) {
18
+ if (!dependencies) return dependencies;
19
+ return Object.fromEntries(
20
+ Object.entries(dependencies).map(([name, range]) => {
21
+ // An exact version, or one behind a `^` or `~`, with any prerelease or build suffix SemVer
22
+ // allows. A compound range, a tag or a link is not a version to move.
23
+ const [, prefix = '', exact = ''] = /^([~^]?)(.*)$/.exec(range) ?? [];
24
+ const moves = name.startsWith('@ng-native/') && semver.valid(exact) !== null;
25
+ return [name, moves ? prefix + version : range];
26
+ }),
27
+ );
28
+ }
29
+
30
+ /** A `package.json`'s contents, or null for one that is not JSON, such as a generator's template. */
31
+ function parsed(text) {
32
+ try {
33
+ return JSON.parse(text);
34
+ } catch {
35
+ return null;
36
+ }
37
+ }
38
+
39
+ /**
40
+ * `manifest` written the way `original` was: its indentation (tabs, or however many spaces), its
41
+ * line endings, and a final newline only if it had one.
42
+ */
43
+ function formatted(manifest, original) {
44
+ const indent = /^[ \t]+(?=")/m.exec(original)?.[0] ?? ' ';
45
+ const newline = original.includes('\r\n') ? '\r\n' : '\n';
46
+ const text = JSON.stringify(manifest, null, indent).replace(/\n/g, newline);
47
+ return /\r?\n$/.test(original) ? text + newline : text;
48
+ }
49
+
50
+ /** The package manager whose lockfile the workspace has. */
51
+ function packageManager(host) {
52
+ const lockfiles = {
53
+ 'pnpm-lock.yaml': 'pnpm',
54
+ 'yarn.lock': 'yarn',
55
+ 'bun.lock': 'bun',
56
+ 'bun.lockb': 'bun',
57
+ };
58
+ return Object.entries(lockfiles).find(([file]) => host.read(file) !== null)?.[1] ?? 'npm';
59
+ }
60
+
61
+ /**
62
+ * @param {import('./host.cjs').Host} host
63
+ * @returns {string[]} the install to run, when a version moved
64
+ */
65
+ function syncAppVersions(host) {
66
+ let changed = false;
67
+ for (const file of files(host)) {
68
+ if (file !== 'package.json' && !file.endsWith('/package.json')) continue;
69
+ const manifest = parsed(host.read(file) ?? '');
70
+ if (!manifest) continue;
71
+ const before = JSON.stringify(manifest);
72
+ for (const field of ['dependencies', 'devDependencies']) {
73
+ if (manifest[field]) manifest[field] = moved(manifest[field]);
74
+ }
75
+ if (JSON.stringify(manifest) === before) continue;
76
+ host.write(file, formatted(manifest, host.read(file) ?? ''));
77
+ changed = true;
78
+ }
79
+ if (!changed) return [];
80
+ return [
81
+ `Run ${packageManager(host)} install to install the @ng-native versions the projects now list.`,
82
+ ];
83
+ }
84
+
85
+ module.exports = { syncAppVersions };