Skip to content

Commit

Permalink
fix(@angular/cli): generating will dasherize all new dirs (#5437)
Browse files Browse the repository at this point in the history
Fixes #5424
  • Loading branch information
Brocco authored Mar 15, 2017
1 parent d94040b commit d6504e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
19 changes: 11 additions & 8 deletions packages/@angular/cli/utilities/dynamic-path-parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var path = require('path');
var process = require('process');
var fs = require('fs');
var stringUtils = require('ember-cli-string-utils');

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

var rootPath = path.join(projectRoot, appRoot);
var outputPath = path.join(rootPath, entityName);

if (entityName.indexOf(path.sep) === 0) {
outputPath = path.join(rootPath, entityName.substr(1));
} else if (cwd.indexOf(rootPath) >= 0) {
outputPath = path.join(cwd, entityName);
}

if (!fs.existsSync(outputPath)) {
// Verify the path exists on disk.
var parsedOutputPath = path.parse(outputPath);
Expand All @@ -25,22 +26,24 @@ module.exports = function dynamicPathParser(project, entityName, appConfig) {
// if (tempPath === '') {
// return part;
// }
var withoutPlus = path.join(tempPath, path.sep, part);
var withPlus = path.join(tempPath, path.sep, '+' + part);
var withoutPlus = path.join(tempPath, part);
var withPlus = path.join(tempPath, '+' + part);
if (fs.existsSync(withoutPlus)) {
return withoutPlus;
} else if (fs.existsSync(withPlus)) {
return withPlus;
}

// Folder not found, create it, and return it
fs.mkdirSync(withoutPlus);
return withoutPlus;
const dasherizedPart = stringUtils.dasherize(part);
const dasherizedDirName = path.join(tempPath, dasherizedPart);
fs.mkdirSync(dasherizedDirName);
return dasherizedDirName;

}, parsedOutputPath.root);
outputPath = path.join(newPath, parsedOutputPath.name);
}

if (outputPath.indexOf(rootPath) < 0) {
throw `Invalid path: "${entityName}" cannot be ` +
`above the "${appRoot}" directory`;
Expand All @@ -49,7 +52,7 @@ module.exports = function dynamicPathParser(project, entityName, appConfig) {
var adjustedPath = outputPath.replace(projectRoot, '');

var parsedPath = path.parse(adjustedPath);

if (parsedPath.dir.indexOf(path.sep) === 0) {
parsedPath.dir = parsedPath.dir.substr(1);
}
Expand Down
15 changes: 11 additions & 4 deletions tests/acceptance/dynamic-path-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ describe('dynamic path parser', () => {
var root = 'src';
beforeEach(() => {
project = {
root: rootName,
root: rootName,
ngConfig: {
apps: [{
root: root
}]
}
}
};
var mockFolder = {};
mockFolder[rootName] = {
Expand All @@ -35,7 +35,7 @@ describe('dynamic path parser', () => {
};
mockFs(mockFolder);
});

afterEach(() => {
mockFs.restore();
});
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('dynamic path parser', () => {
expect(result.dir).to.equal(`${appDir}${path.sep}child-dir`);
expect(result.name).to.equal(entityName);
});

it('auto look for dirs with a "+" when not specified', () => {
var mockFolder = {};
mockFolder[rootName] = {
Expand All @@ -141,4 +141,11 @@ describe('dynamic path parser', () => {
expect(result.dir).to.equal(`${appDir}${path.sep}+my-route`);
expect(result.name).to.equal(entityName);
});

it('create new dirs as dasherized', () => {
process.env.PWD = project.root;
var result = dynamicPathParser(project, path.join('NewDir', entityName), appConfig);
expect(result.dir).to.equal(`${appDir}${path.sep}new-dir`);
expect(result.name).to.equal(entityName);
});
});

0 comments on commit d6504e2

Please sign in to comment.