@bubblewrap/core
Version:
Core Library to generate, build and sign TWA projects
115 lines (114 loc) • 3.73 kB
JavaScript
"use strict";
/*
* Copyright 2019 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConsoleLog = void 0;
;
/**
* An utility class to print nice Log messages.
*/
class ConsoleLog {
/**
* Creates a new Log instance
* @param tag The tag used when logging. Printed at the beggining of a log message.
* @param verbose If the Log is verbose. Debug messages are only printed on verbose logs.
* @param output Where to output the log messages.
*/
constructor(tag = '', verbose = false, output = console) {
this.tag = tag;
this.verbose = verbose;
this.prefix = this.inverse(tag);
this.output = output;
}
/**
* Prints a debug message to the Log. Message is ignored if the Log is not set to verbose.
* @param message The message the be printed.
* @param args Extra arguments for the console.
*/
debug(message, ...args) {
if (!this.verbose) {
return;
}
this.log(this.output.log, this.dim(message), ...args);
}
/**
* Prints an info message to the Log. Message is ignored if the Log is not set to verbose.
* @param message The message the be printed.
* @param args Extra arguments for the console.
*/
info(message, ...args) {
this.log(this.output.log, message, ...args);
}
/**
* Prints an warning message to the Log. Message is ignored if the Log is not set to verbose.
* @param message The message the be printed.
* @param args Extra arguments for the console.
*/
warn(message, ...args) {
this.log(this.output.warn, this.yellow('WARNING ' + message), ...args);
}
/**
* Prints an error message to the Log. Message is ignored if the Log is not set to verbose.
* @param message The message the be printed.
* @param args Extra arguments for the console.
*/
error(message, ...args) {
this.output.error('\n');
this.log(this.output.error, this.red('ERROR ' + message), ...args);
this.output.error('\n');
}
/**
* Sets the verbose.
* @param verbose The verbose value to set.
*/
setVerbose(verbose) {
this.verbose = verbose;
}
/**
* Creates a new Log using the same output and verbositity of the current Log.
* @param newTag The tag the be used on the new Log instance.
*/
newLog(newTag) {
if (this.tag) {
newTag = this.tag + ' ' + newTag;
}
return new ConsoleLog(newTag, this.verbose, this.output);
}
log(fn, message, ...args) {
if (this.prefix) {
message = this.prefix + ' ' + message;
}
if (args) {
fn(...[message].concat(args));
}
else {
fn(message);
}
}
inverse(input) {
return `\x1b[7m${input}\x1b[0m`;
}
dim(input) {
return `\x1b[36m${input}\x1b[0m`;
}
yellow(input) {
return `\x1b[33m${input}\x1b[0m`;
}
red(input) {
return `\x1b[31m${input}\x1b[0m`;
}
}
exports.ConsoleLog = ConsoleLog;