angular-cli/addon/ng2/utilities/dynamic-path-parser.js
Mike Brocchi f2f5ff9250 fix(paths): fix the determination of execution path
Windows does not populate  this fix allows for determination
of the directory from which an ng command was executed
Fixes #297
2016-03-17 22:59:35 -04:00

33 lines
1.0 KiB
JavaScript

var path = require('path');
var process = require('process');
module.exports = function dynamicPathParser(project, entityName) {
var projectRoot = project.root;
var cwd = process.env.PWD;
var rootPath = path.join(projectRoot, 'src', 'app');
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);
} else if (cwd.indexOf(path.join(projectRoot, 'src')) >= 0
&& entityName.indexOf('app') === 0) {
outputPath = path.join(cwd, entityName);
} else if (cwd.indexOf(path.join(projectRoot, 'src')) >= 0) {
outputPath = path.join(cwd, 'app', entityName);
}
if (outputPath.indexOf(rootPath) < 0) {
throw `Invalid path: "${entityName}" cannot be ` +
`above the "${path.join('src', 'app')}" directory`;
}
var adjustedPath = outputPath.replace(rootPath, '');
var parsedPath = path.parse(adjustedPath);
return parsedPath;
}