diff --git a/packages/nest/migrations.json b/packages/nest/migrations.json index 6ded115b8c82f..c19d484f7b166 100644 --- a/packages/nest/migrations.json +++ b/packages/nest/migrations.json @@ -1,13 +1,15 @@ { - "schematics": {}, + "schematics": { + "update-8-7-0.": { + "version": "8.7.0-beta.1", + "description": "Update Nest.js libraries", + "factory": "./src/migrations/update-8-7-0/update-8-7-0" + } + }, "packageJsonUpdates": { "8.7.0": { - "version": "8.7.0-beta.1", + "version": "8.7.0-beta.4", "packages": { - "@nrwl/nest": { - "version": "8.7.0", - "alwaysAddToPackageJson": false - }, "@nestjs/common": { "version": "^6.8.3", "alwaysAddToPackageJson": false diff --git a/packages/nest/src/migrations/update-8-7-0/update-8-7-0.ts b/packages/nest/src/migrations/update-8-7-0/update-8-7-0.ts new file mode 100644 index 0000000000000..1091c37c58316 --- /dev/null +++ b/packages/nest/src/migrations/update-8-7-0/update-8-7-0.ts @@ -0,0 +1,10 @@ +import { Rule } from '@angular-devkit/schematics'; +import { updatePackagesInPackageJson } from '@nrwl/workspace'; +import * as path from 'path'; + +export default function update(): Rule { + return updatePackagesInPackageJson( + path.join(__dirname, '../../../', 'migrations.json'), + '8.7.0' + ); +} diff --git a/packages/react/migrations.json b/packages/react/migrations.json index 8f640292c618e..b44f038efc6cd 100644 --- a/packages/react/migrations.json +++ b/packages/react/migrations.json @@ -16,27 +16,10 @@ "factory": "./src/migrations/update-8-7-0/update-8-7-0" } }, - "packageJsonUpdates": { "8.7.0": { - "version": "8.7.0-beta.1", + "version": "8.7.0-beta.4", "packages": { - "@nrwl/react": { - "version": "8.7.0", - "alwaysAddToPackageJson": false - }, - "@nrwl/web": { - "version": "8.7.0", - "alwaysAddToPackageJson": false - }, - "@nrwl/jest": { - "version": "8.7.0", - "alwaysAddToPackageJson": false - }, - "@nrwl/cypress": { - "version": "8.7.0", - "alwaysAddToPackageJson": false - }, "react": { "version": "16.10.2", "alwaysAddToPackageJson": false diff --git a/packages/react/src/migrations/update-8-7-0/update-8-7-0.ts b/packages/react/src/migrations/update-8-7-0/update-8-7-0.ts index cc1102b783e63..1961023efa818 100644 --- a/packages/react/src/migrations/update-8-7-0/update-8-7-0.ts +++ b/packages/react/src/migrations/update-8-7-0/update-8-7-0.ts @@ -6,9 +6,16 @@ import { } from '@angular-devkit/schematics'; import { readJsonInTree, updatePackagesInPackageJson } from '@nrwl/workspace'; import { stripIndents } from '@angular-devkit/core/src/utils/literals'; +import * as path from 'path'; export default function update(): Rule { - return chain([displayInformation, updatePackagesInPackageJson('8.7.0')]); + return chain([ + displayInformation, + updatePackagesInPackageJson( + path.join(__dirname, '../../../', 'migrations.json'), + '8.7.0' + ) + ]); } function displayInformation(host: Tree, context: SchematicContext) { diff --git a/packages/tao/src/commands/migrate.spec.ts b/packages/tao/src/commands/migrate.spec.ts index a449d7be69c6d..dd216cf7260d6 100644 --- a/packages/tao/src/commands/migrate.spec.ts +++ b/packages/tao/src/commands/migrate.spec.ts @@ -274,6 +274,44 @@ describe('Migration', () => { } }); }); + + // this is temporary. if tao gets used by other projects, + // we will extract the special casing + it('should special case @nrwl/workspace', async () => { + const migrator = new Migrator({ + versions: () => '1.0.0', + fetch: (p, v) => Promise.resolve({ version: '2.0.0' }), + from: {}, + to: {} + }); + + expect( + await migrator.updatePackageJson('@nrwl/workspace', '2.0.0') + ).toEqual({ + migrations: [], + packageJson: { + '@nrwl/workspace': { + version: '2.0.0', + alwaysAddToPackageJson: false + }, + '@nrwl/angular': { version: '2.0.0', alwaysAddToPackageJson: false }, + '@nrwl/cypress': { version: '2.0.0', alwaysAddToPackageJson: false }, + '@nrwl/eslint-plugin-nx': { + version: '2.0.0', + alwaysAddToPackageJson: false + }, + '@nrwl/express': { version: '2.0.0', alwaysAddToPackageJson: false }, + '@nrwl/jest': { version: '2.0.0', alwaysAddToPackageJson: false }, + '@nrwl/linter': { version: '2.0.0', alwaysAddToPackageJson: false }, + '@nrwl/nest': { version: '2.0.0', alwaysAddToPackageJson: false }, + '@nrwl/next': { version: '2.0.0', alwaysAddToPackageJson: false }, + '@nrwl/node': { version: '2.0.0', alwaysAddToPackageJson: false }, + '@nrwl/react': { version: '2.0.0', alwaysAddToPackageJson: false }, + '@nrwl/tao': { version: '2.0.0', alwaysAddToPackageJson: false }, + '@nrwl/web': { version: '2.0.0', alwaysAddToPackageJson: false } + } + }); + }); }); describe('migrations', () => { diff --git a/packages/tao/src/commands/migrate.ts b/packages/tao/src/commands/migrate.ts index 8608048494155..1360af3c31513 100644 --- a/packages/tao/src/commands/migrate.ts +++ b/packages/tao/src/commands/migrate.ts @@ -159,7 +159,34 @@ export class Migrator { // this should be used to know what version to include // we should use from everywhere we use versions + if (packageName === '@nrwl/workspace') { + if (!m.packageJsonUpdates) m.packageJsonUpdates = {}; + m.packageJsonUpdates[targetVersion + '-defaultPackages'] = { + version: targetVersion, + packages: [ + '@nrwl/angular', + '@nrwl/cypress', + '@nrwl/eslint-plugin-nx', + '@nrwl/express', + '@nrwl/jest', + '@nrwl/linter', + '@nrwl/nest', + '@nrwl/next', + '@nrwl/node', + '@nrwl/react', + '@nrwl/tao', + '@nrwl/web' + ].reduce( + (m, c) => ({ + ...m, + [c]: { verson: targetVersion, alwaysAddToPackageJson: false } + }), + {} + ) + }; + } if (!m.packageJsonUpdates) return {}; + return Object.keys(m.packageJsonUpdates) .filter(r => { return ( @@ -271,47 +298,56 @@ function versions(root: string) { } // testing-fetch-start -async function fetch( - packageName: string, - packageVersion: string -): Promise { - const dir = dirSync().name; - execSync(`npm install ${packageName}@${packageVersion} --prefix=${dir}`, { - stdio: [] - }); - const json = JSON.parse( - stripJsonComments( - readFileSync( - path.join(dir, 'node_modules', packageName, 'package.json') - ).toString() - ) - ); - let migrationsFile = json['nx-migrations'] || json['ng-update']; +function createFetcher(logger: logging.Logger) { + let cache = {}; + return async function f( + packageName: string, + packageVersion: string + ): Promise { + if (!cache[`${packageName}-${packageVersion}`]) { + const dir = dirSync().name; + logger.info(`Fetching ${packageName}@${packageVersion}`); + execSync(`npm install ${packageName}@${packageVersion} --prefix=${dir}`, { + stdio: [] + }); + const json = JSON.parse( + stripJsonComments( + readFileSync( + path.join(dir, 'node_modules', packageName, 'package.json') + ).toString() + ) + ); + let migrationsFile = json['nx-migrations'] || json['ng-update']; - // migrationsFile is an object - if (migrationsFile && migrationsFile.migrations) { - migrationsFile = migrationsFile.migrations; - } + // migrationsFile is an object + if (migrationsFile && migrationsFile.migrations) { + migrationsFile = migrationsFile.migrations; + } - // packageVersion can be a tag, resolvedVersion works with semver - const resolvedVersion = json.version; + // packageVersion can be a tag, resolvedVersion works with semver + const resolvedVersion = json.version; - if (migrationsFile) { - const json = JSON.parse( - stripJsonComments( - readFileSync( - path.join(dir, 'node_modules', packageName, migrationsFile) - ).toString() - ) - ); - return { - version: resolvedVersion, - schematics: json.schematics, - packageJsonUpdates: json.packageJsonUpdates - }; - } else { - return { version: resolvedVersion }; - } + if (migrationsFile) { + const json = JSON.parse( + stripJsonComments( + readFileSync( + path.join(dir, 'node_modules', packageName, migrationsFile) + ).toString() + ) + ); + cache[`${packageName}-${packageVersion}`] = { + version: resolvedVersion, + schematics: json.schematics, + packageJsonUpdates: json.packageJsonUpdates + }; + } else { + cache[`${packageName}-${packageVersion}`] = { + version: resolvedVersion + }; + } + } + return cache[`${packageName}-${packageVersion}`]; + }; } // testing-fetch-end @@ -359,7 +395,7 @@ async function generateMigrationsJsonAndUpdatePackageJson( logger.info(`It may take a few minutes.`); const migrator = new Migrator({ versions: versions(root), - fetch, + fetch: createFetcher(logger), from: opts.from, to: opts.to }); @@ -409,12 +445,15 @@ class MigrationEngineHost extends NodeModulesEngineHost { packageJsonPath = path.join(packageJsonPath, 'package.json'); } let pkgJsonSchematics = require(packageJsonPath)['nx-migrations']; - if (!pkgJsonSchematics || typeof pkgJsonSchematics != 'string') { + if (!pkgJsonSchematics) { pkgJsonSchematics = require(packageJsonPath)['ng-update']; if (!pkgJsonSchematics) { throw new Error(`Could find migrations in package: "${name}"`); } } + if (typeof pkgJsonSchematics != 'string') { + pkgJsonSchematics = pkgJsonSchematics.migrations; + } collectionPath = this._resolvePath( pkgJsonSchematics, path.dirname(packageJsonPath) @@ -459,8 +498,8 @@ async function runMigrations( const workflow = new MigrationsWorkflow(host); let p = Promise.resolve(null); migrationsFile.migrations.forEach(m => { - logger.info(`Running migration ${m.package}:${m.name}`); p = p.then(() => { + logger.info(`Running migration ${m.package}:${m.name}`); return workflow .execute({ collection: m.package, diff --git a/packages/workspace/migrations.json b/packages/workspace/migrations.json index 4de37ccdb02fb..0199130284600 100644 --- a/packages/workspace/migrations.json +++ b/packages/workspace/migrations.json @@ -139,7 +139,7 @@ } }, "870": { - "version": "8.7.0-beta.1", + "version": "8.7.0", "packages": { "@nrwl/angular": { "version": "8.7.0", @@ -192,4 +192,4 @@ } } } -} +} \ No newline at end of file diff --git a/packages/workspace/src/utils/update-packages-in-package-json.ts b/packages/workspace/src/utils/update-packages-in-package-json.ts index d63dda8441227..5f3f651488cc9 100644 --- a/packages/workspace/src/utils/update-packages-in-package-json.ts +++ b/packages/workspace/src/utils/update-packages-in-package-json.ts @@ -1,8 +1,11 @@ import { updateJsonInTree } from './ast-utils'; import { readFileSync } from 'fs'; -export function updatePackagesInPackageJson(versionName: string) { - const migrations = JSON.parse(readFileSync('../migrations.json').toString()); +export function updatePackagesInPackageJson( + migrationFilePath: string, + versionName: string +) { + const migrations = JSON.parse(readFileSync(migrationFilePath).toString()); const packageJsonUpdates = migrations.packageJsonUpdates[versionName]; // should never happen @@ -22,5 +25,6 @@ export function updatePackagesInPackageJson(versionName: string) { json.dependencies[p] = updatedPackages[p].version; } }); + return json; }); }