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 27, 2022
1 parent 8c38b86 commit 0f2f1db
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 21 deletions.
3 changes: 0 additions & 3 deletions e2e/angular-core/src/ng-add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {

// check project configuration
const projectConfig = readJson(`apps/${project}/project.json`);
expect(projectConfig.root).toEqual(`apps/${project}`);
expect(projectConfig.sourceRoot).toEqual(`apps/${project}/src`);
expect(projectConfig.targets.build).toEqual({
executor: '@angular-devkit/build-angular:browser',
Expand Down Expand Up @@ -259,7 +258,6 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {

// check e2e project config
const e2eProjectConfig = readJson(`apps/${project}-e2e/project.json`);
expect(e2eProjectConfig.root).toEqual(`apps/${project}-e2e`);
expect(e2eProjectConfig.targets.e2e).toEqual({
executor: '@angular-devkit/build-angular:protractor',
options: {
Expand Down Expand Up @@ -356,7 +354,6 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {

// check e2e project config
const e2eProjectConfig = readJson(`apps/${project}-e2e/project.json`);
expect(e2eProjectConfig.root).toEqual(`apps/${e2eProject}`);
expect(e2eProjectConfig.targets['cypress-run']).toEqual({
executor: '@nrwl/cypress:cypress',
options: {
Expand Down
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
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
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 0f2f1db

Please sign in to comment.