Skip to content

Commit

Permalink
Revert "fix(core): automatically add root to the project.json projects (
Browse files Browse the repository at this point in the history
#9977)"

This reverts commit e6f7186.
  • Loading branch information
FrozenPandaz committed Apr 29, 2022
1 parent 662b52e commit 6f9a4d2
Show file tree
Hide file tree
Showing 41 changed files with 176 additions and 339 deletions.
3 changes: 3 additions & 0 deletions e2e/angular-core/src/ng-add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ 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 @@ -258,6 +259,7 @@ 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 @@ -354,6 +356,7 @@ 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 @@ -277,7 +277,7 @@ describe('Nx Plugin', () => {
checkFilesExist(`libs/subdir/${plugin}/package.json`);
const pluginProject = readProjectConfig(`subdir-${plugin}`);
const pluginE2EProject = readProjectConfig(`subdir-${plugin}-e2e`);
expect(pluginProject.targets).toBeDefined();
expect(pluginProject.root).toBe(`libs/subdir/${plugin}`);
expect(pluginE2EProject).toBeTruthy();
}, 90000);
});
Expand Down
3 changes: 3 additions & 0 deletions e2e/workspace-core/src/aux-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ 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 @@ -444,6 +445,7 @@ 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 @@ -576,6 +578,7 @@ 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,9 +46,6 @@ 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: 0 additions & 3 deletions packages/angular/src/generators/library/lib/update-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@ 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
8 changes: 4 additions & 4 deletions packages/devkit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @category Tree
*/
export type { Tree, FileChange } from 'nx/src/generators/tree';
export type { Tree, FileChange } from 'nx/src/config/tree';

/**
* @category Workspace
Expand Down Expand Up @@ -111,7 +111,7 @@ export { generateFiles } from './src/generators/generate-files';
/**
* @category Generators
*/
export type { WorkspaceConfiguration } from 'nx/src/generators/utils/project-configuration';
export type { WorkspaceConfiguration } from './src/generators/project-configuration';

/**
* @category Generators
Expand All @@ -125,7 +125,7 @@ export {
updateWorkspaceConfiguration,
getProjects,
isStandaloneProject,
} from 'nx/src/generators/utils/project-configuration';
} from './src/generators/project-configuration';

/**
* @category Generators
Expand Down Expand Up @@ -183,7 +183,7 @@ export { ProjectGraphBuilder } from 'nx/src/project-graph/project-graph-builder'
/**
* @category Utils
*/
export { readJson, writeJson, updateJson } from 'nx/src/generators/utils/json';
export { readJson, writeJson, updateJson } from './src/utils/json';

