|
| 1 | +import { formatFiles, Tree, updateJson } from '@nx/devkit'; |
| 2 | +import { readModulePackageJson } from 'nx/src/utils/package-json'; |
| 3 | +import { updateDemoAppPackages } from '../../utils/migrations'; |
| 4 | +import { dirname } from 'path'; |
| 5 | +const migrations = require('./migrations-to-run.json'); |
| 6 | + |
| 7 | +function readPackageMigrationConfig(packageName: string, dir?: string) { |
| 8 | + const { path: packageJsonPath, packageJson: json } = readModulePackageJson(packageName, dir ? [dir] : undefined); |
| 9 | + |
| 10 | + const migrationConfigOrFile = json['nx-migrations'] || json['ng-update']; |
| 11 | + |
| 12 | + if (!migrationConfigOrFile) { |
| 13 | + return { packageJson: json, migrations: null, packageGroup: [] }; |
| 14 | + } |
| 15 | + |
| 16 | + const migrationsConfig = |
| 17 | + typeof migrationConfigOrFile === 'string' |
| 18 | + ? { |
| 19 | + migrations: migrationConfigOrFile, |
| 20 | + packageGroup: [], |
| 21 | + } |
| 22 | + : migrationConfigOrFile; |
| 23 | + |
| 24 | + try { |
| 25 | + const migrationFile = require.resolve(migrationsConfig.migrations, { |
| 26 | + paths: [dirname(packageJsonPath)], |
| 27 | + }); |
| 28 | + |
| 29 | + return { |
| 30 | + packageJson: json, |
| 31 | + migrations: migrationFile, |
| 32 | + packageGroup: migrationsConfig.packageGroup, |
| 33 | + }; |
| 34 | + } catch { |
| 35 | + return { |
| 36 | + packageJson: json, |
| 37 | + migrations: null, |
| 38 | + packageGroup: migrationsConfig.packageGroup, |
| 39 | + }; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +export default async function (tree: Tree) { |
| 44 | + updateDependencies(tree); |
| 45 | + updateJson(tree, 'tsconfig.base.json', (json) => { |
| 46 | + json.compilerOptions = json.compilerOptions || {}; |
| 47 | + json.compilerOptions.target = 'ES2020'; |
| 48 | + json.compilerOptions.module = 'ESNext'; |
| 49 | + json.compilerOptions.lib = ['ESNext', 'dom']; |
| 50 | + return json; |
| 51 | + }); |
| 52 | + updateDemoAppPackages(tree, { |
| 53 | + devDependencies: { |
| 54 | + '@nativescript/android': '~8.7.0', |
| 55 | + '@nativescript/ios': '~8.7.0', |
| 56 | + }, |
| 57 | + }); |
| 58 | + for (const migration of migrations.migrations) { |
| 59 | + const packageName = migration.package; |
| 60 | + const implRelativePath = migration.implementation || migration.factory; |
| 61 | + const collectionPath = readPackageMigrationConfig(packageName).migrations; |
| 62 | + let implPath: string; |
| 63 | + |
| 64 | + if (collectionPath) { |
| 65 | + let fn: any; |
| 66 | + try { |
| 67 | + try { |
| 68 | + implPath = require.resolve(implRelativePath, { |
| 69 | + paths: [dirname(collectionPath)], |
| 70 | + }); |
| 71 | + } catch (e) { |
| 72 | + // workaround for a bug in node 12 |
| 73 | + implPath = require.resolve(`${dirname(collectionPath)}/${implRelativePath}`); |
| 74 | + } |
| 75 | + |
| 76 | + fn = require(implPath).default; |
| 77 | + } catch (e) { |
| 78 | + // ignore, most likely missing package |
| 79 | + } |
| 80 | + if (fn) { |
| 81 | + await fn(tree, {}); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + // TODO: Edit the generators to use the new tsconfig |
| 86 | + |
| 87 | + await formatFiles(tree); |
| 88 | + |
| 89 | + console.log(`\n NOTE: Your plugin workspace is now migrated. Run this to finish the dependency cleanup:`); |
| 90 | + console.log(`\n`); |
| 91 | + console.log(` npm run setup`); |
| 92 | + console.log(`\n`); |
| 93 | + console.log(` This will ensure your workspace is properly reset with all the updates.`); |
| 94 | + console.log(` It is also recommended to clean all your demo apps.`); |
| 95 | + console.log(`\n`); |
| 96 | +} |
| 97 | + |
| 98 | +function updateDependencies(tree: Tree) { |
| 99 | + updateJson(tree, 'package.json', (json) => { |
| 100 | + if (json.devDependencies['@angular/core']) { |
| 101 | + json.devDependencies['@angular-devkit/build-angular'] = '^18.0.0'; |
| 102 | + for (const key in json.devDependencies) { |
| 103 | + if (key.indexOf('@angular/') > -1) { |
| 104 | + json.devDependencies[key] = '^18.0.0'; |
| 105 | + } |
| 106 | + if (key.indexOf('@angular-eslint/') > -1) { |
| 107 | + json.devDependencies[key] = '^18.0.0'; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + json.devDependencies['@nativescript/angular'] = '^18.0.0'; |
| 112 | + json.devDependencies['@nativescript/core'] = '~8.7.0'; |
| 113 | + json.devDependencies['@nativescript/types'] = '~8.7.0'; |
| 114 | + json.devDependencies['@ngtools/webpack'] = '^18.0.0'; |
| 115 | + json.devDependencies['husky'] = '~9.0.0'; |
| 116 | + json.devDependencies['ng-packagr'] = '^18.0.0'; |
| 117 | + json.devDependencies['rxjs'] = '~7.8.0'; |
| 118 | + json.devDependencies['zone.js'] = '~0.14.0'; |
| 119 | + json.devDependencies['typescript'] = '~5.4.0'; |
| 120 | + |
| 121 | + if (json.devDependencies['ts-patch']) { |
| 122 | + json.devDependencies['ts-patch'] = '^3.0.0'; |
| 123 | + } |
| 124 | + return json; |
| 125 | + }); |
| 126 | +} |
0 commit comments