Crust logoCrust

No Color

Control terminal color with a recursive Extension flag.

Usage

cli.ts
import { Crust } from "@crustjs/core";
import { noColor } from "@crustjs/extensions";

const app = new Crust("my-cli")
	.extend(noColor())
	.action(({ stdout }) =>
		stdout(
			`FORCE_COLOR=${process.env.FORCE_COLOR ?? "unset"} NO_COLOR=${process.env.NO_COLOR ?? "unset"}`,
		),
	);

await app.execute();
$ my-cli --no-color
FORCE_COLOR=unset NO_COLOR=1

noColor() adds a recursive boolean color flag with no default. Normal boolean negation provides both --color and --no-color.

Behavior

my-cli --color           # FORCE_COLOR=3; NO_COLOR is cleared
my-cli --no-color        # NO_COLOR=1; FORCE_COLOR is cleared
my-cli deploy --no-color # the flag is recursive

The Extension restores the previous environment values after the command finishes. Overlapping programmatic runs may share a direction (all --color or all --no-color); previous ambient values return only after the last run finishes. Starting a run with the opposing flag while another is in flight throws a CrustError in preRun, because the environment is process-global and cannot hold both values. Register it before Extensions such as help() that may finish in preRun, because later preRun hooks are then skipped.

When neither flag is present, the Extension leaves the environment unchanged. @crustjs/style remains responsible for environment and TTY-based color detection.

On this page