/**
* @category Utils
Expand Down
10 changes: 5 additions & 5 deletions packages/devkit/src/generators/format-files.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Tree } from 'nx/src/generators/tree';
import type { Tree } from 'nx/src/config/tree';
import * as path from 'path';
import type * as Prettier from 'prettier';
import { readJson, updateJson, writeJson } from 'nx/src/generators/utils/json';
import { getWorkspacePath } from '../utils/get-workspace-layout';
import { readJson, updateJson, writeJson } from '../utils/json';
import { sortObjectByKeys } from 'nx/src/utils/object-sort';
import {
getWorkspacePath,
readWorkspaceConfiguration,
updateWorkspaceConfiguration,
WorkspaceConfiguration,
} from 'nx/src/generators/utils/project-configuration';
import { sortObjectByKeys } from 'nx/src/utils/object-sort';
} from './project-configuration';

/**
* Formats all the created or updated files using Prettier
Expand Down
4 changes: 2 additions & 2 deletions packages/devkit/src/generators/generate-files.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Tree } from 'nx/src/generators/tree';
import { createTree } from 'nx/src/generators/testing-utils/create-tree';
import type { Tree } from 'nx/src/config/tree';
import { createTree } from '../tests/create-tree';
import { generateFiles } from './generate-files';
import { join } from 'path';
import * as FileType from 'file-type';
Expand Down
2 changes: 1 addition & 1 deletion packages/devkit/src/generators/generate-files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync, readdirSync, statSync } from 'fs';
import * as path from 'path';
import type { Tree } from 'nx/src/generators/tree';
import type { Tree } from 'nx/src/config/tree';
import { logger } from 'nx/src/utils/logger';

const binaryExts = new Set([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Tree } from '../tree';
import { ProjectConfiguration } from '../../config/workspace-json-project-json';
import { Tree } from 'nx/src/config/tree';
import { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json';

import { createTreeWithEmptyWorkspace } from '../testing-utils/create-tree-with-empty-workspace';
import { createTreeWithEmptyWorkspace } from '../tests/create-tree-with-empty-workspace';
import { readJson, updateJson } from '../utils/json';
import {
addProjectConfiguration,
getProjects,
getWorkspacePath,
readProjectConfiguration,
readWorkspaceConfiguration,
removeProjectConfiguration,
updateProjectConfiguration,
updateWorkspaceConfiguration,
WorkspaceConfiguration,
} from './project-configuration';
import { getWorkspacePath } from '../utils/get-workspace-layout';

type ProjectConfigurationV1 = Pick<
ProjectConfiguration,
Expand Down Expand Up @@ -301,7 +301,6 @@ describe('project configuration', () => {
addProjectConfiguration(tree, 'test', baseTestProjectConfigV2, true);
const expectedProjectConfig = {
...baseTestProjectConfigV2,
root: undefined,
targets: { build: { executor: '' } },
};
updateProjectConfiguration(tree, 'test', expectedProjectConfig);
Expand Down Expand Up @@ -472,7 +471,6 @@ describe('project configuration', () => {
addProjectConfiguration(tree, 'test', baseTestProjectConfigV2, true);
const expectedProjectConfig = {
...baseTestProjectConfigV2,
root: undefined,
targets: { build: { executor: '' } },
};
updateProjectConfiguration(tree, 'test', expectedProjectConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ import {
globForProjectFiles,
reformattedWorkspaceJsonOrNull,
toNewFormat,
} from '../../config/workspaces';
} from 'nx/src/config/workspaces';
import { basename, dirname, relative } from 'path';

import { readJson, updateJson, writeJson } from './json';

import type { Tree } from '../tree';
import type { NxJsonConfiguration } from '../../config/nx-json';
import { joinPathFragments } from '../../utils/path';
import {
getWorkspaceLayout,
getWorkspacePath,
} from '../utils/get-workspace-layout';
import { readJson, updateJson, writeJson } from '../utils/json';

import type { Tree } from 'nx/src/config/tree';
import type { NxJsonConfiguration } from 'nx/src/config/nx-json';
import { joinPathFragments } from 'nx/src/utils/path';
import {
ProjectConfiguration,
RawWorkspaceJsonConfiguration,
WorkspaceJsonConfiguration,
} from '../../config/workspace-json-project-json';
} from 'nx/src/config/workspace-json-project-json';

export type WorkspaceConfiguration = Omit<
WorkspaceJsonConfiguration,
Expand All @@ -41,7 +45,8 @@ export function addProjectConfiguration(
projectConfiguration: ProjectConfiguration,
standalone?: boolean
): void {
standalone = standalone ?? shouldDefaultToUsingStandaloneConfigs(tree);
const workspaceLayout = getWorkspaceLayout(tree);
standalone = standalone ?? workspaceLayout.standaloneAsDefault;
setProjectConfiguration(
tree,
projectName,
Expand Down Expand Up @@ -353,7 +358,7 @@ function addProjectToWorkspaceJson(
workspaceJson.projects[projectName] = project.root;
}
// update the project.json file
writeJson(tree, configFile, { ...project, root: undefined });
writeJson(tree, configFile, project);
}
} else if (mode === 'delete') {
delete workspaceJson.projects[projectName];
Expand Down Expand Up @@ -394,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] = {
root: config,
...readJson<ProjectConfiguration>(tree, configFileLocation),
};
workspaceJson.projects[project] = readJson<ProjectConfiguration>(
tree,
configFileLocation
);
}
});
return workspaceJson as WorkspaceJsonConfiguration;
Expand Down Expand Up @@ -545,33 +550,3 @@ function validateProjectConfigurationOperationsWithoutWorkspaceJson(
);
}
}

export function shouldDefaultToUsingStandaloneConfigs(tree: Tree): boolean {
const workspacePath = getWorkspacePath(tree);
const rawWorkspace =
workspacePath && tree.exists(workspacePath)
? readJson<RawWorkspaceJsonConfiguration>(tree, workspacePath)
: null;
return !rawWorkspace
? true // if workspace.json doesn't exist, all projects **must** be standalone
: Object.values(rawWorkspace.projects).reduce(
// default for second, third... projects should be based on all projects being defined as a path
// for configuration read from ng schematics, this is determined by configFilePath's presence
(allStandalone, next) =>
allStandalone &&
(typeof next === 'string' || 'configFilePath' in next),

// default for first project should be true if using Nx Schema
rawWorkspace.version > 1
);
}

export function getWorkspacePath(
tree: Tree
): '/angular.json' | '/workspace.json' | null {
const possibleFiles: ('/angular.json' | '/workspace.json')[] = [
'/angular.json',
'/workspace.json',
];
return possibleFiles.filter((path) => tree.exists(path))[0];
}
2 changes: 1 addition & 1 deletion packages/devkit/src/generators/to-js.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Tree } from 'nx/src/generators/tree';
import type { Tree } from 'nx/src/config/tree';

/**
* Rename and transpile any new typescript files created to javascript files
Expand Down
4 changes: 2 additions & 2 deletions packages/devkit/src/generators/update-ts-configs-to-js.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Tree } from 'nx/src/generators/tree';
import { updateJson } from 'nx/src/generators/utils/json';
import type { Tree } from 'nx/src/config/tree';
import { updateJson } from '../utils/json';

export function updateTsConfigsToJs(
tree: Tree,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createTree } from 'nx/src/generators/testing-utils/create-tree';
import type { Tree } from 'nx/src/generators/tree';
import { createTree } from '../tests/create-tree';
import type { Tree } from 'nx/src/config/tree';
import { visitNotIgnoredFiles } from './visit-not-ignored-files';

describe('visitNotIgnoredFiles', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/devkit/src/generators/visit-not-ignored-files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Tree } from 'nx/src/generators/tree';
import type { Tree } from 'nx/src/config/tree';
import ignore, { Ignore } from 'ignore';
import { join, relative, sep } from 'path';

Expand Down
2 changes: 1 addition & 1 deletion packages/devkit/src/tasks/install-packages-task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Tree } from 'nx/src/generators/tree';
import type { Tree } from 'nx/src/config/tree';
import { execSync } from 'child_process';
import { join } from 'path';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FsTree } from 'nx/src/generators/tree';
import type { Tree } from 'nx/src/generators/tree';
import { FsTree } from 'nx/src/config/tree';
import type { Tree } from 'nx/src/config/tree';

/**
* Creates a host for testing.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FsTree } from 'nx/src/generators/tree';
import type { Tree } from 'nx/src/generators/tree';
import { FsTree } from 'nx/src/config/tree';
import type { Tree } from 'nx/src/config/tree';

/**
* Creates a host for testing.
Expand Down
41 changes: 33 additions & 8 deletions packages/devkit/src/utils/get-workspace-layout.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {
getWorkspacePath,
readNxJson,
shouldDefaultToUsingStandaloneConfigs,
} from 'nx/src/generators/utils/project-configuration';
import type { Tree } from 'nx/src/generators/tree';
import { RawWorkspaceJsonConfiguration } from 'nx/src/config/workspace-json-project-json';

export { getWorkspacePath } from 'nx/src/generators/utils/project-configuration';
import { readNxJson } from '../generators/project-configuration';
import { readJson } from './json';

import type { Tree } from 'nx/src/config/tree';

/**
* Returns workspace defaults. It includes defaults folders for apps and libs,
Expand All @@ -25,10 +23,37 @@ export function getWorkspaceLayout(tree: Tree): {
npmScope: string;
} {
const nxJson = readNxJson(tree);
const workspacePath = getWorkspacePath(tree);
const rawWorkspace =
workspacePath && tree.exists(workspacePath)
? readJson<RawWorkspaceJsonConfiguration>(tree, workspacePath)
: null;

return {
appsDir: nxJson?.workspaceLayout?.appsDir ?? 'apps',
libsDir: nxJson?.workspaceLayout?.libsDir ?? 'libs',
npmScope: nxJson?.npmScope ?? '',
standaloneAsDefault: shouldDefaultToUsingStandaloneConfigs(tree),
standaloneAsDefault: !rawWorkspace
? true // if workspace.json doesn't exist, all projects **must** be standalone
: Object.values(rawWorkspace.projects).reduce(
// default for second, third... projects should be based on all projects being defined as a path
// for configuration read from ng schematics, this is determined by configFilePath's presence
(allStandalone, next) =>
allStandalone &&
(typeof next === 'string' || 'configFilePath' in next),

// default for first project should be true if using Nx Schema
rawWorkspace.version > 1
),
};
}

export function getWorkspacePath(
tree: Tree
): '/angular.json' | '/workspace.json' | null {
const possibleFiles: ('/angular.json' | '/workspace.json')[] = [
'/angular.json',
'/workspace.json',
];
return possibleFiles.filter((path) => tree.exists(path))[0];
}

0 comments on commit 6f9a4d2

Please sign in to comment.