2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
use std::io::Read;
|
|
|
|
|
|
|
|
use deno_ast::MediaType;
|
|
|
|
use deno_ast::ModuleSpecifier;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_runtime::permissions::Permissions;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
use crate::args::EvalFlags;
|
|
|
|
use crate::args::Flags;
|
2023-05-01 14:35:23 -04:00
|
|
|
use crate::factory::CliFactory;
|
|
|
|
use crate::factory::CliFactoryBuilder;
|
2022-12-09 09:40:48 -05:00
|
|
|
use crate::file_fetcher::File;
|
|
|
|
use crate::util;
|
|
|
|
|
2023-03-22 10:15:53 -04:00
|
|
|
pub async fn run_script(flags: Flags) -> Result<i32, AnyError> {
|
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"#
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags.watch.is_some() {
|
2023-03-22 10:15:53 -04:00
|
|
|
return run_with_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
|
|
|
|
let factory = CliFactory::from_flags(flags).await?;
|
|
|
|
let deno_dir = factory.deno_dir()?;
|
2023-05-01 16:42:05 -04:00
|
|
|
let http_client = factory.http_client();
|
2023-05-01 14:35:23 -04:00
|
|
|
let cli_options = factory.cli_options();
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
// Run a background task that checks for available upgrades. If an earlier
|
|
|
|
// run of this background task found a new version of Deno.
|
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-01-07 11:25:34 -05:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::from_options(
|
2023-05-01 14:35:23 -04:00
|
|
|
&cli_options.permissions_options(),
|
2023-01-07 11:25:34 -05:00
|
|
|
)?);
|
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
|
|
|
|
.create_main_worker(main_module, permissions)
|
|
|
|
.await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
let exit_code = worker.run().await?;
|
|
|
|
Ok(exit_code)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_from_stdin(flags: Flags) -> Result<i32, AnyError> {
|
2023-05-01 14:35:23 -04:00
|
|
|
let factory = CliFactory::from_flags(flags).await?;
|
|
|
|
let cli_options = factory.cli_options();
|
|
|
|
let main_module = cli_options.resolve_main_module()?;
|
|
|
|
let file_fetcher = factory.file_fetcher()?;
|
|
|
|
let worker_factory = factory.create_cli_main_worker_factory().await?;
|
2023-03-22 10:15:53 -04:00
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::from_options(
|
2023-05-01 14:35:23 -04:00
|
|
|
&cli_options.permissions_options(),
|
2023-04-27 10:05:20 -04:00
|
|
|
)?);
|
2022-12-09 09:40:48 -05:00
|
|
|
let mut source = Vec::new();
|
|
|
|
std::io::stdin().read_to_end(&mut source)?;
|
|
|
|
// Create a dummy source file.
|
|
|
|
let source_file = File {
|
2023-04-03 08:05:39 -04:00
|
|
|
local: main_module.clone().to_file_path().unwrap(),
|
2022-12-09 09:40:48 -05:00
|
|
|
maybe_types: None,
|
|
|
|
media_type: MediaType::TypeScript,
|
|
|
|
source: String::from_utf8(source)?.into(),
|
2023-04-27 10:05:20 -04:00
|
|
|
specifier: main_module.clone(),
|
2022-12-09 09:40:48 -05:00
|
|
|
maybe_headers: None,
|
|
|
|
};
|
|
|
|
// Save our fake file into file fetcher cache
|
|
|
|
// to allow module access by TS compiler
|
2023-05-01 14:35:23 -04:00
|
|
|
file_fetcher.insert_cached(source_file);
|
2022-12-09 09:40:48 -05:00
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
let mut worker = worker_factory
|
|
|
|
.create_main_worker(main_module, permissions)
|
|
|
|
.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-03-22 10:15:53 -04:00
|
|
|
async fn run_with_watch(flags: Flags) -> Result<i32, AnyError> {
|
2022-12-09 09:40:48 -05:00
|
|
|
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
|
2023-05-01 14:35:23 -04:00
|
|
|
let factory = CliFactoryBuilder::new()
|
|
|
|
.with_watcher(sender.clone())
|
|
|
|
.build_from_flags(flags)
|
|
|
|
.await?;
|
|
|
|
let file_watcher = factory.file_watcher()?;
|
|
|
|
let cli_options = factory.cli_options();
|
|
|
|
let clear_screen = !cli_options.no_clear_screen();
|
|
|
|
let main_module = cli_options.resolve_main_module()?;
|
|
|
|
let create_cli_main_worker_factory =
|
|
|
|
factory.create_cli_main_worker_factory_func().await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
|
2023-02-03 14:15:16 -05:00
|
|
|
let operation = |main_module: ModuleSpecifier| {
|
2023-05-01 14:35:23 -04:00
|
|
|
file_watcher.reset();
|
2023-04-30 16:51:31 -04:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::from_options(
|
2023-05-01 14:35:23 -04:00
|
|
|
&cli_options.permissions_options(),
|
2023-04-30 16:51:31 -04:00
|
|
|
)?);
|
2023-05-01 14:35:23 -04:00
|
|
|
let create_cli_main_worker_factory = create_cli_main_worker_factory.clone();
|
2023-04-30 16:51:31 -04:00
|
|
|
|
2022-12-09 09:40:48 -05:00
|
|
|
Ok(async move {
|
2023-05-01 14:35:23 -04:00
|
|
|
let worker = create_cli_main_worker_factory()
|
2023-04-27 10:05:20 -04:00
|
|
|
.create_main_worker(main_module, permissions)
|
|
|
|
.await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
worker.run_for_watcher().await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
util::file_watcher::watch_func2(
|
|
|
|
receiver,
|
|
|
|
operation,
|
2023-02-03 14:15:16 -05:00
|
|
|
main_module,
|
2022-12-09 09:40:48 -05:00
|
|
|
util::file_watcher::PrintConfig {
|
|
|
|
job_name: "Process".to_string(),
|
2023-04-12 14:54:28 -04:00
|
|
|
clear_screen,
|
2022-12-09 09:40:48 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn eval_command(
|
|
|
|
flags: Flags,
|
|
|
|
eval_flags: EvalFlags,
|
|
|
|
) -> Result<i32, AnyError> {
|
2023-05-01 14:35:23 -04:00
|
|
|
let factory = CliFactory::from_flags(flags).await?;
|
|
|
|
let cli_options = factory.cli_options();
|
|
|
|
let file_fetcher = factory.file_fetcher()?;
|
|
|
|
let main_worker_factory = factory.create_cli_main_worker_factory().await?;
|
|
|
|
|
|
|
|
let main_module = cli_options.resolve_main_module()?;
|
2023-01-07 11:25:34 -05:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::from_options(
|
2023-05-01 14:35:23 -04:00
|
|
|
&cli_options.permissions_options(),
|
2023-01-07 11:25:34 -05:00
|
|
|
)?);
|
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
|
|
|
|
}
|
|
|
|
.into_bytes();
|
|
|
|
|
|
|
|
let file = File {
|
2023-04-03 08:05:39 -04:00
|
|
|
local: main_module.clone().to_file_path().unwrap(),
|
2022-12-09 09:40:48 -05:00
|
|
|
maybe_types: None,
|
|
|
|
media_type: MediaType::Unknown,
|
|
|
|
source: String::from_utf8(source_code)?.into(),
|
2023-04-27 10:05:20 -04:00
|
|
|
specifier: main_module.clone(),
|
2022-12-09 09:40:48 -05:00
|
|
|
maybe_headers: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Save our fake file into file fetcher cache
|
|
|
|
// to allow module access by TS compiler.
|
2023-05-01 14:35:23 -04:00
|
|
|
file_fetcher.insert_cached(file);
|
2023-04-27 10:05:20 -04:00
|
|
|
|
2023-05-01 14:35:23 -04:00
|
|
|
let mut worker = main_worker_factory
|
2023-04-27 10:05:20 -04:00
|
|
|
.create_main_worker(main_module, permissions)
|
|
|
|
.await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
let exit_code = worker.run().await?;
|
|
|
|
Ok(exit_code)
|
|
|
|
}
|