Skip to content

Commit 098f03d

Browse files
committed
fix(): fixed paths internal in files
The paths/url references inside files were not changed to allow for dynamic paths Added the ability to utlize dynamic paths from within the project structure
1 parent 17cb4da commit 098f03d

File tree

8 files changed

+254
-14
lines changed

8 files changed

+254
-14
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ You can use the `ng generate` (or just `ng g`) command to generate Angular compo
7070
```bash
7171
ng generate component my-new-component
7272
ng g component my-new-component # using the alias
73+
74+
# components support relative path generation
75+
# if in the directory src/app/feature/ and you run
76+
ng g component new-cmp
77+
# your component will be generated in src/app/feature/new-cmp
78+
# but if you were to run
79+
ng g component ../newer-cmp
80+
# your component will be generated in src/app/newer-cmp
7381
```
7482
You can find all possible blueprints in the table below:
7583

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
var component = require('../component');
2-
3-
module.exports = component;
1+
module.exports = {
2+
description: '',
3+
4+
// ******************************************************
5+
// ******************************************************
6+
// LEAVE THIS HERE
7+
// Must override install to prevent ember's component tests
8+
// ******************************************************
9+
// ******************************************************
10+
11+
install: function(options){}
12+
};

addon/ng2/blueprints/component/files/src/app/__path__/__name__/__name__.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {Component} from 'angular2/core';
33

