Skip to content

Commit 09193e4

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 Closes angular#270
1 parent 9759090 commit 09193e4

File tree

8 files changed

+278
-14
lines changed

8 files changed

+278
-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: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,59 @@
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 (entityName.indexOf(path.sep) === 0) {
18+
outputPath = path.join(rootPath, entityName.substr(1));
19+
} else if (cwd.indexOf(rootPath) >= 0) {
20+
outputPath = path.join(cwd, entityName);
21+
} else if (cwd.indexOf(path.join(this.project.root, 'src')) >= 0
22+
&& entityName.indexOf('app') === 0) {
23+
outputPath = path.join(cwd, entityName);
24+
} else if (cwd.indexOf(path.join(this.project.root, 'src')) >= 0) {
25+
outputPath = path.join(cwd, 'app', entityName);
26+
}
27+
28+
if (outputPath.indexOf(rootPath) < 0) {
29+
throw `Invalid component path: "${entityName}" cannot be ` +
30+
`above the "${path.join('src', 'app')}" directory`;
31+
}
32+
33+
var adjustedPath = outputPath.replace(rootPath, '');
34+
35+
var parsedPath = path.parse(adjustedPath);
36+
this.dynamicPath = {
1137
name: parsedPath.name,
1238
path: parsedPath.dir
1339
};
40+
return parsedPath.name;
41+
},
42+
43+
locals: function(options){
44+
return {
45+
dynamicPath: this.dynamicPath.path
46+
}
1447
},
1548

1649
fileMapTokens: function(options) {
1750
// Return custom template variables here.
1851
return {
19-
__name__: function(options) {
20-
return options.locals.name;
52+
__name__: (options) => {
53+
return this.dynamicPath.name;
2154
},
22-
__path__: function(options) {
23-
return options.locals.path || 'components';
55+
__path__: (options) => {
56+
return this.dynamicPath.path;
2457
}
2558
};
2659
}
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: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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' + path.sep + 'my-comp', function() {
58+
return ng([
59+
'generate',
60+
'component',
61+
'test' + path.sep + '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' + path.sep + '..' + path.sep + 'my-comp', function() {
70+
return ng([
71+
'generate',
72+
'component',
73+
'test' + path.sep + '..' + path.sep + '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 dir', () => {
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' + path.sep + 'my-comp from a child dir', () => {
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' + path.sep + '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' + path.sep + '..' + path.sep + 'my-comp from a child dir', () => {
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' + path.sep + '..' + path.sep + '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 ' + path.sep + 'my-comp from a child dir, gens under ' + path.join('src', 'app'), () => {
148+
return new Promise(function (resolve, reject) {
149+
process.chdir('./src');
150+
resolve();
151+
})
152+
.then(_ => process.chdir('./app'))
153+
.then(_ => fs.mkdirsSync('./1'))
154+
.then(_ => process.chdir('./1'))
155+
.then(_ => {
156+
process.env.CWD = process.cwd();
157+
return ng([
158+
'generate',
159+
'component',
160+
path.sep + 'my-comp'
161+
])
162+
})
163+
.then(_ => {
164+
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'my-comp', 'my-comp.ts');
165+
expect(existsSync(testPath)).to.equal(true);
166+
}, err => console.log('ERR: ', err));
167+
});
168+
169+
it('ng generate component ..' + path.sep + 'my-comp from root dir will fail', () => {
170+
return ng([
171+
'generate',
172+
'component',
173+
'..' + path.sep + 'my-comp'
174+
])
175+
.then(_ => {
176+
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', '..', 'my-comp', 'my-comp.ts');
177+
expect(existsSync(testPath)).to.equal(false);
178+
});
179+
});
180+
});

0 commit comments

Comments
 (0)