-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JavaScript (v3): libs - Refactor and add new utility. (#5492)
* JavaScript (v3): libs - Move Prompter.logSeparator to Logger.logSeparator. * JavaScript (v3): Add scenario runner.
- Loading branch information
1 parent
5864ef3
commit 6c9b6b8
Showing
8 changed files
with
190 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export class Logger { | ||
/** | ||
* @param {string} message | ||
*/ | ||
log(message) { | ||
console.log(message); | ||
return Promise.resolve(); | ||
} | ||
|
||
/** | ||
* Log a horizontal rule to the console. If a message is provided, | ||
* log a section header. | ||
* @param {string?} message | ||
*/ | ||
logSeparator(message) { | ||
if (!message) { | ||
console.log("\n", "*".repeat(80), "\n"); | ||
} else { | ||
console.log( | ||
"\n", | ||
"*".repeat(80), | ||
"\n", | ||
"** ", | ||
message, | ||
" ".repeat(80 - message.length - 8), | ||
"**\n", | ||
"*".repeat(80), | ||
"\n", | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Prompter } from "./prompter.js"; | ||
import { Logger } from "./logger.js"; | ||
import { SlowLogger } from "./slow-logger.js"; | ||
|
||
class Step { | ||
/** | ||
* @param {string} name | ||
*/ | ||
constructor(name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
export class ScenarioOutput extends Step { | ||
/** | ||
* @param {string} name | ||
* @param {string | (context: Record<string, any>) => string} value | ||
* @param {{ slow: boolean }} options | ||
*/ | ||
constructor(name, value, options = { slow: true }) { | ||
super(name); | ||
this.value = value; | ||
this.options = options; | ||
this.slowLogger = new SlowLogger(20); | ||
this.logger = new Logger(); | ||
} | ||
|
||
/** | ||
* @param {Record<string, any>} context | ||
*/ | ||
async handle(context) { | ||
const output = | ||
typeof this.value === "function" ? this.value(context) : this.value; | ||
const logger = this.options.slow ? this.slowLogger : this.logger; | ||
await logger.log(JSON.stringify(output)); | ||
} | ||
} | ||
|
||
export class ScenarioInput extends Step { | ||
/** | ||
* @param {string} name | ||
* @param {string} prompt | ||
* @param {{ type: "input" | "multi-select" | "select", choices: { name: string, value: string }[]} options | ||
*/ | ||
constructor(name, prompt, options) { | ||
super(name); | ||
this.prompt = prompt; | ||
this.options = options; | ||
this.prompter = new Prompter(); | ||
} | ||
|
||
/** | ||
* @param {Record<string, any>} context | ||
*/ | ||
async handle(context) { | ||
if (this.options.type === "multi-select") { | ||
context[this.name] = await this.prompter.checkbox({ | ||
message: this.prompt, | ||
choices: this.options.choices, | ||
}); | ||
} else if (this.options.type === "select") { | ||
context[this.name] = await this.prompter.select({ | ||
message: this.prompt, | ||
choices: this.options.choices, | ||
}); | ||
} else if (this.options.type === "input") { | ||
context[this.name] = await this.prompter.input({ message: this.prompt }); | ||
} else { | ||
throw new Error( | ||
`Error handling ScenarioInput, ${this.options.type} is not supported.`, | ||
); | ||
} | ||
} | ||
} | ||
|
||
export class ScenarioAction extends Step { | ||
/** | ||
* | ||
* @param {string} name | ||
* @param {(context: Record<string, any>) => Promise<void>} action | ||
*/ | ||
constructor(name, action) { | ||
super(name); | ||
this.action = action; | ||
} | ||
|
||
async handle(context) { | ||
await this.action(context); | ||
} | ||
} | ||
|
||
export class Scenario { | ||
/** | ||
* @type {Record<string, any>} | ||
*/ | ||
context = {}; | ||
|
||
/** | ||
* @param {(ScenarioOutput | ScenarioInput | ScenarioAction)[]} steps | ||
*/ | ||
constructor(steps = []) { | ||
this.steps = steps; | ||
} | ||
|
||
async run() { | ||
for (const step of this.steps) { | ||
await step.handle(this.context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.