-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·185 lines (169 loc) · 5.38 KB
/
index.js
File metadata and controls
executable file
·185 lines (169 loc) · 5.38 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env node
import inquirer from 'inquirer';
import request from 'superagent';
import commandLineArgs from 'command-line-args';
import commandLineUsage from 'command-line-usage';
import fs from 'fs';
import branch from 'git-branch'; // included so it can be used from defaultFunction in config
const askQuestions = async (questions) => {
const answers = await inquirer.prompt(questions)
console.log(answers)
return answers
}
const addDefaultFunctionSupport = (questions) => {
return questions.map((q) => {
if (q.defaultFunction) {
q.default = eval(q.defaultFunction)
}
return q
})
}
const addChoicesFunctionSupport = (questions) => {
return questions.map((q) => {
if (q.choicesFunction) {
q.choices = eval(q.choicesFunction)
}
return q
})
}
const mapWhenFunctions = (questions) => {
return questions.map((q) => {
return {
...q,
when: eval(q.when)
}
})
}
const loadFlow = (params) => {
try {
let rawdata = fs.readFileSync(params.config_file)
var config = JSON.parse(rawdata)
config.questions = addDefaultFunctionSupport(config.questions)
config.questions = addChoicesFunctionSupport(config.questions)
config.questions = mapWhenFunctions(config.questions)
return config
} catch (err) {
console.log(err)
console.log('Could not read the config file ' + params.config_file)
process.exit(1)
}
}
const iterate = (obj, valueMap) => {
if (Array.isArray(obj)) {
return obj.map((el) => iterate(el, valueMap))
}
var res = {}
Object.keys(obj).forEach(key => {
if (typeof obj[key] === 'object') {
res[key] = iterate(obj[key], valueMap)
} else if (typeof obj[key] === 'string') {
res[key] = valueMap(obj[key])
} else {
res[key] = obj[key]
}
})
return res
}
const updateActionWithAnswers = (action, answers, questions) => {
return iterate(action, (value) => {
Object.keys(answers).forEach((param) => {
value = value.replace("{" + param + "}", answers[param])
})
// Check if value is an unfulfilled parameter (i.e. skipped by 'when')
// and substitute with a default value if available
const re = /(?<=\{).*(?=\})/
const match = value.match(re)
if (match) {
const param = match[0]
const q = questions.find(q => q.name == param)
if (q != null && q.default != undefined) {
if (typeof q.default === 'function') {
value = value.replace("{" + param + "}", q.default())
} else {
value = value.replace("{" + param + "}", q.default)
}
}
}
return value
})
}
const performAction = async (action) => {
if (action.type !== 'http-request') {
console.log("Unsupported action type " + action.type)
process.exit(1)
}
try {
var r = request(action.method, action.url)
Object.keys(action.headers || {}).forEach(key => {
r.set(key, action.headers[key])
})
return r.send(action.json_body)
} catch (err) {
console.log("Error making request: " + err)
}
}
const optionDefinitions = [{
name: 'config_file',
alias: 'c',
type: String,
defaultOption: true,
defaultValue: 'cli-invoke-config.json',
typeLabel: '{underline file}',
description: 'The JSON file with the definition of questions and action. Will look for cli-invoke-config.json in the current dir if not specified.'
}, {
name: 'help',
alias: 'h',
type: Boolean,
defaultValue: false,
description: 'To print this usage instruction'
}, {
name: 'verbose',
alias: 'v',
type: Boolean,
defaultValue: false,
description: 'Make more noise'
}, {
name: 'debug',
alias: 'd',
type: Boolean,
defaultValue: false,
description: 'Debug mode to omit things like running the action.'
},
]
const parseParameters = () => {
return commandLineArgs(optionDefinitions)
}
const printUsageInstructions = () => {
const sections = [{
header: 'cli-invoke',
content: 'cli-invoke will ask questions defined in a configuration file and invoke a web hook based on your answers'
},
{
header: 'Options',
optionList: optionDefinitions
}
]
const usage = commandLineUsage(sections)
console.log(usage)
}
const run = async () => {
const params = parseParameters()
if (params.help) {
printUsageInstructions()
return process.exit(0)
}
const flow = loadFlow(params)
try {
const answers = await askQuestions(flow.questions)
const action = updateActionWithAnswers(flow.action, answers, flow.questions)
if (params.verbose) console.log('Performing action...\n' + JSON.stringify(action, null, 2))
if (params.debug) { process.exit(0) }
const result = await performAction(action)
const response = result.res
console.log('Response: ' + response.statusCode + ' ' + response.statusMessage + '\n' + response.text)
} catch (err) {
console.log(err.message)
process.exit(1)
}
};
run()