Skip to content

Commit d6504e2

Browse files
authored
fix(@angular/cli): generating will dasherize all new dirs (#5437)
Fixes #5424
1 parent d94040b commit d6504e2

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

packages/@angular/cli/utilities/dynamic-path-parser.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var path = require('path');
22
var process = require('process');
33
var fs = require('fs');
4+
var stringUtils = require('ember-cli-string-utils');
45

56
module.exports = function dynamicPathParser(project, entityName, appConfig) {
67
var projectRoot = project.root;
@@ -10,13 +11,13 @@ module.exports = function dynamicPathParser(project, entityName, appConfig) {
1011

1112
var rootPath = path.join(projectRoot, appRoot);
1213
var outputPath = path.join(rootPath, entityName);
13-
14+
1415
if (entityName.indexOf(path.sep) === 0) {
1516
outputPath = path.join(rootPath, entityName.substr(1));
1617
} else if (cwd.indexOf(rootPath) >= 0) {
1718
outputPath = path.join(cwd, entityName);
1819
}
19-
20+
2021
if (!fs.existsSync(outputPath)) {
2122
// Verify the path exists on disk.
2223
var parsedOutputPath = path.parse(outputPath);
@@ -25,22 +26,24 @@ module.exports = function dynamicPathParser(project, entityName, appConfig) {
2526
// if (tempPath === '') {
2627
// return part;
2728
// }
28-
var withoutPlus = path.join(tempPath, path.sep, part);
29-
var withPlus = path.join(tempPath, path.sep, '+' + part);
29+
var withoutPlus = path.join(tempPath, part);
30+
var withPlus = path.join(tempPath, '+' + part);
3031
if (fs.existsSync(withoutPlus)) {
3132
return withoutPlus;
3233
} else if (fs.existsSync(withPlus)) {
3334
return withPlus;
3435
}
3536

3637
// Folder not found, create it, and return it
37-
fs.mkdirSync(withoutPlus);
38-
return withoutPlus;
38+
const dasherizedPart = stringUtils.dasherize(part);
39+
const dasherizedDirName = path.join(tempPath, dasherizedPart);
40+
fs.mkdirSync(dasherizedDirName);
41+
return dasherizedDirName;
3942

4043
}, parsedOutputPath.root);
4144
outputPath = path.join(newPath, parsedOutputPath.name);
4245
}
43-
46+
4447
if (outputPath.indexOf(rootPath) < 0) {
4548
throw `Invalid path: "${entityName}" cannot be ` +
4649
`above the "${appRoot}" directory`;
@@ -49,7 +52,7 @@ module.exports = function dynamicPathParser(project, entityName, appConfig) {
4952
var adjustedPath = outputPath.replace(projectRoot, '');
5053

5154
var parsedPath = path.parse(adjustedPath);
52-
55+
5356
if (parsedPath.dir.indexOf(path.sep) === 0) {
5457
parsedPath.dir = parsedPath.dir.substr(1);
5558
}

tests/acceptance/dynamic-path-parser.spec.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ describe('dynamic path parser', () => {
1717
var root = 'src';
1818
beforeEach(() => {
1919
project = {
20-
root: rootName,
20+
root: rootName,
2121
ngConfig: {
2222
apps: [{
2323
root: root
2424
}]
25-
}
25+
}
2626
};
2727
var mockFolder = {};
2828
mockFolder[rootName] = {
@@ -35,7 +35,7 @@ describe('dynamic path parser', () => {
3535
};
3636
mockFs(mockFolder);
3737
});
38-
38+
3939
afterEach(() => {
4040
mockFs.restore();
4141
});
@@ -125,7 +125,7 @@ describe('dynamic path parser', () => {
125125
expect(result.dir).to.equal(`${appDir}${path.sep}child-dir`);
126126
expect(result.name).to.equal(entityName);
127127
});
128-
128+
129129
it('auto look for dirs with a "+" when not specified', () => {
130130
var mockFolder = {};
131131
mockFolder[rootName] = {
@@ -141,4 +141,11 @@ describe('dynamic path parser', () => {
141141
expect(result.dir).to.equal(`${appDir}${path.sep}+my-route`);
142142
expect(result.name).to.equal(entityName);
143143
});
144+
145+
it('create new dirs as dasherized', () => {
146+
process.env.PWD = project.root;
147+
var result = dynamicPathParser(project, path.join('NewDir', entityName), appConfig);
148+
expect(result.dir).to.equal(`${appDir}${path.sep}new-dir`);
149+
expect(result.name).to.equal(entityName);
150+
});
144151
});

0 commit comments

Comments
 (0)