1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 16:49:18 -05:00

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

@ -233,3 +233,57 @@ itest!(task_npx_on_own {
exit_code: 1, exit_code: 1,
http_server: true, http_server: true,
}); });
itest!(task_pre_post {
args: "task test",
cwd: Some("task/package_json_pre_post/"),
output: "task/package_json_pre_post/bin.out",
copy_temp_dir: Some("task/package_json_pre_post/"),
exit_code: 0,
envs: vec![("NO_COLOR".to_string(), "1".to_string())],
});
itest!(task_pre {
args: "task test",
cwd: Some("task/package_json_pre/"),
output: "task/package_json_pre/bin.out",
copy_temp_dir: Some("task/package_json_pre/"),
exit_code: 0,
envs: vec![("NO_COLOR".to_string(), "1".to_string())],
});
itest!(task_post {
args: "task test",
cwd: Some("task/package_json_post/"),
output: "task/package_json_post/bin.out",
copy_temp_dir: Some("task/package_json_post/"),
exit_code: 0,
envs: vec![("NO_COLOR".to_string(), "1".to_string())],
});
itest!(task_post_only {
args: "task test",
cwd: Some("task/package_json_post_only/"),
output: "task/package_json_post_only/bin.out",
copy_temp_dir: Some("task/package_json_post_only/"),
exit_code: 1,
envs: vec![("NO_COLOR".to_string(), "1".to_string())],
});
itest!(task_pre_only {
args: "task test",
cwd: Some("task/package_json_pre_only/"),
output: "task/package_json_pre_only/bin.out",
copy_temp_dir: Some("task/package_json_pre_only/"),
exit_code: 1,
envs: vec![("NO_COLOR".to_string(), "1".to_string())],
});
itest!(task_deno_no_pre_post {
args: "task test",
cwd: Some("task/deno_json_pre_post/"),
output: "task/deno_json_pre_post/bin.out",
copy_temp_dir: Some("task/deno_json_pre_post/"),
exit_code: 0,
envs: vec![("NO_COLOR".to_string(), "1".to_string())],
});

View file

@ -0,0 +1,2 @@
Task test echo 'test'
test

View file

@ -0,0 +1,7 @@
{
"tasks": {
"pretest": "echo 'pretest'",
"posttest": "echo 'posttest'",
"test": "echo 'test'"
}
}

View file

@ -0,0 +1 @@
0

View file

@ -0,0 +1,5 @@
Warning Currently only basic package.json `scripts` are supported. Programs like `rimraf` or `cross-env` will not work correctly. This will be fixed in an upcoming release.
Task test echo 'test'
test
Task posttest echo 'posttest'
posttest

View file

@ -0,0 +1 @@
0

View file

@ -0,0 +1,6 @@
{
"scripts": {
"posttest": "echo 'posttest'",
"test": "echo 'test'"
}
}

View file

@ -0,0 +1,4 @@
Task not found: test
Available tasks:
- posttest (package.json)
echo 'posttest'

View file

@ -0,0 +1 @@
0

View file

@ -0,0 +1,5 @@
{
"scripts": {
"posttest": "echo 'posttest'"
}
}

View file

@ -0,0 +1,5 @@
Warning Currently only basic package.json `scripts` are supported. Programs like `rimraf` or `cross-env` will not work correctly. This will be fixed in an upcoming release.
Task pretest echo 'pretest'
pretest
Task test echo 'test'
test

View file

@ -0,0 +1 @@
0

View file

@ -0,0 +1,6 @@
{
"scripts": {
"test": "echo 'test'",
"pretest": "echo 'pretest'"
}
}

View file

@ -0,0 +1,4 @@
Task not found: test
Available tasks:
- pretest (package.json)
echo 'pretest'

View file

@ -0,0 +1 @@
1

View file

@ -0,0 +1,5 @@
{
"scripts": {
"pretest": "echo 'pretest'"
}
}

View file

@ -0,0 +1,7 @@
Warning Currently only basic package.json `scripts` are supported. Programs like `rimraf` or `cross-env` will not work correctly. This will be fixed in an upcoming release.
Task pretest echo 'pretest'
pretest
Task test echo 'test'
test
Task posttest echo 'posttest'
posttest

View file

@ -0,0 +1 @@
0

View file

@ -0,0 +1,7 @@
{
"scripts": {
"pretest": "echo 'pretest'",
"posttest": "echo 'posttest'",
"test": "echo 'test'"
}
}

View file

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