Skip to content

Commit c90733e

Browse files
committed
Allow to require and apply rules
1 parent 8a89c01 commit c90733e

3 files changed

Lines changed: 51 additions & 15 deletions

File tree

.csscomb.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"colon-space": true,
77
"rule-indent": true,
88
"stick-brace": "\n",
9+
"strip-spaces": true,
910
"sort-order": [
1011
[
1112
"position",

lib/cli.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ if (fs.existsSync(configPath)) {
2323
if (program.args.length > 0) {
2424
vow.all(program.args.map(function(path) {
2525
return comb.processPath(path);
26-
})).then(function(results) {
27-
console.log('results: ' + results);
28-
}).fail(function(e) {
26+
})).fail(function(e) {
2927
console.log('stack: ', e.stack);
3028
process.exit(1);
3129
});

lib/csscomb.js

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var cssp = require('cssp');
22
var minimatch = require('minimatch');
33
var vow = require('vow');
4-
var vowFs = require('vow-fs');
4+
var vfs = require('vow-fs');
55

66
/**
77
* Starts Code Style processing process.
@@ -12,8 +12,9 @@ var Comb = function() {
1212
this._rules = {
1313
'colon-space': {},
1414
'rule-indent': {},
15+
'sort-order': {},
1516
'stick-brace': {},
16-
'sort-order': {}
17+
'strip-spaces': {}
1718
},
1819
this._config = {};
1920
this._exclude = null;
@@ -29,15 +30,51 @@ Comb.prototype = {
2930
*/
3031
configure: function(config) {
3132
for (var rule in config) {
32-
if (config.hasOwnProperty(rule) && this._rules[rule]) {
33-
this._config[rule] = config[rule];
33+
if (config.hasOwnProperty(rule) && config[rule] && this._rules[rule]) {
34+
var beautifier;
35+
try {
36+
beautifier = require('./rules/' + rule);
37+
beautifier.value = config[rule];
38+
this._config[rule] = beautifier;
39+
} catch (e) {}
3440
}
3541
}
3642
this._exclude = (config.exclude || []).map(function(pattern) {
3743
return new minimatch.Minimatch(pattern);
3844
});
3945
},
4046

47+
/**
48+
* Processes stylesheet tree node.
49+
*
50+
* @param {Array} tree Parsed tree
51+
* @returns {Array}
52+
*/
53+
processTree: function(tree) {
54+
this.processNode(['tree', tree]);
55+
return tree;
56+
},
57+
58+
/**
59+
* Processes tree node.
60+
* @param {Array} node Tree node
61+
*/
62+
processNode: function(node, level) {
63+
var _this = this;
64+
var config = this._config;
65+
node.forEach(function(node) {
66+
if (!Array.isArray(node)) return;
67+
var nodeType = node.shift();
68+
for (var rule in config) {
69+
if (config.hasOwnProperty(rule)) {
70+
config[rule].process(nodeType, node);
71+
}
72+
}
73+
node.unshift(nodeType);
74+
_this.processNode(node, level);
75+
});
76+
},
77+
4178
/**
4279
* Process file provided with a string.
4380
* @param {String} text
@@ -46,11 +83,11 @@ Comb.prototype = {
4683
processString: function(text, filename) {
4784
var tree;
4885
try {
49-
tree = cssp.parse(text);
86+
tree = this.processTree(cssp.parse(text));
5087
} catch (e) {
5188
throw new Error('Parsing error at ' + filename + ': ' + e.message);
5289
}
53-
text = cssp.translate(cssp.transform(tree));
90+
return cssp.translate(cssp.transform(tree));
5491
},
5592

5693
/**
@@ -62,8 +99,8 @@ Comb.prototype = {
6299
processFile: function(path) {
63100
var _this = this;
64101
if (this._shouldProcess(path) && path.match(/\.css$/)) {
65-
return vowFs.read(path, 'utf8').then(function(data) {
66-
return _this.processString(data, path);
102+
return vfs.read(path, 'utf8').then(function(data) {
103+
return vfs.write(path, _this.processString(data, path), 'utf8');
67104
});
68105
}
69106
return null;
@@ -77,10 +114,10 @@ Comb.prototype = {
77114
*/
78115
processDirectory: function(path) {
79116
var _this = this;
80-
return vowFs.listDir(path).then(function(filenames) {
117+
return vfs.listDir(path).then(function(filenames) {
81118
return vow.all(filenames.map(function(filename) {
82119
var fullname = path + '/' + filename;
83-
return vowFs.stat(fullname).then(function(stat) {
120+
return vfs.stat(fullname).then(function(stat) {
84121
if (_this._shouldProcess(fullname)) {
85122
if (stat.isDirectory()) {
86123
return _this.processDirectory(fullname);
@@ -106,9 +143,9 @@ Comb.prototype = {
106143
processPath: function(path) {
107144
path = path.replace(/\/$/, '');
108145
var _this = this;
109-
return vowFs.exists(path).then(function(exists) {
146+
return vfs.exists(path).then(function(exists) {
110147
if (exists) {
111-
return vowFs.stat(path).then(function(stat) {
148+
return vfs.stat(path).then(function(stat) {
112149
if (stat.isDirectory()) {
113150
return _this.processDirectory(path);
114151
} else {

0 commit comments

Comments
 (0)