Skip to content

Commit 6d63bb4

Browse files
Broccohansl
authored andcommitted
feat(generate): add option to auto-export declarations (angular#3876)
Fixes angular#3778
1 parent 18ccf41 commit 6d63bb4

File tree

8 files changed

+82
-7
lines changed

8 files changed

+82
-7
lines changed

packages/@angular-cli/ast-tools/src/ast-utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,11 @@ export function addProviderToModule(modulePath: string, classifiedName: string,
277277
return _addSymbolToNgModuleMetadata(modulePath, 'providers', classifiedName, importPath);
278278
}
279279

280+
/**
281+
* Custom function to insert an export into NgModule. It also imports it.
282+
*/
283+
export function addExportToModule(modulePath: string, classifiedName: string,
284+
importPath: string): Promise<Change> {
285+
return _addSymbolToNgModuleMetadata(modulePath, 'exports', classifiedName, importPath);
286+
}
287+

packages/angular-cli/blueprints/component/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ module.exports = {
2121
{ name: 'view-encapsulation', type: String, aliases: ['ve'] },
2222
{ name: 'change-detection', type: String, aliases: ['cd'] },
2323
{ name: 'skip-import', type: Boolean, default: false },
24-
{ name: 'module', type: String, aliases: ['m'] }
24+
{ name: 'module', type: String, aliases: ['m'] },
25+
{ name: 'export', type: Boolean, default: false }
2526
],
2627

2728
beforeInstall: function(options) {
@@ -161,7 +162,14 @@ module.exports = {
161162
if (!options.skipImport) {
162163
returns.push(
163164
astUtils.addDeclarationToModule(this.pathToModule, className, importPath)
164-
.then(change => change.apply(NodeHost)));
165+
.then(change => change.apply(NodeHost))
166+
.then((result) => {
167+
if (options.export) {
168+
return astUtils.addExportToModule(this.pathToModule, className, importPath)
169+
.then(change => change.apply(NodeHost));
170+
}
171+
return result;
172+
}));
165173
this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule));
166174
}
167175

packages/angular-cli/blueprints/directive/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ module.exports = {
1717
{ name: 'prefix', type: String, default: null },
1818
{ name: 'spec', type: Boolean },
1919
{ name: 'skip-import', type: Boolean, default: false },
20-
{ name: 'module', type: String, aliases: ['m'] }
20+
{ name: 'module', type: String, aliases: ['m'] },
21+
{ name: 'export', type: Boolean, default: false }
2122
],
2223

2324
beforeInstall: function(options) {
@@ -113,7 +114,14 @@ module.exports = {
113114
if (!options.skipImport) {
114115
returns.push(
115116
astUtils.addDeclarationToModule(this.pathToModule, className, importPath)
116-
.then(change => change.apply(NodeHost)));
117+
.then(change => change.apply(NodeHost))
118+
.then((result) => {
119+
if (options.export) {
120+
return astUtils.addExportToModule(this.pathToModule, className, importPath)
121+
.then(change => change.apply(NodeHost));
122+
}
123+
return result;
124+
}));
117125
this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule));
118126
}
119127

packages/angular-cli/blueprints/pipe/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ module.exports = {
1616
{ name: 'flat', type: Boolean, default: true },
1717
{ name: 'spec', type: Boolean },
1818
{ name: 'skip-import', type: Boolean, default: false },
19-
{ name: 'module', type: String, aliases: ['m'] }
19+
{ name: 'module', type: String, aliases: ['m'] },
20+
{ name: 'export', type: Boolean, default: false }
2021
],
2122

2223
beforeInstall: function(options) {
@@ -98,7 +99,14 @@ module.exports = {
9899
if (!options.skipImport) {
99100
returns.push(
100101
astUtils.addDeclarationToModule(this.pathToModule, className, importPath)
101-
.then(change => change.apply(NodeHost)));
102+
.then(change => change.apply(NodeHost))
103+
.then((result) => {
104+
if (options.export) {
105+
return astUtils.addExportToModule(this.pathToModule, className, importPath)
106+
.then(change => change.apply(NodeHost));
107+
}
108+
return result;
109+
}));
102110
this._writeStatusToUI(chalk.yellow, 'update', path.relative(this.project.root, this.pathToModule));
103111
}
104112

packages/angular-cli/utilities/ast-utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export {
88
getContentOfKeyLiteral,
99
getDecoratorMetadata,
1010
addDeclarationToModule,
11-
addProviderToModule
11+
addProviderToModule,
12+
addExportToModule
1213
} from '@angular-cli/ast-tools';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {join} from 'path';
2+
import {ng} from '../../../utils/process';
3+
import {expectFileToMatch} from '../../../utils/fs';
4+
5+
6+
export default function() {
7+
const modulePath = join('src', 'app', 'app.module.ts');
8+
9+
return ng('generate', 'component', 'test-component', '--export')
10+
.then(() => expectFileToMatch(modulePath, 'exports: [TestComponentComponent]'))
11+
12+
// Try to run the unit tests.
13+
.then(() => ng('test', '--single-run'));
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {join} from 'path';
2+
import {ng} from '../../../utils/process';
3+
import {expectFileToMatch} from '../../../utils/fs';
4+
5+
6+
export default function() {
7+
const modulePath = join('src', 'app', 'app.module.ts');
8+
9+
return ng('generate', 'directive', 'test-directive', '--export')
10+
.then(() => expectFileToMatch(modulePath, 'exports: [TestDirectiveDirective]'))
11+
12+
// Try to run the unit tests.
13+
.then(() => ng('test', '--single-run'));
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {join} from 'path';
2+
import {ng} from '../../../utils/process';
3+
import {expectFileToMatch} from '../../../utils/fs';
4+
5+
6+
export default function() {
7+
const modulePath = join('src', 'app', 'app.module.ts');
8+
9+
return ng('generate', 'pipe', 'test-pipe', '--export')
10+
.then(() => expectFileToMatch(modulePath, 'exports: [TestPipePipe]'))
11+
12+
// Try to run the unit tests.
13+
.then(() => ng('test', '--single-run'));
14+
}

0 commit comments

Comments
 (0)