Skip to content

Commit

Permalink
fix(core): automatically add root to the project.json projects
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Apr 26, 2022
1 parent 8c38b86 commit 7bc5a2c
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 19 deletions.
2 changes: 1 addition & 1 deletion e2e/nx-plugin/src/nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe('Nx Plugin', () => {
checkFilesExist(`libs/subdir/${plugin}/package.json`);
const pluginProject = readProjectConfig(`subdir-${plugin}`);
const pluginE2EProject = readProjectConfig(`subdir-${plugin}-e2e`);
expect(pluginProject.root).toBe(`libs/subdir/${plugin}`);
expect(pluginProject.targets).toBeDefined();
expect(pluginE2EProject).toBeTruthy();
}, 90000);
});
Expand Down
3 changes: 0 additions & 3 deletions e2e/workspace-core/src/aux-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ describe('move project', () => {
expect(workspaceJson.projects[`${lib1}-data-access`]).toBeUndefined();
const project = readProjectConfig(newName);
expect(project).toBeTruthy();
expect(project.root).toBe(newPath);
expect(project.sourceRoot).toBe(`${newPath}/src`);
expect(project.targets.lint.options.lintFilePatterns).toEqual([
`libs/shared/${lib1}/data-access/**/*.ts`,
Expand Down Expand Up @@ -445,7 +444,6 @@ describe('move project', () => {
expect(workspaceJson.projects[`${lib1}-data-access`]).toBeUndefined();
const project = readProjectConfig(newName);
expect(project).toBeTruthy();
expect(project.root).toBe(newPath);
expect(project.sourceRoot).toBe(`${newPath}/src`);
expect(project.tags).toEqual([]);
const lib3Config = readProjectConfig(lib3);
Expand Down Expand Up @@ -578,7 +576,6 @@ describe('move project', () => {
expect(workspaceJson.projects[`${lib1}-data-access`]).toBeUndefined();
const project = readProjectConfig(newName);
expect(project).toBeTruthy();
expect(project.root).toBe(newPath);
expect(project.sourceRoot).toBe(`${newPath}/src`);
expect(project.targets.lint.options.lintFilePatterns).toEqual([
`packages/shared/${lib1}/data-access/**/*.ts`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ function updateAppAndE2EProjectConfigurations(
options.ngCliSchematicAppRoot,
options.appProjectRoot
);
// project already has the right root, but the above function, makes it incorrect.
// This corrects it.
project.root = options.appProjectRoot;
}

delete project.targets.test;
Expand Down
3 changes: 3 additions & 0 deletions packages/angular/src/generators/library/lib/update-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ function fixProjectWorkspaceConfig(host: Tree, options: NormalizedSchema) {
options.ngCliSchematicLibRoot,
options.projectRoot
);
// project already has the right root, but the above function, makes it incorrect.
// This corrects it.
project.root = options.projectRoot;
}

if (!options.publishable && !options.buildable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,6 @@ Object {

exports[`workspace move to nx layout should update project configuration 1`] = `
Object {
"root": "apps/myApp",
"sourceRoot": "apps/myApp/src",
"targets": Object {
"build": Object {
Expand Down
2 changes: 2 additions & 0 deletions packages/devkit/src/generators/project-configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ describe('project configuration', () => {
addProjectConfiguration(tree, 'test', baseTestProjectConfigV2, true);
const expectedProjectConfig = {
...baseTestProjectConfigV2,
root: undefined,
targets: { build: { executor: '' } },
};
updateProjectConfiguration(tree, 'test', expectedProjectConfig);
Expand Down Expand Up @@ -471,6 +472,7 @@ describe('project configuration', () => {
addProjectConfiguration(tree, 'test', baseTestProjectConfigV2, true);
const expectedProjectConfig = {
...baseTestProjectConfigV2,
root: undefined,
targets: { build: { executor: '' } },
};
updateProjectConfiguration(tree, 'test', expectedProjectConfig);
Expand Down
10 changes: 5 additions & 5 deletions packages/devkit/src/generators/project-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function addProjectToWorkspaceJson(
workspaceJson.projects[projectName] = project.root;
}
// update the project.json file
writeJson(tree, configFile, project);
writeJson(tree, configFile, { ...project, root: undefined });
}
} else if (mode === 'delete') {
delete workspaceJson.projects[projectName];
Expand Down Expand Up @@ -399,10 +399,10 @@ function inlineProjectConfigurationsWithTree(
Object.entries(workspaceJson.projects || {}).forEach(([project, config]) => {
if (typeof config === 'string') {
const configFileLocation = joinPathFragments(config, 'project.json');
workspaceJson.projects[project] = readJson<ProjectConfiguration>(
tree,
configFileLocation
);
workspaceJson.projects[project] = {
root: config,
...readJson<ProjectConfiguration>(tree, configFileLocation),
};
}
});
return workspaceJson as WorkspaceJsonConfiguration;
Expand Down
9 changes: 5 additions & 4 deletions packages/devkit/src/utils/invoke-nx-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { FileChange, Tree, TreeWriteOptions } from 'nx/src/config/tree';
import { toNewFormat, toOldFormatOrNull } from 'nx/src/config/workspaces';
import { Generator, GeneratorCallback } from 'nx/src/config/misc-interfaces';
import { parseJson, serializeJson } from 'nx/src/utils/json';
import { join, relative } from 'path';
import { join, relative, dirname } from 'path';

class RunCallbackTask {
constructor(private callback: GeneratorCallback) {}
Expand Down Expand Up @@ -192,9 +192,10 @@ class DevkitTreeFromAngularDevkitTree implements Tree {
const w = parseJson(content.toString());
for (const [project, configuration] of Object.entries(w.projects)) {
if (typeof configuration === 'string') {
w.projects[project] = parseJson(
this.tree.read(`${configuration}/project.json`)
);
w.projects[project] = {
root: configuration,
...parseJson(this.tree.read(`${configuration}/project.json`)),
};
w.projects[project].configFilePath = `${configuration}/project.json`;
}
}
Expand Down
9 changes: 8 additions & 1 deletion packages/nx/migrations.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"migrations": {}
"migrations": {
"remove-root": {
"cli": "nx",
"version": "14.1.0-beta.0",
"description": "Removes unnecessary root property from project.json files",
"factory": "./src/migrations/update-14-0-4/"
}
}
}
10 changes: 7 additions & 3 deletions packages/nx/src/adapter/ngcli-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ export class NxScopedHost extends virtualFs.ScopedHost<any> {
// project was read from a project.json file
const configPath = projectConfig.configFilePath;
const fileConfigObject = { ...projectConfig };
delete fileConfigObject.root; // remove the root before writing
delete fileConfigObject.configFilePath; // remove the configFilePath before writing
const projectJsonWrite = super.write(
configPath,
Expand Down Expand Up @@ -539,6 +540,7 @@ export class NxScopedHost extends virtualFs.ScopedHost<any> {
map((x) => ({
project,
projectConfig: {
root: dirname(configFilePath),
...parseJson(Buffer.from(x).toString()),
configFilePath,
},
Expand Down Expand Up @@ -600,9 +602,10 @@ export class NxScopeHostUsedForWrappedSchematics extends NxScopedHost {
const nxJsonInTree = nxJsonChange
? parseJson(nxJsonChange.content.toString())
: parseJson(this.host.read('nx.json').toString());
const readJsonWithHost = (file) =>
parseJson(this.host.read(file).toString());

const readJsonWithHost = (file) => ({
root: dirname(file),
...parseJson(this.host.read(file).toString()),
});
const staticProjects = buildWorkspaceConfigurationFromGlobs(
nxJsonInTree,
globForProjectFiles(this.host.root).filter(
Expand Down Expand Up @@ -1214,6 +1217,7 @@ function saveWorkspaceConfigurationInWrappedSchematic(
) {
const path = config.configFilePath || join(config.root, 'project.json');
workspace.projects[project] = normalize(dirname(path));
delete config.root; // remove the root before writing
delete config.configFilePath;
host.write(path, serializeJson(config));
}
Expand Down
8 changes: 7 additions & 1 deletion packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,10 @@ function inlineProjectConfigurations(w: any, root: string = workspaceRoot) {
if (typeof config === 'string') {
const configFilePath = path.join(root, config, 'project.json');
const fileConfig = readJsonFile(configFilePath);
w.projects[project] = fileConfig;
w.projects[project] = {
root: config,
...fileConfig,
};
}
}
);
Expand Down Expand Up @@ -637,6 +640,9 @@ export function buildWorkspaceConfigurationFromGlobs(
// directory as a package.json should overwrite the inferred package.json
// project configuration.
const configuration = readJson(file);

configuration.root = directory;

let name = configuration.name;
if (!configuration.name) {
name = toProjectName(file, nxJson);
Expand Down
59 changes: 59 additions & 0 deletions packages/nx/src/migrations/update-14-1-0/remove-root.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
addProjectConfiguration,
readJson,
Tree,
updateJson,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import removeRoot from './remove-root';

describe('remove-root', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace(2);
});

describe('project.json projects', () => {
it('should have their root removed', async () => {
addProjectConfiguration(
tree,
'proj1',
{
root: 'proj1',
targets: {},
},
true
);

updateJson(tree, './proj1/project.json', (json) => ({
...json,
root: 'proj1',
}));

await removeRoot(tree);

expect(readJson(tree, './proj1/project.json').root).toBeUndefined();
});
});

describe('workspace.json projects', () => {
it('should not have their root removed', async () => {
addProjectConfiguration(
tree,
'proj1',
{
root: 'proj1',
targets: {},
},
false
);

await removeRoot(tree);

expect(
readJson(tree, './workspace.json').projects.proj1.root
).toBeDefined();
});
});
});
14 changes: 14 additions & 0 deletions packages/nx/src/migrations/update-14-1-0/remove-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
formatFiles,
getProjects,
Tree,
updateProjectConfiguration,
} from '@nrwl/devkit';

export default async function (tree: Tree) {
for (const [projName, projConfig] of getProjects(tree)) {
updateProjectConfiguration(tree, projName, projConfig);
}

await formatFiles(tree);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('convert-to-nx-project', () => {
getProjectConfigurationPath(config)
);

delete config.root;
expect(config).toEqual(newConfigFile);
});

Expand Down Expand Up @@ -112,6 +113,7 @@ describe('convert-to-nx-project', () => {
tree,
getProjectConfigurationPath(config)
);
delete config.root;
expect(config).toEqual(newConfigFile);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ To upgrade change the version number at the top of ${getWorkspacePath(
continue;
}

delete configuration.root;

writeJson(host, configPath, configuration);

updateJson(host, getWorkspacePath(host), (value) => {
Expand Down

0 comments on commit 7bc5a2c

Please sign in to comment.