Skip to content

Commit bd50211

Browse files
committed
feat(task): support NO_COLOR
https://no-color.org/
1 parent 08d33d9 commit bd50211

5 files changed

Lines changed: 41 additions & 1 deletion

File tree

crates/vite_task/src/execute.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ const DEFAULT_PASSTHROUGH_ENVS: &[&str] = &[
179179
"TERM_PROGRAM",
180180
"DISPLAY",
181181
"FORCE_COLOR",
182+
"NO_COLOR",
182183
// Temporary directories
183184
"TMP",
184185
"TEMP",
@@ -275,7 +276,7 @@ impl TaskEnvs {
275276
// Automatically add FORCE_COLOR environment variable if not already set
276277
// This enables color output in subprocesses when color is supported
277278
// TODO: will remove this temporarily until we have a better solution
278-
if !all_envs.contains_key("FORCE_COLOR") {
279+
if !all_envs.contains_key("FORCE_COLOR") && !all_envs.contains_key("NO_COLOR") {
279280
if let Some(support) = on(Stream::Stdout) {
280281
let force_color_value = if support.has_16m {
281282
"3" // True color (16 million colors)
@@ -528,6 +529,23 @@ mod tests {
528529
unsafe {
529530
std::env::remove_var("FORCE_COLOR");
530531
}
532+
533+
// Test when NO_COLOR is already set - should not be overridden
534+
unsafe {
535+
std::env::set_var("NO_COLOR", "1");
536+
}
537+
538+
let result3 = TaskEnvs::resolve(base_dir, &resolved_task_config).unwrap();
539+
assert!(result3.all_envs.contains_key("NO_COLOR"));
540+
let no_color_value = result3.all_envs.get("NO_COLOR").unwrap();
541+
assert_eq!(no_color_value.to_str().unwrap(), "1");
542+
// FORCE_COLOR should not automatically added since NO_COLOR is set
543+
assert!(!result3.all_envs.contains_key("FORCE_COLOR"));
544+
545+
// Clean up
546+
unsafe {
547+
std::env::remove_var("NO_COLOR");
548+
}
531549
}
532550

533551
#[test]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('NO_COLOR=%s', process.env.NO_COLOR);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"scripts": {
3+
"check": "node check.js"
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
> NO_COLOR=1 vite-plus run check -- --foo # get NO_COLOR=1 from env
2+
Cache not found
3+
NO_COLOR=1
4+
5+
> NO_COLOR=2 vite-plus run check -- --bar # get NO_COLOR=2 from env
6+
Cache not found
7+
NO_COLOR=2
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"env": {
3+
"VITE_DISABLE_AUTO_INSTALL": "1"
4+
},
5+
"commands": [
6+
"NO_COLOR=1 vite-plus run check -- --foo # get NO_COLOR=1 from env",
7+
"NO_COLOR=2 vite-plus run check -- --bar # get NO_COLOR=2 from env"
8+
]
9+
}

0 commit comments

Comments
 (0)