Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular/cli): generating will dasherize all new dirs #5437

Merged
merged 1 commit into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});