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