Skip to content

Commit

Permalink
fix(angular): handle not provided path when generating a component wi…
Browse files Browse the repository at this point in the history
…thout the project option
  • Loading branch information
leosvelperez committed Dec 16, 2022
1 parent 1247e04 commit 54c5e28
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
Expand Up @@ -42,6 +42,20 @@ export class ExampleComponent {
"
`;

exports[`component Generator --path should infer project from path and generate component correctly 1`] = `
"import { Component } from '@angular/core';
@Component({
selector: 'example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent {
}
"
`;

exports[`component Generator secondary entry points should create the component correctly and export it in the entry point 1`] = `
"import { Component } from '@angular/core';
Expand Down
Expand Up @@ -2,7 +2,6 @@ import type { Tree } from '@nrwl/devkit';
import {
createProjectGraphAsync,
joinPathFragments,
readCachedProjectGraph,
readProjectConfiguration,
readWorkspaceConfiguration,
} from '@nrwl/devkit';
Expand All @@ -13,6 +12,10 @@ import {
} from 'nx/src/project-graph/utils/find-project-for-path';

async function findProjectFromOptions(options: Schema) {
if (options.path === null || options.path === undefined) {
return null;
}

const projectGraph = await createProjectGraphAsync();
const projectRootMappings = createProjectRootMappings(projectGraph.nodes);
return findProjectForPath(options.path, projectRootMappings);
Expand All @@ -26,6 +29,13 @@ export async function normalizeOptions(
options.project ??
(await findProjectFromOptions(options)) ??
readWorkspaceConfiguration(tree).defaultProject;

if (!project) {
throw new Error(
'No "project" or "path" was specified and "defaultProject" is not set in the workspace configuration. Please provide the "project" option and try again.'
);
}

const { projectType, root, sourceRoot } = readProjectConfiguration(
tree,
project
Expand Down
79 changes: 79 additions & 0 deletions packages/angular/src/generators/component/component.spec.ts
@@ -1,7 +1,16 @@
import type { ProjectGraph } from '@nrwl/devkit';
import { addProjectConfiguration, writeJson } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import componentGenerator from './component';

let projectGraph: ProjectGraph;
jest.mock('@nrwl/devkit', () => {
return {
...jest.requireActual('@nrwl/devkit'),
createProjectGraphAsync: jest.fn().mockImplementation(() => projectGraph),
};
});

describe('component Generator', () => {
it('should create the component correctly and export it in the entry point when "export=true"', async () => {
// ARRANGE
Expand Down Expand Up @@ -406,6 +415,46 @@ describe('component Generator', () => {
);
});

it('should infer project from path and generate component correctly', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
addProjectConfiguration(tree, 'lib1', {
projectType: 'library',
sourceRoot: 'libs/lib1/src',
root: 'libs/lib1',
});
tree.write(
'libs/lib1/src/lib/lib.module.ts',
`
import { NgModule } from '@angular/core';
@NgModule({
declarations: [],
exports: []
})
export class LibModule {}`
);
projectGraph = {
nodes: {
lib1: { name: 'lib1', type: 'lib', data: { root: 'libs/lib1' } },
},
dependencies: {},
};

// ACT
await componentGenerator(tree, {
name: 'example',
path: 'libs/lib1/src/lib/mycomp',
});

// ASSERT
const componentSource = tree.read(
'libs/lib1/src/lib/mycomp/example/example.component.ts',
'utf-8'
);
expect(componentSource).toMatchSnapshot();
});

it('should throw if the path specified is not under the project root', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
Expand Down Expand Up @@ -437,6 +486,36 @@ describe('component Generator', () => {
})
).rejects.toThrow();
});

it('should throw when path and projects are not provided and defaultProject is not set', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
addProjectConfiguration(tree, 'lib1', {
projectType: 'library',
sourceRoot: 'libs/lib1/src',
root: 'libs/lib1',
});
tree.write(
'libs/lib1/src/lib/lib.module.ts',
`
import { NgModule } from '@angular/core';
@NgModule({
declarations: [],
exports: []
})
export class LibModule {}`
);
tree.write('libs/lib1/src/index.ts', 'export * from "./lib/lib.module";');

// ACT & ASSERT
await expect(
componentGenerator(tree, {
name: 'example',
export: false,
})
).rejects.toThrow();
});
});

describe('--module', () => {
Expand Down
Expand Up @@ -2,7 +2,6 @@ import type { Tree } from '@nrwl/devkit';
import {
createProjectGraphAsync,
joinPathFragments,
readCachedProjectGraph,
readProjectConfiguration,
readWorkspaceConfiguration,
} from '@nrwl/devkit';
Expand All @@ -13,6 +12,10 @@ import {
} from 'nx/src/project-graph/utils/find-project-for-path';

async function findProjectFromOptions(options: Schema) {
if (options.path === null || options.path === undefined) {
return null;
}

const projectGraph = await createProjectGraphAsync();
const projectRootMappings = createProjectRootMappings(projectGraph.nodes);
return findProjectForPath(options.path, projectRootMappings);
Expand All @@ -26,6 +29,13 @@ export async function normalizeOptions(
options.project ??
(await findProjectFromOptions(options)) ??
readWorkspaceConfiguration(tree).defaultProject;

if (!project) {
throw new Error(
'No "project" or "path" was specified and "defaultProject" is not set in the workspace configuration. Please provide the "project" option and try again.'
);
}

const { projectType, root, sourceRoot } = readProjectConfiguration(
tree,
project
Expand Down

0 comments on commit 54c5e28

Please sign in to comment.