fix(npm): run pre and post tasks if present (#19178)

This PR optionally runs pre and posts tasks from `package.json` if
available.

Fixes #19157
This commit is contained in:
Marvin Hagemeister 2023-05-18 21:51:17 +02:00 committed by GitHub
parent ff0daa2b9d
commit 66d25df42c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 152 additions and 12 deletions

View file

@ -65,7 +65,7 @@ pub async fn execute_script(
deno_task_shell::execute(seq_list, env_vars, &cwd, Default::default());
let exit_code = local.run_until(future).await;
Ok(exit_code)
} else if let Some(script) = package_json_scripts.get(task_name) {
} else if package_json_scripts.contains_key(task_name) {
let package_json_deps_provider = factory.package_json_deps_provider();
let package_json_deps_installer =
factory.package_json_deps_installer().await?;
@ -105,17 +105,34 @@ pub async fn execute_script(
.unwrap()
.to_owned(),
};
let script = get_script_with_args(script, cli_options);
output_task(task_name, &script);
let seq_list = deno_task_shell::parser::parse(&script)
.with_context(|| format!("Error parsing script '{task_name}'."))?;
let npx_commands = resolve_npm_commands(npm_resolver, node_resolver)?;
let env_vars = collect_env_vars();
let local = LocalSet::new();
let future =
deno_task_shell::execute(seq_list, env_vars, &cwd, npx_commands);
let exit_code = local.run_until(future).await;
Ok(exit_code)
// At this point we already checked if the task name exists in package.json.
// We can therefore check for "pre" and "post" scripts too, since we're only
// dealing with package.json here and not deno.json
let task_names = vec![
format!("pre{}", task_name),
task_name.clone(),
format!("post{}", task_name),
];
for task_name in task_names {
if let Some(script) = package_json_scripts.get(&task_name) {
let script = get_script_with_args(script, cli_options);
output_task(&task_name, &script);
let seq_list = deno_task_shell::parser::parse(&script)
.with_context(|| format!("Error parsing script '{task_name}'."))?;
let npx_commands = resolve_npm_commands(npm_resolver, node_resolver)?;
let env_vars = collect_env_vars();
let local = LocalSet::new();
let future =
deno_task_shell::execute(seq_list, env_vars, &cwd, npx_commands);
let exit_code = local.run_until(future).await;
if exit_code > 0 {
return Ok(exit_code);
}
}
}
Ok(0)
} else {
eprintln!("Task not found: {task_name}");
print_available_tasks(&tasks_config, &package_json_scripts);