1 | var path = require('path');
|
2 | var process = require('process');
|
3 | var fs = require('fs');
|
4 |
|
5 | module.exports = function dynamicPathParser(project, entityName) {
|
6 | var projectRoot = project.root;
|
7 | var sourceDir = project.ngConfig.apps[0].root;
|
8 | var appRoot = path.join(sourceDir, 'app');
|
9 | var cwd = process.env.PWD;
|
10 |
|
11 | var rootPath = path.join(projectRoot, appRoot);
|
12 |
|
13 | var outputPath = path.join(rootPath, entityName);
|
14 |
|
15 | if (entityName.indexOf(path.sep) === 0) {
|
16 | outputPath = path.join(rootPath, entityName.substr(1));
|
17 | } else if (cwd.indexOf(rootPath) >= 0) {
|
18 | outputPath = path.join(cwd, entityName);
|
19 | }
|
20 |
|
21 | if (!fs.existsSync(outputPath)) {
|
22 |
|
23 | var parsedOutputPath = path.parse(outputPath);
|
24 | var parts = parsedOutputPath.dir.split(path.sep).slice(1);
|
25 | var newPath = parts.reduce((tempPath, part) => {
|
26 |
|
27 |
|
28 |
|
29 | var withoutPlus = path.join(tempPath, path.sep, part);
|
30 | var withPlus = path.join(tempPath, path.sep, '+' + part);
|
31 | if (fs.existsSync(withoutPlus)) {
|
32 | return withoutPlus;
|
33 | } else if (fs.existsSync(withPlus)) {
|
34 | return withPlus;
|
35 | }
|
36 |
|
37 |
|
38 | fs.mkdirSync(withoutPlus);
|
39 | return withoutPlus;
|
40 |
|
41 | }, parsedOutputPath.root);
|
42 | outputPath = path.join(newPath, parsedOutputPath.name);
|
43 | }
|
44 |
|
45 | if (outputPath.indexOf(rootPath) < 0) {
|
46 | throw `Invalid path: "${entityName}" cannot be ` +
|
47 | `above the "${appRoot}" directory`;
|
48 | }
|
49 |
|
50 | var adjustedPath = outputPath.replace(projectRoot, '');
|
51 |
|
52 | var parsedPath = path.parse(adjustedPath);
|
53 |
|
54 | if (parsedPath.dir.indexOf(path.sep) === 0) {
|
55 | parsedPath.dir = parsedPath.dir.substr(1);
|
56 | }
|
57 |
|
58 | parsedPath.dir = parsedPath.dir === path.sep ? '' : parsedPath.dir;
|
59 | parsedPath.appRoot = appRoot;
|
60 | parsedPath.sourceDir = sourceDir;
|
61 |
|
62 | return parsedPath;
|
63 | };
|