Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(devkit): ensure that getProjects works properly without a nx.json (
  • Loading branch information
FrozenPandaz committed Sep 28, 2022
1 parent d070fdf commit 35082c7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
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',
});
});
});
});
});

0 comments on commit 35082c7

Please sign in to comment.