From 01b95e74f48cf66b149a5dc47f4e143de0ba0e1d Mon Sep 17 00:00:00 2001 From: Traines Date: Sat, 11 Jan 2025 20:52:14 +0000 Subject: [PATCH] add back cli --- index.js | 8 ++-- lib/default-profile.js | 1 + tools/debug-cli/cli.js | 80 ++++++++++++++++++++++++++++++++++++ tools/debug-cli/package.json | 14 +++++++ 4 files changed, 100 insertions(+), 3 deletions(-) create mode 100755 tools/debug-cli/cli.js create mode 100644 tools/debug-cli/package.json diff --git a/index.js b/index.js index 3177fb05..72acbc7a 100644 --- a/index.js +++ b/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; }); diff --git a/lib/default-profile.js b/lib/default-profile.js index 67ac6edf..5c2e3e49 100644 --- a/lib/default-profile.js +++ b/lib/default-profile.js @@ -51,6 +51,7 @@ const notImplemented = (_ctx, _x) => { }; const defaultProfile = { + DEBUG, request, products, ageGroup, ageGroupFromAge, ageGroupLabel, diff --git a/tools/debug-cli/cli.js b/tools/debug-cli/cli.js new file mode 100755 index 00000000..f8edacee --- /dev/null +++ b/tools/debug-cli/cli.js @@ -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); diff --git a/tools/debug-cli/package.json b/tools/debug-cli/package.json new file mode 100644 index 00000000..43793218 --- /dev/null +++ b/tools/debug-cli/package.json @@ -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 ", + "license": "ISC", + "engines": { + "node": ">=16.17" + } +}