Skip to content

Commit

Permalink
JavaScript (v3): Make logSeparator more flexible.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpyle0819 committed Oct 19, 2023
1 parent 2073a2b commit 10734fb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
27 changes: 16 additions & 11 deletions javascriptv3/example_code/libs/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import wrap from "fast-word-wrap";

export class Logger {
/**
* @param {string} message
Expand All @@ -21,17 +23,20 @@ export class Logger {
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",
);
const maxLengthPerLine = 74;
const chunks = message
.split("\n")
.map((l) => l && wrap(l, maxLengthPerLine).split("\n"))
.flat();

const header = `
${"*".repeat(80)}
${chunks
.map((c) => `* ${c + " ".repeat(maxLengthPerLine - c.length)} *`)
.join("\n")}
${"*".repeat(80)}
`;
console.log(header);
}
}
}
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"
}
}
10 changes: 8 additions & 2 deletions javascriptv3/example_code/libs/scenario/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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 +53,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 10734fb

Please sign in to comment.