1 | var vowFs = require('vow-fs');
|
2 | var Vow = require('vow');
|
3 | var StringChecker = require('./string-checker');
|
4 | var extractJs = require('./extract-js');
|
5 | var utils = require('util');
|
6 | var path = require('path');
|
7 |
|
8 | var NodeConfiguration = require('./config/node-configuration');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var Checker = function() {
|
17 | StringChecker.apply(this, arguments);
|
18 | };
|
19 |
|
20 | utils.inherits(Checker, StringChecker);
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | Checker.prototype.configure = function(config) {
|
28 | StringChecker.prototype.configure.call(this, config);
|
29 |
|
30 | this._fileExtensions = this._configuration.getFileExtensions();
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | Checker.prototype.execute = function() {
|
39 | var method = this._configuration.getFix() === true ? this.fixPath : this.checkPath;
|
40 |
|
41 | return method.apply(this, arguments);
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | Checker.prototype.checkFile = function(path) {
|
51 | if (this._configuration.isFileExcluded(path)) {
|
52 | return Vow.resolve(null);
|
53 | }
|
54 |
|
55 | return vowFs.read(path, 'utf8').then(function(data) {
|
56 | return this.checkString(data, path);
|
57 | }, this);
|
58 | };
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | Checker.prototype.fixFile = function(path) {
|
67 | if (this._configuration.isFileExcluded(path)) {
|
68 | return Vow.resolve(null);
|
69 | }
|
70 |
|
71 | return vowFs.read(path, 'utf8').then(function(data) {
|
72 | var result = this.fixString(data, path);
|
73 | return vowFs.write(path, result.output).then(function() {
|
74 | return result.errors;
|
75 | });
|
76 | }, this);
|
77 | };
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | Checker.prototype.extractFile = function(path) {
|
86 | if (this._configuration.isFileExcluded(path)) {
|
87 | return Vow.resolve(null);
|
88 | }
|
89 |
|
90 | if (!this._configuration.shouldExtractFile(path)) {
|
91 | return Vow.resolve(null);
|
92 | }
|
93 |
|
94 | return vowFs.read(path, 'utf8').then(function(data) {
|
95 | var result = extractJs(path, data);
|
96 |
|
97 | result.sources.forEach(function(script) {
|
98 | this.checkString(script.source, path).getErrorList().forEach(function(error) {
|
99 | error.line += script.line;
|
100 | error.column += script.offset;
|
101 | result.addError(error);
|
102 | });
|
103 | }, this);
|
104 |
|
105 | return result.errors;
|
106 | }, this);
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | Checker.prototype.checkDirectory = function(path) {
|
116 | return this._processDirectory(path, this.checkFile.bind(this));
|
117 | };
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | Checker.prototype.checkPath = function(path) {
|
126 | return this._processPath(path, this.checkFile.bind(this));
|
127 | };
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | Checker.prototype.fixPath = function(path) {
|
136 | return this._processPath(path, this.fixFile.bind(this));
|
137 | };
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | Checker.prototype._processDirectory = function(path, fileHandler) {
|
147 | return vowFs.listDir(path).then(function(filenames) {
|
148 | return Vow.all(filenames.map(function(filename) {
|
149 | var fullname = path + '/' + filename;
|
150 |
|
151 | if (this._configuration.isFileExcluded(fullname)) {
|
152 | return [];
|
153 | }
|
154 |
|
155 | return vowFs.stat(fullname).then(function(stat) {
|
156 | if (stat.isDirectory()) {
|
157 | return this._processDirectory(fullname, fileHandler);
|
158 | }
|
159 |
|
160 | if (!this._hasCorrectExtension(fullname)) {
|
161 | if (!this._configuration.shouldExtractFile(fullname)) {
|
162 | return [];
|
163 | }
|
164 |
|
165 | return this.extractFile(fullname);
|
166 | }
|
167 |
|
168 | return fileHandler(fullname);
|
169 | }, this);
|
170 | }, this)).then(function(results) {
|
171 | return [].concat.apply([], results);
|
172 | });
|
173 | }, this);
|
174 | };
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | Checker.prototype._processPath = function(path, fileHandler) {
|
184 | path = path.replace(/\/$/, '');
|
185 |
|
186 | return vowFs.exists(path).then(function(exists) {
|
187 | if (!exists) {
|
188 | throw new Error('Path ' + path + ' was not found.');
|
189 | }
|
190 |
|
191 | return vowFs.stat(path).then(function(stat) {
|
192 | if (stat.isDirectory()) {
|
193 | return this._processDirectory(path, fileHandler);
|
194 | }
|
195 |
|
196 | return fileHandler(path).then(function(errors) {
|
197 | if (errors) {
|
198 | return [errors];
|
199 | }
|
200 |
|
201 | return [];
|
202 | });
|
203 | }, this);
|
204 | }, this);
|
205 | };
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | Checker.prototype.checkStdin = function() {
|
213 | return this._processStdin(this.checkString.bind(this));
|
214 | };
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | Checker.prototype.fixStdin = function() {
|
222 | return this._processStdin(this.fixString.bind(this));
|
223 | };
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | Checker.prototype._processStdin = function(stdinHandler) {
|
231 | var stdInput = [];
|
232 | var deferred = Vow.defer();
|
233 |
|
234 | process.stdin.setEncoding('utf8');
|
235 |
|
236 | process.stdin.on('data', function(chunk) {
|
237 | stdInput.push(chunk);
|
238 | });
|
239 |
|
240 | process.stdin.on('end', function() {
|
241 | deferred.resolve(stdinHandler(stdInput.join('')));
|
242 | });
|
243 |
|
244 | return deferred.promise();
|
245 | };
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | Checker.prototype._hasCorrectExtension = function(testPath) {
|
253 | var extension = path.extname(testPath).toLowerCase();
|
254 | var basename = path.basename(testPath).toLowerCase();
|
255 |
|
256 | return !(
|
257 | this._fileExtensions.indexOf(extension) < 0 &&
|
258 | this._fileExtensions.indexOf(basename) < 0 &&
|
259 | this._fileExtensions.indexOf('*') < 0
|
260 | );
|
261 | };
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | Checker.prototype._createConfiguration = function() {
|
270 | return new NodeConfiguration();
|
271 | };
|
272 |
|
273 | module.exports = Checker;
|