1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | import program from 'commander'
|
5 | import fs from 'fs'
|
6 | import path from 'path'
|
7 | import graphlib from 'graphlib'
|
8 | import {convertGraph} from './api.js'
|
9 | import getStdin from 'get-stdin'
|
10 |
|
11 | program
|
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 |
|
16 | var processGraph = (str) => {
|
17 | var graph = graphlib.json.read(JSON.parse(str))
|
18 | return Promise.resolve(JSON.stringify(convertGraph(graph), null, 2))
|
19 | }
|
20 |
|
21 | if (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 | }
|