Skip to content

Commit

Permalink
JavaScript (v3): Make scenario logging better. (#5547)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpyle0819 authored and ford-at-aws committed Dec 15, 2023
1 parent d1ff56e commit d99c31c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
50 changes: 38 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,46 @@ export class Logger {
return Promise.resolve();
}

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

/**
* @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();

/**
* @param {string} c
*/
const fill = (c) => c + " ".repeat(maxContentLength - c.length);

/**
* @param {string} c
*/
const line = (c) => `${linePrefix}${fill(c)}${lineSuffix}`;

return [this.hr(), chunks.map(line).join("\n"), this.hr()].join("");
}

/**
* 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");
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) =>
`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.

0 comments on commit d99c31c

Please sign in to comment.