Skip to content

Commit f602be4

Browse files
committed
chore: generation paths are fully dynamic & supports --flat for components/directives/pipes/services
This is a step in aligning with the style guide and will provide the flexibility needed Related to #316
1 parent 673b79c commit f602be4

File tree

18 files changed

+93
-41
lines changed

18 files changed

+93
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
import {provide} from 'angular2/core';
1313
import {<%= classifiedModuleName %>} from './<%= dasherizedModuleName %>';
1414

15-
1615
describe('<%= classifiedModuleName %> Component', () => {
1716

1817
beforeEachProviders((): any[] => []);

addon/ng2/blueprints/component/index.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
var path = require('path');
12
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
23

34
module.exports = {
45
description: '',
6+
7+
availableOptions: [
8+
{ name: 'flat', type: Boolean, default: false, aliases: ['f'] }
9+
],
510

611
normalizeEntityName: function (entityName) {
712
var parsedPath = dynamicPathParser(this.project, entityName);
@@ -10,20 +15,22 @@ module.exports = {
1015
return parsedPath.name;
1116
},
1217

13-
locals: function () {
18+
locals: function (options) {
1419
return {
15-
dynamicPath: this.dynamicPath.dir
20+
dynamicPath: this.dynamicPath.dir.replace(this.dynamicPath.appRoot, ''),
21+
flat: options.flat
1622
};
1723
},
1824

19-
fileMapTokens: function () {
25+
fileMapTokens: function (options) {
2026
// Return custom template variables here.
2127
return {
22-
__name__: () => {
23-
return this.dynamicPath.name;
24-
},
2528
__path__: () => {
26-
return this.dynamicPath.dir;
29+
var dir = this.dynamicPath.dir;
30+
if (!options.locals.flat) {
31+
dir += path.sep + options.dasherizedModuleName;
32+
}
33+
return dir;
2734
}
2835
};
2936
}

addon/ng2/blueprints/directive/index.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
var path = require('path');
12
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
23

34
module.exports = {
45
description: '',
6+
7+
availableOptions: [
8+
{ name: 'flat', type: Boolean, default: false, aliases: ['f'] }
9+
],
510

611
normalizeEntityName: function (entityName) {
712
var parsedPath = dynamicPathParser(this.project, entityName);
@@ -10,20 +15,22 @@ module.exports = {
1015
return parsedPath.name;
1116
},
1217

13-
locals: function () {
18+
locals: function (options) {
1419
return {
15-
dynamicPath: this.dynamicPath.dir
20+
dynamicPath: this.dynamicPath.dir,
21+
flat: options.flat
1622
};
1723
},
1824

19-
fileMapTokens: function () {
25+
fileMapTokens: function (options) {
2026
// Return custom template variables here.
2127
return {
22-
__name__: () => {
23-
return this.dynamicPath.name;
24-
},
2528
__path__: () => {
26-
return this.dynamicPath.dir;
29+
var dir = this.dynamicPath.dir;
30+
if (!options.locals.flat) {
31+
dir += path.sep + options.dasherizedModuleName;
32+
}
33+
return dir;
2734
}
2835
};
2936
}

addon/ng2/blueprints/pipe/index.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
var path = require('path');
12
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
23

34
module.exports = {
45
description: '',
6+
7+
availableOptions: [
8+
{ name: 'flat', type: Boolean, default: false, aliases: ['f'] }
9+
],
510

611
normalizeEntityName: function (entityName) {
712
var parsedPath = dynamicPathParser(this.project, entityName);
@@ -10,18 +15,22 @@ module.exports = {
1015
return parsedPath.name;
1116
},
1217

13-
locals: function () {
14-
return { dynamicPath: this.dynamicPath.dir };
18+
locals: function (options) {
19+
return {
20+
dynamicPath: this.dynamicPath.dir,
21+
flat: options.flat
22+
};
1523
},
1624

17-
fileMapTokens: function () {
25+
fileMapTokens: function (options) {
1826
// Return custom template variables here.
1927
return {
20-
__name__: () => {
21-
return this.dynamicPath.name;
22-
},
2328
__path__: () => {
24-
return this.dynamicPath.dir;
29+
var dir = this.dynamicPath.dir;
30+
if (!options.locals.flat) {
31+
dir += path.sep + options.dasherizedModuleName;
32+
}
33+
return dir;
2534
}
2635
};
2736
}

addon/ng2/blueprints/route-config/index.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
var fs = require('fs-extra');
22
var path = require('path');
3+
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
34

45
var imports, routeDefinitions;
56

67
module.exports = {
78
description: 'Registers the route with the router.',
89

9-
locals: function (options) {
10-
return generateLocals.call(this, options);
10+
normalizeEntityName: function (entityName) {
11+
var parsedPath = dynamicPathParser(this.project, 'ignore');
12+
13+
this.dynamicPath = parsedPath;
14+
return parsedPath.name;
1115
},
1216

1317
beforeInstall: function (options) {
@@ -17,6 +21,19 @@ module.exports = {
1721
} catch (e) {
1822
// doing nothing here
1923
}
24+
},
25+
26+
locals: function (options) {
27+
return generateLocals.call(this, options);
28+
},
29+
30+
fileMapTokens: function (options) {
31+
// Return custom template variables here.
32+
return {
33+
__path__: () => {
34+
return this.dynamicPath.dir;
35+
}
36+
};
2037
}
2138
};
2239

addon/ng2/blueprints/service/index.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
var path = require('path');
12
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
23

34
module.exports = {
45
description: '',
6+
7+
availableOptions: [
8+
{ name: 'flat', type: Boolean, default: false, aliases: ['f'] }
9+
],
510

611
normalizeEntityName: function (entityName) {
712
var parsedPath = dynamicPathParser(this.project, entityName);
@@ -10,18 +15,22 @@ module.exports = {
1015
return parsedPath.name;
1116
},
1217

13-
locals: function () {
14-
return { dynamicPath: this.dynamicPath.dir };
18+
locals: function (options) {
19+
return {
20+
dynamicPath: this.dynamicPath.dir,
21+
flat: options.flat
22+
};
1523
},
1624

17-
fileMapTokens: function () {
25+
fileMapTokens: function (options) {
1826
// Return custom template variables here.
1927
return {
20-
__name__: () => {
21-
return this.dynamicPath.name;
22-
},
2328
__path__: () => {
24-
return this.dynamicPath.dir;
29+
var dir = this.dynamicPath.dir;
30+
if (!options.locals.flat) {
31+
dir += path.sep + options.dasherizedModuleName;
32+
}
33+
return dir;
2534
}
2635
};
2736
}

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ var process = require('process');
33

44
module.exports = function dynamicPathParser(project, entityName) {
55
var projectRoot = project.root;
6+
var appRoot = path.join('src', 'client', 'app');
67
var cwd = process.env.PWD;
78

8-
var rootPath = path.join(projectRoot, 'src', 'client', 'app');
9+
var rootPath = path.join(projectRoot, appRoot);
910

1011
var outputPath = path.join(rootPath, entityName);
1112

@@ -31,14 +32,15 @@ module.exports = function dynamicPathParser(project, entityName) {
3132

3233
if (outputPath.indexOf(rootPath) < 0) {
3334
throw `Invalid path: "${entityName}" cannot be ` +
34-
`above the "${path.join('src', 'app')}" directory`;
35+
`above the "${appRoot}" directory`;
3536
}
3637

37-
var adjustedPath = outputPath.replace(rootPath, '');
38+
var adjustedPath = outputPath.replace(projectRoot, '');
3839

3940
var parsedPath = path.parse(adjustedPath);
4041

4142
parsedPath.dir = parsedPath.dir === path.sep ? '' : parsedPath.dir;
43+
parsedPath.appRoot = appRoot
4244

4345
return parsedPath;
4446
};

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var expect = require('chai').expect;
44
var path = require('path');
55
var dynamicPathParser = require('../../addon/ng2/utilities/dynamic-path-parser');
66

7+
var appDir = `${path.sep}src${path.sep}client${path.sep}app`;
8+
79
describe('dynamic path parser', () => {
810
var project;
911
var entityName = 'temp-name';
@@ -14,50 +16,50 @@ describe('dynamic path parser', () => {
1416
it('parse from proj root dir', () => {
1517
process.env.PWD = process.cwd();
1618
var result = dynamicPathParser(project, entityName);
17-
expect(result.dir).to.equal('');
19+
expect(result.dir).to.equal(appDir);
1820
expect(result.name).to.equal(entityName);
1921
});
2022

2123
it('parse from proj src dir', () => {
2224
process.env.PWD = path.join(process.cwd(), 'src');
2325
var result = dynamicPathParser(project, entityName);
24-
expect(result.dir).to.equal('');
26+
expect(result.dir).to.equal(appDir);
2527
expect(result.name).to.equal(entityName);
2628
});
2729

2830
it(`parse from proj src${path.sep}client dir`, () => {
2931
process.env.PWD = path.join(process.cwd(), 'src', 'client');
3032
var result = dynamicPathParser(project, entityName);
31-
expect(result.dir).to.equal('');
33+
expect(result.dir).to.equal(appDir);
3234
expect(result.name).to.equal(entityName);
3335
});
3436

3537
it(`parse from proj src${path.sep}client${path.sep}app dir`, () => {
3638
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app');
3739
var result = dynamicPathParser(project, entityName);
38-
expect(result.dir).to.equal('');
40+
expect(result.dir).to.equal(appDir);
3941
expect(result.name).to.equal(entityName);
4042
});
4143

4244
it(`parse from proj src${path.sep}client${path.sep}app${path.sep}child-dir`, () => {
4345
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app', 'child-dir');
4446
var result = dynamicPathParser(project, entityName);
45-
expect(result.dir).to.equal(`${path.sep}child-dir`);
47+
expect(result.dir).to.equal(`${appDir}${path.sep}child-dir`);
4648
expect(result.name).to.equal(entityName);
4749
});
4850

4951
it(`parse from proj src${path.sep}client${path.sep}app${path.sep}child-dir w/ ..${path.sep}`, () => {
5052
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app', 'child-dir');
5153
var result = dynamicPathParser(project, '..' + path.sep + entityName);
52-
expect(result.dir).to.equal('');
54+
expect(result.dir).to.equal(appDir);
5355
expect(result.name).to.equal(entityName);
5456
});
5557

5658
it(`parse from proj src${path.sep}client${path.sep}app${path.sep}child-dir${path.sep}grand-child-dir w/ ..${path.sep}`,
5759
() => {
5860
process.env.PWD = path.join(process.cwd(), 'src', 'client', 'app', 'child-dir', 'grand-child-dir');
5961
var result = dynamicPathParser(project, '..' + path.sep + entityName);
60-
expect(result.dir).to.equal(`${path.sep}child-dir`);
62+
expect(result.dir).to.equal(`${appDir}${path.sep}child-dir`);
6163
expect(result.name).to.equal(entityName);
6264
});
6365
});

0 commit comments

Comments
 (0)