UNPKG

1.23 kBJavaScriptView Raw
1#!/usr/bin/env node
2/* global __dirname, process */
3
4import program from 'commander'
5import fs from 'fs'
6import getStdin from 'get-stdin'
7import graphlib from 'graphlib'
8import addTypeConversion from './dynatype-network.js'
9import path from 'path'
10
11program
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
17if (!program.typegraph) {
18 console.error('No type conversion graph given')
19}
20
21try {
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}