Skip to content

Commit

Permalink
Upgrade yargs
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpurra committed Aug 6, 2021
1 parent 1ceda64 commit bc78df7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -39,15 +39,15 @@
"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",
"@types/bluebird": "^3.5.36",
"@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",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -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[]) => {
Expand Down
25 changes: 12 additions & 13 deletions src/runtime-configurator.ts
Expand Up @@ -57,7 +57,7 @@ export type RuntimeConfiguration = {
export type RuntimeConfigurationKeys = keyof RuntimeConfiguration;
export type RuntimeConfigurationTypes = readonly number[] | number | string | boolean | undefined;

const getYargsArgv = (): ReadonlyDeep<Argv["argv"]> => {
const getYargsArgv = async (): Promise<ReadonlyDeep<Argv["argv"]>> => {
const packageJson = getJsonSync("../package.json");
const appBinaryName = Object.keys(packageJson.bin)[0];
const appDescription: string = packageJson.description;
Expand Down Expand Up @@ -85,11 +85,12 @@ const getYargsArgv = (): ReadonlyDeep<Argv["argv"]> => {

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) : {};
Expand Down Expand Up @@ -190,17 +191,15 @@ const getYargsArgv = (): ReadonlyDeep<Argv["argv"]> => {
.epilogue(epilogue);
/* eslint-enable @typescript-eslint/prefer-readonly-parameter-types */

return yargs.argv;
return parser.parseAsync();
};

const mapArgv = (argv: ReadonlyDeep<Argv["argv"]>): RuntimeConfiguration => {
const mapArgv = async (argv: ReadonlyDeep<Argv["argv"]>): Promise<RuntimeConfiguration> => {
// 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
);
Expand All @@ -213,7 +212,7 @@ const mapArgv = (argv: ReadonlyDeep<Argv["argv"]>): RuntimeConfiguration => {
value2,
vendor,
verbose,
} = argv;
} = argv as Record<string, unknown>;

assert(typeof address === "number");
assert(typeof cmd === "string");
Expand Down Expand Up @@ -246,9 +245,9 @@ const mapArgv = (argv: ReadonlyDeep<Argv["argv"]>): RuntimeConfiguration => {
return mappedArgv;
};

export default function runtimeConfigurator(): RuntimeConfiguration {
const rawArgv = getYargsArgv();
const argv = mapArgv(rawArgv);
export default async function runtimeConfigurator(): Promise<RuntimeConfiguration> {
const rawArgv = await getYargsArgv();
const argv = await mapArgv(rawArgv);

return argv;
}

0 comments on commit bc78df7

Please sign in to comment.