Skip to content

Instantly share code, notes, and snippets.

@adelin-b
Created July 12, 2020 16:07
Show Gist options
  • Save adelin-b/9fd60900a71f111368cc31aea8907808 to your computer and use it in GitHub Desktop.
Save adelin-b/9fd60900a71f111368cc31aea8907808 to your computer and use it in GitHub Desktop.
small yargs cli for json-literal-typer using ts-node
#!/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