/
index.js
130 lines (109 loc) · 3.62 KB
/
index.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
/*
* Copyright (C) 2016 Joris "Hyzual" MASSON
*
* This file is part of knotsjs.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function() {
var argv = require('yargs')
.usage('Usage: $0 <file or directory path>')
.demand(1, 'Not enough file arguments: got 0, need at least 1')
.help('h')
.alias('h', 'help')
.version('v')
.alias('v', 'version')
.argv;
// TODO: should probably be in the future bin/knotsjs script
var Promise = require('bluebird');
require('any-promise/register')('bluebird');
var path = require('path');
var fs = require('mz/fs');
var acorn = require('acorn');
var escope = require('escope');
var _ = require('lodash');
var recursive = require('recursive-readdir');
var dependencies = require('./dependencies.js');
var ast_walker = require('./ast-walker.js');
module.exports = {
parse : parseSingleFile,
parseFiles : parseFiles,
parseDirectory: parseDirectory
};
if (! module.parent) {
runAsCLI();
}
function runAsCLI() {
var paths_to_parse = argv._;
if (paths_to_parse.length === 1) {
var path_to_parse = paths_to_parse[0];
parsePath(path_to_parse).then(function (data) {
console.log(data);
});
} else {
_.forEach(paths_to_parse, parsePath);
}
}
function parsePath(path_to_parse) {
var promise = fs.stat(path_to_parse).then(function (stats) {
if (stats.isDirectory()) {
return parseDirectory(path_to_parse);
}
if (stats.isFile()) {
return parseSingleFile(path_to_parse);
}
});
return promise;
}
function ignoreNonJS(file_path, stats) {
return (stats.isFile() && path.extname(file_path) !== '.js');
}
function parseDirectory(directory_path) {
var recursiveReadDir = Promise.promisify(recursive);
var promise = recursiveReadDir(directory_path, ['*_test.js', ignoreNonJS]).then(function (file_paths) {
return parseFiles(file_paths);
});
return promise;
}
function parseFiles(file_paths) {
dependencies.clear();
var promises = _.map(file_paths, function (file_path) {
return parse(file_path);
});
var promise = Promise.all(promises)
.then(function() {
return dependencies.get();
});
return promise;
}
function parseSingleFile(file_path) {
dependencies.clear();
var promise = parse(file_path)
.then(function() {
return dependencies.get();
});
return promise;
}
function parse(file_path) {
return fs.readFile(file_path, 'utf8').then(function(data) {
// TODO: try acorn's program option, need to know a 'first' file.
// This could be possible in ES2015 or node because of modules, but I'm not sure about scripts like angular
// TODO: provide option for acorn's ecmaVersion ?
// TODO: detect acorn's sourceType option ?
var ast = acorn.parse(data);
var scope_manager = escope.analyze(ast);
ast_walker.walk(ast, scope_manager);
});
}
})();