From 2073a2b6f062198d12fa158f9aa8f1d5513ffa66 Mon Sep 17 00:00:00 2001 From: Corey Pyle Date: Wed, 18 Oct 2023 16:48:23 -0400 Subject: [PATCH] JavaScript (v3): Add Scenario parser and move scenario files to their own folder. --- .../example_code/libs/scenario/index.js | 2 + .../libs/{ => scenario}/scenario-example.js | 2 +- .../libs/scenario/scenario-parser.js | 50 +++++++++++++++++++ .../libs/{ => scenario}/scenario.js | 15 ++++-- 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 javascriptv3/example_code/libs/scenario/index.js rename javascriptv3/example_code/libs/{ => scenario}/scenario-example.js (98%) create mode 100644 javascriptv3/example_code/libs/scenario/scenario-parser.js rename javascriptv3/example_code/libs/{ => scenario}/scenario.js (92%) diff --git a/javascriptv3/example_code/libs/scenario/index.js b/javascriptv3/example_code/libs/scenario/index.js new file mode 100644 index 00000000000..3da43209606 --- /dev/null +++ b/javascriptv3/example_code/libs/scenario/index.js @@ -0,0 +1,2 @@ +export * from "./scenario.js"; +export * from "./scenario-parser.js"; diff --git a/javascriptv3/example_code/libs/scenario-example.js b/javascriptv3/example_code/libs/scenario/scenario-example.js similarity index 98% rename from javascriptv3/example_code/libs/scenario-example.js rename to javascriptv3/example_code/libs/scenario/scenario-example.js index e5659d09541..cfb6576c485 100644 --- a/javascriptv3/example_code/libs/scenario-example.js +++ b/javascriptv3/example_code/libs/scenario/scenario-example.js @@ -8,7 +8,7 @@ import { ScenarioAction, ScenarioInput, ScenarioOutput, -} from "@aws-sdk-examples/libs/scenario.js"; +} from "./index.js"; const greet = new ScenarioOutput( "greet", diff --git a/javascriptv3/example_code/libs/scenario/scenario-parser.js b/javascriptv3/example_code/libs/scenario/scenario-parser.js new file mode 100644 index 00000000000..b58f9a340bb --- /dev/null +++ b/javascriptv3/example_code/libs/scenario/scenario-parser.js @@ -0,0 +1,50 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +import { parseArgs } from "node:util"; + +/** + * @param {Record} scenarios + */ +export function parseScenarioArgs(scenarios) { + const help = `Usage: +node . -s <${Object.keys(scenarios).join("|")}> +node . -h + +Options: +[-s|--scenario, ] [-h|--help] + +-s, --scenario The name of a scenario to run. +`; + + const { values } = parseArgs({ + options: { + help: { + type: "boolean", + short: "h", + }, + scenario: { + short: "s", + type: "string", + }, + }, + }); + + if (values.help) { + console.log(help); + return; + } + + if (!values.scenario) { + console.log(`Missing required argument: -s, --scenario\n\n${help}`); + return; + } + + if (!(values.scenario in scenarios)) { + throw new Error(`Invalid scenario: ${values.scenario}\n${help}`); + } + + scenarios[values.scenario].run(); +} diff --git a/javascriptv3/example_code/libs/scenario.js b/javascriptv3/example_code/libs/scenario/scenario.js similarity index 92% rename from javascriptv3/example_code/libs/scenario.js rename to javascriptv3/example_code/libs/scenario/scenario.js index 3b24d9d0360..db18b7da19c 100644 --- a/javascriptv3/example_code/libs/scenario.js +++ b/javascriptv3/example_code/libs/scenario/scenario.js @@ -3,11 +3,11 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { Prompter } from "./prompter.js"; -import { Logger } from "./logger.js"; -import { SlowLogger } from "./slow-logger.js"; +import { Prompter } from "../prompter.js"; +import { Logger } from "../logger.js"; +import { SlowLogger } from "../slow-logger.js"; -class Step { +export class Step { /** * @param {string} name */ @@ -21,6 +21,13 @@ class Step { get key() { return this.name; } + + /** + * @param {Record} context + */ + handle(context) { + console.log(JSON.stringify(context)); + } } export class ScenarioOutput extends Step {