2023-10-16 10:26:10 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2023-10-20 11:37:04 -04:00
import wrap from "fast-word-wrap" ;
2023-10-16 10:26:10 -04:00
export class Logger {
2023-10-20 11:37:04 -04:00
constructor ( lineLength = 80 ) {
this . lineLength = lineLength ;
}
2023-10-16 10:26:10 -04:00
/**
* @param {string} message
*/
log ( message ) {
console . log ( message ) ;
return Promise . resolve ( ) ;
}
2023-11-17 12:14:11 -05:00
/**
* @param {{ oneLineOnly: boolean }} options
*/
hr ( { oneLineOnly } = { oneLineOnly : false } ) {
const rule = "*" . repeat ( this . lineLength ) ;
return oneLineOnly ? rule : [ "\n" , rule , "\n" ] . join ( "" ) ;
2023-10-20 11:37:04 -04:00
}
/**
* @param {string} message
*/
box ( message ) {
const linePrefix = "* " ;
const lineSuffix = " *" ;
const maxContentLength = this . lineLength - ( linePrefix + lineSuffix ) . length ;
const chunks = message
. split ( "\n" )
2024-10-16 11:53:18 -04:00
. flatMap ( ( l ) => l && wrap ( l , maxContentLength ) . split ( "\n" ) ) ;
2023-10-20 11:37:04 -04:00
/**
* @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 ( "" ) ;
}
2023-10-16 10:26:10 -04:00
/**
* Log a horizontal rule to the console. If a message is provided,
* log a section header.
* @param {string?} message
*/
logSeparator ( message ) {
if ( ! message ) {
2023-10-20 11:37:04 -04:00
console . log ( this . hr ( ) ) ;
2023-10-16 10:26:10 -04:00
} else {
2023-10-20 11:37:04 -04:00
console . log ( this . box ( message ) ) ;
2023-10-16 10:26:10 -04:00
}
}
}