Skip to content

Commit

Permalink
fix(core): do not migrate nrwl packages that are not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Jun 28, 2021
1 parent 11afe84 commit 3093fd1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
33 changes: 33 additions & 0 deletions packages/tao/src/commands/migrate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe('Migration', () => {
describe('packageJson patch', () => {
it('should throw an error when the target package is not available', async () => {
const migrator = new Migrator({
packageJson: {},
versions: () => '1.0',
fetch: (_p, _v) => {
throw new Error('cannot fetch');
Expand All @@ -22,6 +23,7 @@ describe('Migration', () => {

it('should return a patch to the new version', async () => {
const migrator = new Migrator({
packageJson: {},
versions: () => '1.0.0',
fetch: (_p, _v) => Promise.resolve({ version: '2.0.0' }),
from: {},
Expand All @@ -38,6 +40,7 @@ describe('Migration', () => {

it('should collect the information recursively from upserts', async () => {
const migrator = new Migrator({
packageJson: {},
versions: () => '1.0.0',
fetch: (p, _v) => {
if (p === 'parent') {
Expand Down Expand Up @@ -81,6 +84,7 @@ describe('Migration', () => {

it('should stop recursive calls when exact version', async () => {
const migrator = new Migrator({
packageJson: {},
versions: () => '1.0.0',
fetch: (p, _v) => {
if (p === 'parent') {
Expand Down Expand Up @@ -128,6 +132,7 @@ describe('Migration', () => {

it('should set the version of a dependency to the newest', async () => {
const migrator = new Migrator({
packageJson: {},
versions: () => '1.0.0',
fetch: (p, _v) => {
if (p === 'parent') {
Expand Down Expand Up @@ -191,6 +196,7 @@ describe('Migration', () => {

it('should skip the versions <= currently installed', async () => {
const migrator = new Migrator({
packageJson: {},
versions: () => '1.0.0',
fetch: (p, _v) => {
if (p === 'parent') {
Expand Down Expand Up @@ -238,6 +244,7 @@ describe('Migration', () => {

it('should conditionally process packages if they are installed', async () => {
const migrator = new Migrator({
packageJson: {},
versions: (p) => (p !== 'not-installed' ? '1.0.0' : null),
fetch: (p, _v) => {
if (p === 'parent') {
Expand Down Expand Up @@ -282,6 +289,29 @@ describe('Migration', () => {
// we will extract the special casing
it('should special case @nrwl/workspace', async () => {
const migrator = new Migrator({
packageJson: {
devDependencies: {
'@nrwl/workspace': '0.9.0',
'@nrwl/cli': '0.9.0',
'@nrwl/angular': '0.9.0',
'@nrwl/cypress': '0.9.0',
'@nrwl/devkit': '0.9.0',
'@nrwl/eslint-plugin-nx': '0.9.0',
'@nrwl/express': '0.9.0',
'@nrwl/gatsby': '0.9.0',
'@nrwl/jest': '0.9.0',
'@nrwl/linter': '0.9.0',
'@nrwl/nest': '0.9.0',
'@nrwl/next': '0.9.0',
'@nrwl/node': '0.9.0',
'@nrwl/nx-cloud': '0.9.0',
'@nrwl/nx-plugin': '0.9.0',
'@nrwl/react': '0.9.0',
'@nrwl/storybook': '0.9.0',
'@nrwl/tao': '0.9.0',
'@nrwl/web': '0.9.0',
},
},
versions: () => '1.0.0',
fetch: (_p, _v) => Promise.resolve({ version: '2.0.0' }),
from: {},
Expand Down Expand Up @@ -339,6 +369,7 @@ describe('Migration', () => {

it('should not throw when packages are missing', async () => {
const migrator = new Migrator({
packageJson: {},
versions: (p) => (p === '@nrwl/nest' ? null : '1.0.0'),
fetch: (_p, _v) =>
Promise.resolve({
Expand All @@ -353,6 +384,7 @@ describe('Migration', () => {

it('should only fetch packages that are installed', async () => {
const migrator = new Migrator({
packageJson: {},
versions: (p) => (p === '@nrwl/nest' ? null : '1.0.0'),
fetch: (p, _v) => {
if (p === '@nrwl/nest') {
Expand All @@ -373,6 +405,7 @@ describe('Migration', () => {
describe('migrations', () => {
it('should create a list of migrations to run', async () => {
const migrator = new Migrator({
packageJson: {},
versions: (p) => {
if (p === 'parent') return '1.0.0';
if (p === 'child') return '1.0.0';
Expand Down
30 changes: 20 additions & 10 deletions packages/tao/src/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,20 @@ function slash(packageName) {
}

export class Migrator {
private readonly packageJson: any;
private readonly versions: (p: string) => string;
private readonly fetch: (p: string, v: string) => Promise<MigrationsJson>;
private readonly from: { [p: string]: string };
private readonly to: { [p: string]: string };

constructor(opts: {
packageJson: any;
versions: (p: string) => string;
fetch: (p: string, v: string) => Promise<MigrationsJson>;
from: { [p: string]: string };
to: { [p: string]: string };
}) {
this.packageJson = opts.packageJson;
this.versions = opts.versions;
this.fetch = opts.fetch;
this.from = opts.from;
Expand Down Expand Up @@ -234,16 +237,21 @@ export class Migrator {
'@nrwl/storybook',
'@nrwl/tao',
'@nrwl/web',
].reduce(
(m, c) => ({
...m,
[c]: {
version: c === '@nrwl/nx-cloud' ? 'latest' : targetVersion,
alwaysAddToPackageJson: false,
},
}),
{}
),
]
.filter((pkg) => {
const { dependencies, devDependencies } = this.packageJson;
return !!dependencies?.[pkg] || !!devDependencies?.[pkg];
})
.reduce(
(m, c) => ({
...m,
[c]: {
version: c === '@nrwl/nx-cloud' ? 'latest' : targetVersion,
alwaysAddToPackageJson: false,
},
}),
{}
),
};
}
if (!m.packageJsonUpdates || !this.versions(packageName)) return {};
Expand Down Expand Up @@ -539,7 +547,9 @@ async function generateMigrationsJsonAndUpdatePackageJson(
try {
logger.info(`Fetching meta data about packages.`);
logger.info(`It may take a few minutes.`);
const originalPackageJson = readJsonFile(join(root, 'package.json'));
const migrator = new Migrator({
packageJson: originalPackageJson,
versions: versions(root, opts.from),
fetch: createFetcher(),
from: opts.from,
Expand Down

0 comments on commit 3093fd1

Please sign in to comment.