Skip to content

Commit

Permalink
feat: add json-to-schema command-line feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jun 23, 2021
1 parent 138e226 commit 18ea55f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const validate = require('./validate')
const toJSON = require('./to-json')
const toSchema = require('./to-schema')
const jsonToSchema = require('./json-to-schema')

const toOpts = {
tabs: {
Expand All @@ -23,6 +24,9 @@ const yargs = require('yargs')
.command('to-schema',
'Accepts .ipldsch and .md files, if none are passed will read from stdin, prints the canonical IPLD Schema form of the schema',
toOpts)
.command('json-to-schema',
'Accepts .json files, if none are passed will read from stdin, prints the canonical IPLD Schema form of the schema represented by the JSON',
toOpts)
.showHelpOnFail(true)
.demandCommand(1, 'must provide a valid command')
.help()
Expand All @@ -45,6 +49,9 @@ switch (yargs.argv._[0]) {
case 'to-schema':
runCommand(toSchema)
break
case 'json-to-schema':
runCommand(jsonToSchema)
break
default:
yargs.showHelp()
}
21 changes: 21 additions & 0 deletions bin/json-to-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

const print = require('../print')
const collectInput = require('./collect-input')

let indent = ' '

async function toSchema (files, options) {
if (options.tabs) {
indent = '\t'
}

const input = await collectInput(files)

for (const { contents } of input) {
const schema = JSON.parse(contents)
console.log(print(schema, indent))
}
}

module.exports = toSchema

0 comments on commit 18ea55f

Please sign in to comment.