Skip to content

Commit a647e51

Browse files
authored
feat(module): select module to add generations to for declaration (angular#1966)
Note: this also displays a warning stating that services are not provided by default
1 parent f286c1c commit a647e51

File tree

7 files changed

+81
-23
lines changed

7 files changed

+81
-23
lines changed

addon/ng2/blueprints/component/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var path = require('path');
22
var chalk = require('chalk');
33
var Blueprint = require('ember-cli/lib/models/blueprint');
44
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
5+
const findParentModule = require('../../utilities/find-parent-module');
56
var getFiles = Blueprint.prototype.files;
67
const stringUtils = require('ember-cli-string-utils');
78
const astUtils = require('../../utilities/ast-utils');
@@ -17,6 +18,14 @@ module.exports = {
1718
{ name: 'spec', type: Boolean, default: true }
1819
],
1920

21+
beforeInstall: function() {
22+
try {
23+
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
24+
} catch(e) {
25+
throw `Error locating module for declaration\n\t${e}`;
26+
}
27+
},
28+
2029
normalizeEntityName: function (entityName) {
2130
var parsedPath = dynamicPathParser(this.project, entityName);
2231

@@ -100,15 +109,14 @@ module.exports = {
100109
}
101110

102111
const returns = [];
103-
const modulePath = path.join(this.project.root, this.dynamicPath.appRoot, 'app.module.ts');
104112
const className = stringUtils.classify(`${options.entity.name}Component`);
105113
const fileName = stringUtils.dasherize(`${options.entity.name}.component`);
106114
const componentDir = path.relative(this.dynamicPath.appRoot, this.generatePath);
107115
const importPath = componentDir ? `./${componentDir}/${fileName}` : `./${fileName}`;
108116

109117
if (!options['skip-import']) {
110118
returns.push(
111-
astUtils.addComponentToModule(modulePath, className, importPath)
119+
astUtils.addComponentToModule(this.pathToModule, className, importPath)
112120
.then(change => change.apply()));
113121
}
114122

addon/ng2/blueprints/directive/index.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var path = require('path');
22
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
33
const stringUtils = require('ember-cli-string-utils');
44
const astUtils = require('../../utilities/ast-utils');
5+
const findParentModule = require('../../utilities/find-parent-module');
56

67
module.exports = {
78
description: '',
@@ -11,11 +12,19 @@ module.exports = {
1112
{ name: 'prefix', type: Boolean, default: true }
1213
],
1314

15+
beforeInstall: function() {
16+
try {
17+
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
18+
} catch(e) {
19+
throw `Error locating module for declaration\n\t${e}`;
20+
}
21+
},
22+
1423
normalizeEntityName: function (entityName) {
1524
var parsedPath = dynamicPathParser(this.project, entityName);
1625

1726
this.dynamicPath = parsedPath;
18-
27+
1928
var defaultPrefix = '';
2029
if (this.project.ngConfig &&
2130
this.project.ngConfig.apps[0] &&
@@ -56,15 +65,14 @@ module.exports = {
5665
}
5766

5867
const returns = [];
59-
const modulePath = path.join(this.project.root, this.dynamicPath.appRoot, 'app.module.ts');
6068
const className = stringUtils.classify(`${options.entity.name}Directive`);
6169
const fileName = stringUtils.dasherize(`${options.entity.name}.directive`);
6270
const componentDir = path.relative(this.dynamicPath.appRoot, this.generatePath);
6371
const importPath = componentDir ? `./${componentDir}/${fileName}` : `./${fileName}`;
6472

6573
if (!options['skip-import']) {
6674
returns.push(
67-
astUtils.addComponentToModule(modulePath, className, importPath)
75+
astUtils.addComponentToModule(this.pathToModule, className, importPath)
6876
.then(change => change.apply()));
6977
}
7078

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import { NgModule } from '@angular/core';
22
import { CommonModule } from '@angular/common';
3-
import { routing } from './<%= dasherizedModuleName %>.routes';
4-
import { <%= classifiedModuleName %>Component } from './<%= dasherizedModuleName %>.component';
53

64
@NgModule({
75
imports: [
8-
CommonModule,
9-
routing
6+
CommonModule
107
],
11-
declarations: [
12-
<%= classifiedModuleName %>Component
13-
]
8+
declarations: []
149
})
1510
export class <%= classifiedModuleName %>Module { }

addon/ng2/blueprints/module/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
availableOptions: [
1010
{ name: 'spec', type: Boolean, default: false }
1111
],
12-
12+
1313
normalizeEntityName: function (entityName) {
1414
this.entityName = entityName;
1515
var parsedPath = dynamicPathParser(this.project, entityName);
@@ -19,7 +19,7 @@ module.exports = {
1919
},
2020

2121
locals: function (options) {
22-
return {
22+
return {
2323
dynamicPath: this.dynamicPath.dir,
2424
spec: options.spec
2525
};
@@ -40,17 +40,17 @@ module.exports = {
4040
this.dasherizedModuleName = options.dasherizedModuleName;
4141
return {
4242
__path__: () => {
43-
this.generatePath = this.dynamicPath.dir
44-
+ path.sep
43+
this.generatePath = this.dynamicPath.dir
44+
+ path.sep
4545
+ options.dasherizedModuleName;
4646
return this.generatePath;
4747
}
4848
};
4949
},
5050

5151
afterInstall: function (options) {
52-
options.entity.name = this.entityName;
53-
options.flat = false;
52+
options.entity.name = path.join(this.entityName, this.dasherizedModuleName);
53+
options.flat = true;
5454
options.route = false;
5555
options.inlineTemplate = false;
5656
options.inlineStyle = false;

addon/ng2/blueprints/pipe/index.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ var path = require('path');
22
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
33
const stringUtils = require('ember-cli-string-utils');
44
const astUtils = require('../../utilities/ast-utils');
5+
const findParentModule = require('../../utilities/find-parent-module');
56

67
module.exports = {
78
description: '',
8-
9+
910
availableOptions: [
1011
{ name: 'flat', type: Boolean, default: true }
1112
],
1213

14+
beforeInstall: function() {
15+
try {
16+
this.pathToModule = findParentModule(this.project, this.dynamicPath.dir);
17+
} catch(e) {
18+
throw `Error locating module for declaration\n\t${e}`;
19+
}
20+
},
21+
1322
normalizeEntityName: function (entityName) {
1423
var parsedPath = dynamicPathParser(this.project, entityName);
1524

@@ -18,7 +27,7 @@ module.exports = {
1827
},
1928

2029
locals: function (options) {
21-
return {
30+
return {
2231
dynamicPath: this.dynamicPath.dir,
2332
flat: options.flat
2433
};
@@ -37,22 +46,21 @@ module.exports = {
3746
}
3847
};
3948
},
40-
49+
4150
afterInstall: function(options) {
4251
if (options.dryRun) {
4352
return;
4453
}
4554

4655
const returns = [];
47-
const modulePath = path.join(this.project.root, this.dynamicPath.appRoot, 'app.module.ts');
4856
const className = stringUtils.classify(`${options.entity.name}Pipe`);
4957
const fileName = stringUtils.dasherize(`${options.entity.name}.pipe`);
5058
const componentDir = path.relative(this.dynamicPath.appRoot, this.generatePath);
5159
const importPath = componentDir ? `./${componentDir}/${fileName}` : `./${fileName}`;
5260

5361
if (!options['skip-import']) {
5462
returns.push(
55-
astUtils.addComponentToModule(modulePath, className, importPath)
63+
astUtils.addComponentToModule(this.pathToModule, className, importPath)
5664
.then(change => change.apply()));
5765
}
5866

addon/ng2/blueprints/service/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var path = require('path');
2+
const chalk = require('chalk');
23
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
34

45
module.exports = {
@@ -34,5 +35,10 @@ module.exports = {
3435
return dir;
3536
}
3637
};
38+
},
39+
40+
afterInstall() {
41+
const warningMessage = 'Service is generated but not provided, it must be provided to be used';
42+
this._writeStatusToUI(chalk.yellow, 'WARNING', warningMessage);
3743
}
3844
};
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
const SilentError = require('silent-error');
4+
5+
module.exports = function findParentModule(project: any, currentDir: string): string {
6+
7+
const sourceRoot = path.join(project.root, project.ngConfig.apps[0].root, 'app');
8+
9+
// trim currentDir
10+
currentDir = currentDir.replace(path.join(project.ngConfig.apps[0].root, 'app'), '');
11+
12+
let pathToCheck = path.join(sourceRoot, currentDir);
13+
14+
while (pathToCheck.length >= sourceRoot.length) {
15+
// let files: string[] = fs.readdirSync(pathToCheck);
16+
17+
// files = files.filter(file => file.indexOf('.module.ts') > 0);
18+
const files = fs.readdirSync(pathToCheck)
19+
.filter(fileName => fileName.endsWith('.module.ts'))
20+
.filter(fileName => fs.statSync(path.join(pathToCheck, fileName)).isFile());
21+
22+
if (files.length === 1) {
23+
return path.join(pathToCheck, files[0]);
24+
} else if (files.length > 1) {
25+
throw new SilentError(`Multiple module files found: ${pathToCheck.replace(sourceRoot, '')}`);
26+
}
27+
28+
// move to parent directory
29+
pathToCheck = path.dirname(pathToCheck);
30+
}
31+
32+
throw new SilentError('No module files found');
33+
};

0 commit comments

Comments
 (0)