mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-22 14:49:36 +02:00
add back cli
This commit is contained in:
parent
911ac17510
commit
01b95e74f4
4 changed files with 100 additions and 3 deletions
8
index.js
8
index.js
|
@ -27,14 +27,16 @@ const validateLocation = (loc, name = 'location') => {
|
|||
}
|
||||
};
|
||||
|
||||
const loadEnrichedStationData = () => new Promise((resolve, reject) => {
|
||||
const loadEnrichedStationData = (profile) => new Promise((resolve, reject) => {
|
||||
const items = {};
|
||||
readStations.full()
|
||||
.on('data', (station) => {
|
||||
items[station.id] = station;
|
||||
})
|
||||
.once('end', () => {
|
||||
console.info('Loaded station index.');
|
||||
if (profile.DEBUG) {
|
||||
console.log('Loaded station index.');
|
||||
}
|
||||
resolve(items);
|
||||
})
|
||||
.once('error', (err) => {
|
||||
|
@ -47,7 +49,7 @@ const createClient = (profile, userAgent, opt = {}) => {
|
|||
validateProfile(profile);
|
||||
const common = {};
|
||||
if (opt.enrichStations !== false) {
|
||||
loadEnrichedStationData()
|
||||
loadEnrichedStationData(profile)
|
||||
.then(locations => {
|
||||
common.locations = locations;
|
||||
});
|
||||
|
|
|
@ -51,6 +51,7 @@ const notImplemented = (_ctx, _x) => {
|
|||
};
|
||||
|
||||
const defaultProfile = {
|
||||
DEBUG,
|
||||
request,
|
||||
products,
|
||||
ageGroup, ageGroupFromAge, ageGroupLabel,
|
||||
|
|
80
tools/debug-cli/cli.js
Executable file
80
tools/debug-cli/cli.js
Executable file
|
@ -0,0 +1,80 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
import {parseArgs} from 'node:util';
|
||||
import {createClient} from '../../index.js';
|
||||
|
||||
const showError = (err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
};
|
||||
|
||||
const toString = val => String(val);
|
||||
const parseJsObject = val => {
|
||||
const res = eval(`(${val})`);
|
||||
return res && 'object' === typeof res
|
||||
? res
|
||||
: {};
|
||||
};
|
||||
|
||||
const methodsAndTheirArgs = [
|
||||
['departures', 0, toString],
|
||||
['departures', 1, parseJsObject],
|
||||
['arrivals', 0, toString],
|
||||
['arrivals', 1, parseJsObject],
|
||||
['journeys', 0, toString],
|
||||
['journeys', 1, toString],
|
||||
['journeys', 2, parseJsObject],
|
||||
['refreshJourney', 0, toString],
|
||||
['refreshJourney', 1, parseJsObject],
|
||||
['journeysFromTrip', 0, toString],
|
||||
['journeysFromTrip', 1, parseJsObject],
|
||||
['journeysFromTrip', 2, toString],
|
||||
['journeysFromTrip', 3, parseJsObject],
|
||||
['locations', 0, toString],
|
||||
['locations', 1, parseJsObject],
|
||||
['stop', 0, toString],
|
||||
['stop', 1, parseJsObject],
|
||||
['nearby', 0, parseJsObject],
|
||||
['nearby', 1, parseJsObject],
|
||||
['trip', 0, toString],
|
||||
['trip', 1, parseJsObject],
|
||||
['tripsByName', 0, toString],
|
||||
['tripsByName', 1, parseJsObject],
|
||||
['radar', 0, parseJsObject],
|
||||
['radar', 1, parseJsObject],
|
||||
['reachableFrom', 0, parseJsObject],
|
||||
['reachableFrom', 1, parseJsObject],
|
||||
['remarks', 0, parseJsObject],
|
||||
['lines', 0, toString],
|
||||
['lines', 1, parseJsObject],
|
||||
['serverInfo', 0, parseJsObject],
|
||||
];
|
||||
|
||||
const {
|
||||
positionals: args,
|
||||
} = parseArgs({
|
||||
strict: true,
|
||||
allowPositionals: true,
|
||||
});
|
||||
|
||||
const profileName = args[0];
|
||||
const fnName = args[1];
|
||||
|
||||
const parsedArgs = args.slice(2)
|
||||
.map((arg, i) => {
|
||||
const parser = methodsAndTheirArgs.find(([_fnName, _i]) => _fnName === fnName && _i === i);
|
||||
return parser
|
||||
? parser[2](arg)
|
||||
: arg;
|
||||
});
|
||||
(async () => {
|
||||
const {profile} = await import(`../../p/${profileName}/index.js`);
|
||||
|
||||
const client = createClient(profile, 'db-vendo-client debug CLI');
|
||||
|
||||
const fn = client[fnName];
|
||||
|
||||
const res = await fn(...parsedArgs);
|
||||
process.stdout.write(JSON.stringify(res) + '\n');
|
||||
})()
|
||||
.catch(showError);
|
14
tools/debug-cli/package.json
Normal file
14
tools/debug-cli/package.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"private": true,
|
||||
"name": "debug-cli",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"bin": {
|
||||
"db-vendo-client-debug-cli": "./cli.js"
|
||||
},
|
||||
"author": "Jannis R <mail@jannisr.de>",
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": ">=16.17"
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue