mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 00:29:09 -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:
parent
ff0daa2b9d
commit
66d25df42c
20 changed files with 152 additions and 12 deletions
|
@ -233,3 +233,57 @@ itest!(task_npx_on_own {
|
|||
exit_code: 1,
|
||||
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())],
|
||||
});
|
||||
|
|
2
cli/tests/testdata/task/deno_json_pre_post/bin.out
vendored
Normal file
2
cli/tests/testdata/task/deno_json_pre_post/bin.out
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
Task test echo 'test'
|
||||
test
|
7
cli/tests/testdata/task/deno_json_pre_post/deno.json
vendored
Normal file
7
cli/tests/testdata/task/deno_json_pre_post/deno.json
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"tasks": {
|
||||
"pretest": "echo 'pretest'",
|
||||
"posttest": "echo 'posttest'",
|
||||
"test": "echo 'test'"
|
||||
}
|
||||
}
|
1
cli/tests/testdata/task/deno_json_pre_post/echo.out
vendored
Normal file
1
cli/tests/testdata/task/deno_json_pre_post/echo.out
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
0
|
5
cli/tests/testdata/task/package_json_post/bin.out
vendored
Normal file
5
cli/tests/testdata/task/package_json_post/bin.out
vendored
Normal 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
|
1
cli/tests/testdata/task/package_json_post/echo.out
vendored
Normal file
1
cli/tests/testdata/task/package_json_post/echo.out
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
0
|
6
cli/tests/testdata/task/package_json_post/package.json
vendored
Normal file
6
cli/tests/testdata/task/package_json_post/package.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"scripts": {
|
||||
"posttest": "echo 'posttest'",
|
||||
"test": "echo 'test'"
|
||||
}
|
||||
}
|
4
cli/tests/testdata/task/package_json_post_only/bin.out
vendored
Normal file
4
cli/tests/testdata/task/package_json_post_only/bin.out
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
Task not found: test
|
||||
Available tasks:
|
||||
- posttest (package.json)
|
||||
echo 'posttest'
|
1
cli/tests/testdata/task/package_json_post_only/echo.out
vendored
Normal file
1
cli/tests/testdata/task/package_json_post_only/echo.out
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
0
|
5
cli/tests/testdata/task/package_json_post_only/package.json
vendored
Normal file
5
cli/tests/testdata/task/package_json_post_only/package.json
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"scripts": {
|
||||
"posttest": "echo 'posttest'"
|
||||
}
|
||||
}
|
5
cli/tests/testdata/task/package_json_pre/bin.out
vendored
Normal file
5
cli/tests/testdata/task/package_json_pre/bin.out
vendored
Normal 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
|
1
cli/tests/testdata/task/package_json_pre/echo.out
vendored
Normal file
1
cli/tests/testdata/task/package_json_pre/echo.out
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
0
|
6
cli/tests/testdata/task/package_json_pre/package.json
vendored
Normal file
6
cli/tests/testdata/task/package_json_pre/package.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"scripts": {
|
||||
"test": "echo 'test'",
|
||||
"pretest": "echo 'pretest'"
|
||||
}
|
||||
}
|
4
cli/tests/testdata/task/package_json_pre_only/bin.out
vendored
Normal file
4
cli/tests/testdata/task/package_json_pre_only/bin.out
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
Task not found: test
|
||||
Available tasks:
|
||||
- pretest (package.json)
|
||||
echo 'pretest'
|
1
cli/tests/testdata/task/package_json_pre_only/echo.out
vendored
Normal file
1
cli/tests/testdata/task/package_json_pre_only/echo.out
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
1
|
5
cli/tests/testdata/task/package_json_pre_only/package.json
vendored
Normal file
5
cli/tests/testdata/task/package_json_pre_only/package.json
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"scripts": {
|
||||
"pretest": "echo 'pretest'"
|
||||
}
|
||||
}
|
7
cli/tests/testdata/task/package_json_pre_post/bin.out
vendored
Normal file
7
cli/tests/testdata/task/package_json_pre_post/bin.out
vendored
Normal 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
|
1
cli/tests/testdata/task/package_json_pre_post/echo.out
vendored
Normal file
1
cli/tests/testdata/task/package_json_pre_post/echo.out
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
0
|
7
cli/tests/testdata/task/package_json_pre_post/package.json
vendored
Normal file
7
cli/tests/testdata/task/package_json_pre_post/package.json
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"scripts": {
|
||||
"pretest": "echo 'pretest'",
|
||||
"posttest": "echo 'posttest'",
|
||||
"test": "echo 'test'"
|
||||
}
|
||||
}
|
|
@ -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,8 +105,19 @@ pub async fn execute_script(
|
|||
.unwrap()
|
||||
.to_owned(),
|
||||
};
|
||||
|
||||
// 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);
|
||||
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)?;
|
||||
|
@ -115,7 +126,13 @@ pub async fn execute_script(
|
|||
let future =
|
||||
deno_task_shell::execute(seq_list, env_vars, &cwd, npx_commands);
|
||||
let exit_code = local.run_until(future).await;
|
||||
Ok(exit_code)
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue