feat(proxy-cli): supports nested sequences

This commit is contained in:
Julien Fontanet 2021-02-26 17:23:49 +01:00
parent 322f2a1728
commit d920a97f4f

View File

@ -64,6 +64,9 @@ ${pkg.name} v${pkg.version}`
)
}
// sequence path of the current call
const callPath = []
const baseRequest = {
headers: {
'content-type': 'application/json',
@ -79,8 +82,11 @@ ${pkg.name} v${pkg.version}`
baseRequest.hostname = hostname
baseRequest.port = port
}
const call = async ({ method, params }) => {
if (callPath.length !== 0) {
process.stderr.write(`\ncall #${callPath.join('.')}\n`)
}
const call = async (method, params) => {
const response = await hrp.post(baseRequest, {
body: format.request(0, method, params),
})
@ -133,24 +139,30 @@ ${pkg.name} v${pkg.version}`
}
}
const seq = async seq => {
const j = callPath.length
for (let i = 0, n = seq.length; i < n; ++i) {
callPath[j] = i + 1
await visit(seq[i])
}
callPath.pop()
}
const visit = node => {
if (Array.isArray(node)) {
return seq(node)
}
return call(node)
}
if (file !== '') {
let data = fs.readFileSync(file, 'utf8')
const data = fs.readFileSync(file, 'utf8')
const ext = extname(file).slice(1).toLowerCase()
const parse = FORMATS[ext]
if (parse === undefined) {
throw new Error(`unsupported file: ${file}`)
}
data = parse(data)
if (!Array.isArray(data)) {
data = [data]
}
for (let i = 0, n = data.length; i < n; ++i) {
process.stderr.write(`\n${i}-th call...\n`)
const { method, params } = data[i]
await call(method, params)
}
await visit(parse(data))
} else {
const method = args[0]
const params = {}
@ -163,7 +175,7 @@ ${pkg.name} v${pkg.version}`
params[param.slice(0, j)] = parseValue(param.slice(j + 1))
}
await call(method, params)
await call({ method, params })
}
}
main(process.argv.slice(2)).then(