Skip to content

Commit

Permalink
fix(@angular/cli): fix component not finding closest module
Browse files Browse the repository at this point in the history
fixes #5127
  • Loading branch information
delasteve committed Mar 4, 2017
1 parent 86d60bb commit f92321f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/@angular/cli/blueprints/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ export default Blueprint.extend({
}
} else {
try {
this.pathToModule = findParentModule(
this.project.root, appConfig.root, this.dynamicPath.dir);
this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath);
} catch (e) {
if (!options.skipImport) {
throw `Error locating module for declaration\n\t${e}`;
Expand Down
4 changes: 4 additions & 0 deletions packages/@angular/cli/utilities/find-parent-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export default function findParentModule(
let pathToCheck = path.join(sourceRoot, currentDir);

while (pathToCheck.length >= sourceRoot.length) {
if (!fs.existsSync(pathToCheck)) {
pathToCheck = path.dirname(pathToCheck);
continue;
}
// TODO: refactor to not be based upon file name
const files = fs.readdirSync(pathToCheck)
.filter(fileName => !fileName.endsWith('routing.module.ts'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { join } from 'path';
import { ng } from '../../../utils/process';
import { expectFileToMatch } from '../../../utils/fs';


export default function () {
const modulePath = join('src', 'app', 'foo', 'foo.module.ts');

return Promise.resolve()
.then(() => ng('generate', 'module', 'foo'))
.then(() => ng('generate', 'component', 'foo'))
.then(() => expectFileToMatch(modulePath, /import { FooComponent } from '.\/foo.component'/));
}

0 comments on commit f92321f

Please sign in to comment.