forked from jscs-dev/node-jscs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
156 lines (123 loc) · 3.86 KB
/
cli.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Command line implementation for JSCS.
*
* Common usage case is:
*
* ./node_modules/.bin/jscs file1 dir1 file2 dir2
*/
var Checker = require('./checker');
var configFile = require('./cli-config');
var preset = require('./options/preset');
var Vow = require('vow');
var supportsColor = require('supports-color');
var fs = require('fs');
var path = require('path');
module.exports = function(program) {
var reporterPath, reporter, config;
var defer = Vow.defer();
var promise = defer.promise();
var checker = new Checker(program.verbose);
var args = program.args;
var returnArgs = {
checker: checker,
reporter: program.reporter,
promise: promise
};
var stdInput = [];
promise.always(function(status) {
process.exit(status.valueOf());
});
try {
config = configFile.load(program.config);
} catch (e) {
console.error('Config source is corrupted -', e.toString());
defer.reject(1);
return returnArgs;
}
/**
* Trying to load config.
* Custom config path can be specified using '-c' option.
*/
if (!config && !program.preset) {
if (program.config) {
console.error('Configuration source', program.config, 'was not found.');
} else {
console.error('No configuration found. Add a .jscsrc file to your project root or use the -c option.');
}
defer.reject(1);
return returnArgs;
}
if (program.preset && !preset.exists(program.preset)) {
console.error(preset.getDoesNotExistError(program.preset));
defer.reject(1);
return returnArgs;
}
if (!args.length && process.stdin.isTTY) {
console.error('No input files specified. Try option --help for usage information.');
defer.reject(1);
return returnArgs;
}
if (!config) {
config = {};
}
if (program.preset) {
config.preset = program.preset;
}
if (program.reporter) {
reporterPath = path.resolve(process.cwd(), program.reporter);
returnArgs.reporter = reporterPath;
if (!fs.existsSync(reporterPath)) {
reporterPath = './reporters/' + program.reporter;
}
} else {
reporterPath = './reporters/' + (
program.colors && supportsColor ? 'console' : 'text'
);
}
try {
reporter = require(reporterPath);
} catch (e) {
console.error('Reporter "%s" doesn\'t exist.', reporterPath);
defer.reject(1);
return returnArgs;
}
checker.registerDefaultRules();
checker.configure(config);
// Handle usage like 'cat myfile.js | jscs' or 'jscs -''
var usedDash = args[args.length - 1] === '-';
if (!args.length || usedDash) {
// So the dash doesn't register as a file
if (usedDash) { args.length--; }
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
stdInput.push(chunk);
});
process.stdin.on('end', function() {
var errors = checker.checkString(stdInput.join(''));
reporter([errors]);
if (!errors.isEmpty()) {
defer.reject(2);
}
defer.resolve(0);
});
}
if (args.length) {
/**
* Processing specified files and dirs.
*/
Vow.all(args.map(checker.checkPath, checker)).then(function(results) {
var errorsCollection = [].concat.apply([], results);
reporter(errorsCollection);
errorsCollection.forEach(function(errors) {
if (!errors.isEmpty()) {
defer.reject(2);
}
});
defer.resolve(0);
}).fail(function(e) {
console.error(e.stack);
defer.reject(1);
});
}
return returnArgs;
};