UNPKG

997 BJavaScriptView Raw
1#!/usr/bin/env node
2/* global __dirname, process */
3
4import program from 'commander'
5import fs from 'fs'
6import path from 'path'
7import graphlib from 'graphlib'
8import {convertGraph} from './api.js'
9import getStdin from 'get-stdin'
10
11program
12 .version(JSON.parse(fs.readFileSync(path.join(__dirname, '/../package.json')))['version'])
13 .option('-f, --graphfile <graphfile>', 'Set graph file to parse. If none is given stdin is read')
14 .parse(process.argv)
15
16var processGraph = (str) => {
17 var graph = graphlib.json.read(JSON.parse(str))
18 return Promise.resolve(JSON.stringify(convertGraph(graph), null, 2))
19}
20
21if (program.graphfile) {
22 var str = fs.readFileSync(program.graphfile)
23 processGraph(str).then((code) => console.log(code))
24 .catch((e) => {
25 console.log('Error while processing: ', e.stack)
26 })
27} else {
28 getStdin().then((str) => {
29 processGraph(str).then((code) => console.log(code))
30 .catch((e) => {
31 console.log('Error while processing: ', e.stack)
32 })
33 })
34}