Skip to content

Commit 14049f4

Browse files
committed
feat: auto correct paths without a '+' to include a '+' if that dir exists
1 parent 0362153 commit 14049f4

File tree

7 files changed

+129
-24
lines changed

7 files changed

+129
-24
lines changed

addon/ng2/utilities/dynamic-path-parser.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var path = require('path');
22
var process = require('process');
3+
var fs = require('fs');
34

45
module.exports = function dynamicPathParser(project, entityName) {
56
var projectRoot = project.root;
@@ -30,6 +31,27 @@ module.exports = function dynamicPathParser(project, entityName) {
3031
}
3132
}
3233

34+
if (!fs.existsSync(outputPath)) {
35+
// Verify the path exists on disk.
36+
var parsedOutputPath = path.parse(outputPath);
37+
var parts = parsedOutputPath.dir.split(path.sep).slice(1);
38+
var newPath = parts.reduce((tempPath, part) => {
39+
// if (tempPath === '') {
40+
// return part;
41+
// }
42+
var withoutPlus = path.join(tempPath, path.sep, part);
43+
var withPlus = path.join(tempPath, path.sep, '+' + part);
44+
if (fs.existsSync(withoutPlus)) {
45+
return withoutPlus;
46+
} else if (fs.existsSync(withPlus)) {
47+
return withPlus;
48+
}
49+
50+
throw `Invalid path: "${withoutPlus}"" is not a valid path.`
51+
}, parsedOutputPath.root);
52+
outputPath = path.join(newPath, parsedOutputPath.name);
53+
}
54+
3355
if (outputPath.indexOf(rootPath) < 0) {
3456
throw `Invalid path: "${entityName}" cannot be ` +
3557
`above the "${appRoot}" directory`;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"exists-sync": "0.0.3",
6666
"minimatch": "^3.0.0",
6767
"mocha": "^2.4.5",
68+
"mock-fs": "^3.8.0",
6869
"object-assign": "^4.0.1",
6970
"rewire": "^2.5.1",
7071
"sinon": "^1.17.3",

tests/acceptance/dynamic-path-parser.spec.js

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,141 @@
33
var expect = require('chai').expect;
44
var path = require('path');
55
var dynamicPathParser = require('../../addon/ng2/utilities/dynamic-path-parser');
6+
var mockFs = require('mock-fs');
67

78
var appDir = `src${path.sep}client${path.sep}app`;
89

910
describe('dynamic path parser', () => {
1011
var project;
1112
var entityName = 'temp-name';
13+
var rootName = path.parse(process.cwd()).root + 'project';
1214
beforeEach(() => {
13-
project = { root: process.cwd() }
15+
project = { root: rootName };
16+
var mockFolder = {};
17+
mockFolder[rootName] = {
18+
src: {
19+
client: {
20+
app: {
21+
'index.html': '<html></html>',
22+
'temp-name': {}
23+
}
24+
}
25+
}
26+
};
27+
mockFs(mockFolder);
28+
});
29+
30+
afterEach(() => {
31+
mockFs.restore();
1432
});
1533

1634
it('parse from proj root dir', () => {
17-
process.env.PWD = process.cwd();
35+
process.env.PWD = project.root;
1836
var result = dynamicPathParser(project, entityName);
1937
expect(result.dir).to.equal(appDir);
2038
expect(result.name).to.equal(entityName);
2139
});
2240

2341
it('parse from proj src dir', () => {
24-
process.env.PWD = path.join(process.cwd(), 'src');
42+
process.env.PWD = path.join(project.root, 'src');
2543
var result = dynamicPathParser(project, entityName);
2644
expect(result.dir).to.equal(appDir);
2745
expect(result.name).to.equal(entityName);
2846
});
2947

3048
it(`parse from proj src${path.sep}client dir`, () => {
31-
process.env.PWD = path.join(process.cwd(), 'src', 'client');
49+
process.env.PWD = path.join(project.root, 'src', 'client');
3250
var result = dynamicPathParser(project, entityName);
3351
expect(result.dir).to.equal(appDir);
3452
expect(result.name).to.equal(entityName);
3553
});
3654

3755
it(`parse from proj src${path.sep}client${path.sep}app dir`, () => {
38-
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app');
56+
process.env.PWD = path.join(project.root, 'src', 'client', 'app');
3957
var result = dynamicPathParser(project, entityName);
4058
expect(result.dir).to.equal(appDir);
4159
expect(result.name).to.equal(entityName);
4260
});
4361

4462
it(`parse from proj src${path.sep}client${path.sep}app${path.sep}child-dir`, () => {
45-
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app', 'child-dir');
63+
var mockFolder = {};
64+
mockFolder[rootName] = {
65+
src: {
66+
client: {
67+
app: {
68+
'index.html': '<html></html>',
69+
'child-dir': {
70+
'temp-name': {}
71+
}
72+
}
73+
}
74+
}
75+
};
76+
mockFs(mockFolder);
77+
process.env.PWD = path.join(project.root, 'src', 'client', 'app', 'child-dir');
4678
var result = dynamicPathParser(project, entityName);
4779
expect(result.dir).to.equal(`${appDir}${path.sep}child-dir`);
4880
expect(result.name).to.equal(entityName);
4981
});
5082

5183
it(`parse from proj src${path.sep}client${path.sep}app${path.sep}child-dir w/ ..${path.sep}`, () => {
52-
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app', 'child-dir');
84+
var mockFolder = {};
85+
mockFolder[rootName] = {
86+
src: {
87+
client: {
88+
app: {
89+
'index.html': '<html></html>',
90+
'child-dir': {},
91+
'temp-name': {}
92+
}
93+
}
94+
}
95+
};
96+
mockFs(mockFolder);
97+
process.env.PWD = path.join(project.root, 'src', 'client', 'app', 'child-dir');
5398
var result = dynamicPathParser(project, '..' + path.sep + entityName);
5499
expect(result.dir).to.equal(appDir);
55100
expect(result.name).to.equal(entityName);
56101
});
57102

58103
it(`parse from proj src${path.sep}client${path.sep}app${path.sep}child-dir${path.sep}grand-child-dir w/ ..${path.sep}`,
59104
() => {
60-
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app', 'child-dir', 'grand-child-dir');
105+
var mockFolder = {};
106+
mockFolder[rootName] = {
107+
src: {
108+
client: {
109+
app: {
110+
'index.html': '<html></html>',
111+
'child-dir': {
112+
'grand-child-dir': {},
113+
'temp-name': {}
114+
}
115+
}
116+
}
117+
}
118+
};
119+
mockFs(mockFolder);
120+
process.env.PWD = path.join(project.root, 'src', 'client', 'app', 'child-dir', 'grand-child-dir');
61121
var result = dynamicPathParser(project, '..' + path.sep + entityName);
62122
expect(result.dir).to.equal(`${appDir}${path.sep}child-dir`);
63123
expect(result.name).to.equal(entityName);
64124
});
125+
126+
it('auto look for dirs with a "+" when not specified', () => {
127+
var mockFolder = {};
128+
mockFolder[rootName] = {
129+
src: {
130+
client: {
131+
app: {
132+
'+my-route': {}
133+
}
134+
}
135+
}
136+
};
137+
mockFs(mockFolder);
138+
process.env.PWD = path.join(project.root, 'src', 'client', 'app', 'my-route');
139+
var result = dynamicPathParser(project, entityName);
140+
expect(result.dir).to.equal(`${appDir}${path.sep}+my-route`);
141+
expect(result.name).to.equal(entityName);
142+
});
65143
});

tests/acceptance/generate-component.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe('Acceptance: ng generate component', function () {
3838
});
3939

4040
it('ng generate component test' + path.sep + 'my-comp', function () {
41+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', 'test'));
4142
return ng(['generate', 'component', 'test' + path.sep + 'my-comp']).then(() => {
4243
var testPath = path.join(root, 'tmp', 'foo', 'src', 'client', 'app', 'test', 'my-comp', 'my-comp.component.ts');
4344
expect(existsSync(testPath)).to.equal(true);
@@ -53,13 +54,13 @@ describe('Acceptance: ng generate component', function () {
5354
});
5455

5556
it('ng generate component my-comp from a child dir', () => {
57+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1'));
5658
return new Promise(function (resolve) {
5759
process.chdir('./src');
5860
resolve();
5961
})
6062
.then(() => process.chdir('./client'))
6163
.then(() => process.chdir('./app'))
62-
.then(() => fs.mkdirsSync('./1'))
6364
.then(() => process.chdir('./1'))
6465
.then(() => {
6566
return ng(['generate', 'component', 'my-comp'])
@@ -71,13 +72,13 @@ describe('Acceptance: ng generate component', function () {
7172
});
7273

7374
it('ng generate component child-dir' + path.sep + 'my-comp from a child dir', () => {
75+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1', 'child-dir'));
7476
return new Promise(function (resolve) {
7577
process.chdir('./src');
7678
resolve();
7779
})
7880
.then(() => process.chdir('./client'))
7981
.then(() => process.chdir('./app'))
80-
.then(() => fs.mkdirsSync('./1'))
8182
.then(() => process.chdir('./1'))
8283
.then(() => {
8384
return ng(['generate', 'component', 'child-dir' + path.sep + 'my-comp'])
@@ -91,13 +92,13 @@ describe('Acceptance: ng generate component', function () {
9192

9293
it('ng generate component child-dir' + path.sep + '..' + path.sep + 'my-comp from a child dir',
9394
() => {
95+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1'));
9496
return new Promise(function (resolve) {
9597
process.chdir('./src');
9698
resolve();
9799
})
98100
.then(() => process.chdir('./client'))
99101
.then(() => process.chdir('./app'))
100-
.then(() => fs.mkdirsSync('./1'))
101102
.then(() => process.chdir('./1'))
102103
.then(() => {
103104
return ng([
@@ -114,13 +115,13 @@ describe('Acceptance: ng generate component', function () {
114115
it('ng generate component ' + path.sep + 'my-comp from a child dir, gens under ' +
115116
path.join('src', 'client', 'app'),
116117
() => {
118+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1'));
117119
return new Promise(function (resolve) {
118120
process.chdir('./src');
119121
resolve();
120122
})
121123
.then(() => process.chdir('./client'))
122124
.then(() => process.chdir('./app'))
123-
.then(() => fs.mkdirsSync('./1'))
124125
.then(() => process.chdir('./1'))
125126
.then(() => {
126127
return ng(['generate', 'component', path.sep + 'my-comp'])

tests/acceptance/generate-directive.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('Acceptance: ng generate directive', function () {
4545
});
4646

4747
it('ng generate directive test' + path.sep + 'my-dir', function () {
48+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', 'test'));
4849
return ng(['generate', 'directive', 'test' + path.sep + 'my-dir', '--flat', 'false']).then(() => {
4950
var testPath = path.join(root, 'tmp', 'foo', 'src', 'client', 'app', 'test', 'my-dir', 'my-dir.directive.ts');
5051
expect(existsSync(testPath)).to.equal(true);
@@ -60,13 +61,13 @@ describe('Acceptance: ng generate directive', function () {
6061
});
6162

6263
it('ng generate directive my-dir from a child dir', () => {
64+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1'));
6365
return new Promise(function (resolve) {
6466
process.chdir('./src');
6567
resolve();
6668
})
6769
.then(() => process.chdir('./client'))
6870
.then(() => process.chdir('./app'))
69-
.then(() => fs.mkdirsSync('./1'))
7071
.then(() => process.chdir('./1'))
7172
.then(() => {
7273
process.env.CWD = process.cwd();
@@ -79,13 +80,13 @@ describe('Acceptance: ng generate directive', function () {
7980
});
8081

8182
it('ng generate directive child-dir' + path.sep + 'my-dir from a child dir', () => {
83+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1', 'child-dir'));
8284
return new Promise(function (resolve) {
8385
process.chdir('./src');
8486
resolve();
8587
})
8688
.then(() => process.chdir('./client'))
8789
.then(() => process.chdir('./app'))
88-
.then(() => fs.mkdirsSync('./1'))
8990
.then(() => process.chdir('./1'))
9091
.then(() => {
9192
process.env.CWD = process.cwd();
@@ -100,13 +101,13 @@ describe('Acceptance: ng generate directive', function () {
100101

101102
it('ng generate directive child-dir' + path.sep + '..' + path.sep + 'my-dir from a child dir',
102103
() => {
104+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1'));
103105
return new Promise(function (resolve) {
104106
process.chdir('./src');
105107
resolve();
106108
})
107109
.then(() => process.chdir('./client'))
108110
.then(() => process.chdir('./app'))
109-
.then(() => fs.mkdirsSync('./1'))
110111
.then(() => process.chdir('./1'))
111112
.then(() => {
112113
process.env.CWD = process.cwd();
@@ -123,13 +124,13 @@ describe('Acceptance: ng generate directive', function () {
123124
it('ng generate directive ' + path.sep + 'my-dir from a child dir, gens under ' +
124125
path.join('src', 'client', 'app'),
125126
() => {
127+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1'));
126128
return new Promise(function (resolve) {
127129
process.chdir('./src');
128130
resolve();
129131
})
130132
.then(() => process.chdir('./client'))
131133
.then(() => process.chdir('./app'))
132-
.then(() => fs.mkdirsSync('./1'))
133134
.then(() => process.chdir('./1'))
134135
.then(() => {
135136
process.env.CWD = process.cwd();

tests/acceptance/generate-pipe.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe('Acceptance: ng generate pipe', function () {
3838
});
3939

4040
it('ng generate pipe test' + path.sep + 'my-pipe', function () {
41+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', 'test'));
4142
return ng(['generate', 'pipe', 'test' + path.sep + 'my-pipe']).then(() => {
4243
var testPath = path.join(root, 'tmp', 'foo', 'src', 'client', 'app', 'test', 'my-pipe.pipe.ts');
4344
expect(existsSync(testPath)).to.equal(true);
@@ -52,13 +53,13 @@ describe('Acceptance: ng generate pipe', function () {
5253
});
5354

5455
it('ng generate pipe my-pipe from a child dir', () => {
56+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1'));
5557
return new Promise(function (resolve) {
5658
process.chdir('./src');
5759
resolve();
5860
})
5961
.then(() => process.chdir('./client'))
6062
.then(() => process.chdir('./app'))
61-
.then(() => fs.mkdirsSync('./1'))
6263
.then(() => process.chdir('./1'))
6364
.then(() => {
6465
process.env.CWD = process.cwd();
@@ -71,13 +72,13 @@ describe('Acceptance: ng generate pipe', function () {
7172
});
7273

7374
it('ng generate pipe child-dir' + path.sep + 'my-pipe from a child dir', () => {
75+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1', 'child-dir'));
7476
return new Promise(function (resolve) {
7577
process.chdir('./src');
7678
resolve();
7779
})
7880
.then(() => process.chdir('./client'))
7981
.then(() => process.chdir('./app'))
80-
.then(() => fs.mkdirsSync('./1'))
8182
.then(() => process.chdir('./1'))
8283
.then(() => {
8384
process.env.CWD = process.cwd();
@@ -91,13 +92,13 @@ describe('Acceptance: ng generate pipe', function () {
9192
});
9293

9394
it('ng generate pipe child-dir' + path.sep + '..' + path.sep + 'my-pipe from a child dir', () => {
95+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1'));
9496
return new Promise(function (resolve) {
9597
process.chdir('./src');
9698
resolve();
9799
})
98100
.then(() => process.chdir('./client'))
99101
.then(() => process.chdir('./app'))
100-
.then(() => fs.mkdirsSync('./1'))
101102
.then(() => process.chdir('./1'))
102103
.then(() => {
103104
process.env.CWD = process.cwd();
@@ -112,13 +113,13 @@ describe('Acceptance: ng generate pipe', function () {
112113
it('ng generate pipe ' + path.sep + 'my-pipe from a child dir, gens under ' +
113114
path.join('src', 'client', 'app'),
114115
() => {
116+
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'client', 'app', '1'));
115117
return new Promise(function (resolve) {
116118
process.chdir('./src');
117119
resolve();
118120
})
119121
.then(() => process.chdir('./client'))
120122
.then(() => process.chdir('./app'))
121-
.then(() => fs.mkdirsSync('./1'))
122123
.then(() => process.chdir('./1'))
123124
.then(() => {
124125
process.env.CWD = process.cwd();

0 commit comments

Comments
 (0)