1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | import program from 'commander'
|
5 | import fs from 'fs'
|
6 | import getStdin from 'get-stdin'
|
7 | import graphlib from 'graphlib'
|
8 | import addTypeConversion from './dynatype-network.js'
|
9 | import path from 'path'
|
10 |
|
11 | program
|
12 | .version(JSON.parse(fs.readFileSync(path.join(__dirname, '/../package.json')))['version'])
|
13 | .option('-t, --typegraph <typegraph>', 'Set the dynatype type conversion graph.')
|
14 | .option('-f, --graphfile [graphfile]', 'Set graph file to parse. If none is given stdin is read')
|
15 | .parse(process.argv)
|
16 |
|
17 | if (!program.typegraph) {
|
18 | console.error('No type conversion graph given')
|
19 | }
|
20 |
|
21 | try {
|
22 | var typeGraph = graphlib.json.read(JSON.parse(fs.readFileSync(program.typegraph)))
|
23 |
|
24 | var processGraph = (str) => {
|
25 | var graph = graphlib.json.read(JSON.parse(str))
|
26 | var typed = addTypeConversion(graph, typeGraph)
|
27 | return JSON.stringify(graphlib.json.write(typed))
|
28 | }
|
29 |
|
30 | if (program.graphfile) {
|
31 | var str = fs.readFileSync(program.graphfile)
|
32 | console.log(processGraph(str))
|
33 | } else {
|
34 | getStdin().then((str) => {
|
35 | try {
|
36 | console.log(processGraph(str))
|
37 | } catch (e) {
|
38 | console.error(e)
|
39 | }
|
40 | })
|
41 | }
|
42 | } catch (e) {
|
43 | console.error(e)
|
44 | }
|