Skip to content

Commit

Permalink
fix(core): fix migrations for projects in workspace.json (#13226)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Nov 17, 2022
1 parent b0b3b5c commit bae31b2
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 27 deletions.
@@ -1,4 +1,7 @@
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
import {
createTreeWithEmptyV1Workspace,
createTreeWithEmptyWorkspace,
} from '../../generators/testing-utils/create-tree-with-empty-workspace';
import type { Tree } from '../../generators/tree';
import {
addProjectConfiguration,
Expand Down Expand Up @@ -294,3 +297,48 @@ describe('15.0.0 migration (migrate-to-inputs)', () => {
expect(updatedWorkspace.namedInputs).not.toBeDefined();
});
});

describe('15.0.0 migration (migrate-to-inputs) (v1)', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyV1Workspace();
});

it('should add project specific implicit dependencies to project namedInputs', async () => {
updateWorkspaceConfiguration(tree, {
version: 2,
implicitDependencies: {
'tools/scripts/build-app.js': ['app1', 'app2'],
},
});
addProjectConfiguration(tree, 'app1', {
root: 'app1',
});
addProjectConfiguration(tree, 'app2', {
root: 'app2',
});
addProjectConfiguration(tree, 'lib1', {
root: 'lib1',
});

await migrateToInputs(tree);

const updated = readWorkspaceConfiguration(tree);
expect(updated.implicitDependencies).toBeUndefined();
expect(updated.namedInputs.projectSpecificFiles).toEqual([]);
expect(updated.namedInputs.default).toContain('projectSpecificFiles');

const app1 = readProjectConfiguration(tree, 'app1');
expect(app1.namedInputs.projectSpecificFiles).toContain(
'{workspaceRoot}/tools/scripts/build-app.js'
);
const app2 = readProjectConfiguration(tree, 'app2');
expect(app2.namedInputs.projectSpecificFiles).toContain(
'{workspaceRoot}/tools/scripts/build-app.js'
);

const lib = readProjectConfiguration(tree, 'lib1');
expect(lib.namedInputs).toBeUndefined();
});
});
30 changes: 16 additions & 14 deletions packages/nx/src/migrations/update-15-0-0/migrate-to-inputs.ts
Expand Up @@ -83,21 +83,23 @@ export default async function (tree: Tree) {
projectSpecificFileset
);

if (tree.exists(join(project.root, 'project.json'))) {
try {
updateProjectConfiguration(tree, dependent, project);
} else if (tree.exists(join(project.root, 'package.json'))) {
updateJson<PackageJson>(
tree,
join(project.root, 'package.json'),
(json) => {
json.nx ??= {};
json.nx.namedInputs ??= {};
json.nx.namedInputs.projectSpecificFiles ??=
project.namedInputs.projectSpecificFiles;

return json;
}
);
} catch {
if (tree.exists(join(project.root, 'package.json'))) {
updateJson<PackageJson>(
tree,
join(project.root, 'package.json'),
(json) => {
json.nx ??= {};
json.nx.namedInputs ??= {};
json.nx.namedInputs.projectSpecificFiles ??=
project.namedInputs.projectSpecificFiles;

return json;
}
);
}
}
}
} else {
Expand Down
50 changes: 49 additions & 1 deletion packages/nx/src/migrations/update-15-0-0/prefix-outputs.spec.ts
@@ -1,4 +1,7 @@
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
import {
createTreeWithEmptyV1Workspace,
createTreeWithEmptyWorkspace,
} from '../../generators/testing-utils/create-tree-with-empty-workspace';
import type { Tree } from '../../generators/tree';
import {
addProjectConfiguration,
Expand Down Expand Up @@ -117,3 +120,48 @@ describe('15.0.0 migration (prefix-outputs)', () => {
await prefixOutputs(tree);
});
});

describe('15.0.0 migration (prefix-outputs) (v1)', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyV1Workspace();
});

it('should prefix project outputs', async () => {
addProjectConfiguration(tree, 'proj', {
root: 'proj',
targets: {
build: {
executor: 'nx:run-commands',
outputs: [
'dist',
'dist/{projectRoot}',
'dist/{projectRoot}/**/*.js',
'proj/coverage',
'./test-results',
'{projectRoot}/build',
'{options.outputDirectory}',
],
options: {},
},
},
});

await prefixOutputs(tree);

const updated = readProjectConfiguration(tree, 'proj');

expect(updated.targets.build.outputs).toEqual([
'{workspaceRoot}/dist',
'{workspaceRoot}/dist/{projectRoot}',
'{workspaceRoot}/dist/{projectRoot}/**/*.js',
'{projectRoot}/coverage',
'{projectRoot}/test-results',
'{projectRoot}/build',
'{options.outputDirectory}',
]);

expect(() => validateOutputs(updated.targets.build.outputs)).not.toThrow();
});
});
24 changes: 13 additions & 11 deletions packages/nx/src/migrations/update-15-0-0/prefix-outputs.ts
Expand Up @@ -39,19 +39,21 @@ export default async function (tree: Tree) {
target.outputs = transformLegacyOutputs(project.root, e);
}
}
if (tree.exists(join(project.root, 'project.json'))) {
try {
updateProjectConfiguration(tree, projectName, project);
} else if (tree.exists(join(project.root, 'package.json'))) {
updateJson<PackageJson>(
tree,
join(project.root, 'package.json'),
(json) => {
json.nx ??= {};
json.nx.targets ??= project.targets;
} catch {
if (tree.exists(join(project.root, 'package.json'))) {
updateJson<PackageJson>(
tree,
join(project.root, 'package.json'),
(json) => {
json.nx ??= {};
json.nx.targets ??= project.targets;

return json;
}
);
return json;
}
);
}
}
}

Expand Down

1 comment on commit bae31b2

@vercel
Copy link

@vercel vercel bot commented on bae31b2 Nov 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app

Please sign in to comment.