[cli] Add --no-color mode (#8826)

Co-authored-by: Sean Massa <EndangeredMassa@gmail.com>
Co-authored-by: Chris Barber <chris.barber@vercel.com>
This commit is contained in:
최지민(Jeemin Choi)
2023-02-07 07:45:17 +09:00
committed by GitHub
parent a585969dd3
commit 76d58673fc
26 changed files with 179 additions and 2 deletions

View File

@@ -5,12 +5,14 @@ import renderLink from './link';
import wait, { StopSpinner } from './wait';
import type { WritableTTY } from '../../types';
import { errorToString } from '@vercel/error-utils';
import { removeEmoji } from '../emoji';
const IS_TEST = process.env.NODE_ENV === 'test';
export interface OutputOptions {
debug?: boolean;
supportsHyperlink?: boolean;
noColor?: boolean;
}
export interface LogOptions {
@@ -25,6 +27,7 @@ export class Output {
stream: WritableTTY;
debugEnabled: boolean;
supportsHyperlink: boolean;
colorDisabled: boolean;
private spinnerMessage: string;
private _spinner: StopSpinner | null;
@@ -33,6 +36,7 @@ export class Output {
{
debug: debugEnabled = false,
supportsHyperlink = detectSupportsHyperlink(stream),
noColor = false,
}: OutputOptions = {}
) {
this.stream = stream;
@@ -40,6 +44,11 @@ export class Output {
this.supportsHyperlink = supportsHyperlink;
this.spinnerMessage = '';
this._spinner = null;
this.colorDisabled = getNoColor(noColor);
if (this.colorDisabled) {
chalk.level = 0;
}
}
isDebugEnabled = () => {
@@ -47,6 +56,9 @@ export class Output {
};
print = (str: string) => {
if (this.colorDisabled) {
str = removeEmoji(str);
}
this.stopSpinner();
this.stream.write(str);
};
@@ -203,3 +215,14 @@ export class Output {
return ansiEscapes.link(chalk.cyan(text), url);
};
}
function getNoColor(noColorArg: boolean | undefined): boolean {
// FORCE_COLOR: the standard supported by chalk https://github.com/chalk/chalk#supportscolor
// NO_COLOR: the standard we want to support https://no-color.org/
// noColorArg: the `--no-color` arg passed to the CLI command
const noColor =
process.env.FORCE_COLOR === '0' ||
process.env.NO_COLOR === '1' ||
noColorArg;
return !!noColor;
}