Skip to content

Commit

Permalink
JavaScript (v3): Add --verbose|-v flag to scenario parser. (#5556)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpyle0819 authored Oct 23, 2023
1 parent 170b53c commit 25dcb14
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
25 changes: 23 additions & 2 deletions javascriptv3/example_code/libs/scenario/scenario-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ node . -s <${Object.keys(scenarios).join("|")}>
node . -h
Options:
[-s|--scenario, <scenario>] [-h|--help] [-y|--yes]
[-s|--scenario, <scenario>] [-h|--help] [-y|--yes] [-v|--verbose]
-h, --help Show this help message.
-s, --scenario The name of a scenario to run.
-y, --yes Assume "yes" to all prompts.
-v, --verbose Show debug information.
`;

const { values } = parseArgs({
Expand All @@ -35,6 +36,10 @@ Options:
short: "y",
type: "boolean",
},
verbose: {
short: "v",
type: "boolean",
},
},
});

Expand All @@ -52,5 +57,21 @@ Options:
throw new Error(`Invalid scenario: ${values.scenario}\n${help}`);
}

scenarios[values.scenario].run({ confirmAll: values.yes });
if (values.verbose) {
console.log(
`[DEBUG ${new Date().toISOString()}] Running scenario: ${
scenarios[values.scenario].name
}`,
);
console.log(
`[DEBUG ${new Date().toISOString()}] Context: ${JSON.stringify(
scenarios[values.scenario].context,
)}`,
);
}

scenarios[values.scenario].run({
confirmAll: values.yes,
verbose: values.verbose,
});
}
45 changes: 33 additions & 12 deletions javascriptv3/example_code/libs/scenario/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@ export class Step {
}

/**
* @param {Record<string, any>} context
* @param {Record<string, any>} context,
* @param {{ verbose: boolean }} options
*/
handle(context) {
console.log(JSON.stringify(context));
return Promise.resolve();
handle(context, { verbose }) {
if (verbose) {
console.log(
`[DEBUG ${new Date().toISOString()}] Handling step: ${
this.constructor.name
}<${this.name}>`,
);
console.log(
`[DEBUG ${new Date().toISOString()}] Context: ${JSON.stringify(
context,
)}`,
);
}
}
}

Expand All @@ -40,8 +51,11 @@ export class ScenarioOutput extends Step {

/**
* @param {Record<string, any>} context
* @param {{ verbose: boolean }} options
*/
async handle(context) {
async handle(context, options) {
super.handle(context, options);

const output =
typeof this.value === "function" ? this.value(context) : this.value;
const paddingTop = "\n";
Expand Down Expand Up @@ -72,9 +86,11 @@ export class ScenarioInput extends Step {

/**
* @param {Record<string, any>} context
* @param {boolean} confirmAll
* @param {{ confirmAll: boolean, verbose: boolean }} options
*/
async handle(context, confirmAll = false) {
async handle(context, options) {
super.handle(context, options);

const choices =
this.options.choices && typeof this.options.choices[0] === "string"
? this.options.choices.map((s) => ({ name: s, value: s }))
Expand All @@ -93,7 +109,7 @@ export class ScenarioInput extends Step {
} else if (this.options.type === "input") {
context[this.name] = await this.prompter.input({ message: this.prompt });
} else if (this.options.type === "confirm") {
if (confirmAll) {
if (options.confirmAll) {
return;
}

Expand All @@ -118,7 +134,12 @@ export class ScenarioAction extends Step {
this.action = action;
}

async handle(context) {
/**
* @param {Record<string, any>} context
* @param {{ verbose: boolean }} options
*/
async handle(context, options) {
super.handle(context, options);
await this.action(context);
}
}
Expand All @@ -141,11 +162,11 @@ export class Scenario {
}

/**
* @param {{ confirmAll: boolean }} runConfig
* @param {{ confirmAll: boolean, verbose: boolean }} runConfig
*/
async run({ confirmAll }) {
async run(runConfig) {
for (const step of this.steps) {
await step.handle(this.context, confirmAll);
await step.handle(this.context, runConfig);
}
}
}

0 comments on commit 25dcb14

Please sign in to comment.