1 | /**
|
2 | * Copyright IBM Corp. 2019, 2019
|
3 | *
|
4 | * This source code is licensed under the Apache-2.0 license found in the
|
5 | * LICENSE file in the root directory of this source tree.
|
6 | */
|
7 |
|
8 | ;
|
9 |
|
10 | const chalk = require('chalk');
|
11 |
|
12 | /**
|
13 | * Create a logger to be used in a handler. This is typically just for
|
14 | * formatting the output, adding a prefix, and connecting the output with
|
15 | * box-drawing ASCII characters.
|
16 | * @returns {object}
|
17 | */
|
18 | function createLogger(command) {
|
19 | let start;
|
20 |
|
21 | /**
|
22 | * Display the given message with a box character. This also includes
|
23 | * formatting for the logger prefix and box character itself.
|
24 | * @param {string} boxCharacter
|
25 | * @param {string?} message
|
26 | * @returns {void}
|
27 | */
|
28 | function log(boxCharacter, message = '') {
|
29 | console.log(chalk`{yellow ${command} ▐} {gray ${boxCharacter}} ${message}`);
|
30 | }
|
31 |
|
32 | return {
|
33 | info(message) {
|
34 | log('┣', chalk.gray(message));
|
35 | },
|
36 | start(message) {
|
37 | start = Date.now();
|
38 | log('┏', message);
|
39 | },
|
40 | stop(message) {
|
41 | const duration = ((Date.now() - start) / 1000).toFixed(2);
|
42 | if (message) {
|
43 | log('┗', message);
|
44 | } else {
|
45 | log('┗', chalk`{gray Done in {italic ${duration}s}}`);
|
46 | }
|
47 | },
|
48 | newline() {
|
49 | log('┃');
|
50 | },
|
51 | };
|
52 | }
|
53 |
|
54 | /**
|
55 | * Display the banner in the console, typically at the beginning of a handler
|
56 | * @returns {void}
|
57 | */
|
58 | function displayBanner() {
|
59 | console.log(`
|
60 | _
|
61 | | |
|
62 | ___ __ _ _ __| |__ ___ _ __
|
63 | / __/ _\` | '__| '_ \\ / _ \\| '_ \\
|
64 | | (_| (_| | | | |_) | (_) | | | |
|
65 | \\___\\__,_|_| |_.__/ \\___/|_| |_|
|
66 |
|
67 | `);
|
68 | }
|
69 |
|
70 | module.exports = {
|
71 | createLogger,
|
72 | displayBanner,
|
73 | };
|