@augment-vir/common
Version:
A collection of augments, helpers types, functions, and classes for any JavaScript environment.
144 lines (143 loc) • 5.08 kB
JavaScript
import { perEnv, RuntimeEnv } from '@augment-vir/core';
/**
* Supported log output types.
*
* @category Log : Util
* @category Package : @augment-vir/common
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export var LogOutputType;
(function (LogOutputType) {
/** Logged to stdout if the current environment supports it, or just `console.log`. */
LogOutputType["Standard"] = "stdout";
/** Logged to stderr if the current environment supports it, or just `console.error`. */
LogOutputType["Error"] = "stderr";
})(LogOutputType || (LogOutputType = {}));
/**
* Standardized color keys for logging. If you want to use customized colors, use
* [ansi-styles](https://www.npmjs.com/package/ansi-styles) in Node.js or [custom
* CSS](https://developer.mozilla.org/docs/Web/API/console#styling_console_output) in browsers.
*
* @category Log : Util
* @category Package : @augment-vir/common
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export var LogColorKey;
(function (LogColorKey) {
LogColorKey["Bold"] = "bold";
LogColorKey["Debug"] = "debug";
LogColorKey["Error"] = "error";
LogColorKey["Faint"] = "faint";
LogColorKey["Info"] = "info";
LogColorKey["Mutate"] = "mutate";
LogColorKey["NormalWeight"] = "normalWeight";
LogColorKey["Plain"] = "plain";
LogColorKey["Reset"] = "reset";
LogColorKey["Success"] = "success";
LogColorKey["Warning"] = "warning";
})(LogColorKey || (LogColorKey = {}));
async function determineDefaultLogColors() {
return await perEnv({
/** We calculate coverage in web, so the node code will never run in coverage tests. */
/* node:coverage disable */
async [RuntimeEnv.Node]() {
const styles = (await import('ansi-styles')).default;
return {
[]: styles.bold.open,
[]: styles.blueBright.open,
[]: styles.red.open,
[]: styles.gray.open,
[]: styles.cyan.open,
[]: styles.magenta.open,
[]: '\x1b[22m',
[]: '',
[]: styles.reset.open,
[]: styles.green.open,
[]: styles.yellow.open,
};
},
/* node:coverage enable */
[]() {
return Promise.resolve({
[]: 'font-weight: bold',
[]: 'color: blue',
[]: 'color: red',
[]: 'color: grey',
[]: 'color: teal',
[]: 'color: magenta',
[]: '',
[]: '',
[]: '',
[]: 'color: green',
[]: 'color: orange',
});
},
});
}
/**
* Mapping of color keys to the current color string.
*
* @category Log : Util
* @category Package : @augment-vir/common
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export const logColors = await determineDefaultLogColors();
/**
* Default implementation of {@link LogColorConfig}.
*
* @category Log : Util
* @category Package : @augment-vir/common
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export const defaultLogColorConfig = {
[]: {
colors: [logColors.bold],
logType: LogOutputType.Standard,
},
[]: {
colors: [logColors.debug],
logType: LogOutputType.Standard,
},
[]: {
colors: [logColors.faint],
logType: LogOutputType.Standard,
},
[]: {
colors: [logColors.info],
logType: LogOutputType.Standard,
},
[]: {
colors: [
logColors.mutate,
logColors.bold,
],
logType: LogOutputType.Standard,
},
[]: {
colors: [logColors.normalWeight],
logType: LogOutputType.Standard,
},
[]: { colors: [], logType: LogOutputType.Standard },
[]: {
colors: [logColors.reset],
logType: LogOutputType.Standard,
},
[]: {
colors: [
logColors.success,
logColors.bold,
],
logType: LogOutputType.Standard,
},
[]: {
colors: [
logColors.error,
logColors.bold,
],
logType: LogOutputType.Error,
},
[]: {
colors: [logColors.warning],
logType: LogOutputType.Error,
},
};