db-vendo-client/tools/debug-cli/cli.js

77 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2019-07-02 20:50:33 +02:00
#!/usr/bin/env node
2023-04-25 00:32:45 +02:00
import {parseArgs} from 'node:util'
2022-05-07 16:17:37 +02:00
import {createClient} from '../../index.js'
2019-07-02 20:50:33 +02:00
const showError = (err) => {
console.error(err)
process.exit(1)
}
2019-07-04 18:50:11 +02:00
const toString = val => val + ''
2020-02-03 20:15:47 +01:00
const parseJsObject = val => {
const res = eval(`(${val})`)
return res && 'object' === typeof res ? res : {}
}
2023-04-25 00:32:45 +02:00
const methodsAndTheirArgs = [
2019-07-04 18:50:11 +02:00
['departures', 0, toString],
2020-02-03 20:15:47 +01:00
['departures', 1, parseJsObject],
2019-07-04 18:50:11 +02:00
['arrivals', 0, toString],
2020-02-03 20:15:47 +01:00
['arrivals', 1, parseJsObject],
2019-07-04 18:50:11 +02:00
['journeys', 0, toString],
['journeys', 1, toString],
2020-02-03 20:15:47 +01:00
['journeys', 2, parseJsObject],
2019-07-04 18:50:11 +02:00
['refreshJourney', 0, toString],
2020-02-03 20:15:47 +01:00
['refreshJourney', 1, parseJsObject],
2018-10-02 16:36:37 +02:00
['journeysFromTrip', 0, toString],
['journeysFromTrip', 1, parseJsObject],
['journeysFromTrip', 2, toString],
['journeysFromTrip', 3, parseJsObject],
2019-07-04 18:50:11 +02:00
['locations', 0, toString],
2020-02-03 20:15:47 +01:00
['locations', 1, parseJsObject],
2019-07-04 18:50:11 +02:00
['stop', 0, toString],
2020-02-03 20:15:47 +01:00
['stop', 1, parseJsObject],
['nearby', 0, parseJsObject],
['nearby', 1, parseJsObject],
2019-07-04 18:50:11 +02:00
['trip', 0, toString],
['trip', 1, parseJsObject],
['tripsByName', 0, toString],
['tripsByName', 1, parseJsObject],
2020-02-03 20:15:47 +01:00
['radar', 0, parseJsObject],
['radar', 1, parseJsObject],
['reachableFrom', 0, parseJsObject],
['reachableFrom', 1, parseJsObject],
2020-03-09 20:54:43 +01:00
['remarks', 0, parseJsObject],
2020-03-09 21:44:35 +01:00
['lines', 0, toString],
['lines', 1, parseJsObject],
['serverInfo', 0, parseJsObject],
2019-07-04 18:50:11 +02:00
]
2023-04-25 00:32:45 +02:00
const {
positionals: args,
} = parseArgs({
strict: true,
allowPositionals: true,
})
2019-07-02 20:50:33 +02:00
2023-04-25 00:32:45 +02:00
const profileName = args[0]
const fnName = args[1]
2019-07-04 18:50:11 +02:00
2023-04-25 00:32:45 +02:00
const parsedArgs = args.slice(2).map((arg, i) => {
const parser = methodsAndTheirArgs.find(([_fnName, _i]) => _fnName === fnName && _i === i)
2019-07-04 18:50:11 +02:00
return parser ? parser[2](arg) : arg
})
2019-07-02 20:50:33 +02:00
2022-05-07 16:17:37 +02:00
;(async () => {
2023-04-25 00:32:45 +02:00
const {profile} = await import(`../../p/${profileName}/index.js`)
2022-05-07 16:17:37 +02:00
const client = createClient(profile, 'hafas-client debug CLI')
const fn = client[fnName]
2023-04-25 00:32:45 +02:00
const res = await fn(...parsedArgs)
2019-07-02 20:50:33 +02:00
process.stdout.write(JSON.stringify(res) + '\n')
2022-05-07 16:17:37 +02:00
})()
2019-07-02 20:50:33 +02:00
.catch(showError)