-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·30 lines (26 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env node
const { transform } = require('babel-core')
const transformJsonTypes = require('transform-json-types')
const babelJsonToProptypes = require('babel-plugin-json-to-proptypes')
const program = require('commander')
const getStdin = require('get-stdin')
const R = require('ramda')
const isString = R.is(String)
const jsonTo = R.flip(transformJsonTypes)
const jsonToFlow = jsonTo({ lang: 'flow' })
const jsonToTS = jsonTo({ lang: 'typescript' })
const jsonToProptypes = x => transform(x, { plugins: [babelJsonToProptypes] }).code + "\n"
program
.option('-t, --to <output>', 'set output format', /^(flow|typescript|proptypes|scala|rust)$/i)
.parse(process.argv)
if (! isString(program.to)) {
console.error('not a valid transformer, must be "flow", "typescript", or "proptypes"')
process.exit(1)
}
getStdin().then(R.prop(program.to.toLowerCase(), {
flow: jsonToFlow,
typescript: jsonToTS,
proptypes: jsonToProptypes,
}))
.then(x => process.stdout.write(x))
.catch(e => { console.error(e); process.exit(1) })