44
@Component({
55
selector: '<%= dasherizedModuleName %>',
6-
templateUrl: 'app/components/<%= dasherizedModuleName %>/<%= dasherizedModuleName %>.html',
7-
styleUrls: ['app/components/<%= dasherizedModuleName %>/<%= dasherizedModuleName %>.css'],
6+
templateUrl: 'app/<%= dynamicPath %>/<%= dasherizedModuleName %>/<%= dasherizedModuleName %>.html',
7+
styleUrls: ['app/<%= dynamicPath %>/<%= dasherizedModuleName %>/<%= dasherizedModuleName %>.css'],
88
providers: [],
99
directives: [],
1010
pipes: []

addon/ng2/blueprints/component/index.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,57 @@
11
var stringUtils = require('ember-cli/lib/utilities/string');
22
var path = require('path');
3+
var process = require('process');
34

45
module.exports = {
56
description: '',
6-
7-
locals: function(options) {
8-
var parsedPath = path.parse(options.entity.name);
9-
// build the locals for generating the name & path
10-
return {
7+
8+
normalizeEntityName: function(entityName){
9+
var cwd = this.project.cli.testing
10+
? process.cwd()
11+
: process.env.PWD;
12+
13+
var rootPath = path.join(this.project.root, 'src', 'app');
14+
15+
var outputPath = path.join(rootPath, entityName);
16+
17+
if (cwd.indexOf(rootPath) >= 0) {
18+
outputPath = path.join(cwd, entityName);
19+
} else if (cwd.indexOf(path.join(this.project.root, 'src')) >= 0
20+
&& entityName.indexOf('app') === 0) {
21+
outputPath = path.join(cwd, entityName);
22+
} else if (cwd.indexOf(path.join(this.project.root, 'src')) >= 0) {
23+
outputPath = path.join(cwd, 'app', entityName);
24+
}
25+
26+
if (outputPath.indexOf(rootPath) < 0) {
27+
throw `Invalid component path: "${entityName}" cannot be ` +
28+
`above the "${path.join('src', 'app')}" directory`;
29+
}
30+
31+
var adjustedPath = outputPath.replace(rootPath, '');
32+
33+
var parsedPath = path.parse(adjustedPath);
34+
this.dynamicPath = {
1135
name: parsedPath.name,
1236
path: parsedPath.dir
1337
};
38+
return parsedPath.name;
39+
},
40+
41+
locals: function(options){
42+
return {
43+
dynamicPath: this.dynamicPath.path
44+
}
1445
},
1546

1647
fileMapTokens: function(options) {
1748
// Return custom template variables here.
1849
return {
19-
__name__: function(options) {
20-
return options.locals.name;
50+
__name__: (options) => {
51+
return this.dynamicPath.name;
2152
},
22-
__path__: function(options) {
23-
return options.locals.path || 'components';
53+
__path__: (options) => {
54+
return this.dynamicPath.path;
2455
}
2556
};
2657
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {
2+
it,
3+
iit,
4+
describe,
5+
ddescribe,
6+
expect,
7+
inject,
8+
injectAsync,
9+
TestComponentBuilder,
10+
beforeEachProviders
11+
} from 'angular2/testing';
12+
import {provide} from 'angular2/core';
13+
import {<%= classifiedModuleName %>} from './<%= dasherizedModuleName %>';
14+
15+
16+
describe('<%= classifiedModuleName %> Service', () => {
17+
18+
beforeEachProviders(() => [<%= classifiedModuleName %>]);
19+
20+
21+
it('should ...', inject([<%= classifiedModuleName %>], (service:<%= classifiedModuleName %>) => {
22+
23+
}));
24+
25+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {Injectable} from 'angular2/core';
2+
3+
4+
@Injectable()
5+
export class <%= classifiedModuleName %> {
6+
7+
constructor() {}
8+
9+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
'use strict';
2+
3+
var fs = require('fs-extra');
4+
var ng = require('../helpers/ng');
5+
var existsSync = require('exists-sync');
6+
var expect = require('chai').expect;
7+
var forEach = require('lodash/collection/forEach');
8+
var walkSync = require('walk-sync');
9+
var Blueprint = require('ember-cli/lib/models/blueprint');
10+
var path = require('path');
11+
var tmp = require('../helpers/tmp');
12+
var root = process.cwd();
13+
var util = require('util');
14+
var conf = require('ember-cli/tests/helpers/conf');
15+
var EOL = require('os').EOL;
16+
var path = require('path');
17+
var Promise = require('ember-cli/lib/ext/promise');
18+
19+
describe('Acceptance: ng generate component', function() {
20+
before(conf.setup);
21+
22+
after(conf.restore);
23+
24+
beforeEach(function() {
25+
return tmp.setup('./tmp')
26+
.then(function() {
27+
process.chdir('./tmp');
28+
})
29+
.then(function(){
30+
return ng([
31+
'new',
32+
'foo',
33+
'--skip-npm',
34+
'--skip-bower'
35+
]);
36+
});
37+
});
38+
39+
afterEach(function() {
40+
this.timeout(10000);
41+
42+
// return tmp.teardown('./tmp');
43+
});
44+
45+
it('ng generate component my-comp', function() {
46+
return ng([
47+
'generate',
48+
'component',
49+
'my-comp'
50+
])
51+
.then(_ => {
52+
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'my-comp', 'my-comp.ts');
53+
expect(existsSync(testPath)).to.equal(true);
54+
});
55+
});
56+
57+
it('ng generate component test/my-comp', function() {
58+
return ng([
59+
'generate',
60+
'component',
61+
'test/my-comp'
62+
])
63+
.then(_ => {
64+
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'test', 'my-comp', 'my-comp.ts');
65+
expect(existsSync(testPath)).to.equal(true);
66+
});
67+
});
68+
69+
it('ng generate component test/../my-comp', function() {
70+
return ng([
71+
'generate',
72+
'component',
73+
'test/../my-comp'
74+
])
75+
.then(_ => {
76+
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'my-comp', 'my-comp.ts');
77+
expect(existsSync(testPath)).to.equal(true);
78+
});
79+
});
80+
81+
it('ng generate component my-comp from a child directory', () => {
82+
return new Promise(function (resolve, reject) {
83+
process.chdir('./src');
84+
resolve();
85+
})
86+
.then(_ => process.chdir('./app'))
87+
.then(_ => fs.mkdirsSync('./1'))
88+
.then(_ => process.chdir('./1'))
89+
.then(_ => {
90+
process.env.CWD = process.cwd();
91+
return ng([
92+
'generate',
93+
'component',
94+
'my-comp'
95+
])
96+
})
97+
.then(_ => {
98+
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'my-comp', 'my-comp.ts');
99+
expect(existsSync(testPath)).to.equal(true);
100+
}, err => console.log('ERR: ', err));
101+
});
102+
103+
it('ng generate component child-dir/my-comp from a child directory', () => {
104+
return new Promise(function (resolve, reject) {
105+
process.chdir('./src');
106+
resolve();
107+
})
108+
.then(_ => process.chdir('./app'))
109+
.then(_ => fs.mkdirsSync('./1'))
110+
.then(_ => process.chdir('./1'))
111+
.then(_ => {
112+
process.env.CWD = process.cwd();
113+
return ng([
114+
'generate',
115+
'component',
116+
'child-dir/my-comp'
117+
])
118+
})
119+
.then(_ => {
120+
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'child-dir', 'my-comp', 'my-comp.ts');
121+
expect(existsSync(testPath)).to.equal(true);
122+
}, err => console.log('ERR: ', err));
123+
});
124+
125+
it('ng generate component child-dir/../my-comp from a child directory', () => {
126+
return new Promise(function (resolve, reject) {
127+
process.chdir('./src');
128+
resolve();
129+
})
130+
.then(_ => process.chdir('./app'))
131+
.then(_ => fs.mkdirsSync('./1'))
132+
.then(_ => process.chdir('./1'))
133+
.then(_ => {
134+
process.env.CWD = process.cwd();
135+
return ng([
136+
'generate',
137+
'component',
138+
'child-dir/../my-comp'
139+
])
140+
})
141+
.then(_ => {
142+
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'my-comp', 'my-comp.ts');
143+
expect(existsSync(testPath)).to.equal(true);
144+
}, err => console.log('ERR: ', err));
145+
});
146+
147+
it('ng generate component ../my-comp from root directory will fail', () => {
148+
return ng([
149+
'generate',
150+
'component',
151+
'../my-comp'
152+
])
153+
.then(_ => {
154+
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', '..', 'my-comp', 'my-comp.ts');
155+
expect(existsSync(testPath)).to.equal(false);
156+
});
157+
});
158+
});

0 commit comments

Comments
 (0)