Skip to content

Commit 918c3db

Browse files
committed
refactor(cli): share banner gate via napi binding
Expose `shouldPrintVitePlusHeader()` from the NAPI binding so the Node CLI's `printHeader()` helper can defer to the same Rust predicate used by the Rust CLI. Removes the `process.stdout.isTTY` + `GIT_INDEX_FILE` checks that were duplicated on the JS side; future changes to the gate (e.g. lefthook detection) now only need to touch `vite_shared::header`. Regenerates the NAPI type definitions.
1 parent 5fc11da commit 918c3db

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

packages/cli/binding/index.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,4 +775,5 @@ module.exports.rewritePrettier = nativeBinding.rewritePrettier;
775775
module.exports.rewriteScripts = nativeBinding.rewriteScripts;
776776
module.exports.run = nativeBinding.run;
777777
module.exports.runCommand = nativeBinding.runCommand;
778+
module.exports.shouldPrintVitePlusHeader = nativeBinding.shouldPrintVitePlusHeader;
778779
module.exports.vitePlusHeader = nativeBinding.vitePlusHeader;

packages/cli/binding/index.d.cts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,5 +369,13 @@ export interface RunCommandResult {
369369
pathAccesses: Record<string, PathAccess>;
370370
}
371371

372+
/**
373+
* Whether the Vite+ banner should be emitted in the current environment.
374+
*
375+
* Mirrors `vite_shared::header::should_print_header` so both CLIs apply
376+
* the same TTY + git-hook gating without duplicating the rules in JS.
377+
*/
378+
export declare function shouldPrintVitePlusHeader(): boolean;
379+
372380
/** Render the Vite+ header using the Rust implementation. */
373381
export declare function vitePlusHeader(): string;

packages/cli/binding/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,12 @@ pub async fn run(options: CliOptions) -> Result<i32> {
215215
pub fn vite_plus_header() -> String {
216216
vite_shared::header::vite_plus_header()
217217
}
218+
219+
/// Whether the Vite+ banner should be emitted in the current environment.
220+
///
221+
/// Mirrors `vite_shared::header::should_print_header` so both CLIs apply
222+
/// the same TTY + git-hook gating without duplicating the rules in JS.
223+
#[napi]
224+
pub fn should_print_vite_plus_header() -> bool {
225+
vite_shared::header::should_print_header()
226+
}

packages/cli/src/utils/terminal.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { styleText } from 'node:util';
22

3-
import { vitePlusHeader } from '../../binding/index.js';
3+
import { shouldPrintVitePlusHeader, vitePlusHeader } from '../../binding/index.js';
44

55
export function log(message: string) {
66
/* oxlint-disable-next-line no-console */
@@ -9,19 +9,11 @@ export function log(message: string) {
99

1010
/**
1111
* Emit the Vite+ banner (header line + trailing blank line) to stdout.
12-
*
13-
* Silent when:
14-
* - stdout is piped or redirected (lefthook/husky, `execSync`, CI, pagers) —
15-
* the banner's ANSI styling renders inconsistently there.
16-
* - a git commit-flow hook is running. Direct shell hooks inherit the
17-
* terminal for stdout, so the TTY check alone doesn't catch them; git
18-
* sets `GIT_INDEX_FILE` for pre-commit / commit-msg / prepare-commit-msg.
12+
* Gating (non-TTY, git hooks) lives in `shouldPrintVitePlusHeader` on the
13+
* Rust side so both CLIs stay in sync.
1914
*/
2015
export function printHeader() {
21-
if (!process.stdout.isTTY) {
22-
return;
23-
}
24-
if (process.env.GIT_INDEX_FILE) {
16+
if (!shouldPrintVitePlusHeader()) {
2517
return;
2618
}
2719
log(vitePlusHeader());

0 commit comments

Comments
 (0)