diff --git a/packages/@angular-cli/ast-tools/src/ast-utils.ts b/packages/@angular-cli/ast-tools/src/ast-utils.ts index 6d79d1ab8111..c3ba6f37152b 100644 --- a/packages/@angular-cli/ast-tools/src/ast-utils.ts +++ b/packages/@angular-cli/ast-tools/src/ast-utils.ts @@ -277,3 +277,11 @@ export function addProviderToModule(modulePath: string, classifiedName: string, return _addSymbolToNgModuleMetadata(modulePath, 'providers', classifiedName, importPath); } +/** + * Custom function to insert an export into NgModule. It also imports it. + */ +export function addExportToModule(modulePath: string, classifiedName: string, + importPath: string): Promise { + return _addSymbolToNgModuleMetadata(modulePath, 'exports', classifiedName, importPath); +} + diff --git a/packages/angular-cli/blueprints/component/index.js b/packages/angular-cli/blueprints/component/index.js index f5f37d645d96..60abb7f238a0 100644 --- a/packages/angular-cli/blueprints/component/index.js +++ b/packages/angular-cli/blueprints/component/index.js @@ -21,7 +21,8 @@ module.exports = { { name: 'view-encapsulation', type: String, aliases: ['ve'] }, { name: 'change-detection', type: String, aliases: ['cd'] }, { name: 'skip-import', type: Boolean, default: false }, - { name: 'module', type: String, aliases: ['m'] } + { name: 'module', type: String, aliases: ['m'] }, + { name: 'export', type: Boolean, default: false } ], beforeInstall: function(options) { @@ -161,7 +162,14 @@ module.exports = { if (!options.skipImport) { returns.push( astUtils.addDeclarationToModule(this.pathToModule, className, importPath) - .then(change => change.apply(NodeHost))); + .then(change => change.apply(NodeHost)) + .then((result) => { + if (options.export) { + return astUtils.addExportToModule(this.pathToModule, className, importPath) + .then(change => change.apply(NodeHost)); + } + return result; + })); this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule)); } diff --git a/packages/angular-cli/blueprints/directive/index.js b/packages/angular-cli/blueprints/directive/index.js index dd5731be0f3a..aee282a2435b 100644 --- a/packages/angular-cli/blueprints/directive/index.js +++ b/packages/angular-cli/blueprints/directive/index.js @@ -17,7 +17,8 @@ module.exports = { { name: 'prefix', type: String, default: null }, { name: 'spec', type: Boolean }, { name: 'skip-import', type: Boolean, default: false }, - { name: 'module', type: String, aliases: ['m'] } + { name: 'module', type: String, aliases: ['m'] }, + { name: 'export', type: Boolean, default: false } ], beforeInstall: function(options) { @@ -113,7 +114,14 @@ module.exports = { if (!options.skipImport) { returns.push( astUtils.addDeclarationToModule(this.pathToModule, className, importPath) - .then(change => change.apply(NodeHost))); + .then(change => change.apply(NodeHost)) + .then((result) => { + if (options.export) { + return astUtils.addExportToModule(this.pathToModule, className, importPath) + .then(change => change.apply(NodeHost)); + } + return result; + })); this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule)); } diff --git a/packages/angular-cli/blueprints/pipe/index.js b/packages/angular-cli/blueprints/pipe/index.js index 10bf9fb02d3b..e7555f52e31b 100644 --- a/packages/angular-cli/blueprints/pipe/index.js +++ b/packages/angular-cli/blueprints/pipe/index.js @@ -16,7 +16,8 @@ module.exports = { { name: 'flat', type: Boolean, default: true }, { name: 'spec', type: Boolean }, { name: 'skip-import', type: Boolean, default: false }, - { name: 'module', type: String, aliases: ['m'] } + { name: 'module', type: String, aliases: ['m'] }, + { name: 'export', type: Boolean, default: false } ], beforeInstall: function(options) { @@ -98,7 +99,14 @@ module.exports = { if (!options.skipImport) { returns.push( astUtils.addDeclarationToModule(this.pathToModule, className, importPath) - .then(change => change.apply(NodeHost))); + .then(change => change.apply(NodeHost)) + .then((result) => { + if (options.export) { + return astUtils.addExportToModule(this.pathToModule, className, importPath) + .then(change => change.apply(NodeHost)); + } + return result; + })); this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule)); } diff --git a/packages/angular-cli/utilities/ast-utils.ts b/packages/angular-cli/utilities/ast-utils.ts index 6adbe798d071..0a8a6b0be3fe 100644 --- a/packages/angular-cli/utilities/ast-utils.ts +++ b/packages/angular-cli/utilities/ast-utils.ts @@ -8,5 +8,6 @@ export { getContentOfKeyLiteral, getDecoratorMetadata, addDeclarationToModule, - addProviderToModule + addProviderToModule, + addExportToModule } from '@angular-cli/ast-tools'; diff --git a/tests/e2e/tests/generate/component/component-module-export.ts b/tests/e2e/tests/generate/component/component-module-export.ts new file mode 100644 index 000000000000..77d0862ec4b2 --- /dev/null +++ b/tests/e2e/tests/generate/component/component-module-export.ts @@ -0,0 +1,14 @@ +import {join} from 'path'; +import {ng} from '../../../utils/process'; +import {expectFileToMatch} from '../../../utils/fs'; + + +export default function() { + const modulePath = join('src', 'app', 'app.module.ts'); + + return ng('generate', 'component', 'test-component', '--export') + .then(() => expectFileToMatch(modulePath, 'exports: [TestComponentComponent]')) + + // Try to run the unit tests. + .then(() => ng('test', '--single-run')); +} diff --git a/tests/e2e/tests/generate/directive/directive-module-export.ts b/tests/e2e/tests/generate/directive/directive-module-export.ts new file mode 100644 index 000000000000..7940cf466a1d --- /dev/null +++ b/tests/e2e/tests/generate/directive/directive-module-export.ts @@ -0,0 +1,14 @@ +import {join} from 'path'; +import {ng} from '../../../utils/process'; +import {expectFileToMatch} from '../../../utils/fs'; + + +export default function() { + const modulePath = join('src', 'app', 'app.module.ts'); + + return ng('generate', 'directive', 'test-directive', '--export') + .then(() => expectFileToMatch(modulePath, 'exports: [TestDirectiveDirective]')) + + // Try to run the unit tests. + .then(() => ng('test', '--single-run')); +} diff --git a/tests/e2e/tests/generate/pipe/pipe-module-export.ts b/tests/e2e/tests/generate/pipe/pipe-module-export.ts new file mode 100644 index 000000000000..ae0c1348dfbd --- /dev/null +++ b/tests/e2e/tests/generate/pipe/pipe-module-export.ts @@ -0,0 +1,14 @@ +import {join} from 'path'; +import {ng} from '../../../utils/process'; +import {expectFileToMatch} from '../../../utils/fs'; + + +export default function() { + const modulePath = join('src', 'app', 'app.module.ts'); + + return ng('generate', 'pipe', 'test-pipe', '--export') + .then(() => expectFileToMatch(modulePath, 'exports: [TestPipePipe]')) + + // Try to run the unit tests. + .then(() => ng('test', '--single-run')); +}