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(devkit): ensure that getProjects works properly without a nx.json #12295

Merged
merged 1 commit into from Sep 28, 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
15 changes: 8 additions & 7 deletions packages/nx/src/config/workspaces.ts
Expand Up @@ -725,22 +725,23 @@ function buildProjectConfigurationFromPackageJson(
): ProjectConfiguration & { name: string } {
const directory = dirname(path).split('\\').join('/');
let name = packageJson.name ?? toProjectName(directory, nxJson);
if (nxJson.npmScope) {
if (nxJson?.npmScope) {
const npmPrefix = `@${nxJson.npmScope}/`;
if (name.startsWith(npmPrefix)) {
name = name.replace(npmPrefix, '');
}
}
const projectType =
nxJson?.workspaceLayout?.appsDir != nxJson?.workspaceLayout?.libsDir &&
nxJson?.workspaceLayout?.appsDir &&
directory.startsWith(nxJson.workspaceLayout.appsDir)
? 'application'
: 'library';
return {
root: directory,
sourceRoot: directory,
name,
projectType:
nxJson.workspaceLayout?.appsDir != nxJson.workspaceLayout?.libsDir &&
nxJson.workspaceLayout?.appsDir &&
directory.startsWith(nxJson.workspaceLayout.appsDir)
? 'application'
: 'library',
projectType,
};
}

Expand Down
76 changes: 76 additions & 0 deletions packages/nx/src/generators/utils/project-configuration.spec.ts
Expand Up @@ -2,6 +2,7 @@ import Ajv from 'ajv';
import { Tree } from '../tree';
import { ProjectConfiguration } from '../../config/workspace-json-project-json';

import { createTree } from '../testing-utils/create-tree';
import {
createTreeWithEmptyWorkspace,
createTreeWithEmptyV1Workspace,
Expand Down Expand Up @@ -432,6 +433,23 @@ describe('project configuration', () => {
});
});

describe('getProjects', () => {
it('should get a map of projects', () => {
addProjectConfiguration(tree, 'proj', {
root: 'proj',
});

const projects = getProjects(tree);

expect(projects.size).toEqual(1);
expect(projects.get('proj')).toEqual({
$schema: '../node_modules/nx/schemas/project-schema.json',
name: 'proj',
root: 'proj',
});
});
});

describe('without nx.json', () => {
beforeEach(() => tree.delete('nx.json'));

Expand Down Expand Up @@ -568,6 +586,64 @@ describe('project configuration', () => {
)
).toBeFalsy();
});

describe('getProjects', () => {
it('should get a map of projects', () => {
addProjectConfiguration(tree, 'proj', {
root: 'proj',
});

const projects = getProjects(tree);

expect(projects.size).toEqual(1);
expect(projects.get('proj')).toEqual({
$schema: '../node_modules/nx/schemas/project-schema.json',
name: 'proj',
root: 'proj',
});
});
});
});
});

describe('for npm workspaces', () => {
beforeEach(() => {
tree = createTree();
});

describe('readProjectConfiguration', () => {
it('should read project configuration from package.json files', () => {
writeJson(tree, 'proj/package.json', {
name: 'proj',
});

const proj = readProjectConfiguration(tree, 'proj');

expect(proj).toEqual({
root: 'proj',
sourceRoot: 'proj',
projectType: 'library',
});
});
});

describe('getProjects', () => {
beforeEach(() => {
writeJson(tree, 'proj/package.json', {
name: 'proj',
});
});

it('should get a map of projects', () => {
const projects = getProjects(tree);

expect(projects.size).toEqual(1);
expect(projects.get('proj')).toEqual({
root: 'proj',
sourceRoot: 'proj',
projectType: 'library',
});
});
});
});
});