Permalink
Browse files

adjustable ratio and output format. URLs for modules.

  • Loading branch information...
fiatjaf committed Jul 11, 2017
1 parent 3273467 commit 6deb27f2d244fbf7559942136480b3dff1191dff
Showing with 37 additions and 15 deletions.
  1. +7 −13 graph.js
  2. +1 −0 package.json
  3. +29 −2 server.js
View
@@ -1,17 +1,13 @@
const Viz = require('viz.js')
const download = require('co-download')
const decompress = require('decompress')
const mkdirp = require('mkdirp')
const rmr = require('rmr')
const dep = require('./dependency-tree')
var cache = {}
var base
module.exports = function (user, repo) {
let tmpfolder = '/tmp/' + user + '/' + repo
base = tmpfolder + '/' + repo + '-master'
let base = tmpfolder + '/' + repo + '-master'
mkdirp.sync(tmpfolder)
return download('https://github.com/' + user + '/' + repo + '/archive/master.zip', '/tmp/master.zip')
@@ -20,22 +16,20 @@ module.exports = function (user, repo) {
let rels = dep(base)
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'
if (kind === 'external' && !(to in cache)) {
dot += ` "${to}" [shape="box"]` + '\n'
dot += ` "${from}" [URL="https://github.com/${user}/${repo}/blob/master/${from.slice(2)}"]` + '\n'
if (kind === 'external') {
dot += ` "${to}" [shape="box", URL="https://npmjs.com/package/${to}"]` + '\n'
} else if (kind === 'local') {
dot += ` "${to}" [URL="https://github.com/${user}/${repo}/blob/master/${to.slice(2)}"]` + '\n'
}
}
dot += '}'
console.log(dot)
rmr.sync(tmpfolder)
return Viz(dot)
return dot
})
.catch(console.log)
}
View
@@ -8,6 +8,7 @@
"dependency-tree": "^5.9.1",
"express": "^4.15.3",
"glob": "^7.1.2",
"md5": "^2.2.1",
"mkdirp": "^0.5.1",
"precinct": "^3.6.0",
"resolve-from": "^3.0.0",
View
@@ -1,12 +1,39 @@
const express = require('express')
const md5 = require('md5')
const Viz = require('viz.js')
const graph = require('./graph')
const app = express()
app.get('/:user/:repo', (r, w) => {
graph(r.params.user, r.params.repo)
.then(svg => w.send(svg))
var format = 'svg'
var repo = r.params.repo
let rsplitted = r.params.repo.split('.')
if (rsplitted.length > 1) {
let f = rsplitted[rsplitted.length - 1]
if (f === 'dot' || f === 'svg') {
format = f
repo = rsplitted.slice(0, -1).join('.')
}
}
graph(r.params.user, repo)
.then(dot => {
if (format === 'svg') {
let ratio = r.query.ratio || '0.5'
let lines = dot.split('\n')
dot = [lines[0], `ratio="${ratio}"`].concat(lines.slice(1)).join('\n')
w.append('Cache-Control', 'no-cache')
w.append('ETag', md5(dot))
w.send(Viz(dot))
} else if (format === 'dot') {
w.send(dot)
} else {
w.send('this will never happen.')
}
})
.catch(console.log)
})

0 comments on commit 6deb27f

Please sign in to comment.