diff --git a/packages/@angular/cli/blueprints/component/index.ts b/packages/@angular/cli/blueprints/component/index.ts index ef5d9bcbd45c..2ef24c7c102e 100644 --- a/packages/@angular/cli/blueprints/component/index.ts +++ b/packages/@angular/cli/blueprints/component/index.ts @@ -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}`; diff --git a/packages/@angular/cli/blueprints/directive/index.ts b/packages/@angular/cli/blueprints/directive/index.ts index d5b02d81d22e..50ddaf00c0d7 100644 --- a/packages/@angular/cli/blueprints/directive/index.ts +++ b/packages/@angular/cli/blueprints/directive/index.ts @@ -70,8 +70,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}`; diff --git a/packages/@angular/cli/blueprints/pipe/index.ts b/packages/@angular/cli/blueprints/pipe/index.ts index 9cf81773a420..99c50281a8fe 100644 --- a/packages/@angular/cli/blueprints/pipe/index.ts +++ b/packages/@angular/cli/blueprints/pipe/index.ts @@ -65,8 +65,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}`; diff --git a/packages/@angular/cli/ember-cli/lib/ui/index.js b/packages/@angular/cli/ember-cli/lib/ui/index.js index 7837f04e0f52..3e749d1a0166 100644 --- a/packages/@angular/cli/ember-cli/lib/ui/index.js +++ b/packages/@angular/cli/ember-cli/lib/ui/index.js @@ -174,12 +174,10 @@ UI.prototype.prompt = function(questions, callback) { // If no callback was provided, automatically return a promise if (callback) { - inquirer.prompt(questions, callback); - } else { - return new Promise(function(resolve) { - inquirer.prompt(questions, resolve); - }); + return inquirer.prompt(questions, callback); } + + return inquirer.prompt(questions); }; /** diff --git a/packages/@angular/cli/utilities/find-parent-module.ts b/packages/@angular/cli/utilities/find-parent-module.ts index 3236167121cf..61be86780b5e 100644 --- a/packages/@angular/cli/utilities/find-parent-module.ts +++ b/packages/@angular/cli/utilities/find-parent-module.ts @@ -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')) diff --git a/tests/e2e/tests/generate/component/component-in-existing-module-dir.ts b/tests/e2e/tests/generate/component/component-in-existing-module-dir.ts new file mode 100644 index 000000000000..bb98447bb4a1 --- /dev/null +++ b/tests/e2e/tests/generate/component/component-in-existing-module-dir.ts @@ -0,0 +1,12 @@ +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'/)); +} diff --git a/tests/e2e/tests/generate/directive/directive-in-existing-module-dir.ts b/tests/e2e/tests/generate/directive/directive-in-existing-module-dir.ts new file mode 100644 index 000000000000..7dd270fe81a1 --- /dev/null +++ b/tests/e2e/tests/generate/directive/directive-in-existing-module-dir.ts @@ -0,0 +1,12 @@ +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', 'directive', 'foo', '--no-flat')) + .then(() => expectFileToMatch(modulePath, /import { FooDirective } from '.\/foo.directive'/)); +} diff --git a/tests/e2e/tests/generate/pipe/pipe-in-existing-module-dir.ts b/tests/e2e/tests/generate/pipe/pipe-in-existing-module-dir.ts new file mode 100644 index 000000000000..2055643a1b5b --- /dev/null +++ b/tests/e2e/tests/generate/pipe/pipe-in-existing-module-dir.ts @@ -0,0 +1,12 @@ +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', 'pipe', 'foo', '--no-flat')) + .then(() => expectFileToMatch(modulePath, /import { FooPipe } from '.\/foo.pipe'/)); +}