Skip to content

Commit

Permalink
feat(generate): add option to auto-export declarations (#3876)
Browse files Browse the repository at this point in the history
Fixes #3778
  • Loading branch information
Brocco authored and hansl committed Jan 13, 2017
1 parent 18ccf41 commit 6d63bb4
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 7 deletions.
8 changes: 8 additions & 0 deletions packages/@angular-cli/ast-tools/src/ast-utils.ts
Expand Up @@ -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<Change> {
return _addSymbolToNgModuleMetadata(modulePath, 'exports', classifiedName, importPath);
}

12 changes: 10 additions & 2 deletions packages/angular-cli/blueprints/component/index.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}

Expand Down
12 changes: 10 additions & 2 deletions packages/angular-cli/blueprints/directive/index.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}

Expand Down
12 changes: 10 additions & 2 deletions packages/angular-cli/blueprints/pipe/index.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}

Expand Down
3 changes: 2 additions & 1 deletion packages/angular-cli/utilities/ast-utils.ts
Expand Up @@ -8,5 +8,6 @@ export {
getContentOfKeyLiteral,
getDecoratorMetadata,
addDeclarationToModule,
addProviderToModule
addProviderToModule,
addExportToModule
} from '@angular-cli/ast-tools';
14 changes: 14 additions & 0 deletions 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'));
}
14 changes: 14 additions & 0 deletions 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'));
}
14 changes: 14 additions & 0 deletions 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'));
}

0 comments on commit 6d63bb4

Please sign in to comment.