-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathindex.ts
62 lines (55 loc) · 1.98 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { isDebug, isTest, isCI } from "std-env";
import { LogLevels, LogLevel } from "./constants";
import type { ConsolaOptions } from "./types";
import { BasicReporter } from "./reporters/basic";
import { FancyReporter } from "./reporters/fancy";
import { ConsolaInstance, createConsola as _createConsola } from "./consola";
export * from "./shared";
/**
* Factory function to create a new Consola instance tailored for use in different environments.
* It automatically adjusts logging levels based on environment variables and execution context.
*
* @param {Partial<ConsolaOptions & { fancy: boolean }>} [options={}] - Optional configuration options. See {@link ConsolaOptions}.
* @returns {ConsolaInstance} A new Consola instance with configurations based on the given options and the execution environment.
*/
export function createConsola(
options: Partial<ConsolaOptions & { fancy: boolean }> = {},
): ConsolaInstance {
// Log level
let level = _getDefaultLogLevel();
if (process.env.CONSOLA_LEVEL) {
level = Number.parseInt(process.env.CONSOLA_LEVEL) ?? level;
}
// Create new consola instance
const consola = _createConsola({
level: level as LogLevel,
defaults: { level },
stdout: process.stdout,
stderr: process.stderr,
prompt: (...args) => import("./prompt").then((m) => m.prompt(...args)),
reporters: options.reporters || [
(options.fancy ?? !(isCI || isTest))
? new FancyReporter()
: new BasicReporter(),
],
...options,
});
return consola;
}
function _getDefaultLogLevel() {
if (isDebug) {
return LogLevels.debug;
}
if (isTest) {
return LogLevels.warn;
}
return LogLevels.info;
}
/**
* A default instance of Consola, created and configured for immediate use.
* This instance is configured based on the execution environment and the options provided.
*
* @type {ConsolaInstance} consola - The default Consola instance, ready to use.
*/
export const consola = createConsola();
export default consola;