forked from fiatjaf/node-dependencies-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependency-tree.js
43 lines (40 loc) · 1.11 KB
/
dependency-tree.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
const fs = require('fs')
const glob = require('glob')
const path = require('path')
const uniq = require('array-unique')
const resolve = require('resolve-from')
const precinct = require('precinct')
module.exports = function (base) {
var rels = []
let files = glob.sync(path.join(base, '**/*.{js,jsx,es,ts}'))
for (let i = 0; i < files.length; i++) {
let file = files[i]
if (file.indexOf('/node_modules/') !== -1) continue
rels = rels.concat(inspect(file, base))
}
return rels
}
function inspect (file, base) {
var requires
try {
requires = uniq(precinct(fs.readFileSync(file, 'utf8')))
} catch (e) {}
var deps = []
let filerel = '.' + file.slice(base.length)
for (let i = 0; i < requires.length; i++) {
let req = requires[i]
if (req[0] !== '.') {
deps.push([filerel, 'external', req])
} else {
var resolved
try {
resolved = resolve(path.dirname(file), req)
} catch (e) {
resolved = path.resolve(path.dirname(file), req)
}
let rel = '.' + resolved.slice(base.length)
deps.push([filerel, 'local', rel])
}
}
return deps
}