xref: /expo/packages/@expo/cli/src/log.ts (revision af644ed4)
1import chalk from 'chalk';
2
3export function time(label?: string): void {
4  console.time(label);
5}
6
7export function timeEnd(label?: string): void {
8  console.timeEnd(label);
9}
10
11export function error(...message: string[]): void {
12  console.error(...message);
13}
14
15/** Print an error and provide additional info (the stack trace) in debug mode. */
16export function exception(e: Error): void {
17  const { env } = require('./utils/env');
18  error(chalk.red(e.toString()) + (env.EXPO_DEBUG ? '\n' + chalk.gray(e.stack) : ''));
19}
20
21export function warn(...message: string[]): void {
22  console.warn(...message.map((value) => chalk.yellow(value)));
23}
24
25export function log(...message: string[]): void {
26  console.log(...message);
27}
28
29export function debug(...message: any[]): void {
30  if (require('./utils/env').env.EXPO_DEBUG) console.log(...message);
31}
32
33/** Clear the terminal of all text. */
34export function clear(): void {
35  process.stdout.write(process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H');
36}
37
38/** Log a message and exit the current process. If the `code` is non-zero then `console.error` will be used instead of `console.log`. */
39export function exit(message: string | Error, code: number = 1): never {
40  if (message instanceof Error) {
41    exception(message);
42    process.exit(code);
43  }
44
45  if (message) {
46    if (code === 0) {
47      log(message);
48    } else {
49      error(message);
50    }
51  }
52
53  process.exit(code);
54}
55
56// The re-export makes auto importing easier.
57export const Log = {
58  time,
59  timeEnd,
60  error,
61  exception,
62  warn,
63  log,
64  debug,
65  clear,
66  exit,
67};
68