forked from sanack/node-jq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
72 lines (64 loc) · 2.01 KB
/
command.js
File metadata and controls
72 lines (64 loc) · 2.01 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
import * as Joi from 'joi'
import path from 'path'
import {
parseOptions,
optionsSchema,
preSpawnSchema,
spawnSchema
} from './options'
const JQ_PATH = process.env.JQ_PATH || path.join(__dirname, '..', 'bin', 'jq')
export const FILTER_UNDEFINED_ERROR =
'node-jq: invalid filter argument supplied: "undefined"'
export const INPUT_JSON_UNDEFINED_ERROR =
'node-jq: invalid json argument supplied: "undefined"'
export const INPUT_STRING_ERROR =
'node-jq: invalid json string argument supplied'
const NODE_JQ_ERROR_TEMPLATE =
'node-jq: invalid {#label} ' +
'argument supplied{if(#label == "path" && #type == "json", " (not a .json file)", "")}' +
'{if(#label == "path" && #type == "path", " (not a valid path)", "")}: ' +
'"{if(#value != undefined, #value, "undefined")}"'
const messages = {
'any.invalid': NODE_JQ_ERROR_TEMPLATE,
'any.required': NODE_JQ_ERROR_TEMPLATE,
'string.base': NODE_JQ_ERROR_TEMPLATE,
'string.empty': NODE_JQ_ERROR_TEMPLATE
}
const validateArguments = (filter, json, options) => {
const context = { filter, json }
const validatedOptions = Joi.attempt(options, optionsSchema)
const validatedPreSpawn = Joi.attempt(
context,
preSpawnSchema.tailor(validatedOptions.input),
{ messages, errors: { wrap: { label: '' } } }
)
const validatedArgs = parseOptions(
validatedOptions,
validatedPreSpawn.filter,
validatedPreSpawn.json
)
const validatedSpawn = Joi.attempt(
{},
spawnSchema.tailor(validatedOptions.input),
{ context: { ...validatedPreSpawn, options: validatedOptions } }
)
if (validatedOptions.input === 'file') {
return {
args: validatedArgs,
stdin: validatedSpawn.stdin
}
}
return {
args: validatedArgs,
stdin: validatedSpawn.stdin
}
}
export const commandFactory = (filter, json, options = {}, jqPath) => {
const command = jqPath ? path.join(jqPath, './jq') : JQ_PATH
const result = validateArguments(filter, json, options)
return {
command,
args: result.args,
stdin: result.stdin
}
}