From bc78df74d7652f7d2c32670b9c65dce1b5608b72 Mon Sep 17 00:00:00 2001 From: Joel Purra Date: Fri, 6 Aug 2021 12:10:37 +0200 Subject: [PATCH] Upgrade yargs --- package-lock.json | 30 +++++++++++++++--------------- package.json | 4 ++-- src/index.ts | 2 +- src/runtime-configurator.ts | 25 ++++++++++++------------- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/package-lock.json b/package-lock.json index 706c1b3..840cef7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,7 @@ "sort-keys": "^4.2.0", "stream-to-promise": "^3.0.0", "uvc-control": "github:joelpurra/node-uvc-control#v2", - "yargs": "^16.2.0" + "yargs": "^17.1.0" }, "bin": { "uvcc": "dist/index.js" @@ -30,7 +30,7 @@ "@types/engine-check": "^1.1.1", "@types/stream-to-promise": "^2.2.1", "@types/usb": "^1.5.3", - "@types/yargs": "^16.0.4", + "@types/yargs": "^17.0.2", "eslint-config-joelpurra": "github:joelpurra/eslint-config-joelpurra#semver:^v3.0.0", "husky": "^4.3.8", "rimraf": "^3.0.2", @@ -748,9 +748,9 @@ } }, "node_modules/@types/yargs": { - "version": "16.0.4", - "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz", - "integrity": "sha512-T8Yc9wt/5LbJyCaLiHPReJa0kApcIgJ7Bn735GjItUfh08Z1pJvu8QZqb9s+mMvKV6WUQRV7K2R46YbjMXTTJw==", + "version": "17.0.2", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.2.tgz", + "integrity": "sha512-JhZ+pNdKMfB0rXauaDlrIvm+U7V4m03PPOSVoPS66z8gf+G4Z/UW8UlrVIj2MRQOBzuoEvYtjS0bqYwnpZaS9Q==", "dev": true, "dependencies": { "@types/yargs-parser": "*" @@ -9583,9 +9583,9 @@ } }, "node_modules/yargs": { - "version": "16.2.0", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", - "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", + "version": "17.1.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.1.0.tgz", + "integrity": "sha512-SQr7qqmQ2sNijjJGHL4u7t8vyDZdZ3Ahkmo4sc1w5xI9TBX0QDdG/g4SFnxtWOsGLjwHQue57eFALfwFCnixgg==", "dependencies": { "cliui": "^7.0.2", "escalade": "^3.1.1", @@ -9596,7 +9596,7 @@ "yargs-parser": "^20.2.2" }, "engines": { - "node": ">=10" + "node": ">=12" } }, "node_modules/yargs-parser": { @@ -10194,9 +10194,9 @@ } }, "@types/yargs": { - "version": "16.0.4", - "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz", - "integrity": "sha512-T8Yc9wt/5LbJyCaLiHPReJa0kApcIgJ7Bn735GjItUfh08Z1pJvu8QZqb9s+mMvKV6WUQRV7K2R46YbjMXTTJw==", + "version": "17.0.2", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.2.tgz", + "integrity": "sha512-JhZ+pNdKMfB0rXauaDlrIvm+U7V4m03PPOSVoPS66z8gf+G4Z/UW8UlrVIj2MRQOBzuoEvYtjS0bqYwnpZaS9Q==", "dev": true, "requires": { "@types/yargs-parser": "*" @@ -16899,9 +16899,9 @@ "dev": true }, "yargs": { - "version": "16.2.0", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", - "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", + "version": "17.1.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.1.0.tgz", + "integrity": "sha512-SQr7qqmQ2sNijjJGHL4u7t8vyDZdZ3Ahkmo4sc1w5xI9TBX0QDdG/g4SFnxtWOsGLjwHQue57eFALfwFCnixgg==", "requires": { "cliui": "^7.0.2", "escalade": "^3.1.1", diff --git a/package.json b/package.json index b0eec24..a339477 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "sort-keys": "^4.2.0", "stream-to-promise": "^3.0.0", "uvc-control": "github:joelpurra/node-uvc-control#v2", - "yargs": "^16.2.0" + "yargs": "^17.1.0" }, "devDependencies": { "@sindresorhus/tsconfig": "^1.0.2", @@ -47,7 +47,7 @@ "@types/engine-check": "^1.1.1", "@types/stream-to-promise": "^2.2.1", "@types/usb": "^1.5.3", - "@types/yargs": "^16.0.4", + "@types/yargs": "^17.0.2", "eslint-config-joelpurra": "github:joelpurra/eslint-config-joelpurra#semver:^v3.0.0", "husky": "^4.3.8", "rimraf": "^3.0.2", diff --git a/src/index.ts b/src/index.ts index b73b5df..8a4d9d4 100755 --- a/src/index.ts +++ b/src/index.ts @@ -45,7 +45,7 @@ import UvcDeviceLister from "./uvc-device-lister"; const mainAsync = async () => { try { // NOTE: ignoring unhandled rejections and exceptions, as there is (practically) nothing to gracefully shut down. - const runtimeConfig = runtimeConfigurator(); + const runtimeConfig = await runtimeConfigurator(); const output = new Output(runtimeConfig.verbose); process.on("unhandledRejection", (...args: readonly unknown[]) => { diff --git a/src/runtime-configurator.ts b/src/runtime-configurator.ts index f914ede..713ad65 100644 --- a/src/runtime-configurator.ts +++ b/src/runtime-configurator.ts @@ -57,7 +57,7 @@ export type RuntimeConfiguration = { export type RuntimeConfigurationKeys = keyof RuntimeConfiguration; export type RuntimeConfigurationTypes = readonly number[] | number | string | boolean | undefined; -const getYargsArgv = (): ReadonlyDeep => { +const getYargsArgv = async (): Promise> => { const packageJson = getJsonSync("../package.json"); const appBinaryName = Object.keys(packageJson.bin)[0]; const appDescription: string = packageJson.description; @@ -85,11 +85,12 @@ const getYargsArgv = (): ReadonlyDeep => { fromImplicitConfigFile = configFromNearestConfigPath; } + const parserRoot: Argv = yargs(process.argv.slice(2)); /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */ - yargs + const parser = parserRoot .strict() - .wrap(yargs.terminalWidth()) + .wrap(parserRoot.terminalWidth()) .config(fromImplicitConfigFile) .config("config", "Load command arguments from a JSON file.", (argumentConfigPath) => { const fromExplicitConfigFile = argumentConfigPath ? getJsonSync(argumentConfigPath) : {}; @@ -190,17 +191,15 @@ const getYargsArgv = (): ReadonlyDeep => { .epilogue(epilogue); /* eslint-enable @typescript-eslint/prefer-readonly-parameter-types */ - return yargs.argv; + return parser.parseAsync(); }; -const mapArgv = (argv: ReadonlyDeep): RuntimeConfiguration => { +const mapArgv = async (argv: ReadonlyDeep): Promise => { // NOTE HACK: workaround yargs not being consistent with yargs.cmd versus yargs._ for defined/non-defined commands. - // eslint-disable-next-line @typescript-eslint/dot-notation - const cmd = typeof argv["cmd"] === "string" - // eslint-disable-next-line @typescript-eslint/dot-notation + const cmd = "cmd" in argv && typeof argv["cmd"] === "string" ? argv["cmd"] : ( - typeof argv._[0] === "string" + "_" in argv && typeof argv._[0] === "string" ? argv._[0] : null ); @@ -213,7 +212,7 @@ const mapArgv = (argv: ReadonlyDeep): RuntimeConfiguration => { value2, vendor, verbose, - } = argv; + } = argv as Record; assert(typeof address === "number"); assert(typeof cmd === "string"); @@ -246,9 +245,9 @@ const mapArgv = (argv: ReadonlyDeep): RuntimeConfiguration => { return mappedArgv; }; -export default function runtimeConfigurator(): RuntimeConfiguration { - const rawArgv = getYargsArgv(); - const argv = mapArgv(rawArgv); +export default async function runtimeConfigurator(): Promise { + const rawArgv = await getYargsArgv(); + const argv = await mapArgv(rawArgv); return argv; }