forked from katiefenn/parker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparker.js
executable file
·119 lines (99 loc) · 2.97 KB
/
parker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env node
/*! csstool v0.0.0 - MIT license */
'use strict';
/**
* Module dependencies
*/
var _ = require('lodash'),
Parker = require('./lib/Parker'),
CliController = require('./lib/CliController'),
metrics = require('./metrics/All'),
formatters = require('./lib/Formatters'),
argv = require('minimist')(process.argv.slice(2)),
fs = require('graceful-fs'),
async = require('async'),
path = require('path'),
info = require('./lib/Info');
var cliController = new CliController();
cliController.on('runPaths', function (filePaths) {
var stylesheets = [];
async.each(filePaths, function (filePath, onAllLoad) {
var onFileLoad = function (err, data) {
stylesheets.push(data);
};
read(filePath, onFileLoad, onAllLoad);
}, function (err) {
runReport(stylesheets, metrics);
});
});
cliController.on('runStdin', function () {
process.stdin.resume();
process.stdin.setEncoding('utf8');
var stdinData = '';
process.stdin.on('data', function(chunk) {
stdinData += chunk;
});
process.stdin.on('end', function() {
runReport(stdinData, metrics);
});
});
cliController.on('showVersion', function () {
info.version();
process.exit();
});
cliController.on('showHelp', function () {
info.help();
process.exit();
});
cliController.on('setFormat', function (format) {
formatter = formatters[format];
if (!formatter) {
console.error('Unknown output format: %s', argv.format);
console.error(' available: ' + Object.keys(formatters).join(' '));
process.exit(1);
}
});
cliController.on('showNumericOnly', function () {
metrics = _.filter(metrics, function (metric) {
return metric.format == 'number';
});
});
var read = function (filePath, onFileLoad, onAllLoad) {
if (fs.lstatSync(filePath).isDirectory()) {
readDirectory(filePath, onFileLoad, onAllLoad);
}
else if (fileIsStylesheet(filePath)) {
readFile(filePath, function (err, data) {
onFileLoad(err, data);
onAllLoad();
});
} else {
onAllLoad();
}
}
var readDirectory = function (directoryPath, onFileLoad, onAllLoad) {
fs.readdir(directoryPath, function (err, files) {
async.each(files, function (file, fileDone) {
read(path.join(directoryPath, file), onFileLoad, fileDone);
}, onAllLoad);
});
};
var readFile = function (filePath, onLoad) {
fs.readFile(filePath, {encoding: 'utf8'}, function (err, fileData) {
onLoad(err, fileData);
});
};
var fileIsStylesheet = function (filePath) {
return filePath.indexOf('.css') !== -1;
};
var runReport = function (stylesheets, metrics) {
var results = parker.run(stylesheets);
console.log(formatter(metrics, results));
};
if (module.parent) {
module.exports = Parker;
} else {
var parker = new Parker(metrics),
formatter = formatters['human'];
cliController.dispatch(argv);
}