Skip to content

Commit 7c377b2

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 7c377b2

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-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

+23-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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;
88
const path = require('path');
@@ -40,7 +40,7 @@ describe('Acceptance: ng generate module', function () {
4040
});
4141

4242
it('ng generate module generate routing and component files when passed flag --routing', function () {
43-
return ng(['generate', 'module', 'my-module', '--routing']).then( () => {
43+
return ng(['generate', 'module', 'my-module', '--routing']).then(() => {
4444
expect(existsSync(path.join(testPath, 'my-module', 'my-module.module.ts'))).to.equal(true);
4545
expect(existsSync(path.join(testPath, 'my-module', 'my-module-routing.module.ts'))).to.equal(true);
4646
expect(existsSync(path.join(testPath, 'my-module', 'my-module.module.spec.ts'))).to.equal(false);
@@ -72,6 +72,27 @@ describe('Acceptance: ng generate module', function () {
7272
);
7373
});
7474

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

0 commit comments

Comments
 (0)