• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

json5 / json5 / 473

pending completion
473

push

travis-ci-com

jordanbtucker
1.0.2

388 of 388 branches covered (100.0%)

Branch coverage included in aggregate %.

614 of 614 relevant lines covered (100.0%)

144.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/cli.js
1
#!/usr/bin/env node
2

3
import fs from 'fs'
4
import path from 'path'
5
import minimist from 'minimist'
6

7
import pkg from '../package.json'
8
import JSON5 from './'
9

10
const argv = minimist(process.argv.slice(2), {
30✔
11
    alias: {
12
        'convert': 'c',
13
        'space': 's',
14
        'validate': 'v',
15
        'out-file': 'o',
16
        'version': 'V',
17
        'help': 'h',
18
    },
19
    boolean: [
20
        'convert',
21
        'validate',
22
        'version',
23
        'help',
24
    ],
25
    string: [
26
        'space',
27
        'out-file',
28
    ],
29
})
30

31
if (argv.version) {
30✔
32
    version()
3✔
33
} else if (argv.help) {
27✔
34
    usage()
3✔
35
} else {
36
    const inFilename = argv._[0]
24✔
37

38
    let readStream
39
    if (inFilename) {
24✔
40
        readStream = fs.createReadStream(inFilename)
21✔
41
    } else {
42
        readStream = process.stdin
3✔
43
    }
44

45
    let json5 = ''
24✔
46
    readStream.on('data', data => {
24✔
47
        json5 += data
24✔
48
    })
49

50
    readStream.on('end', () => {
24✔
51
        let space
52
        if (argv.space === 't' || argv.space === 'tab') {
24✔
53
            space = '\t'
3✔
54
        } else {
55
            space = Number(argv.space)
21✔
56
        }
57

58
        let value
59
        try {
24✔
60
            value = JSON5.parse(json5)
24✔
61
            if (!argv.validate) {
21✔
62
                const json = JSON.stringify(value, null, space)
18✔
63

64
                let writeStream
65

66
                // --convert is for backward compatibility with v0.5.1. If
67
                // specified with <file> and not --out-file, then a file with
68
                // the same name but with a .json extension will be written.
69
                if (argv.convert && inFilename && !argv.o) {
18✔
70
                    const parsedFilename = path.parse(inFilename)
3✔
71
                    const outFilename = path.format(
3✔
72
                        Object.assign(
73
                            parsedFilename,
74
                            {base: path.basename(parsedFilename.base, parsedFilename.ext) + '.json'}
75
                        )
76
                    )
77

78
                    writeStream = fs.createWriteStream(outFilename)
3✔
79
                } else if (argv.o) {
15✔
80
                    writeStream = fs.createWriteStream(argv.o)
3✔
81
                } else {
82
                    writeStream = process.stdout
12✔
83
                }
84

85
                writeStream.write(json)
18✔
86
            }
87
        } catch (err) {
88
            console.error(err.message)
3✔
89
            process.exit(1)
3✔
90
        }
91
    })
92
}
93

94
function version () {
95
    console.log(pkg.version)
3✔
96
}
97

98
function usage () {
99
    console.log(
3✔
100
        `
101
  Usage: json5 [options] <file>
102

103
  If <file> is not provided, then STDIN is used.
104

105
  Options:
106

107
    -s, --space              The number of spaces to indent or 't' for tabs
108
    -o, --out-file [file]    Output to the specified file, otherwise STDOUT
109
    -v, --validate           Validate JSON5 but do not output JSON
110
    -V, --version            Output the version number
111
    -h, --help               Output usage information`
112
    )
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc