Skip to content

Commit

Permalink
JavaScript (v3): Add Scenario parser and move scenario files to their…
Browse files Browse the repository at this point in the history
… own folder.
  • Loading branch information
cpyle0819 committed Oct 18, 2023
1 parent b1323ba commit 2073a2b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
2 changes: 2 additions & 0 deletions javascriptv3/example_code/libs/scenario/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./scenario.js";
export * from "./scenario-parser.js";
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ScenarioAction,
ScenarioInput,
ScenarioOutput,
} from "@aws-sdk-examples/libs/scenario.js";
} from "./index.js";

const greet = new ScenarioOutput(
"greet",
Expand Down
50 changes: 50 additions & 0 deletions javascriptv3/example_code/libs/scenario/scenario-parser.js
Original file line number Diff line number Diff line change
@@ -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<string, import('./scenario.js').Scenario>} scenarios
*/
export function parseScenarioArgs(scenarios) {
const help = `Usage:
node . -s <${Object.keys(scenarios).join("|")}>
node . -h
Options:
[-s|--scenario, <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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -21,6 +21,13 @@ class Step {
get key() {
return this.name;
}

/**
* @param {Record<string, any>} context
*/
handle(context) {
console.log(JSON.stringify(context));
}
}

export class ScenarioOutput extends Step {
Expand Down

0 comments on commit 2073a2b

Please sign in to comment.