Skip to content

Commit

Permalink
fix(nx): simplify migrations by always updating all first-party plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Oct 23, 2019
1 parent 383b175 commit 7109577
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 70 deletions.
14 changes: 8 additions & 6 deletions packages/nest/migrations.json
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions packages/nest/src/migrations/update-8-7-0/update-8-7-0.ts
Original file line number Diff line number Diff line change
@@ -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'
);
}
19 changes: 1 addition & 18 deletions packages/react/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion packages/react/src/migrations/update-8-7-0/update-8-7-0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
38 changes: 38 additions & 0 deletions packages/tao/src/commands/migrate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
121 changes: 80 additions & 41 deletions packages/tao/src/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -271,47 +298,56 @@ function versions(root: string) {
}

// testing-fetch-start
async function fetch(
packageName: string,
packageVersion: string
): Promise<MigrationsJson> {
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<MigrationsJson> {
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

Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/workspace/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
}
},
"870": {
"version": "8.7.0-beta.1",
"version": "8.7.0",
"packages": {
"@nrwl/angular": {
"version": "8.7.0",
Expand Down Expand Up @@ -192,4 +192,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,5 +25,6 @@ export function updatePackagesInPackageJson(versionName: string) {
json.dependencies[p] = updatedPackages[p].version;
}
});
return json;
});
}

0 comments on commit 7109577

Please sign in to comment.