From 947c5d364acf9717187c3b0a86e9da1f5d7eff9a Mon Sep 17 00:00:00 2001 From: Jannis R Date: Tue, 22 Feb 2022 20:21:04 +0100 Subject: [PATCH] tools: highest-endpoint-version -> endpoint-hci-version --- tools/endpoint-hci-version/cli.js | 49 +++++++++++ .../package.json | 4 +- tools/highest-endpoint-version/cli.js | 83 ------------------- 3 files changed, 51 insertions(+), 85 deletions(-) create mode 100755 tools/endpoint-hci-version/cli.js rename tools/{highest-endpoint-version => endpoint-hci-version}/package.json (60%) delete mode 100755 tools/highest-endpoint-version/cli.js diff --git a/tools/endpoint-hci-version/cli.js b/tools/endpoint-hci-version/cli.js new file mode 100755 index 00000000..e34b0211 --- /dev/null +++ b/tools/endpoint-hci-version/cli.js @@ -0,0 +1,49 @@ +#!/usr/bin/env node +'use strict' + +const mri = require('mri') + +const argv = mri(process.argv.slice(2), { + boolean: [ + 'help', 'h', + 'silent', 's', + ] +}) + +if (argv.help || argv.h) { + process.stdout.write(` +Usage: + endpoint-hci-version +Options: + --silent -s Output just the version instead of a pretty + represenation. +Examples: + endpoint-hci-version oebb +\n`) + process.exit(0) +} + +const {join} = require('path') +const createClient = require('../..') + +const profile = require(join(__dirname, '..', '..', 'p', argv._[0])) +const client = createClient( + profile, + 'hafas-client-endpoint-hci-version', +) + +const silent = argv.silent || argv.s + +;(async () => { + const {hciVersion: v} = await client.serverInfo() + + if ('string' !== typeof v || !v) { + throw new Error('invalid/unexpected server response') + } + if (silent) console.log(v) + else console.log(v + ' reported as the endpoint version ✔︎') +})() +.catch((err) => { + console.error(err) + process.exit(1) +}) diff --git a/tools/highest-endpoint-version/package.json b/tools/endpoint-hci-version/package.json similarity index 60% rename from tools/highest-endpoint-version/package.json rename to tools/endpoint-hci-version/package.json index fc2f8227..f2178771 100644 --- a/tools/highest-endpoint-version/package.json +++ b/tools/endpoint-hci-version/package.json @@ -1,9 +1,9 @@ { "private": true, - "name": "find-highest-endpoint-version", + "name": "endpoint-hci-version", "version": "1.0.0", "bin": { - "hafas-client-find-highest-endpoint-version": "./cli.js" + "hafas-client-endpoint-hci-version": "./cli.js" }, "author": "Jannis R ", "license": "ISC", diff --git a/tools/highest-endpoint-version/cli.js b/tools/highest-endpoint-version/cli.js deleted file mode 100755 index a4405360..00000000 --- a/tools/highest-endpoint-version/cli.js +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env node -'use strict' - -const mri = require('mri') - -const argv = mri(process.argv.slice(2), { - boolean: [ - 'help', 'h', - 'silent', 's', - ] -}) - -if (argv.help || argv.h) { - process.stdout.write(` -Usage: - highest-endpoint-version -Options: - --silent -s Output just the version instead of a pretty - represenation. -Examples: - highest-endpoint-version oebb -\n`) - process.exit(0) -} - -const {join} = require('path') -const createClient = require('../..') - -const profile = require(join(__dirname, '..', '..', 'p', argv._[0])) -const id = (ctx, val) => val -const origTransformReqBody = profile.transformReqBody || id - -const checkWithVersion = async (version) => { - const transformReqBody = (ctx, body) => { - body = origTransformReqBody(ctx, body) - return {...body, ver: version} - } - const client = createClient({ - ...profile, transformReqBody - }, 'hafas-client-highest-endpoint-version') - - try { - await client.locations('foo', {results: 1}) - } catch (err) { - if (err && err.code === 'VERSION') return false - const wrapped = new Error(`check for ${version} support failed with an unknown error`) - wrapped.originalError = err - throw wrapped - } - return true -} - -const silent = argv.silent || argv.s - -;(async () => { - const parseVer = ver => parseInt(ver.split('.')[1]) - let baseMinor = 16 - if ('string' === typeof profile.ver && profile.ver) { - baseMinor = parseVer(profile.ver) - } else { - const ctx = {opt: {}} - const body = profile.transformReqBody(ctx, {}) - if ('string' === typeof body.ver && body.ver) { - baseMinor = parseVer(body.ver) - } - } - - for (let minor = baseMinor; minor <= 70; minor++) { - const v = '1.' + minor - if (await checkWithVersion(v)) { - if (!silent) console.log(v + ' is supported ✔︎') - } else { - if (silent) console.log(v) - else console.log(v + ' is not supported ✘') - process.exit(0) - } - } - process.exit(1) -})() -.catch((err) => { - console.error(err) - process.exit(1) -})