Skip to content

Commit e9da693

Browse files
committed
fix(generate): correct component path when module is generated in subfolder, and parent folder is not a module too
fixes angular#3255
1 parent 338e69b commit e9da693

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,20 @@ module.exports = {
5858
},
5959

6060
afterInstall: function (options) {
61-
// Note that `this.generatePath` already contains `this.dasherizedModuleName`
62-
// So, the path will end like `name/name`,
63-
// which is correct for `name.component.ts` created in module `name`
6461
if (this.options && this.options.routing) {
65-
var componentPath = path.join(this.generatePath, this.dasherizedModuleName);
66-
options.entity.name = path.relative(this.dynamicPath.appRoot, componentPath);
62+
63+
// Component folder needs to be `/{moduleName}/{ComponentName}`
64+
// Note that we are using `flat`, so no extra dir will be created
65+
// We need the leading `/` so the component path resolution work for both cases below:
66+
// 1. If module name has no path (no `/`), that's going to be `/mod-name/mod-name`
67+
// as `this.dynamicPath.dir` will be the same as `this.dynamicPath.appRoot`
68+
// 2. If it does have `/` (like `parent/mod-name`), it'll be `/parent/mod-name/mod-name`
69+
// as `this.dynamicPath.dir` minus `this.dynamicPath.appRoot` will be `/parent`
70+
let dmouleDir =
71+
this.dynamicPath.dir.replace(this.dynamicPath.appRoot, '') + path.sep + this.dasherizedModuleName;
72+
options.entity.name = dmouleDir + path.sep + this.dasherizedModuleName;
6773
options.flat = true;
74+
6875
options.route = false;
6976
options.inlineTemplate = false;
7077
options.inlineStyle = false;

tests/acceptance/generate-module.spec.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
const ng = require('../helpers/ng');
44
const tmp = require('../helpers/tmp');
5-
5+
const fs = require('fs-extra');
66
const existsSync = require('exists-sync');
77
const expect = require('chai').expect;
8+
const Promise = require('angular-cli/ember-cli/lib/ext/promise');
89
const path = require('path');
910
const root = process.cwd();
1011

@@ -40,7 +41,7 @@ describe('Acceptance: ng generate module', function () {
4041
});
4142

4243
it('ng generate module generate routing and component files when passed flag --routing', function () {
43-
return ng(['generate', 'module', 'my-module', '--routing']).then( () => {
44+
return ng(['generate', 'module', 'my-module', '--routing']).then(() => {
4445
expect(existsSync(path.join(testPath, 'my-module', 'my-module.module.ts'))).to.equal(true);
4546
expect(existsSync(path.join(testPath, 'my-module', 'my-module-routing.module.ts'))).to.equal(true);
4647
expect(existsSync(path.join(testPath, 'my-module', 'my-module.module.spec.ts'))).to.equal(false);
@@ -72,6 +73,33 @@ describe('Acceptance: ng generate module', function () {
7273
);
7374
});
7475

76+
it('ng generate module child should work in sub-dir', function () {
77+
return Promise.resolve().then(() => {
78+
fs.mkdirpSync(path.join(testPath, './sub-dir'));
79+
process.chdir(path.join(testPath, './sub-dir'));
80+
}).then(() =>
81+
ng(['generate', 'module', 'child']).then(() => {
82+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.module.ts'))).to.equal(true);
83+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.module.spec.ts'))).to.equal(false);
84+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.component.ts'))).to.equal(false);
85+
})
86+
);
87+
});
88+
89+
it('ng generate module child should work in sub-dir with routing and component files when passed --routing flag', function () {
90+
return Promise.resolve().then(() => {
91+
fs.mkdirpSync(path.join(testPath, './sub-dir'));
92+
process.chdir(path.join(testPath, './sub-dir'));
93+
}).then(() =>
94+
ng(['generate', 'module', 'child', '--routing']).then(() => {
95+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.module.ts'))).to.equal(true);
96+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child-routing.module.ts'))).to.equal(true);
97+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.module.spec.ts'))).to.equal(false);
98+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.component.ts'))).to.equal(true);
99+
})
100+
);
101+
});
102+
75103
it('ng generate module should generate parent/child module with routing and component files when passed --routing flag', function () {
76104
return ng(['generate', 'module', 'parent']).then(() =>
77105
ng(['generate', 'module', 'parent/child', '--routing']).then(() => {

0 commit comments

Comments
 (0)