fix(devkit): ensure that getProjects works properly without a nx.json (#12295)
This commit is contained in:
parent
14a3d6e364
commit
40c8859ff0
@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -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',
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user