fix(devkit): ensure that getProjects works properly without a nx.json (#12295)

This commit is contained in:
Jason Jean 2022-09-28 14:32:06 -07:00 committed by GitHub
parent 14a3d6e364
commit 40c8859ff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 7 deletions

View File

@ -725,22 +725,23 @@ function buildProjectConfigurationFromPackageJson(
): ProjectConfiguration & { name: string } { ): ProjectConfiguration & { name: string } {
const directory = dirname(path).split('\\').join('/'); const directory = dirname(path).split('\\').join('/');
let name = packageJson.name ?? toProjectName(directory, nxJson); let name = packageJson.name ?? toProjectName(directory, nxJson);
if (nxJson.npmScope) { if (nxJson?.npmScope) {
const npmPrefix = `@${nxJson.npmScope}/`; const npmPrefix = `@${nxJson.npmScope}/`;
if (name.startsWith(npmPrefix)) { if (name.startsWith(npmPrefix)) {
name = name.replace(npmPrefix, ''); name = name.replace(npmPrefix, '');
} }
} }
const projectType =
nxJson?.workspaceLayout?.appsDir != nxJson?.workspaceLayout?.libsDir &&
nxJson?.workspaceLayout?.appsDir &&
directory.startsWith(nxJson.workspaceLayout.appsDir)
? 'application'
: 'library';
return { return {
root: directory, root: directory,
sourceRoot: directory, sourceRoot: directory,
name, name,
projectType: projectType,
nxJson.workspaceLayout?.appsDir != nxJson.workspaceLayout?.libsDir &&
nxJson.workspaceLayout?.appsDir &&
directory.startsWith(nxJson.workspaceLayout.appsDir)
? 'application'
: 'library',
}; };
} }

View File

@ -2,6 +2,7 @@ import Ajv from 'ajv';
import { Tree } from '../tree'; import { Tree } from '../tree';
import { ProjectConfiguration } from '../../config/workspace-json-project-json'; import { ProjectConfiguration } from '../../config/workspace-json-project-json';
import { createTree } from '../testing-utils/create-tree';
import { import {
createTreeWithEmptyWorkspace, createTreeWithEmptyWorkspace,
createTreeWithEmptyV1Workspace, 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', () => { describe('without nx.json', () => {
beforeEach(() => tree.delete('nx.json')); beforeEach(() => tree.delete('nx.json'));
@ -568,6 +586,64 @@ describe('project configuration', () => {
) )
).toBeFalsy(); ).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',
});
});
}); });
}); });
}); });