2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
use std::io::Read;
|
2024-07-23 19:00:48 -04:00
|
|
|
use std::sync::Arc;
|
2022-12-09 09:40:48 -05:00
|
|
|
|
2024-08-30 17:58:24 -04:00
|
|
|
use deno_config::deno_json::NodeModulesDirMode;
|
2022-12-09 09:40:48 -05:00
|
|
|
use deno_core::error::AnyError;
|
2024-04-24 15:45:49 -04:00
|
|
|
use deno_runtime::WorkerExecutionMode;
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
use crate::args::EvalFlags;
|
|
|
|
use crate::args::Flags;
|
2023-06-15 13:09:37 -04:00
|
|
|
use crate::args::WatchFlagsWithPaths;
|
2023-05-01 14:35:23 -04:00
|
|
|
use crate::factory::CliFactory;
|
2022-12-09 09:40:48 -05:00
|
|
|
use crate::file_fetcher::File;
|
|
|
|
use crate::util;
|
2023-10-30 20:25:58 -04:00
|
|
|
use crate::util::file_watcher::WatcherRestartMode;
|
|
|
|
|
|
|
|
pub mod hmr;
|
2022-12-09 09:40:48 -05:00
|
|
|
|
2024-08-14 18:26:21 -04:00
|
|
|
pub fn check_permission_before_script(flags: &Flags) {
|
2022-12-09 09:40:48 -05:00
|
|
|
if !flags.has_permission() && flags.has_permission_in_argv() {
|
|
|
|
log::warn!(
|
|
|
|
"{}",
|
|
|
|
crate::colors::yellow(
|
|
|
|
r#"Permission flags have likely been incorrectly set after the script argument.
|
|
|
|
To grant permissions, set them before the script argument. For example:
|
|
|
|
deno run --allow-read=. main.js"#
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-08-14 18:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_script(
|
|
|
|
mode: WorkerExecutionMode,
|
|
|
|
flags: Arc<Flags>,
|
|
|
|
watch: Option<WatchFlagsWithPaths>,
|
|
|
|
) -> Result<i32, AnyError> {
|
|
|
|
check_permission_before_script(&flags);
|
2022-12-09 09:40:48 -05:00
|
|
|
|
2024-04-24 15:45:49 -04:00
|
|
|
if let Some(watch_flags) = watch {
|
|
|
|
return run_with_watch(mode, flags, watch_flags).await;
|
2022-12-09 09:40:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bartlomieju): actually I think it will also fail if there's an import
|
2023-05-01 14:35:23 -04:00
|
|
|
// map specified and bare specifier is used on the command line
|
2024-07-23 19:00:48 -04:00
|
|
|
let factory = CliFactory::from_flags(flags);
|
|
|
|
let cli_options = factory.cli_options()?;
|
2023-05-01 14:35:23 -04:00
|
|
|
let deno_dir = factory.deno_dir()?;
|
2024-06-03 17:17:08 -04:00
|
|
|
let http_client = factory.http_client_provider();
|
2022-12-09 09:40:48 -05:00
|
|
|
|
2023-11-16 17:29:35 -05:00
|
|
|
// Run a background task that checks for available upgrades or output
|
|
|
|
// if an earlier run of this background task found a new version of Deno.
|
2023-11-29 13:52:25 -05:00
|
|
|
#[cfg(feature = "upgrade")]
|
2022-12-12 21:30:44 -05:00
|
|
|
super::upgrade::check_for_upgrades(
|
2023-05-01 14:35:23 -04:00
|
|
|
http_client.clone(),
|
|
|
|
deno_dir.upgrade_check_file_path(),
|
2022-12-12 21:30:44 -05:00
|
|
|
);
|
2022-12-09 09:40:48 -05:00
|
|
|
|
2023-05-01 14:35:23 -04:00
|
|
|
let main_module = cli_options.resolve_main_module()?;
|
2023-03-22 10:15:53 -04:00
|
|
|
|
2023-05-23 18:51:48 -04:00
|
|
|
maybe_npm_install(&factory).await?;
|
|
|
|
|
2023-05-01 14:35:23 -04:00
|
|
|
let worker_factory = factory.create_cli_main_worker_factory().await?;
|
2023-04-27 10:05:20 -04:00
|
|
|
let mut worker = worker_factory
|
2024-09-25 21:50:54 -04:00
|
|
|
.create_main_worker(mode, main_module.clone())
|
2023-04-27 10:05:20 -04:00
|
|
|
.await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
let exit_code = worker.run().await?;
|
|
|
|
Ok(exit_code)
|
|
|
|
}
|
|
|
|
|
2024-07-23 19:00:48 -04:00
|
|
|
pub async fn run_from_stdin(flags: Arc<Flags>) -> Result<i32, AnyError> {
|
|
|
|
let factory = CliFactory::from_flags(flags);
|
|
|
|
let cli_options = factory.cli_options()?;
|
2023-05-01 14:35:23 -04:00
|
|
|
let main_module = cli_options.resolve_main_module()?;
|
2023-05-23 18:51:48 -04:00
|
|
|
|
|
|
|
maybe_npm_install(&factory).await?;
|
|
|
|
|
2023-05-01 14:35:23 -04:00
|
|
|
let file_fetcher = factory.file_fetcher()?;
|
|
|
|
let worker_factory = factory.create_cli_main_worker_factory().await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
let mut source = Vec::new();
|
|
|
|
std::io::stdin().read_to_end(&mut source)?;
|
2024-01-31 22:15:22 -05:00
|
|
|
// Save a fake file into file fetcher cache
|
|
|
|
// to allow module access by TS compiler
|
2024-02-10 10:02:31 -05:00
|
|
|
file_fetcher.insert_memory_files(File {
|
2023-04-27 10:05:20 -04:00
|
|
|
specifier: main_module.clone(),
|
2022-12-09 09:40:48 -05:00
|
|
|
maybe_headers: None,
|
2024-01-31 22:15:22 -05:00
|
|
|
source: source.into(),
|
|
|
|
});
|
2022-12-09 09:40:48 -05:00
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
let mut worker = worker_factory
|
2024-09-25 21:50:54 -04:00
|
|
|
.create_main_worker(WorkerExecutionMode::Run, main_module.clone())
|
2023-04-27 10:05:20 -04:00
|
|
|
.await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
let exit_code = worker.run().await?;
|
|
|
|
Ok(exit_code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bartlomieju): this function is not handling `exit_code` set by the runtime
|
|
|
|
// code properly.
|
2023-06-15 13:09:37 -04:00
|
|
|
async fn run_with_watch(
|
2024-04-24 15:45:49 -04:00
|
|
|
mode: WorkerExecutionMode,
|
2024-07-23 19:00:48 -04:00
|
|
|
flags: Arc<Flags>,
|
2023-06-15 13:09:37 -04:00
|
|
|
watch_flags: WatchFlagsWithPaths,
|
|
|
|
) -> Result<i32, AnyError> {
|
2023-10-30 20:25:58 -04:00
|
|
|
util::file_watcher::watch_recv(
|
2023-06-14 18:29:19 -04:00
|
|
|
flags,
|
2023-10-30 20:25:58 -04:00
|
|
|
util::file_watcher::PrintConfig::new_with_banner(
|
|
|
|
if watch_flags.hmr { "HMR" } else { "Watcher" },
|
|
|
|
"Process",
|
|
|
|
!watch_flags.no_clear_screen,
|
|
|
|
),
|
|
|
|
WatcherRestartMode::Automatic,
|
2023-10-19 01:05:00 -04:00
|
|
|
move |flags, watcher_communicator, _changed_paths| {
|
2023-06-14 18:29:19 -04:00
|
|
|
Ok(async move {
|
2024-07-23 19:00:48 -04:00
|
|
|
let factory = CliFactory::from_flags_for_watcher(
|
|
|
|
flags,
|
|
|
|
watcher_communicator.clone(),
|
|
|
|
);
|
|
|
|
let cli_options = factory.cli_options()?;
|
2023-06-14 18:29:19 -04:00
|
|
|
let main_module = cli_options.resolve_main_module()?;
|
|
|
|
|
|
|
|
maybe_npm_install(&factory).await?;
|
|
|
|
|
2023-10-19 01:05:00 -04:00
|
|
|
let _ = watcher_communicator.watch_paths(cli_options.watch_paths());
|
2023-06-14 18:29:19 -04:00
|
|
|
|
2023-10-30 20:25:58 -04:00
|
|
|
let mut worker = factory
|
2023-06-14 18:29:19 -04:00
|
|
|
.create_cli_main_worker_factory()
|
|
|
|
.await?
|
2024-09-25 21:50:54 -04:00
|
|
|
.create_main_worker(mode, main_module.clone())
|
2023-06-14 18:29:19 -04:00
|
|
|
.await?;
|
2023-10-30 20:25:58 -04:00
|
|
|
|
|
|
|
if watch_flags.hmr {
|
|
|
|
worker.run().await?;
|
|
|
|
} else {
|
|
|
|
worker.run_for_watcher().await?;
|
|
|
|
}
|
2023-06-14 18:29:19 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
},
|
2022-12-09 09:40:48 -05:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn eval_command(
|
2024-07-23 19:00:48 -04:00
|
|
|
flags: Arc<Flags>,
|
2022-12-09 09:40:48 -05:00
|
|
|
eval_flags: EvalFlags,
|
|
|
|
) -> Result<i32, AnyError> {
|
2024-07-23 19:00:48 -04:00
|
|
|
let factory = CliFactory::from_flags(flags);
|
|
|
|
let cli_options = factory.cli_options()?;
|
2023-05-01 14:35:23 -04:00
|
|
|
let file_fetcher = factory.file_fetcher()?;
|
|
|
|
let main_module = cli_options.resolve_main_module()?;
|
2023-05-23 18:51:48 -04:00
|
|
|
|
|
|
|
maybe_npm_install(&factory).await?;
|
|
|
|
|
2022-12-09 09:40:48 -05:00
|
|
|
// Create a dummy source file.
|
|
|
|
let source_code = if eval_flags.print {
|
|
|
|
format!("console.log({})", eval_flags.code)
|
|
|
|
} else {
|
|
|
|
eval_flags.code
|
|
|
|
};
|
|
|
|
|
2024-01-31 22:15:22 -05:00
|
|
|
// Save a fake file into file fetcher cache
|
2022-12-09 09:40:48 -05:00
|
|
|
// to allow module access by TS compiler.
|
2024-02-10 10:02:31 -05:00
|
|
|
file_fetcher.insert_memory_files(File {
|
2024-01-31 22:15:22 -05:00
|
|
|
specifier: main_module.clone(),
|
|
|
|
maybe_headers: None,
|
|
|
|
source: source_code.into_bytes().into(),
|
|
|
|
});
|
2023-04-27 10:05:20 -04:00
|
|
|
|
2023-05-23 18:51:48 -04:00
|
|
|
let worker_factory = factory.create_cli_main_worker_factory().await?;
|
|
|
|
let mut worker = worker_factory
|
2024-09-25 21:50:54 -04:00
|
|
|
.create_main_worker(WorkerExecutionMode::Eval, main_module.clone())
|
2023-04-27 10:05:20 -04:00
|
|
|
.await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
let exit_code = worker.run().await?;
|
|
|
|
Ok(exit_code)
|
|
|
|
}
|
2023-05-23 18:51:48 -04:00
|
|
|
|
2024-08-14 18:26:21 -04:00
|
|
|
pub async fn maybe_npm_install(factory: &CliFactory) -> Result<(), AnyError> {
|
2023-05-23 18:51:48 -04:00
|
|
|
// ensure an "npm install" is done if the user has explicitly
|
2023-09-30 12:06:38 -04:00
|
|
|
// opted into using a managed node_modules directory
|
2024-08-30 17:58:24 -04:00
|
|
|
if factory.cli_options()?.node_modules_dir()?
|
|
|
|
== Some(NodeModulesDirMode::Auto)
|
2024-08-29 18:57:43 -04:00
|
|
|
{
|
2023-09-30 12:06:38 -04:00
|
|
|
if let Some(npm_resolver) = factory.npm_resolver().await?.as_managed() {
|
|
|
|
npm_resolver.ensure_top_level_package_json_install().await?;
|
|
|
|
}
|
2023-05-23 18:51:48 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|