This repository has been archived by the owner on Oct 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·91 lines (79 loc) · 2.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict'
const fs = require('fs')
const jsPath = require('jspath')
const packageJson = require('./package.json')
const minimist = require('minimist')
module.exports = (process) => {
var argv = minimist(process.argv.slice(2), {
boolean: ['p', 's'],
alias: {
h: 'help',
p: 'pretty',
s: 'strict'
}
})
process.printUsage = printUsage.bind(process)
process.makeError = makeError.bind(process)
process.readStdin = readStdin.bind(process)
process.dataRead = dataRead.bind(process)
process.exitCode = 0
process.flags = {
pretty: argv.pretty,
strict: argv.strict
}
if (argv.help) return process.printUsage()
else if (argv.version) return process.stdout.write(`Version: ${packageJson.version}\n`) && process.exit()
if (argv._.length === 0) return process.makeError('No JSPath specified.')
else process.readStdin(argv._[0], argv._[1])
}
function readStdin (jsPathQuery, filePathArg) {
let data = []
this.stdin.setEncoding('utf8')
this.stdin.on('readable', () => {
const chunk = this.stdin.read()
if (chunk === null && data.length === 0) {
this.stdin.destroy()
if (typeof filePathArg !== 'string') return this.makeError('File or stdin needs to be specified.')
fs.readFile(filePathArg, 'utf8', (err, data) => {
if (err) return this.makeError(err.message)
this.dataRead(jsPathQuery, data)
})
} else data.push(chunk)
})
this.stdin.on('end', () => data.length > 0 && this.dataRead(jsPathQuery, data.join('')))
}
function dataRead (jsPathQuery, data) {
let parsedData
try {
parsedData = JSON.parse(data)
} catch (e) {
return this.makeError(`JSON parse error: ${e.message}`)
}
try {
const res = jsPath.apply(jsPathQuery, parsedData)
const normalized = Array.isArray(res) ? res : [res]
if (normalized.length === 0) this.stdout.write('\n')
else if (normalized.length === 1 && !this.flags.strict && typeof normalized[0] === 'string') this.stdout.write(`${normalized[0]}\n`)
else if (normalized.length === 1 && !this.flags.strict) this.stdout.write(`${JSON.stringify(normalized[0], null, this.flags.pretty ? 4 : 0)}\n`)
else this.stdout.write(`${JSON.stringify(normalized, null, this.flags.pretty ? 4 : 0)}\n`)
} catch (e) {
return this.makeError(`JSPath error: ${e.message}`)
}
this.exit()
}
function makeError (error) {
this.stderr.write(`${error}\n`)
this.exitCode = 1
this.printUsage()
}
function printUsage () {
this.stdout.write(`Usage:
jspath <options> <jspath> (file | [-])
Options:
-h --help Show this message.
--version Show version.
-p --pretty Enable pretty printing.
-s --strict Enable strict mode, always conform to the JSON spec.
`)
this.exit()
}