Skip to content

Commit dbb1dbf

Browse files
committed
feat(pm): auto run install
1 parent 72e914d commit dbb1dbf

6 files changed

Lines changed: 58 additions & 11 deletions

File tree

crates/vite_task/src/config/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ pub struct ResolvedTask {
6565
pub args: Arc<[Str]>,
6666
pub resolved_config: ResolvedTaskConfig,
6767
pub resolved_command: ResolvedTaskCommand,
68+
/// If true, the task will not be replayed from the cache.
69+
/// This is useful for tasks that should not be replayed, like auto run install command.
70+
/// TODO: this is a temporary solution, we should find a better way to handle this.
71+
pub ignore_replay: bool,
6872
}
6973

7074
impl ResolvedTask {
@@ -119,6 +123,7 @@ impl ResolvedTask {
119123
task_name,
120124
args,
121125
ResolveCommandResult { bin_path, envs },
126+
false,
122127
)
123128
}
124129

@@ -127,6 +132,7 @@ impl ResolvedTask {
127132
task_name: &str,
128133
args: impl Iterator<Item = impl AsRef<str>> + Clone,
129134
command_result: ResolveCommandResult,
135+
ignore_replay: bool,
130136
) -> Result<Self, Error> {
131137
let ResolveCommandResult { bin_path, envs } = command_result;
132138
let builtin_task = TaskCommand::Parsed(TaskParsedCommand {
@@ -156,6 +162,7 @@ impl ResolvedTask {
156162
args: args.map(|arg| arg.as_ref().into()).collect(),
157163
resolved_config: resolved_task_config,
158164
resolved_command,
165+
ignore_replay,
159166
})
160167
}
161168
}

crates/vite_task/src/config/workspace.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ impl Workspace {
229229
args: task_args,
230230
resolved_command,
231231
resolved_config,
232+
ignore_replay: false,
232233
})
233234
}
234235

crates/vite_task/src/execute.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,16 @@ fn is_default_passthrough_env(name: &str) -> bool {
186186
}
187187

188188
// Wildcard patterns for common development tools and platforms
189-
const WILDCARD_PATTERNS: &[&str] =
190-
&["VSCODE_*", "DOCKER_*", "BUILDKIT_*", "COMPOSE_*", "JB_IDE_*", "VERCEL_*", "NEXT_*"];
189+
const WILDCARD_PATTERNS: &[&str] = &[
190+
"VSCODE_*",
191+
"DOCKER_*",
192+
"BUILDKIT_*",
193+
"COMPOSE_*",
194+
"JB_IDE_*",
195+
"VERCEL_*",
196+
"NEXT_*",
197+
"*_TOKEN",
198+
];
191199

192200
// Check wildcard patterns
193201
for pattern in WILDCARD_PATTERNS {
@@ -264,6 +272,7 @@ impl TaskEnvs {
264272
);
265273
}
266274
}
275+
tracing::debug!("all_envs: {:?}", all_envs);
267276

