Skip to content

Commit

Permalink
chore(npm): simplify pre + post script logic
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed May 18, 2023
1 parent 4b44277 commit 16bce38
Showing 1 changed file with 24 additions and 69 deletions.
93 changes: 24 additions & 69 deletions cli/tools/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use deno_task_shell::ShellCommand;
use deno_task_shell::ShellCommandContext;
use indexmap::IndexMap;
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;
use tokio::task::LocalSet;
Expand Down Expand Up @@ -66,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?;
Expand Down Expand Up @@ -107,53 +106,29 @@ pub async fn execute_script(
.to_owned(),
};

let mut pre_task_name: String = "pre".to_owned();
pre_task_name.push_str(task_name);
if let Some(pre_script) = package_json_scripts.get(&pre_task_name) {
let exit_code = run_npm_script(
&pre_task_name,
pre_script,
cli_options,
&cwd,
npm_resolver,
node_resolver,
)
.await?;

if exit_code > 0 {
return Ok(exit_code);
}
}

let exit_code = run_npm_script(
task_name,
script,
cli_options,
&cwd,
npm_resolver,
node_resolver,
)
.await?;

if exit_code > 0 {
return Ok(exit_code);
}

let mut post_task_name: String = "post".to_owned();
post_task_name.push_str(task_name);
if let Some(post_script) = package_json_scripts.get(&post_task_name) {
let exit_code = run_npm_script(
&post_task_name,
post_script,
cli_options,
&cwd,
npm_resolver,
node_resolver,
)
.await?;

if exit_code > 0 {
return 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);
}
}
}

Expand All @@ -165,26 +140,6 @@ pub async fn execute_script(
}
}

async fn run_npm_script(
task_name: &str,
script: &str,
cli_options: &CliOptions,
cwd: &Path,
npm_resolver: &CliNpmResolver,
node_resolver: &NodeResolver,
) -> Result<i32, AnyError> {
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)
}

fn get_script_with_args(script: &str, options: &CliOptions) -> String {
let additional_args = options
.argv()
Expand Down

0 comments on commit 16bce38

Please sign in to comment.