migrate superfluous info from readme to docs
This commit is contained in:
parent
743e917733
commit
5e866ca8d2
6 changed files with 175 additions and 170 deletions
|
@ -13,7 +13,7 @@ import {
|
|||
writeFile,
|
||||
} from 'fs/promises';
|
||||
|
||||
import { log } from './log.js';
|
||||
import { log } from '../src/log.js';
|
||||
|
||||
/**
|
||||
* Validates a string as a MediaEntry id
|
||||
|
|
|
@ -1,103 +0,0 @@
|
|||
import chalk from 'chalk';
|
||||
|
||||
/**
|
||||
* Formats the given date for logging
|
||||
* @param {Date} date the date to format
|
||||
* @returns {string} the formatted date string in yyyy/mm/dd hh/mm/ss/SSS
|
||||
*/
|
||||
export const time = (date) => {
|
||||
/**
|
||||
* Stringifies the given number, prepends with 0s if the number string isn't long enough
|
||||
* @param {number} num the number to stringify
|
||||
* @param {number} expectedLength how long the number should be
|
||||
* @returns {string} the stringified number
|
||||
*/
|
||||
const stringifyNumber = (num, expectedLength) => {
|
||||
let str = `${num}`;
|
||||
while (str.length < expectedLength) {
|
||||
str = `0${str}`;
|
||||
}
|
||||
return str;
|
||||
};
|
||||
|
||||
const year = date.getFullYear()
|
||||
const month = stringifyNumber(date.getMonth(), 2);
|
||||
const day = stringifyNumber(date.getDay(), 2);
|
||||
const hour = stringifyNumber(date.getHours(), 2);
|
||||
const minute = stringifyNumber(date.getMinutes(), 2);
|
||||
const second = stringifyNumber(date.getSeconds(), 2);
|
||||
const millisecond = stringifyNumber(date.getMilliseconds(), 2);
|
||||
|
||||
return `${year}/${month}/${day} ${hour}:${minute}:${second}.${millisecond}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a message to the console
|
||||
* @param {string} message the message to log
|
||||
* @param {'debug'|'info'|'log'|'warn'|'error'} kind the kind of message being logged; denotes logging level
|
||||
* - debug is lowest level of message (0)
|
||||
* - info is the second lowest level of message (1)
|
||||
* - log is the default level level of message (2)
|
||||
* - warn is the second highest level of message (3)
|
||||
* - error is the highest level of message (4)
|
||||
* @param {string} context the context; if undefined, assume part of a parent context and indent the message
|
||||
* @param {string} extraData any extra data (e.g. if there was an error, pass the error object here)
|
||||
*/
|
||||
export const log = (message, kind, context, extraData) => {
|
||||
const logLevel = process.env.npm_config_logging || 2;
|
||||
const formatted = `${chalk.bold(`[${time(new Date())}] `)}${context ? context + ': ' : ' '}${message}`;
|
||||
switch (kind) {
|
||||
case 'debug': {
|
||||
if (logLevel > 0) {
|
||||
return;
|
||||
}
|
||||
if (extraData) {
|
||||
console.debug(`${chalk.gray.bold('[debug]')}${formatted}`, extraData);
|
||||
} else {
|
||||
console.debug(`${chalk.gray.bold('[debug]')}${formatted}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'info': {
|
||||
if (logLevel > 1) {
|
||||
return;
|
||||
}
|
||||
if (extraData) {
|
||||
console.info(`${chalk.white.bold('[info]')}${formatted}`, extraData);
|
||||
} else {
|
||||
console.info(`${chalk.white.bold('[info]')}${formatted}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'warn': {
|
||||
if (logLevel > 3) {
|
||||
return;
|
||||
}
|
||||
if (extraData) {
|
||||
console.warn(`${chalk.yellow.bold('[warn]')}${formatted}`, extraData);
|
||||
} else {
|
||||
console.warn(`${chalk.yellow.bold('[warn]')}${formatted}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'error': {
|
||||
if (extraData) {
|
||||
console.error(`${chalk.red.bold('[error]')}${formatted}`, extraData);
|
||||
} else {
|
||||
console.error(`${chalk.red.bold('[error]')}${formatted}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
if (logLevel > 2) {
|
||||
return;
|
||||
}
|
||||
if (extraData) {
|
||||
console.log(`${chalk.blue.bold('[log]')}${formatted}`, extraData);
|
||||
} else {
|
||||
console.log(`${chalk.blue.bold('[log]')}${formatted}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue