Skip to content

Commit 8a89c01

Browse files
committed
Parse/serialize: as-is processing
1 parent 6eb13e2 commit 8a89c01

4 files changed

Lines changed: 136 additions & 8 deletions

File tree

.csscomb.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"exclude": [
3+
".git/**",
4+
"node_modules/**"
5+
],
26
"colon-space": true,
37
"rule-indent": true,
48
"stick-brace": "\n",

lib/cli.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,32 @@
44
* Usage example:
55
* ./node_modules/.bin/csscomb file1 [dir1 [fileN [dirN]]]
66
*/
7-
var fs = require('fs'),
8-
program = require('commander');
7+
var fs = require('fs');
8+
var program = require('commander');
9+
var vow = require('vow');
10+
var Comb = require('./csscomb');
911

1012
program
1113
.version(require('../package.json').version)
1214
.usage('[options] <file ...>')
1315
.option('-c, --config [path]', 'configuration file path')
1416
.parse(process.argv);
1517

16-
var Comb = require('./csscomb'),
17-
comb = new Comb(),
18-
configPath = program.config || (process.cwd() + '/.csscomb.json');
18+
var comb = new Comb();
19+
var configPath = program.config || (process.cwd() + '/.csscomb.json');
1920

2021
if (fs.existsSync(configPath)) {
2122
comb.configure(require(configPath));
23+
if (program.args.length > 0) {
24+
vow.all(program.args.map(function(path) {
25+
return comb.processPath(path);
26+
})).then(function(results) {
27+
console.log('results: ' + results);
28+
}).fail(function(e) {
29+
console.log('stack: ', e.stack);
30+
process.exit(1);
31+
});
32+
}
2233
} else {
2334
console.log('Configuration file ' + configPath + ' was not found.');
2435
process.exit(1);

lib/csscomb.js

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
var cssp = require('cssp');
2+
var minimatch = require('minimatch');
3+
var vow = require('vow');
4+
var vowFs = require('vow-fs');
5+
16
/**
2-
* Starts Code Style checking process.
7+
* Starts Code Style processing process.
38
*
49
* @name Comb
510
*/
@@ -11,13 +16,14 @@ var Comb = function() {
1116
'sort-order': {}
1217
},
1318
this._config = {};
14-
this._excludes = null;
19+
this._exclude = null;
1520
};
1621

1722
Comb.prototype = {
1823

1924
/**
2025
* Loads configuration from JSON.
26+
* Activates and configures required rules.
2127
*
2228
* @param {Object} config
2329
*/
@@ -27,6 +33,109 @@ Comb.prototype = {
2733
this._config[rule] = config[rule];
2834
}
2935
}
36+
this._exclude = (config.exclude || []).map(function(pattern) {
37+
return new minimatch.Minimatch(pattern);
38+
});
39+
},
40+
41+
/**
42+
* Process file provided with a string.
43+
* @param {String} text
44+
* @param {String} filename
45+
*/
46+
processString: function(text, filename) {
47+
var tree;
48+
try {
49+
tree = cssp.parse(text);
50+
} catch (e) {
51+
throw new Error('Parsing error at ' + filename + ': ' + e.message);
52+
}
53+
text = cssp.translate(cssp.transform(tree));
54+
},
55+
56+
/**
57+
* Process single file.
58+
*
59+
* @param {String} path
60+
* @returns {Promise}
61+
*/
62+
processFile: function(path) {
63+
var _this = this;
64+
if (this._shouldProcess(path) && path.match(/\.css$/)) {
65+
return vowFs.read(path, 'utf8').then(function(data) {
66+
return _this.processString(data, path);
67+
});
68+
}
69+
return null;
70+
},
71+
72+
/**
73+
* Process directory recursively.
74+
*
75+
* @param {String} path
76+
* @returns {Promise}
77+
*/
78+
processDirectory: function(path) {
79+
var _this = this;
80+
return vowFs.listDir(path).then(function(filenames) {
81+
return vow.all(filenames.map(function(filename) {
82+
var fullname = path + '/' + filename;
83+
return vowFs.stat(fullname).then(function(stat) {
84+
if (_this._shouldProcess(fullname)) {
85+
if (stat.isDirectory()) {
86+
return _this.processDirectory(fullname);
87+
} else if (fullname.match(/\.css$/)) {
88+
return vow.when(_this.processFile(fullname)).then(function(errors) {
89+
if (errors) return errors;
90+
});
91+
}
92+
}
93+
return;
94+
});
95+
})).then(function(results) {
96+
return [].concat.apply([], results);
97+
});
98+
});
99+
},
100+
101+
/**
102+
* Processs directory or file.
103+
*
104+
* @param {String} path
105+
*/
106+
processPath: function(path) {
107+
path = path.replace(/\/$/, '');
108+
var _this = this;
109+
return vowFs.exists(path).then(function(exists) {
110+
if (exists) {
111+
return vowFs.stat(path).then(function(stat) {
112+
if (stat.isDirectory()) {
113+
return _this.processDirectory(path);
114+
} else {
115+
return vow.when(_this.processFile(path)).then(function(errors) {
116+
if (errors) return [errors];
117+
return;
118+
});
119+
}
120+
});
121+
} else {
122+
throw new Error('Path ' + path + ' was not found.');
123+
}
124+
});
125+
},
126+
127+
/**
128+
* Returns true if specified path is not in exluded list.
129+
*
130+
* @returns {Boolean}
131+
*/
132+
_shouldProcess: function(path) {
133+
path = path.replace(/^\.\//, '');
134+
var exclude = this._exclude;
135+
for (var i = exclude.length; i--;) {
136+
if (exclude[i].match(path)) return false;
137+
}
138+
return true;
30139
}
31140

32141
};

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
"node": ">= 0.8.0"
2222
},
2323
"dependencies": {
24-
"commander": "1.1.1"
24+
"commander": "1.1.1",
25+
"cssp": "1.0.6",
26+
"minimatch": "0.2.12",
27+
"vow": "0.3.7",
28+
"vow-fs": "0.1.13"
2529
},
2630
"devDependencies": {
2731
"jshint-groups": "0.5.1",

0 commit comments

Comments
 (0)