[d565449] | 1 | #!/usr/bin/env node
|
---|
| 2 |
|
---|
| 3 | const fs = require('fs')
|
---|
| 4 | const path = require('path')
|
---|
| 5 | const pkg = require('../package.json')
|
---|
| 6 | const JSON5 = require('./')
|
---|
| 7 |
|
---|
| 8 | const argv = parseArgs()
|
---|
| 9 |
|
---|
| 10 | if (argv.version) {
|
---|
| 11 | version()
|
---|
| 12 | } else if (argv.help) {
|
---|
| 13 | usage()
|
---|
| 14 | } else {
|
---|
| 15 | const inFilename = argv.defaults[0]
|
---|
| 16 |
|
---|
| 17 | let readStream
|
---|
| 18 | if (inFilename) {
|
---|
| 19 | readStream = fs.createReadStream(inFilename)
|
---|
| 20 | } else {
|
---|
| 21 | readStream = process.stdin
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | let json5 = ''
|
---|
| 25 | readStream.on('data', data => {
|
---|
| 26 | json5 += data
|
---|
| 27 | })
|
---|
| 28 |
|
---|
| 29 | readStream.on('end', () => {
|
---|
| 30 | let space
|
---|
| 31 | if (argv.space === 't' || argv.space === 'tab') {
|
---|
| 32 | space = '\t'
|
---|
| 33 | } else {
|
---|
| 34 | space = Number(argv.space)
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | let value
|
---|
| 38 | try {
|
---|
| 39 | value = JSON5.parse(json5)
|
---|
| 40 | if (!argv.validate) {
|
---|
| 41 | const json = JSON.stringify(value, null, space)
|
---|
| 42 |
|
---|
| 43 | let writeStream
|
---|
| 44 |
|
---|
| 45 | // --convert is for backward compatibility with v0.5.1. If
|
---|
| 46 | // specified with <file> and not --out-file, then a file with
|
---|
| 47 | // the same name but with a .json extension will be written.
|
---|
| 48 | if (argv.convert && inFilename && !argv.outFile) {
|
---|
| 49 | const parsedFilename = path.parse(inFilename)
|
---|
| 50 | const outFilename = path.format(
|
---|
| 51 | Object.assign(
|
---|
| 52 | parsedFilename,
|
---|
| 53 | {base: path.basename(parsedFilename.base, parsedFilename.ext) + '.json'}
|
---|
| 54 | )
|
---|
| 55 | )
|
---|
| 56 |
|
---|
| 57 | writeStream = fs.createWriteStream(outFilename)
|
---|
| 58 | } else if (argv.outFile) {
|
---|
| 59 | writeStream = fs.createWriteStream(argv.outFile)
|
---|
| 60 | } else {
|
---|
| 61 | writeStream = process.stdout
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | writeStream.write(json)
|
---|
| 65 | }
|
---|
| 66 | } catch (err) {
|
---|
| 67 | console.error(err.message)
|
---|
| 68 | process.exit(1)
|
---|
| 69 | }
|
---|
| 70 | })
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | function parseArgs () {
|
---|
| 74 | let convert
|
---|
| 75 | let space
|
---|
| 76 | let validate
|
---|
| 77 | let outFile
|
---|
| 78 | let version
|
---|
| 79 | let help
|
---|
| 80 | const defaults = []
|
---|
| 81 |
|
---|
| 82 | const args = process.argv.slice(2)
|
---|
| 83 | for (let i = 0; i < args.length; i++) {
|
---|
| 84 | const arg = args[i]
|
---|
| 85 | switch (arg) {
|
---|
| 86 | case '--convert':
|
---|
| 87 | case '-c':
|
---|
| 88 | convert = true
|
---|
| 89 | break
|
---|
| 90 |
|
---|
| 91 | case '--space':
|
---|
| 92 | case '-s':
|
---|
| 93 | space = args[++i]
|
---|
| 94 | break
|
---|
| 95 |
|
---|
| 96 | case '--validate':
|
---|
| 97 | case '-v':
|
---|
| 98 | validate = true
|
---|
| 99 | break
|
---|
| 100 |
|
---|
| 101 | case '--out-file':
|
---|
| 102 | case '-o':
|
---|
| 103 | outFile = args[++i]
|
---|
| 104 | break
|
---|
| 105 |
|
---|
| 106 | case '--version':
|
---|
| 107 | case '-V':
|
---|
| 108 | version = true
|
---|
| 109 | break
|
---|
| 110 |
|
---|
| 111 | case '--help':
|
---|
| 112 | case '-h':
|
---|
| 113 | help = true
|
---|
| 114 | break
|
---|
| 115 |
|
---|
| 116 | default:
|
---|
| 117 | defaults.push(arg)
|
---|
| 118 | break
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | return {
|
---|
| 123 | convert,
|
---|
| 124 | space,
|
---|
| 125 | validate,
|
---|
| 126 | outFile,
|
---|
| 127 | version,
|
---|
| 128 | help,
|
---|
| 129 | defaults,
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | function version () {
|
---|
| 134 | console.log(pkg.version)
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | function usage () {
|
---|
| 138 | console.log(
|
---|
| 139 | `
|
---|
| 140 | Usage: json5 [options] <file>
|
---|
| 141 |
|
---|
| 142 | If <file> is not provided, then STDIN is used.
|
---|
| 143 |
|
---|
| 144 | Options:
|
---|
| 145 |
|
---|
| 146 | -s, --space The number of spaces to indent or 't' for tabs
|
---|
| 147 | -o, --out-file [file] Output to the specified file, otherwise STDOUT
|
---|
| 148 | -v, --validate Validate JSON5 but do not output JSON
|
---|
| 149 | -V, --version Output the version number
|
---|
| 150 | -h, --help Output usage information`
|
---|
| 151 | )
|
---|
| 152 | }
|
---|