diff --git a/packages/nx/src/config/workspaces.ts b/packages/nx/src/config/workspaces.ts index 8e6fe241ebdb3..8305fb38aa9c8 100644 --- a/packages/nx/src/config/workspaces.ts +++ b/packages/nx/src/config/workspaces.ts @@ -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, }; } diff --git a/packages/nx/src/generators/utils/project-configuration.spec.ts b/packages/nx/src/generators/utils/project-configuration.spec.ts index cdf8e7b78b131..b48a73bb3858b 100644 --- a/packages/nx/src/generators/utils/project-configuration.spec.ts +++ b/packages/nx/src/generators/utils/project-configuration.spec.ts @@ -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, @@ -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')); @@ -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', + }); + }); }); }); });