This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathchecker.js
186 lines (153 loc) · 4.42 KB
/
checker.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var vowFs = require('vow-fs');
var Vow = require('vow');
var StringChecker = require('./string-checker');
var utils = require('util');
var path = require('path');
var additionalRules = require('./options/additional-rules');
var excludeFiles = require('./options/exclude-files');
var fileExtensions = require('./options/file-extensions');
var esnext = require('./options/esnext');
/**
* Starts Code Style checking process.
*
* @name Checker
*/
var Checker = function() {
StringChecker.apply(this, arguments);
};
utils.inherits(Checker, StringChecker);
/**
* Loads configuration from JS Object. Activates and configures required rules.
*
* @param {Object} config
*/
Checker.prototype.configure = function(config) {
var cwd = config.configPath ? path.dirname(config.configPath) : process.cwd();
fileExtensions(config, this);
excludeFiles(config, this, cwd);
additionalRules(config, this, cwd);
esnext(config);
StringChecker.prototype.configure.apply(this, arguments);
};
/**
* Checks single file.
*
* @param {String} path
* @returns {Promise * Errors}
*/
Checker.prototype.checkFile = function(path) {
var _this = this;
if (!_this._isExcluded(path)) {
return vowFs.read(path, 'utf8').then(function(data) {
return _this.checkString(data, path);
});
}
var defer = Vow.defer();
defer.resolve(null);
return defer.promise();
};
/**
* Checks directory recursively.
*
* @param {String} path
* @returns {Promise * Error[]}
*/
Checker.prototype.checkDirectory = function(path) {
var _this = this;
return vowFs.listDir(path).then(function(filenames) {
return Vow.all(filenames.map(function(filename) {
var fullname = path + '/' + filename;
// check for exclude path
if (_this._isExcluded(fullname)) {
return [];
}
return vowFs.stat(fullname).then(function(stat) {
if (stat.isDirectory()) {
return _this.checkDirectory(fullname);
}
if (!_this._hasCorrectExtension(fullname)) {
return [];
}
return Vow.when(_this.checkFile(fullname)).then(function(errors) {
if (errors) {
return errors;
}
return [];
});
});
})).then(function(results) {
return [].concat.apply([], results);
});
});
};
/**
* Checks directory or file.
*
* @param {String} path
* @returns {Error[]}
*/
Checker.prototype.checkPath = function(path) {
path = path.replace(/\/$/, '');
var _this = this;
return vowFs.exists(path).then(function(exists) {
if (!exists) {
throw new Error('Path ' + path + ' was not found.');
}
return vowFs.stat(path).then(function(stat) {
if (stat.isDirectory()) {
return _this.checkDirectory(path);
}
return _this.checkFile(path).then(function(errors) {
if (errors) {
return [errors];
}
return [];
});
});
});
};
/**
* Checks stdin for input
*
* @returns {Promise}
*/
Checker.prototype.checkStdin = function() {
var stdInput = [];
var deferred = Vow.defer();
var _this = this;
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
stdInput.push(chunk);
});
process.stdin.on('end', function() {
var errors = _this.checkString(stdInput.join(''));
deferred.resolve(errors);
});
return deferred.promise();
};
/**
* Returns true if specified path is in excluded list.
*
* @returns {Boolean}
*/
Checker.prototype._isExcluded = function(testPath) {
testPath = path.resolve(testPath);
return !this._excludes.every(function(exclude) {
return !exclude.match(testPath);
});
};
/**
* Returns true if the file extension matches a file extension to process.
*
* @returns {Boolean}
*/
Checker.prototype._hasCorrectExtension = function(testPath) {
var extension = path.extname(testPath).toLowerCase();
var basename = path.basename(testPath).toLowerCase();
return !(
this._fileExtensions.indexOf(extension) < 0 &&
this._fileExtensions.indexOf(basename) < 0 &&
this._fileExtensions.indexOf('*') < 0
);
};
module.exports = Checker;