Skip to content

Commit

Permalink
fix(core): fix reading project config when nx.json doesn't have proje…
Browse files Browse the repository at this point in the history
…cts (nrwl#6802)
  • Loading branch information
FrozenPandaz authored and ManojBahuguna committed Sep 16, 2021
1 parent 9297fdf commit 3442a99
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
58 changes: 57 additions & 1 deletion packages/devkit/src/generators/project-configuration.spec.ts
Expand Up @@ -2,7 +2,7 @@ import { Tree } from '@nrwl/tao/src/shared/tree';
import { ProjectConfiguration } from '@nrwl/tao/src/shared/workspace';

import { createTreeWithEmptyWorkspace } from '../tests/create-tree-with-empty-workspace';
import { readJson } from '../utils/json';
import { readJson, updateJson } from '../utils/json';
import {
addProjectConfiguration,
getProjects,
Expand All @@ -13,6 +13,7 @@ import {
updateWorkspaceConfiguration,
WorkspaceConfiguration,
} from './project-configuration';
import { getWorkspacePath } from '../utils/get-workspace-layout';

const baseTestProjectConfig: ProjectConfiguration = {
root: 'libs/test',
Expand All @@ -27,6 +28,61 @@ describe('project configuration', () => {
tree = createTreeWithEmptyWorkspace();
});

describe('readProjectConfiguration', () => {
it('should get info from workspace.json', () => {
updateJson(tree, getWorkspacePath(tree), (json) => {
json.projects['proj1'] = {
root: 'proj1',
};
return json;
});

const config = readProjectConfiguration(tree, 'proj1');
expect(config).toEqual({
root: 'proj1',
});
});

it('should get info from nx.json', () => {
updateJson(tree, getWorkspacePath(tree), (json) => {
json.projects['proj1'] = {
root: 'proj1',
};
return json;
});
updateJson(tree, 'nx.json', (json) => {
json.projects['proj1'] = {
tags: ['tag1'],
};
return json;
});

const config = readProjectConfiguration(tree, 'proj1');
expect(config).toEqual({
root: 'proj1',
tags: ['tag1'],
});
});

it('should should not fail if projects is not defined in nx.json', () => {
updateJson(tree, getWorkspacePath(tree), (json) => {
json.projects['proj1'] = {
root: 'proj1',
};
return json;
});
updateJson(tree, 'nx.json', (json) => {
delete json.projects;
return json;
});

const config = readProjectConfiguration(tree, 'proj1');
expect(config).toEqual({
root: 'proj1',
});
});
});

describe('addProjectConfiguration', () => {
it('should create project.json file when adding a project if standalone is true', () => {
addProjectConfiguration(tree, 'test', baseTestProjectConfig, true);
Expand Down
2 changes: 1 addition & 1 deletion packages/devkit/src/generators/project-configuration.ts
Expand Up @@ -288,7 +288,7 @@ function readWorkspaceSection(
}

function readNxJsonSection(nxJson: NxJsonConfiguration, projectName: string) {
return nxJson.projects[projectName];
return nxJson.projects?.[projectName];
}

function setProjectConfiguration(
Expand Down

0 comments on commit 3442a99

Please sign in to comment.