2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-03-10 20:56:14 -05:00
|
|
|
|
2024-09-03 09:56:39 -04:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-07-19 15:56:07 -04:00
|
|
|
use deno_config::deno_json::Task;
|
2024-07-03 20:54:33 -04:00
|
|
|
use deno_config::workspace::TaskOrScript;
|
2024-07-19 15:56:07 -04:00
|
|
|
use deno_config::workspace::WorkspaceDirectory;
|
2024-07-03 20:54:33 -04:00
|
|
|
use deno_config::workspace::WorkspaceTasksConfig;
|
2024-08-06 11:05:30 -04:00
|
|
|
use deno_core::anyhow::anyhow;
|
2022-03-10 20:56:14 -05:00
|
|
|
use deno_core::anyhow::bail;
|
|
|
|
use deno_core::anyhow::Context;
|
|
|
|
use deno_core::error::AnyError;
|
2024-07-03 20:54:33 -04:00
|
|
|
use deno_core::normalize_path;
|
2023-02-22 22:45:35 -05:00
|
|
|
use deno_task_shell::ShellCommand;
|
2024-09-03 09:56:39 -04:00
|
|
|
|
|
|
|
use crate::args::CliOptions;
|
|
|
|
use crate::args::Flags;
|
|
|
|
use crate::args::TaskFlags;
|
|
|
|
use crate::colors;
|
|
|
|
use crate::factory::CliFactory;
|
|
|
|
use crate::npm::CliNpmResolver;
|
|
|
|
use crate::task_runner;
|
|
|
|
use crate::util::fs::canonicalize_path;
|
2024-05-23 11:39:48 -04:00
|
|
|
|
2022-03-10 20:56:14 -05:00
|
|
|
pub async fn execute_script(
|
2024-07-23 19:00:48 -04:00
|
|
|
flags: Arc<Flags>,
|
2022-03-10 20:56:14 -05:00
|
|
|
task_flags: TaskFlags,
|
|
|
|
) -> Result<i32, AnyError> {
|
2024-07-23 19:00:48 -04:00
|
|
|
let factory = CliFactory::from_flags(flags);
|
|
|
|
let cli_options = factory.cli_options()?;
|
2024-07-19 15:56:07 -04:00
|
|
|
let start_dir = &cli_options.start_dir;
|
|
|
|
if !start_dir.has_deno_or_pkg_json() {
|
2024-08-21 09:54:59 -04:00
|
|
|
if task_flags.is_run {
|
|
|
|
bail!(
|
|
|
|
r#"deno run couldn't find deno.json(c).
|
|
|
|
If you meant to run a script, specify it, e.g., `deno run ./script.ts`.
|
|
|
|
To run a task, ensure the config file exists.
|
|
|
|
Examples:
|
|
|
|
- Script: `deno run ./script.ts`
|
|
|
|
- Task: `deno run dev`
|
|
|
|
See https://docs.deno.com/go/config"#
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
bail!("deno task couldn't find deno.json(c). See https://docs.deno.com/go/config")
|
|
|
|
}
|
2024-07-03 20:54:33 -04:00
|
|
|
}
|
2024-07-09 23:06:08 -04:00
|
|
|
let force_use_pkg_json =
|
|
|
|
std::env::var_os(crate::task_runner::USE_PKG_JSON_HIDDEN_ENV_VAR_NAME)
|
|
|
|
.map(|v| {
|
|
|
|
// always remove so sub processes don't inherit this env var
|
|
|
|
std::env::remove_var(
|
|
|
|
crate::task_runner::USE_PKG_JSON_HIDDEN_ENV_VAR_NAME,
|
|
|
|
);
|
|
|
|
v == "1"
|
|
|
|
})
|
|
|
|
.unwrap_or(false);
|
2024-07-19 15:56:07 -04:00
|
|
|
let tasks_config = start_dir.to_tasks_config()?;
|
2024-07-03 20:54:33 -04:00
|
|
|
let tasks_config = if force_use_pkg_json {
|
|
|
|
tasks_config.with_only_pkg_json()
|
|
|
|
} else {
|
|
|
|
tasks_config
|
|
|
|
};
|
2023-02-22 22:45:35 -05:00
|
|
|
|
|
|
|
let task_name = match &task_flags.task {
|
|
|
|
Some(task) => task,
|
|
|
|
None => {
|
2024-05-08 22:45:06 -04:00
|
|
|
print_available_tasks(
|
|
|
|
&mut std::io::stdout(),
|
2024-07-19 15:56:07 -04:00
|
|
|
&cli_options.start_dir,
|
2024-05-08 22:45:06 -04:00
|
|
|
&tasks_config,
|
|
|
|
)?;
|
2023-02-22 22:45:35 -05:00
|
|
|
return Ok(1);
|
|
|
|
}
|
2022-03-10 20:56:14 -05:00
|
|
|
};
|
2024-07-03 20:54:33 -04:00
|
|
|
|
2024-04-20 20:13:46 -04:00
|
|
|
let npm_resolver = factory.npm_resolver().await?;
|
|
|
|
let node_resolver = factory.node_resolver().await?;
|
2024-07-09 23:06:08 -04:00
|
|
|
let env_vars = task_runner::real_env_vars();
|
2023-05-01 14:35:23 -04:00
|
|
|
|
2024-07-03 20:54:33 -04:00
|
|
|
match tasks_config.task(task_name) {
|
|
|
|
Some((dir_url, task_or_script)) => match task_or_script {
|
|
|
|
TaskOrScript::Task(_tasks, script) => {
|
|
|
|
let cwd = match task_flags.cwd {
|
|
|
|
Some(path) => canonicalize_path(&PathBuf::from(path))
|
|
|
|
.context("failed canonicalizing --cwd")?,
|
|
|
|
None => normalize_path(dir_url.to_file_path().unwrap()),
|
|
|
|
};
|
2023-02-24 19:35:43 -05:00
|
|
|
|
2024-07-09 23:06:08 -04:00
|
|
|
let custom_commands = task_runner::resolve_custom_commands(
|
|
|
|
npm_resolver.as_ref(),
|
|
|
|
node_resolver,
|
|
|
|
)?;
|
2024-07-03 20:54:33 -04:00
|
|
|
run_task(RunTaskOptions {
|
2024-05-23 11:39:48 -04:00
|
|
|
task_name,
|
2024-04-20 20:13:46 -04:00
|
|
|
script,
|
2024-05-23 11:39:48 -04:00
|
|
|
cwd: &cwd,
|
2024-07-03 20:54:33 -04:00
|
|
|
env_vars,
|
|
|
|
custom_commands,
|
2024-07-09 23:06:08 -04:00
|
|
|
npm_resolver: npm_resolver.as_ref(),
|
|
|
|
cli_options,
|
2024-05-23 11:39:48 -04:00
|
|
|
})
|
2024-07-03 20:54:33 -04:00
|
|
|
.await
|
2023-05-18 15:51:17 -04:00
|
|
|
}
|
2024-07-03 20:54:33 -04:00
|
|
|
TaskOrScript::Script(scripts, _script) => {
|
2024-09-03 09:56:39 -04:00
|
|
|
// ensure the npm packages are installed if using a managed resolver
|
|
|
|
if let Some(npm_resolver) = npm_resolver.as_managed() {
|
|
|
|
npm_resolver.ensure_top_level_package_json_install().await?;
|
2024-07-03 20:54:33 -04:00
|
|
|
}
|
2023-05-18 15:51:17 -04:00
|
|
|
|
2024-07-03 20:54:33 -04:00
|
|
|
let cwd = match task_flags.cwd {
|
|
|
|
Some(path) => canonicalize_path(&PathBuf::from(path))?,
|
|
|
|
None => normalize_path(dir_url.to_file_path().unwrap()),
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
];
|
2024-07-09 23:06:08 -04:00
|
|
|
let custom_commands = task_runner::resolve_custom_commands(
|
|
|
|
npm_resolver.as_ref(),
|
|
|
|
node_resolver,
|
|
|
|
)?;
|
2024-07-03 20:54:33 -04:00
|
|
|
for task_name in &task_names {
|
|
|
|
if let Some(script) = scripts.get(task_name) {
|
|
|
|
let exit_code = run_task(RunTaskOptions {
|
|
|
|
task_name,
|
|
|
|
script,
|
|
|
|
cwd: &cwd,
|
|
|
|
env_vars: env_vars.clone(),
|
|
|
|
custom_commands: custom_commands.clone(),
|
2024-07-09 23:06:08 -04:00
|
|
|
npm_resolver: npm_resolver.as_ref(),
|
|
|
|
cli_options,
|
2024-07-03 20:54:33 -04:00
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
if exit_code > 0 {
|
|
|
|
return Ok(exit_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
2024-08-21 09:54:59 -04:00
|
|
|
if task_flags.is_run {
|
2024-08-06 11:05:30 -04:00
|
|
|
return Err(anyhow!("Task not found: {}", task_name));
|
|
|
|
}
|
|
|
|
log::error!("Task not found: {}", task_name);
|
2024-07-03 20:54:33 -04:00
|
|
|
if log::log_enabled!(log::Level::Error) {
|
|
|
|
print_available_tasks(
|
|
|
|
&mut std::io::stderr(),
|
2024-07-19 15:56:07 -04:00
|
|
|
&cli_options.start_dir,
|
2024-07-03 20:54:33 -04:00
|
|
|
&tasks_config,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Ok(1)
|
2024-05-08 22:45:06 -04:00
|
|
|
}
|
2022-03-10 20:56:14 -05:00
|
|
|
}
|
2023-02-22 22:45:35 -05:00
|
|
|
}
|
2022-03-10 20:56:14 -05:00
|
|
|
|
2024-05-23 11:39:48 -04:00
|
|
|
struct RunTaskOptions<'a> {
|
|
|
|
task_name: &'a str,
|
|
|
|
script: &'a str,
|
|
|
|
cwd: &'a Path,
|
2024-05-13 18:55:14 -04:00
|
|
|
env_vars: HashMap<String, String>,
|
|
|
|
custom_commands: HashMap<String, Rc<dyn ShellCommand>>,
|
2024-07-09 23:06:08 -04:00
|
|
|
npm_resolver: &'a dyn CliNpmResolver,
|
|
|
|
cli_options: &'a CliOptions,
|
2024-05-23 11:39:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_task(opts: RunTaskOptions<'_>) -> Result<i32, AnyError> {
|
2024-07-09 23:06:08 -04:00
|
|
|
let RunTaskOptions {
|
|
|
|
task_name,
|
|
|
|
script,
|
|
|
|
cwd,
|
2024-05-23 11:39:48 -04:00
|
|
|
env_vars,
|
2024-07-09 23:06:08 -04:00
|
|
|
custom_commands,
|
|
|
|
npm_resolver,
|
|
|
|
cli_options,
|
|
|
|
} = opts;
|
|
|
|
|
|
|
|
output_task(
|
|
|
|
opts.task_name,
|
|
|
|
&task_runner::get_script_with_args(script, cli_options.argv()),
|
2024-05-23 11:39:48 -04:00
|
|
|
);
|
2024-04-20 20:13:46 -04:00
|
|
|
|
2024-07-09 23:06:08 -04:00
|
|
|
task_runner::run_task(task_runner::RunTaskOptions {
|
|
|
|
task_name,
|
|
|
|
script,
|
|
|
|
cwd,
|
|
|
|
env_vars,
|
|
|
|
custom_commands,
|
|
|
|
init_cwd: opts.cli_options.initial_cwd(),
|
|
|
|
argv: cli_options.argv(),
|
|
|
|
root_node_modules_dir: npm_resolver
|
|
|
|
.root_node_modules_path()
|
|
|
|
.map(|p| p.as_path()),
|
|
|
|
})
|
|
|
|
.await
|
2023-02-22 22:45:35 -05:00
|
|
|
}
|
2022-03-10 20:56:14 -05:00
|
|
|
|
2023-02-22 22:45:35 -05:00
|
|
|
fn output_task(task_name: &str, script: &str) {
|
|
|
|
log::info!(
|
|
|
|
"{} {} {}",
|
|
|
|
colors::green("Task"),
|
2024-05-13 18:55:14 -04:00
|
|
|
colors::cyan(task_name),
|
2023-02-22 22:45:35 -05:00
|
|
|
script,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_available_tasks(
|
2024-05-08 22:45:06 -04:00
|
|
|
writer: &mut dyn std::io::Write,
|
2024-07-19 15:56:07 -04:00
|
|
|
workspace_dir: &Arc<WorkspaceDirectory>,
|
2024-07-03 20:54:33 -04:00
|
|
|
tasks_config: &WorkspaceTasksConfig,
|
2024-05-08 22:45:06 -04:00
|
|
|
) -> Result<(), std::io::Error> {
|
|
|
|
writeln!(writer, "{}", colors::green("Available tasks:"))?;
|
2024-07-03 20:54:33 -04:00
|
|
|
let is_cwd_root_dir = tasks_config.root.is_none();
|
2023-02-22 22:45:35 -05:00
|
|
|
|
2024-07-03 20:54:33 -04:00
|
|
|
if tasks_config.is_empty() {
|
2024-05-08 22:45:06 -04:00
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
" {}",
|
|
|
|
colors::red("No tasks found in configuration file")
|
|
|
|
)?;
|
|
|
|
} else {
|
2024-07-03 20:54:33 -04:00
|
|
|
let mut seen_task_names =
|
|
|
|
HashSet::with_capacity(tasks_config.tasks_count());
|
|
|
|
for maybe_config in [&tasks_config.member, &tasks_config.root] {
|
|
|
|
let Some(config) = maybe_config else {
|
|
|
|
continue;
|
2024-05-08 22:45:06 -04:00
|
|
|
};
|
2024-07-03 20:54:33 -04:00
|
|
|
for (is_root, is_deno, (key, task)) in config
|
|
|
|
.deno_json
|
|
|
|
.as_ref()
|
|
|
|
.map(|config| {
|
|
|
|
let is_root = !is_cwd_root_dir
|
2024-07-19 15:56:07 -04:00
|
|
|
&& config.folder_url
|
|
|
|
== *workspace_dir.workspace.root_dir().as_ref();
|
2024-07-03 20:54:33 -04:00
|
|
|
config
|
|
|
|
.tasks
|
|
|
|
.iter()
|
|
|
|
.map(move |(k, t)| (is_root, true, (k, Cow::Borrowed(t))))
|
|
|
|
})
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
|
|
|
.chain(
|
|
|
|
config
|
|
|
|
.package_json
|
|
|
|
.as_ref()
|
|
|
|
.map(|config| {
|
|
|
|
let is_root = !is_cwd_root_dir
|
2024-07-19 15:56:07 -04:00
|
|
|
&& config.folder_url
|
|
|
|
== *workspace_dir.workspace.root_dir().as_ref();
|
2024-07-03 20:54:33 -04:00
|
|
|
config.tasks.iter().map(move |(k, v)| {
|
2024-07-19 15:56:07 -04:00
|
|
|
(is_root, false, (k, Cow::Owned(Task::Definition(v.clone()))))
|
2024-07-03 20:54:33 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.into_iter()
|
|
|
|
.flatten(),
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if !seen_task_names.insert(key) {
|
|
|
|
continue; // already seen
|
|
|
|
}
|
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"- {}{}",
|
|
|
|
colors::cyan(key),
|
|
|
|
if is_root {
|
|
|
|
if is_deno {
|
|
|
|
format!(" {}", colors::italic_gray("(workspace)"))
|
|
|
|
} else {
|
|
|
|
format!(" {}", colors::italic_gray("(workspace package.json)"))
|
|
|
|
}
|
|
|
|
} else if is_deno {
|
|
|
|
"".to_string()
|
|
|
|
} else {
|
|
|
|
format!(" {}", colors::italic_gray("(package.json)"))
|
|
|
|
}
|
|
|
|
)?;
|
|
|
|
let definition = match task.as_ref() {
|
2024-07-19 15:56:07 -04:00
|
|
|
Task::Definition(definition) => definition,
|
|
|
|
Task::Commented { definition, .. } => definition,
|
2024-07-03 20:54:33 -04:00
|
|
|
};
|
2024-07-19 15:56:07 -04:00
|
|
|
if let Task::Commented { comments, .. } = task.as_ref() {
|
2024-07-03 20:54:33 -04:00
|
|
|
let slash_slash = colors::italic_gray("//");
|
|
|
|
for comment in comments {
|
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
" {slash_slash} {}",
|
|
|
|
colors::italic_gray(comment)
|
|
|
|
)?;
|
|
|
|
}
|
2024-05-08 22:45:06 -04:00
|
|
|
}
|
2024-07-03 20:54:33 -04:00
|
|
|
writeln!(writer, " {definition}")?;
|
2024-03-27 17:14:27 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-22 22:45:35 -05:00
|
|
|
}
|
2024-05-08 22:45:06 -04:00
|
|
|
|
|
|
|
Ok(())
|
2023-02-22 22:45:35 -05:00
|
|
|
}
|