Created
July 12, 2020 16:07
-
-
Save adelin-b/9fd60900a71f111368cc31aea8907808 to your computer and use it in GitHub Desktop.
small yargs cli for json-literal-typer using ts-node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ts-node | |
import * as yargs from 'yargs'; | |
import { default as analyze, typify } from 'json-literal-typer'; | |
import * as fs from 'fs'; | |
const argv = yargs | |
.option('output', { | |
alias: 'o', | |
description: 'output file', | |
type: 'string', | |
required: true, | |
}) | |
.option('input', { | |
alias: 'i', | |
description: 'input file', | |
type: 'string', | |
required: true, | |
}) | |
.option('typename', { | |
alias: 't', | |
description: 'name of the root type to export', | |
type: 'string', | |
required: false, | |
}) | |
.help().argv; | |
try { | |
const file = fs.readFileSync(argv.input).toString('utf8'); | |
const jsonified = JSON.parse(file); | |
const analyzed = analyze(jsonified); | |
const result = typify(analyzed); | |
const typename = argv.typename ?? 'Root'; | |
const exportResult = result.replace( | |
'interface Root {', | |
`export interface ${typename} {`, | |
); | |
fs.writeFileSync(argv.output, exportResult); | |
} catch (error) { | |
throw new Error(error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment