mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00

* feat: (AppRunner) Mechanism to configure cli args and derive componentsjs vars from them implemented * fix: (AppRunner) tidying * fix: (AppRunner) tidying up * fix: (AppRunner) runCli method made sync * fix; (VarResolver) refactored to multiple files, and other stylistic fixes. * chore: (AppRunner) Uses builder pattern for yargs base arguments setup to enable better typescript inference * fix(AppRunner): refactoring AppRunner and VarResolver * fix(AppRunner): refactoring AppRunner promise handling * fix(AppRunner): verror dependency removal * fix: Simplify CLI error handling * feat: Use same config for both CLI and app instantiation * fix: Update typings and imports * feat: Split VariableResolver behaviour to 2 classes * feat: Move default value behaviour from CLI to ValueComputers * test: Add unit tests for new CLI classes * feat: Integrate new CLI configuration with all default configurations * feat: Add createApp function to AppRunner * docs: Update comments in CLI-related classes * fix: Various fixes and refactors Co-authored-by: damooo <damodara@protonmail.com>
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
/* eslint-disable tsdoc/syntax */
|
|
import type { Arguments, Argv, Options } from 'yargs';
|
|
import yargs from 'yargs';
|
|
import { CliExtractor } from './CliExtractor';
|
|
|
|
export type YargsArgOptions = Record<string, Options>;
|
|
|
|
export interface CliOptions {
|
|
// Usage string printed in case of CLI errors
|
|
usage?: string;
|
|
// Errors on unknown CLI parameters when enabled.
|
|
// @see https://yargs.js.org/docs/#api-reference-strictenabledtrue
|
|
strictMode?: boolean;
|
|
// Loads CLI args from environment variables when enabled.
|
|
// @see http://yargs.js.org/docs/#api-reference-envprefix
|
|
loadFromEnv?: boolean;
|
|
// Prefix to be used when `loadFromEnv` is enabled.
|
|
// @see http://yargs.js.org/docs/#api-reference-envprefix
|
|
envVarPrefix?: string;
|
|
}
|
|
|
|
/**
|
|
* Parses CLI args using the yargs library.
|
|
* Specific settings can be enabled through the provided options.
|
|
*/
|
|
export class YargsCliExtractor extends CliExtractor {
|
|
protected readonly yargsArgOptions: YargsArgOptions;
|
|
protected readonly yargvOptions: CliOptions;
|
|
|
|
/**
|
|
* @param parameters - Parameters that should be parsed from the CLI. @range {json}
|
|
* Format details can be found at https://yargs.js.org/docs/#api-reference-optionskey-opt
|
|
* @param options - Additional options to configure yargs. @range {json}
|
|
*/
|
|
public constructor(parameters: YargsArgOptions = {}, options: CliOptions = {}) {
|
|
super();
|
|
this.yargsArgOptions = parameters;
|
|
this.yargvOptions = options;
|
|
}
|
|
|
|
public async handle(argv: readonly string[]): Promise<Arguments> {
|
|
return this.createYArgv(argv).parse();
|
|
}
|
|
|
|
/**
|
|
* Creates the yargs Argv object based on the input CLI argv.
|
|
*/
|
|
private createYArgv(argv: readonly string[]): Argv {
|
|
let yArgv = yargs(argv.slice(2));
|
|
if (this.yargvOptions.usage !== undefined) {
|
|
yArgv = yArgv.usage(this.yargvOptions.usage);
|
|
}
|
|
if (this.yargvOptions.strictMode) {
|
|
yArgv = yArgv.strict();
|
|
}
|
|
if (this.yargvOptions.loadFromEnv) {
|
|
yArgv = yArgv.env(this.yargvOptions.envVarPrefix ?? '');
|
|
}
|
|
return yArgv.options(this.yargsArgOptions);
|
|
}
|
|
}
|