-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjfq.js
More file actions
84 lines (72 loc) · 2.04 KB
/
Copy pathjfq.js
File metadata and controls
84 lines (72 loc) · 2.04 KB
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
const { colorize } = require('json-colorizer');
import jsonata from 'jsonata'
import readInput from 'read-input'
import getopts from './getopts'
import YAML from 'js-yaml'
const main = async () => {
const { files, ndjson, json, yamlIn, yamlOut, query, plainText } = await getopts(process.argv)
const evaluator = parseQuery(query)
const data = await readInput(files)
for (const file of data.files) {
if (file.error) {
throw file.error
} else {
const input = yamlIn ? parseYaml(file.data, file.name) : parseJson(file.data, file.name)
const result = await evaluator.evaluate(input)
const output = yamlOut ? formatYaml(result) : formatJson(result, ndjson, json, plainText)
console.log(output)
}
}
}
const parseQuery = query => {
try {
return jsonata(query)
} catch (err) {
throw new Error('Failed to compile JSONata expression: ' + err.message)
}
}
const formatJson = (data, ndjson, json, plainText) => {
if (typeof data === 'undefined') {
return ''
}
if (!json) {
if (typeof data === 'string') {
return data
}
if (isSimpleArray(data)) {
return data.join('\n')
}
}
const formatted = ndjson ? JSON.stringify(data) : JSON.stringify(data, null, 2)
return plainText ? formatted : colorize(formatted)
}
// Is it an array containing only simple types
const isSimpleArray = arr => Array.isArray(arr) && !arr.some(i => typeof i !== 'string' && typeof i !== 'number')
const parseYaml = (string, fileName) => {
try {
return YAML.load(string, { json: true })
} catch (err) {
if (fileName) {
throw new Error(err.message + ' in file ' + fileName)
} else {
throw err
}
}
}
const formatYaml = yaml => typeof yaml === 'undefined' ? '' : YAML.dump(yaml)
const parseJson = (string, fileName) => {
try {
return JSON.parse(string)
} catch (err) {
if (fileName) {
throw new Error(err.message + ' in file ' + fileName)
} else {
throw err
}
}
}
main()
.catch(err => {
console.error(err.message)
process.exit(1)
})