CoolFace
Apppublic

sourav-das/stem-separator

sourceHugging Facemitupdated 6mo agoView on Hugging Face
3likes
cli.js153 linesDownload Raw Back to lib
1#!/usr/bin/env node2 3const fs = require('fs')4const path = require('path')5const pkg = require('../package.json')6const JSON5 = require('./')7 8const argv = parseArgs()9 10if (argv.version) {11    version()12} else if (argv.help) {13    usage()14} else {15    const inFilename = argv.defaults[0]16 17    let readStream18    if (inFilename) {19        readStream = fs.createReadStream(inFilename)20    } else {21        readStream = process.stdin22    }23 24    let json5 = ''25    readStream.on('data', data => {26        json5 += data27    })28 29    readStream.on('end', () => {30        let space31        if (argv.space === 't' || argv.space === 'tab') {32            space = '\t'33        } else {34            space = Number(argv.space)35        }36 37        let value38        try {39            value = JSON5.parse(json5)40            if (!argv.validate) {41                const json = JSON.stringify(value, null, space)42 43                let writeStream44 45                // --convert is for backward compatibility with v0.5.1. If46                // specified with <file> and not --out-file, then a file with47                // 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.stdout62                }63 64                writeStream.write(json)65            }66        } catch (err) {67            console.error(err.message)68            process.exit(1)69        }70    })71}72 73function parseArgs () {74    let convert75    let space76    let validate77    let outFile78    let version79    let help80    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 = true89            break90 91        case '--space':92        case '-s':93            space = args[++i]94            break95 96        case '--validate':97        case '-v':98            validate = true99            break100 101        case '--out-file':102        case '-o':103            outFile = args[++i]104            break105 106        case '--version':107        case '-V':108            version = true109            break110 111        case '--help':112        case '-h':113            help = true114            break115 116        default:117            defaults.push(arg)118            break119        }120    }121 122    return {123        convert,124        space,125        validate,126        outFile,127        version,128        help,129        defaults,130    }131}132 133function version () {134    console.log(pkg.version)135}136 137function 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 tabs147    -o, --out-file [file]    Output to the specified file, otherwise STDOUT148    -v, --validate           Validate JSON5 but do not output JSON149    -V, --version            Output the version number150    -h, --help               Output usage information`151    )152}153