xref: /expo/packages/@expo/cli/src/log.ts (revision d04463cb)
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 { EXPO_DEBUG } = require('./utils/env');
18  error(chalk.red(e.toString()) + (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').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 (code === 0) {
46    log(message);
47  } else {
48    error(message);
49  }
50
51  process.exit(code);
52}
53