-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileScanner.js
410 lines (369 loc) · 13.3 KB
/
FileScanner.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"use strict";
var fs = require('fs');
var path = require('path');
var Q = require('q');
var _ = require('lodash');
var program = require('commander');
var parsejs = require('./parse-js');
var cache = require('./cache');
var readFile = Q.denodeify(fs.readFile);
var symbolsCache = 'symbols';
var processedSymbolsCache = 'processedSymbols';
var rawSymbolsDef = {};
var dependencyTypes = {
use: 0,
useOptional: 0,
require: 1,
requireOptional: 1,
follow: 1,
followOptional: 1,
ignore: 2
};
var scriptRegexp = /<script(?: type="text\/javascript")?>([\s\S]*?)<\/script>/g;
var phpIncludeRegexp = /\b(?:include|require)(?:_once)?\s*(?:\(\s*)?(?:(?:__DIR__|dirname\(__FILE__\)|(dirname\(__DIR__\)))\s*\.\s*)?['"]([\w\d\/_\-\.]+)['"]\s*(?:\)\s*)?(?:;|\?>)( ?\/\/jsdep:ignore)?/g;
var commentRegexp = new RegExp(
'(?:/[/*]|<!--) ?\\+(' + Object.keys(dependencyTypes).join('|') + ') (.+?) ?(?:\\*/|-->)?(?:\\r?\\n|$)', 'g');
/**
* Сканирует файл на наличие в нём символов
* @param {string} file
* @param {Object} symbolsMap
* @param {Object} packages
* @param {Object} options
* @constructor
*/
var FileScanner = function(file, symbolsMap, packages, options) {
this.__file = file;
this.__symbolsMap = symbolsMap;
this.__packages = packages;
this.__rawSymbols = [];
this.__symbols = {};
this.__options = options;
if (program.missfile && !fs.existsSync(file)) {
this.__result = Q({});
return;
}
this.__result = Q()
.then(function() {
var fromCache = cache.getData(symbolsCache, this.__file);
if (fromCache) {
this.__rawSymbols = fromCache;
return;
}
else if (rawSymbolsDef[this.__file]) {
return rawSymbolsDef[this.__file].then(function(symbols) {
this.__rawSymbols = symbols;
}.bind(this));
}
var promise = readFile(this.__file, 'utf8').then(
function(content) {
//this.__rawSymbols.push = function(symbol) {
// if (symbol.symbol === 'common') {
// console.trace();
// process.exit();
// }
// return Array.prototype.push.apply(this.__rawSymbols, arguments);
//}.bind(this);
this.__scanFile(content, this.__file);
}.bind(this),
function() {
throw new Error('Read file error: ' + this.__file);
}.bind(this));
return rawSymbolsDef[this.__file] = promise.then(function() {
delete rawSymbolsDef[this.__file];
cache.setData(symbolsCache, this.__file, this.__rawSymbols);
return this.__rawSymbols;
}.bind(this));
}.bind(this))
.then(function() {
var fromCache = program.nodirchange && cache.getData(processedSymbolsCache, this.__file);
if (fromCache) {
this.__symbols = fromCache;
}
else {
this.__processRawSymbols();
_.forOwn(this.__symbols, function(depType, symbol) {
if (depType === 'ignore') {
delete this.__symbols[symbol];
}
}, this);
}
cache.setData(processedSymbolsCache, this.__file, this.__symbols);
return this.__symbols;
}.bind(this));
};
FileScanner.prototype = {
constructor: FileScanner,
/**
* Результат работы компонента
* @returns {Q}
*/
getResult: function() {
return this.__result;
},
/**
* @param {boolean} options
* @param {string} options.symbol
* @param [options.depType]
* @param [options.wildcard = false]
* @param [options.meta]
*/
__addDependency: function(options) {
var symbol = options.symbol;
var depType = options.depType;
if (options.wildcard && symbol[symbol.length - 1] === '*' && symbol[symbol.length - 2] === '.') {
var prefix = symbol.substr(0, symbol.length - 1);
_.forOwn(this.__symbolsMap.symbolsHash, function(value, name) {
if (name.substr(0, prefix.length) === prefix) {
this.__addDependency({symbol: name, depType: depType, meta: options.meta});
}
}, this);
}
else {
var curSymbolType = this.__symbols[symbol];
if (!curSymbolType || dependencyTypes[curSymbolType] < dependencyTypes[depType]) {
this.__symbols[symbol] = options.meta ? {depType: depType, meta: options.meta} : depType;
}
}
},
/**
* @param struct
* @returns {*}
* @private
*/
__composeSymbol: function(struct) {
var operand = this.__getOperand(struct);
if (operand === 'name') {
return [struct[1]];
}
else if (operand === 'dot') {
var prefix = this.__composeSymbol(struct[1]);
return prefix && prefix.concat(struct[2]);
}
return false;
},
/**
* @param struct
* @param [useSymbol=false]
* @private
*/
__findSymbols: function(struct, useSymbol) {
if (!Array.isArray(struct)) {
return;
}
var symbol = this.__composeSymbol(struct);
if (symbol) {
this.__rawSymbols.push({symbol: symbol, depType: useSymbol ? 'use' : 'require'});
}
else {
if (!useSymbol) {
useSymbol = !this.__requireAll && this.__getOperand(struct) === 'function';
}
struct.forEach(function(subStruct) {
this.__findSymbols(subStruct, useSymbol);
}, this);
}
},
/**
* @param struct
* @returns {string}
* @private
*/
__getOperand: function(struct) {
return struct === null ? 'null' :
typeof struct === 'string' ? 'string' :
!Array.isArray(struct) ? 'token' :
!struct.length || Array.isArray(struct[0]) || !struct[0] ? 'nope' :
typeof struct[0] === 'string' ? struct[0] : struct[0].name;
},
/**
* @param text
* @private
*/
__parseComments: function(text) {
var match;
while (match = commentRegexp.exec(text)) { // jshint ignore:line
if (match) {
if (match[1] === 'require' && match[2] === 'all') {
this.__requireAll = true;
}
else {
this.__rawSymbols.push({symbol: match[2], depType: match[1], wildcard: true});
}
}
}
},
/**
* @param {string} content
* @param {boolean} [suppressErrors=false]
* @private
*/
__parseJS: function(content, suppressErrors) {
var parsedJS;
if (suppressErrors) {
try {
parsedJS = parsejs.parse(content, false, true);
}
catch (ex) {
}
}
else {
parsedJS = parsejs.parse(content, false, true);
}
if (parsedJS) {
this.__findSymbols(parsedJS, false);
}
},
/**
* @private
*/
__processRawSymbols: function() {
this.__rawSymbols.forEach(function(symbolDesc) {
if (Array.isArray(symbolDesc.symbol)) {
var symbol = symbolDesc.symbol.concat();
if (this.__packages[symbol[0]]) {
while (symbol.length) {
var curSymbol = symbol.join('.');
if (this.__symbolsMap.symbolsHash[curSymbol]) {
this.__addDependency({symbol: curSymbol, depType: symbolDesc.depType});
}
symbol.pop();
}
}
}
else {
this.__addDependency(symbolDesc);
}
}, this);
},
/**
* @param {string} content
* @param {string} [filePath]
* @private
*/
__scanFile: function(content, filePath) {
var extName = filePath && path.extname(filePath);
var isInlineJs = !extName;
var isPhp = extName === '.php';
var isTpl = !isInlineJs && extName !== '.js' && extName !== '.css';
var isJs = extName === '.js';
if (path.basename(filePath) === '.bower.json') {
var bower = JSON.parse(content);
if (bower.dependencies) {
_.forOwn(bower.dependencies, function(version, dep) {
this.__rawSymbols.push({
symbol: 'bower:' + dep,
depType: 'require'
});
}, this);
}
(Array.isArray(bower.main) ? bower.main : [bower.main]).forEach(function(include) {
if (path.extname(include) === '.js') {
this.__rawSymbols.push({
symbol: '!!' + path.join(path.dirname(filePath), include),
meta: {bower: true, analyze: false},
depType: 'follow'
});
}
}, this);
return;
}
if (extName === '.coffee') {
var coffeeCompiler = require('iced-coffee-script').compile;
content = coffeeCompiler(content, {bare: true});
isJs = true;
}
if (isTpl && (content.indexOf('<?php //+ignore') === 0 || content.indexOf('<?php /*+ignore*/') === 0)) {
return;
}
if (!isInlineJs) {
this.__parseComments(content);
}
if (isTpl || isJs) {
this.__scanMatches(content, this.__options.htmlSymbolRegexp, this.__options.htmlSymbolsMap);
//scan css
var cssHash = this.__symbolsMap.typesHash.css;
if (cssHash) {
var classes = Object.keys(cssHash).filter(function(cls){ return cls.indexOf('.') === -1; });
var cssRe = new RegExp('\\b(?:' + classes.join('|') + ')(?![\\d\\w\\-_])', 'g');
var cssMatch = content.match(cssRe);
if (cssMatch) {
cssMatch.forEach(function(symbol) {
this.__rawSymbols.push({symbol: symbol, depType: 'use'});
}, this);
}
}
}
if (isJs) {
this.__scanMatches(content, this.__options.jsSymbolRegexp, this.__options.jsSymbolsMap);
}
if (isJs || isInlineJs) {
this.__parseJS(content, isInlineJs);
}
if (isTpl) {
var scriptMatch;
while (scriptMatch = scriptRegexp.exec(content)) { // jshint ignore:line
this.__scanFile(scriptMatch[1]);
}
if (this.__options.tplJs) {
this.__options.tplJs.forEach(function(func) {
var code = func(content);
if (code) {
this.__parseJS(content, true);
}
}, this);
}
}
if (isPhp) {
var includeMatch;
while (includeMatch = phpIncludeRegexp.exec(content)) { // jshint ignore:line
if (!includeMatch[3]) {
//!! - absolute path
var dir = path.dirname(filePath);
if (includeMatch[1]) {
dir = path.dirname(dir);
}
this.__rawSymbols.push({
symbol: '!!' + path.join(dir, includeMatch[2]),
depType: 'use'
});
}
}
}
},
/**
* @param {string} source
* @param {RegExp} regexp
* @param {Object} map
* @private
*/
__scanMatches: function(source, regexp, map) {
if (regexp) {
if (!Array.isArray(regexp)) {
regexp = [regexp];
}
regexp.forEach(function(re) {
if (re instanceof RegExp) {
re = {
re: re, symbol: function(match) {
return match[1];
}
};
}
var match;
while (match = re.re.exec(source)) { // jshint ignore:line
this.__rawSymbols.push({symbol: re.symbol.call(this.__options, match), depType: 'use'});
}
}, this);
}
if (map) {
_.forOwn(map, function(symbol, pattern) {
if (source.indexOf(pattern) !== -1) {
(Array.isArray(symbol) ? symbol : [symbol]).forEach(function(curSymbol) {
this.__rawSymbols.push({symbol: curSymbol, depType: 'use'});
}, this);
}
}, this);
}
}
};
module.exports = FileScanner;