Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions crates/vite_global_cli/src/command_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,7 @@ fn render_picker(
let pad = if vite_shared::header::is_warp_terminal() { " " } else { "" };
let max_width = usize::from(columns).saturating_sub(4 + pad.len());
let viewport_end = (viewport_start + viewport_size).min(filtered_indices.len());
let instruction = truncate_line(
&format!("Select a command (↑/↓, Enter to run, Esc to cancel): {query}"),
max_width,
);
let instruction = truncate_line(&picker_instruction(query), max_width);

execute!(stdout, cursor::MoveTo(0, 0), terminal::Clear(ClearType::All),)?;
if vite_shared::header::is_warp_terminal() {
Expand Down Expand Up @@ -440,6 +437,10 @@ fn render_picker(
stdout.flush()
}

fn picker_instruction(query: &str) -> String {
format!("Select a command (↑/↓, Enter to run, type to search): {query}")
}

fn compute_viewport_size(
terminal_rows: usize,
total_commands: usize,
Expand Down Expand Up @@ -552,7 +553,7 @@ fn filtered_command_indices(query: &str, command_order: &[usize]) -> Vec<usize>
mod tests {
use super::{
COMMANDS, align_viewport, compute_viewport_size, default_command_order,
filtered_command_indices, selected_command_parts,
filtered_command_indices, picker_instruction, selected_command_parts,
};

#[test]
Expand Down Expand Up @@ -670,4 +671,12 @@ mod tests {
assert_eq!(help.label, "help");
assert_eq!(help.summary, "View all commands and details");
}

#[test]
fn picker_instruction_mentions_search() {
assert_eq!(
picker_instruction(""),
"Select a command (↑/↓, Enter to run, type to search): "
);
}
}
16 changes: 14 additions & 2 deletions packages/cli/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod package_manager;
#[allow(dead_code)]
mod utils;

use std::{collections::HashMap, ffi::OsStr, sync::Arc};
use std::{collections::HashMap, error::Error as StdError, ffi::OsStr, fmt::Write as _, sync::Arc};

use napi::{anyhow, bindgen_prelude::*, threadsafe_function::ThreadsafeFunction};
use napi_derive::napi;
Expand Down Expand Up @@ -111,6 +111,18 @@ fn create_vite_config_resolver(
})
}

fn format_error_message(error: &(dyn StdError + 'static)) -> String {
let mut message = error.to_string();
let mut source = error.source();

while let Some(current) = source {
let _ = write!(message, "\n* {current}");
source = current.source();
}

message
}

/// Main entry point for the CLI, called from JavaScript.
///
/// This is an async function that spawns a new thread for the non-Send async code
Expand Down Expand Up @@ -180,7 +192,7 @@ pub async fn run(options: CliOptions) -> Result<i32> {
vite_error::Error::UserCancelled => Ok(130),
_ => {
tracing::error!("Rust error: {:?}", e);
Err(anyhow::Error::from(e).into())
Err(napi::Error::from_reason(format_error_message(&e)))
}
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "run-task-command-conflict-test",
"private": true,
"scripts": {
"build": "echo 'build from package.json'"
}
}
5 changes: 5 additions & 0 deletions packages/cli/snap-tests/run-task-command-conflict/snap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
> # Test that conflicting package.json and vite.config.ts task commands render cleanly
[1]> vp run build
error: Failed to load task graph
* Failed to resolve task config for task run-task-command-conflict-test#build
* Both package.json script and vite.config.* task define commands for the task
6 changes: 6 additions & 0 deletions packages/cli/snap-tests/run-task-command-conflict/steps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"commands": [
"# Test that conflicting package.json and vite.config.ts task commands render cleanly",
"vp run build"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
run: {
tasks: {
build: {
command: "echo 'build from vite.config.ts'",
},
},
},
};
16 changes: 14 additions & 2 deletions packages/cli/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ import { pack } from './resolve-pack.js';
import { test } from './resolve-test.js';
import { resolveUniversalViteConfig } from './resolve-vite-config.js';
import { vite } from './resolve-vite.js';
import { accent, log } from './utils/terminal.js';
import { accent, errorMsg, log } from './utils/terminal.js';

function getErrorMessage(err: unknown): string {
if (err instanceof Error) {
return err.message;
}

if (typeof err === 'object' && err && 'message' in err && typeof err.message === 'string') {
return err.message;
}

return String(err);
}

// Parse command line arguments
let args = process.argv.slice(2);
Expand Down Expand Up @@ -113,7 +125,7 @@ if (command === 'create') {

process.exit(finalExitCode);
} catch (err) {
console.error('[Vite+] run error:', err);
errorMsg(getErrorMessage(err));
process.exit(1);
}
}
Loading