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