268277
Ok(Self { all_envs, envs_without_pass_through })
269278
}
@@ -471,6 +480,7 @@ mod tests {
471480
assert!(is_default_passthrough_env("JB_IDE_PROJECT_DIR"));
472481
assert!(is_default_passthrough_env("VERCEL_URL"));
473482
assert!(is_default_passthrough_env("NEXT_PUBLIC_API_URL"));
483+
assert!(is_default_passthrough_env("API_TOKEN"));
474484

475485
// Test patterns that should not match anymore (since we removed the example patterns)
476486
assert!(!is_default_passthrough_env("MY_TEST_VARIABLE"));
@@ -479,7 +489,6 @@ mod tests {
479489

480490
// Test variables that should NOT be passed through
481491
assert!(!is_default_passthrough_env("SECRET_KEY"));
482-
assert!(!is_default_passthrough_env("API_TOKEN"));
483492
assert!(!is_default_passthrough_env("CUSTOM_VAR"));
484493
assert!(!is_default_passthrough_env("RANDOM_ENV"));
485494
assert!(!is_default_passthrough_env("MY_SECRET"));

crates/vite_task/src/install.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use vite_path::AbsolutePathBuf;
2222
///
2323
pub struct InstallCommand {
2424
workspace_root: AbsolutePathBuf,
25+
ignore_replay: bool,
2526
}
2627

2728
/// Install command builder.
@@ -30,6 +31,7 @@ pub struct InstallCommand {
3031
///
3132
pub struct InstallCommandBuilder {
3233
workspace_root: AbsolutePathBuf,
34+
ignore_replay: bool,
3335
}
3436

3537
impl InstallCommand {
@@ -58,6 +60,7 @@ impl InstallCommand {
5860
"install",
5961
iter::once("install").chain(args.iter().map(String::as_str)),
6062
ResolveCommandResult { bin_path: resolve_command.bin_path, envs: resolve_command.envs },
63+
self.ignore_replay,
6164
)?;
6265
let mut task_graph: StableGraph<ResolvedTask, ()> = Default::default();
6366
task_graph.add_node(resolved_task);
@@ -70,11 +73,16 @@ impl InstallCommand {
7073

7174
impl InstallCommandBuilder {
7275
pub fn new(workspace_root: AbsolutePathBuf) -> Self {
73-
Self { workspace_root }
76+
Self { workspace_root, ignore_replay: false }
77+
}
78+
79+
pub fn ignore_replay(mut self) -> Self {
80+
self.ignore_replay = true;
81+
self
7482
}
7583

7684
pub fn build(self) -> InstallCommand {
77-
InstallCommand { workspace_root: self.workspace_root }
85+
InstallCommand { workspace_root: self.workspace_root, ignore_replay: self.ignore_replay }
7886
}
7987
}
8088

crates/vite_task/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ const fn resolve_bool_flag(positive: bool, negative: bool) -> bool {
105105
if negative { false } else { positive }
106106
}
107107

108+
/// Automatically run install command
109+
async fn auto_install(workspace_root: &AbsolutePathBuf) -> Result<(), Error> {
110+
tracing::debug!("Running install automatically...");
111+
crate::install::InstallCommand::builder(workspace_root.clone())
112+
.ignore_replay()
113+
.build()
114+
.execute(&vec![])
115+
.await?;
116+
Ok(())
117+
}
118+
108119
pub struct CliOptions<
109120
Lint: Future<Output = Result<ResolveCommandResult, Error>> = Pin<
110121
Box<dyn Future<Output = Result<ResolveCommandResult, Error>>>,
@@ -171,6 +182,11 @@ pub async fn main<
171182
args: Args,
172183
options: Option<CliOptions<Lint, LintFn, Vite, ViteFn, Test, TestFn>>,
173184
) -> Result<(), Error> {
185+
// Auto-install dependencies if needed, but skip for install command itself
186+
if !matches!(args.commands, Some(Commands::Install { .. })) {
187+
auto_install(&cwd).await?;
188+
}
189+
174190
let mut recursive_run = false;
175191
let mut parallel_run = false;
176192
let (tasks, mut workspace, task_args) = match &args.commands {

crates/vite_task/src/schedule.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl ExecutionPlan {
8383
let command = step.resolved_command.fingerprint.command.clone();
8484
let cwd = step.resolved_command.fingerprint.cwd.clone();
8585
let pretty_command = format!("~/{}$ {}", cwd, command);
86+
let ignore_replay = step.ignore_replay;
8687

8788
// Check cache and prepare execution
8889
let (cache_miss, execute_or_replay) = get_cached_or_execute(
@@ -112,12 +113,14 @@ impl ExecutionPlan {
112113
);
113114
}
114115
None => {
115-
println!(
116-
"{} {} {}",
117-
"►".style(Style::new().bright_green()),
118-
pretty_command.style(Style::new().dimmed()),
119-
"(Cache hit, replaying)".style(Style::new().green())
120-
);
116+
if !ignore_replay {
117+
println!(
118+
"{} {} {}",
119+
"►".style(Style::new().bright_green()),
120+
pretty_command.style(Style::new().dimmed()),
121+
"(Cache hit, replaying)".style(Style::new().green())
122+
);
123+
}
121124
}
122125
}
123126

@@ -140,6 +143,9 @@ async fn get_cached_or_execute<'a>(
140143
None,
141144
({
142145
async move {
146+
if task.ignore_replay {
147+
return Ok(());
148+
}
143149
// replay
144150
let std_outputs = Arc::clone(&cache_task.std_outputs);
145151
let mut stdout = tokio::io::stdout();

0 commit comments

Comments
 (0)