Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix migrations for projects with package.json configuration #12977

Merged
merged 1 commit into from Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -7,6 +7,7 @@ import {
updateWorkspaceConfiguration,
WorkspaceConfiguration,
} from '../../generators/utils/project-configuration';
import { readJson, writeJson } from '../../generators/utils/json';
import migrateToInputs from './migrate-to-inputs';

describe('15.0.0 migration (migrate-to-inputs)', () => {
Expand Down Expand Up @@ -211,6 +212,45 @@ describe('15.0.0 migration (migrate-to-inputs)', () => {
expect(lib.namedInputs).toBeUndefined();
});

it('should add project specific implicit dependencies to projects with package.json', async () => {
updateWorkspaceConfiguration(tree, {
version: 2,
implicitDependencies: {
'tools/scripts/build-app.js': ['app1', 'app2'],
},
});
addProjectConfiguration(tree, 'app1', {
root: 'app1',
});
addProjectConfiguration(tree, 'app2', {
root: 'app2',
});
tree.delete('app2/project.json');
writeJson(tree, 'app2/package.json', { name: '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 = readJson(tree, 'app2/package.json');
expect(app2.nx.namedInputs.projectSpecificFiles).toContain(
'{workspaceRoot}/tools/scripts/build-app.js'
);

const lib = readProjectConfiguration(tree, 'lib1');
expect(lib.namedInputs).toBeUndefined();
});

it('should do nothing if there are no implicitDependencies', async () => {
const workspace: WorkspaceConfiguration = {
version: 2,
Expand Down
21 changes: 20 additions & 1 deletion packages/nx/src/migrations/update-15-0-0/migrate-to-inputs.ts
Expand Up @@ -7,6 +7,9 @@ import {
updateWorkspaceConfiguration,
} from '../../generators/utils/project-configuration';
import { joinPathFragments } from '../../utils/path';
import { join } from 'path';
import { updateJson } from '../../generators/utils/json';
import { PackageJson } from '../../utils/package-json';

const skippedFiles = [
'package.json', // Not to be added to filesets
Expand Down Expand Up @@ -79,7 +82,23 @@ export default async function (tree: Tree) {
project.namedInputs.projectSpecificFiles = Array.from(
projectSpecificFileset
);
updateProjectConfiguration(tree, dependent, project);

if (tree.exists(join(project.root, 'project.json'))) {
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;
}
);
}
}
} else {
workspaceConfiguration.namedInputs.sharedGlobals.push(
Expand Down
27 changes: 25 additions & 2 deletions packages/nx/src/migrations/update-15-0-0/prefix-outputs.spec.ts
Expand Up @@ -6,9 +6,9 @@ import {
readWorkspaceConfiguration,
updateWorkspaceConfiguration,
} from '../../generators/utils/project-configuration';
import { writeJson } from '../../generators/utils/json';
import { readJson, writeJson } from '../../generators/utils/json';
import prefixOutputs from './prefix-outputs';
import { validateOutputs } from 'nx/src/tasks-runner/utils';
import { validateOutputs } from '../../tasks-runner/utils';

describe('15.0.0 migration (prefix-outputs)', () => {
let tree: Tree;
Expand Down Expand Up @@ -82,6 +82,29 @@ describe('15.0.0 migration (prefix-outputs)', () => {
`);
});

it('should migrate package.json projects', async () => {
writeJson(tree, 'proj/package.json', {
name: 'proj',
scripts: {
build: 'echo',
},
nx: {
targets: {
build: {
outputs: ['dist/proj'],
},
},
},
});
tree.delete('workspace.json');

await prefixOutputs(tree);

expect(readJson(tree, 'proj/package.json').nx.targets.build).toEqual({
outputs: ['dist/proj'],
});
});

it('should not error for package.json projects', async () => {
writeJson(tree, 'proj/package.json', {
name: 'proj',
Expand Down
19 changes: 17 additions & 2 deletions packages/nx/src/migrations/update-15-0-0/prefix-outputs.ts
Expand Up @@ -7,11 +7,13 @@ import {
updateWorkspaceConfiguration,
} from '../../generators/utils/project-configuration';
import { joinPathFragments } from '../../utils/path';
import { relative } from 'path';
import { join, relative } from 'path';
import {
transformLegacyOutputs,
validateOutputs,
} from 'nx/src/tasks-runner/utils';
import { updateJson } from '../../generators/utils/json';
import { PackageJson } from '../../utils/package-json';

export default async function (tree: Tree) {
// If the workspace doesn't have a nx.json, don't make any changes
Expand All @@ -37,7 +39,20 @@ export default async function (tree: Tree) {
target.outputs = transformLegacyOutputs(project.root, e);
}
}
updateProjectConfiguration(tree, projectName, project);
if (tree.exists(join(project.root, 'project.json'))) {
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;

return json;
}
);
}
}

if (workspaceConfiguration.targetDefaults) {
Expand Down