Permalink
Browse files

my own dependency inspector and graphs with both local and external m…

…odules.
  • Loading branch information...
fiatjaf committed Jul 11, 2017
1 parent 12e283c commit 3273467067e37d313ac6b7c5074db1db94ee776f

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
View
@@ -0,0 +1,4 @@
package-lock.json
*.swo
*.swp
node_modules
View

This file was deleted.

Oops, something went wrong.
View
@@ -0,0 +1,37 @@
const fs = require('fs')
const glob = require('glob')
const path = require('path')
const uniq = require('array-unique')
const precinct = require('precinct')
const resolve = require('resolve-from')
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 {
let rel = '.' + resolve(path.dirname(file), req).slice(base.length)
deps.push([filerel, 'local', rel])
}
}
return deps
}
View
@@ -1,65 +1,41 @@
const fs = require('fs')
const Viz = require('viz.js')
const download = require('co-download')
const decompress = require('decompress')
const mkdirp = require('mkdirp')
const dep = require('dependency-tree')
const relative = require('relative')
const rmr = require('rmr')
const dep = require('./dependency-tree')
var cache = {}
var inc = 0
var base
module.exports = function (user, repo, file = 'index.js') {
let folder = '/tmp/' + user + '/' + repo
base = folder + '/' + repo + '-master'
mkdirp.sync(folder)
module.exports = function (user, repo) {
let tmpfolder = '/tmp/' + user + '/' + repo
base = tmpfolder + '/' + repo + '-master'
mkdirp.sync(tmpfolder)
return download('https://github.com/' + user + '/' + repo + '/archive/master.zip', '/tmp/master.zip')
.then(filepath => decompress(filepath, folder))
.then(() => {
let res = dep({
filename: base + '/' + file,
directory: base
})
var dot = {dot: 'digraph main {\n'}
recurse(dot, res)
dot.dot += '}'
return Viz(dot.dot)
})
.catch(console.log)
}
function nodecache (dot, path) {
let p = './' + relative(base, path)
var id
if (p in cache) {
id = cache[p]
} else {
id = inc++
cache[p] = id
dot.dot += ' f' + id + '[label="' + p + '"]\n'
}
return [id, p]
}
.then(filepath => decompress(filepath, tmpfolder))
.then(() => {
let rels = dep(base)
function relcache (dot, from, to) {
let def = 'f' + from + '->f' + to
if (!(def in cache)) {
dot.dot += ' ' + def + '\n'
}
}
var dot = 'digraph main {\n'
dot += ' size="400,800"\n'
dot += ' ratio="compress"\n'
for (let i = 0; i < rels.length; i++) {
let [from, kind, to] = rels[i]
dot += ` "${from}"->"${to}"` + '\n'
function recurse (dot, paths) {
for (let path in paths) {
let [id, p] = nodecache(dot, path)
if (typeof paths[path] === 'object') {
Object.keys(paths[path]).forEach(dep => {
let [did] = nodecache(dot, dep)
relcache(dot, id, did)
})
recurse(dot, paths[path])
if (kind === 'external' && !(to in cache)) {
dot += ` "${to}" [shape="box"]` + '\n'
}
}
}
}
dot += '}'
console.log(dot)
rmr.sync(tmpfolder)
return Viz(dot)
})
.catch(console.log)
}
View
@@ -2,15 +2,19 @@
"name": "node-dependencies-view",
"description": "nada",
"dependencies": {
"viz.js": "^1.8.0",
"array-unique": "^0.3.2",
"co-download": "^0.2.0",
"decompress": "^4.2.0",
"mkdirp": "^0.5.1",
"dependency-tree": "^5.9.1",
"relative": "^3.0.2",
"express": "^4.15.3"
"express": "^4.15.3",
"glob": "^7.1.2",
"mkdirp": "^0.5.1",
"precinct": "^3.6.0",
"resolve-from": "^3.0.0",
"rmr": "0.0.4",
"viz.js": "^1.8.0"
},
"scripts": {
"start": "node server.js"
}
}
}
View
@@ -4,12 +4,11 @@ const graph = require('./graph')
const app = express()
app.get('/:user/:repo/**', (r, w) => {
console.log(r.params)
graph(r.params.user, r.params.repo, r.params[0])
app.get('/:user/:repo', (r, w) => {
graph(r.params.user, r.params.repo)
.then(svg => w.send(svg))
.catch(console.log)
})
let port = process.env.PORT || 3000
app.listen(port, () => console.log('listening at :' + port))
app.listen(port, () => console.log('listening at :' + port))

0 comments on commit 3273467

Please sign in to comment.