Skip to content

Commit f70feae

Browse files
MeligyBrocco
authored andcommitted
fix(generate): correct component path when module is generated in subfolder, and parent folder is not a module too (#3916)
fixes #3255
1 parent faa81ce commit f70feae

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-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+
var moduleDir =
71+
this.dynamicPath.dir.replace(this.dynamicPath.appRoot, '') + path.sep + this.dasherizedModuleName;
72+
options.entity.name = moduleDir + 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

+32-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,35 @@ describe('Acceptance: ng generate module', function () {
7273
);
7374
});
7475

76+
it('ng generate module child should work in sub-dir', function () {
77+
fs.mkdirSync(path.join(testPath, './sub-dir'));
78+
return new Promise(resolve => {
79+
process.chdir(path.join(testPath, './sub-dir'));
80+
return resolve();
81+
}).then(() =>
82+
ng(['generate', 'module', 'child']).then(() => {
83+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.module.ts'))).to.equal(true);
84+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.module.spec.ts'))).to.equal(false);
85+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.component.ts'))).to.equal(false);
86+
})
87+
);
88+
});
89+
90+
it('ng generate module child should work in sub-dir with routing and component files when passed --routing flag', function () {
91+
fs.mkdirSync(path.join(testPath, './sub-dir'));
92+
return new Promise(resolve => {
93+
process.chdir(path.join(testPath, './sub-dir'));
94+
return resolve();
95+
}).then(() =>
96+
ng(['generate', 'module', 'child', '--routing']).then(() => {
97+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.module.ts'))).to.equal(true);
98+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child-routing.module.ts'))).to.equal(true);
99+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.module.spec.ts'))).to.equal(false);
100+
expect(existsSync(path.join(testPath, 'sub-dir/child', 'child.component.ts'))).to.equal(true);
101+
})
102+
);
103+
});
104+
75105
it('ng generate module should generate parent/child module with routing and component files when passed --routing flag', function () {
76106
return ng(['generate', 'module', 'parent']).then(() =>
77107
ng(['generate', 'module', 'parent/child', '--routing']).then(() => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as fs from 'fs-extra';
2+
import { join } from 'path';
3+
import { ng } from '../../../utils/process';
4+
import { expectFileToExist } from '../../../utils/fs';
5+
import { expectToFail } from '../../../utils/utils';
6+
7+
8+
const Promise = require('angular-cli/ember-cli/lib/ext/promise');
9+
10+
export default function () {
11+
const root = process.cwd();
12+
const testPath = join(root, 'src', 'app');
13+
14+
process.chdir(testPath);
15+
fs.mkdirSync('./sub-dir');
16+
17+
return Promise.resolve()
18+
.then(() =>
19+
ng('generate', 'module', 'sub-dir/child', '--routing')
20+
.then(() => expectFileToExist(join(testPath, 'sub-dir/child')))
21+
.then(() => expectFileToExist(join(testPath, 'sub-dir/child', 'child.module.ts')))
22+
.then(() => expectFileToExist(join(testPath, 'sub-dir/child', 'child-routing.module.ts')))
23+
.then(() => expectFileToExist(join(testPath, 'sub-dir/child', 'child.component.ts')))
24+
.then(() => expectFileToExist(join(testPath, 'sub-dir/child', 'child.component.spec.ts')))
25+
.then(() => expectFileToExist(join(testPath, 'sub-dir/child', 'child.component.html')))
26+
.then(() => expectFileToExist(join(testPath, 'sub-dir/child', 'child.component.css')))
27+
.then(() => expectToFail(() =>
28+
expectFileToExist(join(testPath, 'sub-dir/child', 'child.spec.ts'))
29+
))
30+
// Try to run the unit tests.
31+
.then(() => ng('test', '--single-run'))
32+
);
33+
}

0 commit comments

Comments
 (0)