Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript (v3): Make scenario logging better. #5547

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions javascriptv3/example_code/libs/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

import wrap from "fast-word-wrap";

export class Logger {
constructor(lineLength = 80) {
this.lineLength = lineLength;
}

/**
* @param {string} message
*/
Expand All @@ -12,26 +18,47 @@ export class Logger {
return Promise.resolve();
}

hr() {
return "\n", "*".repeat(this.lineLength), "\n";
}

/**
* @param {string} message
*/
box(message) {
const linePrefix = "* ";
const lineSuffix = " *";

const maxContentLength = this.lineLength - (linePrefix + lineSuffix).length;
const chunks = message
.split("\n")
.map((l) => l && wrap(l, maxContentLength).split("\n"))
.flat();

return `
${"*".repeat(this.lineLength)}
cpyle0819 marked this conversation as resolved.
Show resolved Hide resolved
${chunks
.map(
(c) =>
`${linePrefix}${
c + " ".repeat(maxContentLength - c.length)
}${lineSuffix}`,
)
.join("\n")}
${"*".repeat(this.lineLength)}
`;
}

/**
* Log a horizontal rule to the console. If a message is provided,
* log a section header.
* @param {string?} message
*/
logSeparator(message) {
if (!message) {
cpyle0819 marked this conversation as resolved.
Show resolved Hide resolved
console.log("\n", "*".repeat(80), "\n");
console.log(this.hr());
} else {
console.log(
"\n",
"*".repeat(80),
"\n",
"** ",
message,
" ".repeat(80 - message.length - 8),
"**\n",
"*".repeat(80),
"\n",
);
console.log(this.box(message));
}
}
}
1 change: 1 addition & 0 deletions javascriptv3/example_code/libs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@aws-sdk/client-lambda": "^3.168.0",
"@types/archiver": "^5.3.1",
"archiver": "^5.3.1",
"fast-word-wrap": "^1.1.0",
"uuid": "^9.0.0"
}
}
6 changes: 3 additions & 3 deletions javascriptv3/example_code/libs/scenario/scenario-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

const greet = new ScenarioOutput(
"greet",
"Hi! This is a scenario. It can handle your " + "inputs and outputs.",
"Hi! This is a scenario. It can handle your inputs and outputs.",
);

const describeInput = new ScenarioOutput(
Expand All @@ -29,7 +29,7 @@ const getFoods = new ScenarioInput("foods", "Choose your favorite foods.", {
const getAge = new ScenarioInput(
"age",
"Select your age range (my apologies if you're younger or older than the provided ranges):",
{ type: "select", choices: ["18-30", "31-50", "50-100"] },
{ type: "select", choices: ["18-30", "31-50", "51-100"] },
);

const getName = new ScenarioInput("name", "What's your name?", {
Expand All @@ -48,7 +48,7 @@ const describeOutput = new ScenarioOutput(
const dynamicOutput = new ScenarioOutput(
"dynamicOutput",
/**
* @param {{ "foods": string[] }} c
* @param {{ name: string, age: string, foods: string[] }} c
*/
(c) =>
cpyle0819 marked this conversation as resolved.
Show resolved Hide resolved
`Hi, ${c.name}! You are between the ages of ${c.age}. ` +
Expand Down
17 changes: 8 additions & 9 deletions javascriptv3/example_code/libs/scenario/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ export class Step {
this.name = name;
}

/**
* Alias for "name".
*/
get key() {
return this.name;
}

/**
* @param {Record<string, any>} context
*/
Expand All @@ -34,7 +27,7 @@ export class ScenarioOutput extends Step {
/**
* @param {string} name
* @param {string | (context: Record<string, any>) => string} value
* @param {{ slow: boolean }} options
* @param {{ slow: boolean, header: boolean }} options
*/
constructor(name, value, options = { slow: true }) {
super(name);
Expand All @@ -53,7 +46,13 @@ export class ScenarioOutput extends Step {
const paddingTop = "\n";
const paddingBottom = "\n";
const logger = this.options.slow ? this.slowLogger : this.logger;
await logger.log(paddingTop + output + paddingBottom);
const message = paddingTop + output + paddingBottom;

if (this.options.header) {
this.logger.logSeparator(message);
} else {
await logger.log(message);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions javascriptv3/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.