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 std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_ast::MediaType;
|
|
|
|
use deno_ast::ModuleSpecifier;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::resolve_url_or_path;
|
2023-02-15 13:20:40 -05:00
|
|
|
use deno_graph::npm::NpmPackageReference;
|
2022-12-09 09:40:48 -05:00
|
|
|
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;
|
|
|
|
use crate::args::RunFlags;
|
|
|
|
use crate::file_fetcher::File;
|
|
|
|
use crate::proc_state::ProcState;
|
|
|
|
use crate::util;
|
|
|
|
use crate::worker::create_main_worker;
|
|
|
|
|
|
|
|
pub async fn run_script(
|
|
|
|
flags: Flags,
|
|
|
|
run_flags: RunFlags,
|
|
|
|
) -> Result<i32, AnyError> {
|
|
|
|
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() {
|
|
|
|
return run_with_watch(flags, run_flags.script).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bartlomieju): actually I think it will also fail if there's an import
|
|
|
|
// map specified and bare specifier is used on the command line - this should
|
|
|
|
// probably call `ProcState::resolve` instead
|
|
|
|
let ps = ProcState::build(flags).await?;
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
ps.http_client.clone(),
|
|
|
|
ps.dir.upgrade_check_file_path(),
|
|
|
|
);
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
let main_module = if NpmPackageReference::from_str(&run_flags.script).is_ok()
|
|
|
|
{
|
|
|
|
ModuleSpecifier::parse(&run_flags.script)?
|
|
|
|
} else {
|
|
|
|
resolve_url_or_path(&run_flags.script)?
|
|
|
|
};
|
2023-01-07 11:25:34 -05:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::from_options(
|
|
|
|
&ps.options.permissions_options(),
|
|
|
|
)?);
|
2023-01-13 16:39:19 -05:00
|
|
|
let mut worker = create_main_worker(&ps, 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> {
|
|
|
|
let ps = ProcState::build(flags).await?;
|
|
|
|
let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap();
|
|
|
|
let mut worker = create_main_worker(
|
2023-01-13 16:39:19 -05:00
|
|
|
&ps,
|
2022-12-09 09:40:48 -05:00
|
|
|
main_module.clone(),
|
2023-01-07 11:25:34 -05:00
|
|
|
PermissionsContainer::new(Permissions::from_options(
|
|
|
|
&ps.options.permissions_options(),
|
|
|
|
)?),
|
2022-12-09 09:40:48 -05:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let mut source = Vec::new();
|
|
|
|
std::io::stdin().read_to_end(&mut source)?;
|
|
|
|
// Create a dummy source file.
|
|
|
|
let source_file = File {
|
|
|
|
local: main_module.clone().to_file_path().unwrap(),
|
|
|
|
maybe_types: None,
|
|
|
|
media_type: MediaType::TypeScript,
|
|
|
|
source: String::from_utf8(source)?.into(),
|
2023-01-13 16:39:19 -05:00
|
|
|
specifier: main_module,
|
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
|
|
|
|
ps.file_fetcher.insert_cached(source_file);
|
|
|
|
|
|
|
|
let exit_code = worker.run().await?;
|
|
|
|
Ok(exit_code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bartlomieju): this function is not handling `exit_code` set by the runtime
|
|
|
|
// code properly.
|
|
|
|
async fn run_with_watch(flags: Flags, script: String) -> Result<i32, AnyError> {
|
|
|
|
let flags = Arc::new(flags);
|
|
|
|
let main_module = resolve_url_or_path(&script)?;
|
|
|
|
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
|
2023-02-03 14:15:16 -05:00
|
|
|
let mut ps =
|
|
|
|
ProcState::build_for_file_watcher((*flags).clone(), sender.clone()).await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
|
2023-02-03 14:15:16 -05:00
|
|
|
let operation = |main_module: ModuleSpecifier| {
|
|
|
|
ps.reset_for_file_watcher();
|
|
|
|
let ps = ps.clone();
|
2022-12-09 09:40:48 -05:00
|
|
|
Ok(async move {
|
2023-01-07 11:25:34 -05:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::from_options(
|
|
|
|
&ps.options.permissions_options(),
|
|
|
|
)?);
|
2023-01-13 16:39:19 -05:00
|
|
|
let worker = create_main_worker(&ps, 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(),
|
|
|
|
clear_screen: !flags.no_clear_screen,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn eval_command(
|
|
|
|
flags: Flags,
|
|
|
|
eval_flags: EvalFlags,
|
|
|
|
) -> Result<i32, AnyError> {
|
|
|
|
// deno_graph works off of extensions for local files to determine the media
|
|
|
|
// type, and so our "fake" specifier needs to have the proper extension.
|
|
|
|
let main_module =
|
|
|
|
resolve_url_or_path(&format!("./$deno$eval.{}", eval_flags.ext))?;
|
|
|
|
let ps = ProcState::build(flags).await?;
|
2023-01-07 11:25:34 -05:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::from_options(
|
|
|
|
&ps.options.permissions_options(),
|
|
|
|
)?);
|
2022-12-09 09:40:48 -05:00
|
|
|
let mut worker =
|
|
|
|
create_main_worker(&ps, main_module.clone(), permissions).await?;
|
|
|
|
// 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 {
|
|
|
|
local: main_module.clone().to_file_path().unwrap(),
|
|
|
|
maybe_types: None,
|
|
|
|
media_type: MediaType::Unknown,
|
|
|
|
source: String::from_utf8(source_code)?.into(),
|
2023-01-13 16:39:19 -05:00
|
|
|
specifier: main_module,
|
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.
|
|
|
|
ps.file_fetcher.insert_cached(file);
|
|
|
|
let exit_code = worker.run().await?;
|
|
|
|
Ok(exit_code)
|
|
|
|
}
|