Skip to content

Commit 01b4c48

Browse files
committed
feat(component): add less support
- Add optional support for less via `style` option
1 parent ba4f81b commit 01b4c48

File tree

6 files changed

+151
-7
lines changed

6 files changed

+151
-7
lines changed
+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var stringUtils = require('ember-cli/lib/utilities/string');
2+
var fs = require('fs');
23

34
module.exports = {
4-
description: ''
5+
description: 'Generates a component.',
56

67
//locals: function(options) {
78
// // Return custom template variables here.
@@ -10,7 +11,10 @@ module.exports = {
1011
// };
1112
//}
1213

13-
// afterInstall: function(options) {
14-
// // Perform extra work here.
15-
// }
14+
afterInstall: function(options) {
15+
switch (options.style) {
16+
case 'less':
17+
fs.renameSync(options.project.root + '/src/app/components/' + options.args[1] + '/' + options.args[1] + '.css', options.project.root + '/src/app/components/' + options.args[1] + '/' + options.args[1] + '.less');
18+
}
19+
}
1620
};

addon/ng2/commands/generate.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
var GenerateCommand = require('ember-cli/lib/commands/generate');
4+
5+
module.exports = GenerateCommand.extend({
6+
availableOptions: [
7+
{ name: 'dry-run', type: Boolean, default: false, aliases: ['d'] },
8+
{ name: 'verbose', type: Boolean, default: false, aliases: ['v'] },
9+
{ name: 'blueprint', type: String, default: 'ng2', aliases: ['b'] },
10+
{ name: 'name', type: String, default: '', aliases: ['n'] },
11+
{ name: 'style', type: String, default: 'css', aliases: ['s'] }
12+
],
13+
14+
_defaultBlueprint: function() {
15+
if (this.project.isEmberCLIAddon()) {
16+
return 'addon';
17+
} else {
18+
return 'ng2';
19+
}
20+
},
21+
});
22+
23+
module.exports.overrideCore = true;

addon/ng2/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module.exports = {
66
includedCommands: function() {
77
return {
88
'new': require('./commands/new'),
9-
'init': require('./commands/init')
9+
'init': require('./commands/init'),
10+
'generate': require('./commands/generate')
1011
};
1112
}
1213
};

lib/broccoli/angular2-app.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Concat = require('broccoli-concat');
22
var configReplace = require('./broccoli-config-replace');
33
var compileWithTypescript = require('./broccoli-typescript').default;
4+
var compileLess = require('broccoli-less');
45
var fs = require('fs');
56
var Funnel = require('broccoli-funnel');
67
var mergeTrees = require('broccoli-merge-trees');
@@ -45,7 +46,7 @@ Angular2App.prototype.toTree = function () {
4546
'systemjs/dist/system.src.js'
4647
],
4748
destDir: 'vendor'
48-
})
49+
});
4950

5051
// var appJs = new Concat(mergeTrees([tsTree, jsTree]), {
5152
// inputFiles: [
@@ -55,7 +56,13 @@ Angular2App.prototype.toTree = function () {
5556
// outputFile: '/app.js'
5657
// });
5758

58-
return mergeTrees([assetTree, tsSrcTree, tsTree, jsTree, this.index(), vendorJsTree], { overwrite: true });
59+
var lessTree = new Funnel(sourceTree, {
60+
include: ['**/*.less'],
61+
allowEmpty: true
62+
});
63+
var lessSrcTree = compileLess(lessTree);
64+
65+
return mergeTrees([assetTree, tsSrcTree, tsTree, jsTree, this.index(), lessSrcTree, vendorJsTree], { overwrite: true });
5966
};
6067

6168
/**

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"broccoli": "^0.16.3",
3333
"broccoli-concat": "0.0.13",
3434
"broccoli-funnel": "^1.0.0",
35+
"broccoli-less": "^2.1.0",
3536
"broccoli-merge-trees": "^1.0.0",
3637
"broccoli-writer": "^0.1.1",
3738
"chalk": "^1.1.0",

tests/acceptance/generate.spec.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
'use strict';
2+
3+
var ng = require('../helpers/ng');
4+
var expect = require('chai').expect;
5+
var Blueprint = require('ember-cli/lib/models/blueprint');
6+
var path = require('path');
7+
var tmp = require('../helpers/tmp');
8+
var root = process.cwd();
9+
var conf = require('ember-cli/tests/helpers/conf');
10+
var forEach = require('lodash/collection/forEach');
11+
var existsSync = require('exists-sync');
12+
13+
var defaultIgnoredFiles = Blueprint.ignoredFiles;
14+
15+
describe('Acceptance: ng generate', function() {
16+
this.timeout(20000);
17+
18+
before(function() {
19+
conf.setup();
20+
});
21+
22+
after(function() {
23+
conf.restore();
24+
});
25+
26+
beforeEach(function() {
27+
Blueprint.ignoredFiles = defaultIgnoredFiles;
28+
29+
return tmp.setup('./tmp')
30+
.then(function() {
31+
process.chdir('./tmp');
32+
}).then(function() {
33+
return ng([
34+
'new',
35+
'foo',
36+
'--skip-npm',
37+
'--skip-bower',
38+
'--skip-git'
39+
]);
40+
});
41+
});
42+
43+
afterEach(function() {
44+
return tmp.teardown('./tmp');
45+
});
46+
47+
function confirmBlueprinted(files) {
48+
forEach(files, function(file) {
49+
expect(existsSync(file));
50+
});
51+
}
52+
53+
function confirmBlueprintedComponent(settings) {
54+
settings = settings || {
55+
style: 'css'
56+
};
57+
58+
var componentPath = path.join(process.cwd(), 'src/app/components/bar');
59+
60+
var files = [
61+
path.join(componentPath, 'bar.' + settings.style),
62+
path.join(componentPath, 'bar.html'),
63+
path.join(componentPath, 'bar.ts'),
64+
path.join(componentPath, 'bar.spec.ts')
65+
];
66+
67+
confirmBlueprinted(files);
68+
}
69+
70+
function confirmBlueprintedService() {
71+
process.chdir('./src/app/services/bar');
72+
73+
var servicePath = path.join(process.cwd(), 'src/app/services/bar');
74+
75+
var files = [
76+
path.join(servicePath, 'bar.ts'),
77+
path.join(servicePath, 'bar.spec.ts')
78+
];
79+
80+
confirmBlueprinted(files);
81+
}
82+
83+
it('component', function() {
84+
return ng([
85+
'generate',
86+
'component',
87+
'bar'
88+
]).then(confirmBlueprintedComponent());
89+
});
90+
91+
it('component with less', function() {
92+
return ng([
93+
'generate',
94+
'component',
95+
'bar',
96+
'--style=less'
97+
]).then(confirmBlueprintedComponent({style: 'less'}));
98+
});
99+
100+
it('services', function() {
101+
return ng([
102+
'generate',
103+
'service',
104+
'bar'
105+
]).then(confirmBlueprintedService);
106+
});
107+
108+
});

0 commit comments

Comments
 (0)