@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/LICENSE +21 -0
- package/README.md +35 -2
- package/adapters.cjs +65 -0
- package/cli.cjs +121 -0
- package/device-react-native-import.cjs +62 -0
- package/host.cjs +38 -0
- package/index.cjs +10 -0
- package/migrations.cjs +57 -0
- package/move-imports.cjs +193 -0
- package/native-app.cjs +228 -0
- package/package.json +50 -3
- package/split-store-and-player.cjs +25 -0
- package/sync-app-versions.cjs +85 -0
- package/tracking-available-getter.cjs +363 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ashley Hunter
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ng-native/migrate
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The migrations that update an Angular Native app to a new release. `nx migrate @ng-native/nx` and
|
|
4
|
+
`ng update @ng-native/schematics` run them for you; an app with neither, such as one made from
|
|
5
|
+
`@ng-native/template`, runs them with `npx`.
|
|
6
|
+
|
|
7
|
+
Alpha: APIs may change before 1.0.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
Run it before installing the new versions - there is nothing to add as a dependency:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npx @ng-native/migrate@latest
|
|
15
|
+
npm install
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Example
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
npx @ng-native/migrate@latest --dry-run # print what would change, write nothing
|
|
22
|
+
npx @ng-native/migrate@latest --from 0.2.0 # the version the app was on
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
It runs every migration newer than the `@ng-native/*` version the app's `package.json` lists, moves
|
|
26
|
+
those versions to the new release, and prints each file it changed and anything left to do by hand.
|
|
27
|
+
|
|
28
|
+
## Docs
|
|
29
|
+
|
|
30
|
+
- [Updating an app](https://ng-native.com/guide/updating) for every way to update and what each
|
|
31
|
+
migration does
|
|
32
|
+
- [Root README](https://github.com/ng-native/ng-native/blob/main/README.md)
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
MIT
|
package/adapters.cjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The migrations as `nx migrate` and `ng update` call them.
|
|
3
|
+
*
|
|
4
|
+
* Each tool loads a migration from its package's `migrations.json` as `./migrations/run.cjs#name`,
|
|
5
|
+
* and `@ng-native/nx` and `@ng-native/schematics` make that file one line: `forNx()` or
|
|
6
|
+
* `forAngular()`. Neither imports Nx or the Angular devkit here; each only wraps the `Tree` the
|
|
7
|
+
* tool passes in as a host.
|
|
8
|
+
*/
|
|
9
|
+
const { migrations } = require('./migrations.cjs');
|
|
10
|
+
|
|
11
|
+
/** @param {import('@nx/devkit').Tree} tree @returns {import('./host.cjs').Host} */
|
|
12
|
+
function nxHost(tree) {
|
|
13
|
+
return {
|
|
14
|
+
read: (path) => (tree.isFile(path) ? tree.read(path, 'utf-8') : null),
|
|
15
|
+
write: (path, text) => tree.write(path, text),
|
|
16
|
+
list(dir) {
|
|
17
|
+
const files = [];
|
|
18
|
+
const dirs = [];
|
|
19
|
+
for (const name of tree.children(dir)) {
|
|
20
|
+
(tree.isFile(dir ? `${dir}/${name}` : name) ? files : dirs).push(name);
|
|
21
|
+
}
|
|
22
|
+
return { files, dirs };
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** @param {import('@angular-devkit/schematics').Tree} tree @returns {import('./host.cjs').Host} */
|
|
28
|
+
function angularHost(tree) {
|
|
29
|
+
return {
|
|
30
|
+
read: (path) => (tree.exists(path) ? tree.readText(path) : null),
|
|
31
|
+
write: (path, text) =>
|
|
32
|
+
tree.exists(path) ? tree.overwrite(path, text) : tree.create(path, text),
|
|
33
|
+
list(dir) {
|
|
34
|
+
const entry = tree.getDir(`/${dir}`);
|
|
35
|
+
return { files: entry.subfiles.map(String), dirs: entry.subdirs.map(String) };
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Each migration as the function `nx migrate` calls with its `Tree`. What it answers is printed as
|
|
42
|
+
* the next steps.
|
|
43
|
+
*/
|
|
44
|
+
function forNx() {
|
|
45
|
+
return Object.fromEntries(
|
|
46
|
+
migrations.map(({ name, run }) => [name, async (tree) => run(nxHost(tree))]),
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Each migration as the factory `ng update` calls, whose rule logs what is left to do as a
|
|
52
|
+
* warning, which `ng update` prints.
|
|
53
|
+
*/
|
|
54
|
+
function forAngular() {
|
|
55
|
+
return Object.fromEntries(
|
|
56
|
+
migrations.map(({ name, run }) => [
|
|
57
|
+
name,
|
|
58
|
+
() => (tree, context) => {
|
|
59
|
+
for (const note of run(angularHost(tree))) context.logger.warn(note);
|
|
60
|
+
},
|
|
61
|
+
]),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
module.exports = { forNx, forAngular };
|
package/cli.cjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `npx @ng-native/migrate@latest`: updates an app that has neither `nx migrate` nor `ng update`,
|
|
4
|
+
* such as one made from `@ng-native/template`.
|
|
5
|
+
*
|
|
6
|
+
* ng-native-migrate [--from <version>] [--to <version>] [--dry-run] [directory]
|
|
7
|
+
*
|
|
8
|
+
* Run it before installing, as `nx migrate` is. It updates the app from the `@ng-native/*` version
|
|
9
|
+
* its `package.json` lists (or `--from`) to this package's own version, by running every migration
|
|
10
|
+
* the update crosses (newer than `from`, and no newer than this version), oldest first, as
|
|
11
|
+
* `nx migrate` and `ng update` choose them. `--to` sets another version to stop at, which is how a
|
|
12
|
+
* test runs a migration registered for a release that has not happened yet. One of them moves the versions in `package.json`,
|
|
13
|
+
* so the install comes after. It prints each file it changed and what is left to do by hand;
|
|
14
|
+
* `--dry-run` prints the same and writes nothing.
|
|
15
|
+
*/
|
|
16
|
+
const fs = require('node:fs');
|
|
17
|
+
const path = require('node:path');
|
|
18
|
+
const { parseArgs } = require('node:util');
|
|
19
|
+
const semver = require('semver');
|
|
20
|
+
const { migrations } = require('./migrations.cjs');
|
|
21
|
+
const { version } = require('./package.json');
|
|
22
|
+
|
|
23
|
+
/** The app's directory as a host whose writes are kept until `save()`. */
|
|
24
|
+
function directoryHost(root) {
|
|
25
|
+
/** @type {Map<string, string>} */
|
|
26
|
+
const written = new Map();
|
|
27
|
+
const read = (file) => {
|
|
28
|
+
if (written.has(file)) return written.get(file) ?? null;
|
|
29
|
+
const full = path.join(root, file);
|
|
30
|
+
return fs.existsSync(full) && fs.statSync(full).isFile() ? fs.readFileSync(full, 'utf8') : null;
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
read,
|
|
34
|
+
write: (file, text) => written.set(file, text),
|
|
35
|
+
list(dir) {
|
|
36
|
+
const files = [];
|
|
37
|
+
const dirs = [];
|
|
38
|
+
for (const entry of fs.readdirSync(path.join(root, dir), { withFileTypes: true })) {
|
|
39
|
+
if (entry.isFile()) files.push(entry.name);
|
|
40
|
+
else if (entry.isDirectory()) dirs.push(entry.name);
|
|
41
|
+
}
|
|
42
|
+
return { files, dirs };
|
|
43
|
+
},
|
|
44
|
+
/** The files whose text changed, as they are now. */
|
|
45
|
+
changes() {
|
|
46
|
+
const original = (file) => {
|
|
47
|
+
const full = path.join(root, file);
|
|
48
|
+
return fs.existsSync(full) ? fs.readFileSync(full, 'utf8') : null;
|
|
49
|
+
};
|
|
50
|
+
return [...written].filter(([file, text]) => original(file) !== text);
|
|
51
|
+
},
|
|
52
|
+
save() {
|
|
53
|
+
for (const [file, text] of this.changes()) {
|
|
54
|
+
fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true });
|
|
55
|
+
fs.writeFileSync(path.join(root, file), text);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The lowest `@ng-native/*` version the app's `package.json` lists, or null for none. */
|
|
62
|
+
function listedVersion(root) {
|
|
63
|
+
const file = path.join(root, 'package.json');
|
|
64
|
+
if (!fs.existsSync(file)) return null;
|
|
65
|
+
const manifest = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
66
|
+
const versions = Object.entries({ ...manifest.dependencies, ...manifest.devDependencies })
|
|
67
|
+
.filter(([name]) => name.startsWith('@ng-native/'))
|
|
68
|
+
.map(([, range]) => semver.validRange(range) && semver.minVersion(range))
|
|
69
|
+
.filter((found) => !!found);
|
|
70
|
+
return versions.length ? semver.sort(versions)[0].version : null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function main(argv) {
|
|
74
|
+
const { values, positionals } = parseArgs({
|
|
75
|
+
args: argv,
|
|
76
|
+
allowPositionals: true,
|
|
77
|
+
options: {
|
|
78
|
+
from: { type: 'string' },
|
|
79
|
+
to: { type: 'string' },
|
|
80
|
+
'dry-run': { type: 'boolean' },
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
const root = path.resolve(positionals[0] ?? '.');
|
|
84
|
+
const from = values.from ?? listedVersion(root);
|
|
85
|
+
if (!from || !semver.valid(from)) {
|
|
86
|
+
console.error(
|
|
87
|
+
`No @ng-native version found in ${path.join(root, 'package.json')}. ` +
|
|
88
|
+
'Say which version the app is on with --from, as in --from 0.2.0.',
|
|
89
|
+
);
|
|
90
|
+
return 1;
|
|
91
|
+
}
|
|
92
|
+
const to = values.to ?? version;
|
|
93
|
+
if (!semver.valid(to)) {
|
|
94
|
+
console.error(`--to ${to} is not a version, such as 0.3.0.`);
|
|
95
|
+
return 1;
|
|
96
|
+
}
|
|
97
|
+
const due = migrations
|
|
98
|
+
.filter((migration) => semver.gt(migration.version, from) && semver.lte(migration.version, to))
|
|
99
|
+
.sort((a, b) => semver.compare(a.version, b.version));
|
|
100
|
+
if (!due.length) {
|
|
101
|
+
console.log(`Nothing to migrate from ${from} to ${to}.`);
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
console.log(`Migrating from ${from} to ${to}.`);
|
|
105
|
+
const host = directoryHost(root);
|
|
106
|
+
const notes = [];
|
|
107
|
+
for (const migration of due) {
|
|
108
|
+
console.log(`- ${migration.name}: ${migration.description}`);
|
|
109
|
+
notes.push(...migration.run(host));
|
|
110
|
+
}
|
|
111
|
+
const changes = host.changes();
|
|
112
|
+
for (const [file] of changes) console.log(`UPDATE ${file}`);
|
|
113
|
+
for (const note of notes) console.log(`NOTE ${note}`);
|
|
114
|
+
if (values['dry-run']) console.log('Dry run: nothing was written.');
|
|
115
|
+
else host.save();
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (require.main === module) process.exitCode = main(process.argv.slice(2));
|
|
120
|
+
|
|
121
|
+
module.exports = { main };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.3.0 stopped exporting `reactNative()` and its `ReactNative` type from `@ng-native/device`. They
|
|
3
|
+
* are how the package reaches React Native without a static import, typed for the few modules it
|
|
4
|
+
* uses, and an app has `react-native` itself to import from. Nothing is rewritten: what an app read
|
|
5
|
+
* through `reactNative()` is a question only it can answer, so each import or re-export of either
|
|
6
|
+
* name is noted.
|
|
7
|
+
*/
|
|
8
|
+
const { scriptKind, SOURCE, typescript } = require('./move-imports.cjs');
|
|
9
|
+
const { files } = require('./host.cjs');
|
|
10
|
+
|
|
11
|
+
const ENTRY = '@ng-native/device';
|
|
12
|
+
const GONE = new Set(['reactNative', 'ReactNative']);
|
|
13
|
+
|
|
14
|
+
/** The names an import or re-export takes from `@ng-native/device` that it no longer exports. */
|
|
15
|
+
function goneFrom(ts, statement) {
|
|
16
|
+
const declaration = ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement);
|
|
17
|
+
if (!declaration || statement.moduleSpecifier?.text !== ENTRY) return [];
|
|
18
|
+
const bindings = ts.isImportDeclaration(statement)
|
|
19
|
+
? statement.importClause?.namedBindings
|
|
20
|
+
: statement.exportClause;
|
|
21
|
+
const named =
|
|
22
|
+
bindings && (ts.isNamedImports(bindings) || ts.isNamedExports(bindings))
|
|
23
|
+
? bindings.elements
|
|
24
|
+
: [];
|
|
25
|
+
return named
|
|
26
|
+
.map((element) => (element.propertyName ?? element.name).text)
|
|
27
|
+
.filter((name) => GONE.has(name));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Whether a file can import either name at all, before it is parsed. */
|
|
31
|
+
const mentions = (text) =>
|
|
32
|
+
text !== null && text.includes(ENTRY) && /\breactNative\b|\bReactNative\b/.test(text);
|
|
33
|
+
|
|
34
|
+
/** @param {import('./host.cjs').Host} host */
|
|
35
|
+
function deviceReactNativeImport(host) {
|
|
36
|
+
const ts = typescript();
|
|
37
|
+
const notes = [];
|
|
38
|
+
for (const file of files(host)) {
|
|
39
|
+
const text = SOURCE.test(file) ? host.read(file) : null;
|
|
40
|
+
if (!mentions(text)) continue;
|
|
41
|
+
const source = ts.createSourceFile(
|
|
42
|
+
file,
|
|
43
|
+
text,
|
|
44
|
+
ts.ScriptTarget.Latest,
|
|
45
|
+
true,
|
|
46
|
+
scriptKind(ts, file),
|
|
47
|
+
);
|
|
48
|
+
for (const statement of source.statements) {
|
|
49
|
+
const gone = goneFrom(ts, statement);
|
|
50
|
+
if (!gone.length) continue;
|
|
51
|
+
const line = source.getLineAndCharacterOfPosition(statement.getStart()).line + 1;
|
|
52
|
+
const what = gone.length === 1 ? `${gone[0]} is` : `${gone.join(' and ')} are`;
|
|
53
|
+
notes.push(
|
|
54
|
+
`${file}:${line}: ${what} no longer exported from '${ENTRY}'. ` +
|
|
55
|
+
"Import what you use from 'react-native' directly.",
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return notes;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
module.exports = { deviceReactNativeImport };
|
package/host.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a migration reads and writes: an app's files, through whichever tool runs it.
|
|
3
|
+
*
|
|
4
|
+
* `nx migrate` hands a migration an Nx `Tree`, `ng update` an Angular schematics `Tree`, and the
|
|
5
|
+
* `ng-native-migrate` command the app's own directory. Each is turned into a host with these
|
|
6
|
+
* three methods, so a migration is written once and runs the same under all three. Paths are
|
|
7
|
+
* relative to the workspace root, with `/` between segments and no leading `/`.
|
|
8
|
+
*
|
|
9
|
+
* @typedef {object} Host
|
|
10
|
+
* @property {(path: string) => string | null} read The file's text, or null when there is none.
|
|
11
|
+
* @property {(path: string, text: string) => void} write Creates the file or replaces it.
|
|
12
|
+
* @property {(dir: string) => { files: string[], dirs: string[] }} list The names in a directory.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Directories no migration looks in: installed packages, native projects' build output and pods,
|
|
17
|
+
* and anything hidden (`.git`, `.nx`, `.angular`, `.expo`).
|
|
18
|
+
*/
|
|
19
|
+
const SKIPPED = new Set(['node_modules', 'dist', 'build', 'Pods']);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Every file in the workspace a migration may change, outside the directories above, in the same
|
|
23
|
+
* order whichever tool lists them, so the notes come out in the same order too.
|
|
24
|
+
*
|
|
25
|
+
* @param {Host} host
|
|
26
|
+
* @param {string} [dir]
|
|
27
|
+
* @returns {Generator<string>}
|
|
28
|
+
*/
|
|
29
|
+
function* files(host, dir = '') {
|
|
30
|
+
const { files: names, dirs } = host.list(dir);
|
|
31
|
+
for (const name of [...names].sort()) yield dir ? `${dir}/${name}` : name;
|
|
32
|
+
for (const name of [...dirs].sort()) {
|
|
33
|
+
if (SKIPPED.has(name) || name.startsWith('.')) continue;
|
|
34
|
+
yield* files(host, dir ? `${dir}/${name}` : name);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = { files };
|
package/index.cjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The migrations that update an Angular Native app to a new release, and what runs them: `forNx()`
|
|
3
|
+
* for `nx migrate`, `forAngular()` for `ng update`, and the `ng-native-migrate` command for an app
|
|
4
|
+
* with neither.
|
|
5
|
+
*/
|
|
6
|
+
const { forNx, forAngular } = require('./adapters.cjs');
|
|
7
|
+
const { files } = require('./host.cjs');
|
|
8
|
+
const { migrations } = require('./migrations.cjs');
|
|
9
|
+
|
|
10
|
+
module.exports = { migrations, files, forNx, forAngular };
|
package/migrations.cjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every migration, in the one place each is written.
|
|
3
|
+
*
|
|
4
|
+
* `version` is the release that needs it: `nx migrate`, `ng update` and `ng-native-migrate` run a
|
|
5
|
+
* migration when an upgrade crosses its version, from the version an app is on to the one it moves
|
|
6
|
+
* to. A new migration takes the version of the release it ships in. `sync-app-versions` alone
|
|
7
|
+
* takes this package's own, which every release moves, so every upgrade runs it.
|
|
8
|
+
*
|
|
9
|
+
* `packages/nx/migrations.json` and `packages/schematics/migrations.json` list each one too,
|
|
10
|
+
* because that is where `nx migrate` and `ng update` look. A test in `packages/integration-tests`
|
|
11
|
+
* fails when they do not match this list.
|
|
12
|
+
*
|
|
13
|
+
* @typedef {object} Migration
|
|
14
|
+
* @property {string} name
|
|
15
|
+
* @property {string} version
|
|
16
|
+
* @property {string} description
|
|
17
|
+
* @property {(host: import('./host.cjs').Host) => string[]} run Changes the files, and answers
|
|
18
|
+
* what is left for the developer to do, one line each.
|
|
19
|
+
*/
|
|
20
|
+
const { deviceReactNativeImport } = require('./device-react-native-import.cjs');
|
|
21
|
+
const { splitStoreAndPlayer } = require('./split-store-and-player.cjs');
|
|
22
|
+
const { syncAppVersions } = require('./sync-app-versions.cjs');
|
|
23
|
+
const { trackingAvailableGetter } = require('./tracking-available-getter.cjs');
|
|
24
|
+
const { version } = require('./package.json');
|
|
25
|
+
|
|
26
|
+
/** @type {Migration[]} */
|
|
27
|
+
const migrations = [
|
|
28
|
+
{
|
|
29
|
+
name: 'sync-app-versions',
|
|
30
|
+
version,
|
|
31
|
+
description:
|
|
32
|
+
"Move the @ng-native packages each project's own package.json lists to the new version.",
|
|
33
|
+
run: syncAppVersions,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: 'split-store-and-player',
|
|
37
|
+
version: '0.3.0',
|
|
38
|
+
description:
|
|
39
|
+
'Import Storage, SecureStorage, audioPlayer and videoPlayer from the entry points that now hold them.',
|
|
40
|
+
run: splitStoreAndPlayer,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'tracking-available-getter',
|
|
44
|
+
version: '0.3.0',
|
|
45
|
+
description: "Read Tracking's available as a property rather than calling it.",
|
|
46
|
+
run: trackingAvailableGetter,
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: 'device-react-native-import',
|
|
50
|
+
version: '0.3.0',
|
|
51
|
+
description:
|
|
52
|
+
'Note each import of reactNative or ReactNative from @ng-native/device, which no longer exports them.',
|
|
53
|
+
run: deviceReactNativeImport,
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
module.exports = { migrations };
|
package/move-imports.cjs
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Moves named imports from one entry point to another, for a release that moved what an entry
|
|
3
|
+
* point exports.
|
|
4
|
+
*
|
|
5
|
+
* `table` maps an entry point to the names that left it and where each one went:
|
|
6
|
+
*
|
|
7
|
+
* { '@ng-native/expo/store': { Storage: '@ng-native/expo/async-storage' } }
|
|
8
|
+
*
|
|
9
|
+
* Every source file is parsed with TypeScript, and each `import { ... } from` and
|
|
10
|
+
* `export { ... } from` that names a moved export is split: one statement per new entry point, and
|
|
11
|
+
* the old one kept for the names that stayed. `type` modifiers and `as` aliases go with their name.
|
|
12
|
+
* Only the statement is replaced, so the file's comments and formatting stay as they were. What
|
|
13
|
+
* cannot be split by reading the statement alone (a namespace import, `export *`, `require()`,
|
|
14
|
+
* `import()`, or a test's `vi.mock`) is left alone and answered as a note with its file and line.
|
|
15
|
+
* A second run finds nothing to move, so running it again changes nothing.
|
|
16
|
+
*/
|
|
17
|
+
const { files } = require('./host.cjs');
|
|
18
|
+
|
|
19
|
+
const SOURCE = /\.[cm]?[jt]sx?$/;
|
|
20
|
+
const MOCKS = /^(mock|doMock|importActual|requireActual)$/;
|
|
21
|
+
|
|
22
|
+
/** @typedef {Record<string, Record<string, string>>} Table */
|
|
23
|
+
|
|
24
|
+
/** Where each moved name of an entry point now comes from, as a note says it. */
|
|
25
|
+
function where(moves) {
|
|
26
|
+
const byTarget = new Map();
|
|
27
|
+
for (const [name, target] of Object.entries(moves)) {
|
|
28
|
+
byTarget.set(target, [...(byTarget.get(target) ?? []), name]);
|
|
29
|
+
}
|
|
30
|
+
return [...byTarget]
|
|
31
|
+
.map(([target, names]) => `${names.join(', ')} from '${target}'`)
|
|
32
|
+
.join(' and ');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The workspace's TypeScript, whose JavaScript API parses the files. TypeScript 7 has no
|
|
37
|
+
* JavaScript API, and cannot even be `require`d.
|
|
38
|
+
*/
|
|
39
|
+
function typescript() {
|
|
40
|
+
let ts = null;
|
|
41
|
+
try {
|
|
42
|
+
ts = require('typescript');
|
|
43
|
+
} catch {
|
|
44
|
+
// Reported below, with what to do instead.
|
|
45
|
+
}
|
|
46
|
+
if (typeof ts?.createSourceFile !== 'function') {
|
|
47
|
+
throw new Error(
|
|
48
|
+
'Moving imports needs TypeScript 5.9 or 6, whose JavaScript API it parses with, and the ' +
|
|
49
|
+
'workspace has none (TypeScript 7 has no JavaScript API). Run npx @ng-native/migrate@latest, ' +
|
|
50
|
+
'which installs its own.',
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return ts;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function scriptKind(ts, file) {
|
|
57
|
+
if (/x$/.test(file)) return ts.ScriptKind.TSX;
|
|
58
|
+
return /\.[cm]?js$/.test(file) ? ts.ScriptKind.JS : ts.ScriptKind.TS;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* An `import` or `export ... from` statement rewritten as one statement per entry point, or null
|
|
63
|
+
* when it names nothing that moved.
|
|
64
|
+
*/
|
|
65
|
+
function split(statement, keyword, moves, { elements, typeOnly, defaultName }) {
|
|
66
|
+
const entry = statement.moduleSpecifier.text;
|
|
67
|
+
const stays = [];
|
|
68
|
+
const moved = new Map();
|
|
69
|
+
for (const element of elements) {
|
|
70
|
+
const target = moves[(element.propertyName ?? element.name).text];
|
|
71
|
+
if (target) moved.set(target, [...(moved.get(target) ?? []), element.getText()]);
|
|
72
|
+
else stays.push(element.getText());
|
|
73
|
+
}
|
|
74
|
+
if (!moved.size) return null;
|
|
75
|
+
const quote = statement.moduleSpecifier.getText()[0];
|
|
76
|
+
const end = statement.getText().endsWith(';') ? ';' : '';
|
|
77
|
+
const type = typeOnly ? 'type ' : '';
|
|
78
|
+
const line = (head, names, target) => {
|
|
79
|
+
const bindings = [head, names.length ? `{ ${names.join(', ')} }` : null].filter(Boolean);
|
|
80
|
+
return `${keyword} ${type}${bindings.join(', ')} from ${quote}${target}${quote}${end}`;
|
|
81
|
+
};
|
|
82
|
+
const lines = [];
|
|
83
|
+
if (defaultName || stays.length) lines.push(line(defaultName, stays, entry));
|
|
84
|
+
for (const [target, names] of moved) lines.push(line(null, names, target));
|
|
85
|
+
return lines.join('\n');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The moves for the entry point an `import` or `export ... from` names, if it is in the table. */
|
|
89
|
+
function movesOf(ts, statement, table) {
|
|
90
|
+
if (!ts.isImportDeclaration(statement) && !ts.isExportDeclaration(statement)) return undefined;
|
|
91
|
+
const specifier = statement.moduleSpecifier;
|
|
92
|
+
return specifier && ts.isStringLiteral(specifier) ? table[specifier.text] : undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** What an import becomes: `{ text }` to replace it, `{ note }` when it cannot be split, or null. */
|
|
96
|
+
function rewriteImport(ts, statement, moves) {
|
|
97
|
+
const bindings = statement.importClause?.namedBindings;
|
|
98
|
+
if (!bindings) return null;
|
|
99
|
+
if (ts.isNamespaceImport(bindings)) return { note: 'A namespace import' };
|
|
100
|
+
const { isTypeOnly, name } = statement.importClause;
|
|
101
|
+
const text = split(statement, 'import', moves, {
|
|
102
|
+
elements: bindings.elements,
|
|
103
|
+
typeOnly: isTypeOnly,
|
|
104
|
+
defaultName: name?.text,
|
|
105
|
+
});
|
|
106
|
+
return text === null ? null : { text };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** What an `export ... from` becomes, as `rewriteImport` answers. */
|
|
110
|
+
function rewriteExport(ts, statement, moves) {
|
|
111
|
+
const clause = statement.exportClause;
|
|
112
|
+
if (!clause || ts.isNamespaceExport(clause)) return { note: 'export *' };
|
|
113
|
+
const text = split(statement, 'export', moves, {
|
|
114
|
+
elements: clause.elements,
|
|
115
|
+
typeOnly: statement.isTypeOnly,
|
|
116
|
+
defaultName: null,
|
|
117
|
+
});
|
|
118
|
+
return text === null ? null : { text };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** `text` with each edit's range replaced, the last first so the earlier offsets still hold. */
|
|
122
|
+
function applied(text, edits) {
|
|
123
|
+
let next = text;
|
|
124
|
+
for (const { start, end, text: replacement } of edits.sort((a, b) => b.start - a.start)) {
|
|
125
|
+
next = next.slice(0, start) + replacement + next.slice(end);
|
|
126
|
+
}
|
|
127
|
+
return next;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Each call that names a moved entry point: `require()`, `import()`, or a mock of it. */
|
|
131
|
+
function* namingCalls(ts, node, table) {
|
|
132
|
+
if (ts.isCallExpression(node) && node.arguments.length) {
|
|
133
|
+
const [first] = node.arguments;
|
|
134
|
+
const callee = node.expression;
|
|
135
|
+
const named =
|
|
136
|
+
callee.kind === ts.SyntaxKind.ImportKeyword ||
|
|
137
|
+
(ts.isIdentifier(callee) && callee.text === 'require') ||
|
|
138
|
+
(ts.isPropertyAccessExpression(callee) && MOCKS.test(callee.name.text));
|
|
139
|
+
if (named && ts.isStringLiteralLike(first) && table[first.text]) yield node;
|
|
140
|
+
}
|
|
141
|
+
for (const child of node.getChildren()) yield* namingCalls(ts, child, table);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The edits to a file's top-level imports and exports; each one that cannot be split is noted. */
|
|
145
|
+
function statementEdits(ts, source, table, note) {
|
|
146
|
+
const edits = [];
|
|
147
|
+
for (const statement of source.statements) {
|
|
148
|
+
const moves = movesOf(ts, statement, table);
|
|
149
|
+
if (!moves) continue;
|
|
150
|
+
const rewrite = ts.isImportDeclaration(statement) ? rewriteImport : rewriteExport;
|
|
151
|
+
const change = rewrite(ts, statement, moves);
|
|
152
|
+
if (change?.note) note(statement, change.note, statement.moduleSpecifier.text);
|
|
153
|
+
if (change?.text)
|
|
154
|
+
edits.push({ start: statement.getStart(), end: statement.getEnd(), ...change });
|
|
155
|
+
}
|
|
156
|
+
return edits;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @param {import('./host.cjs').Host} host
|
|
161
|
+
* @param {Table} table
|
|
162
|
+
* @returns {string[]} what is left to change by hand, one line per place
|
|
163
|
+
*/
|
|
164
|
+
function moveImports(host, table) {
|
|
165
|
+
const ts = typescript();
|
|
166
|
+
const notes = [];
|
|
167
|
+
for (const file of files(host)) {
|
|
168
|
+
const text = SOURCE.test(file) ? host.read(file) : null;
|
|
169
|
+
if (text === null || !Object.keys(table).some((entry) => text.includes(entry))) continue;
|
|
170
|
+
const source = ts.createSourceFile(
|
|
171
|
+
file,
|
|
172
|
+
text,
|
|
173
|
+
ts.ScriptTarget.Latest,
|
|
174
|
+
true,
|
|
175
|
+
scriptKind(ts, file),
|
|
176
|
+
);
|
|
177
|
+
const note = (node, what, entry) => {
|
|
178
|
+
const line = source.getLineAndCharacterOfPosition(node.getStart()).line + 1;
|
|
179
|
+
notes.push(
|
|
180
|
+
`${file}:${line}: ${what} cannot be split automatically. Import ${where(table[entry])} instead.`,
|
|
181
|
+
);
|
|
182
|
+
};
|
|
183
|
+
const edits = statementEdits(ts, source, table, note);
|
|
184
|
+
for (const call of namingCalls(ts, source, table)) {
|
|
185
|
+
const entry = call.arguments[0].text;
|
|
186
|
+
note(call, `${call.expression.getText()}('${entry}')`, entry);
|
|
187
|
+
}
|
|
188
|
+
if (edits.length) host.write(file, applied(text, edits));
|
|
189
|
+
}
|
|
190
|
+
return notes;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
module.exports = { moveImports, typescript, scriptKind, applied, SOURCE };
|