2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2019-09-03 22:12:21 -04:00
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
mod args;
|
2021-02-15 21:50:27 -05:00
|
|
|
mod auth_tokens;
|
2021-10-10 17:26:22 -04:00
|
|
|
mod cache;
|
2022-08-29 14:19:54 -04:00
|
|
|
mod deno_std;
|
2021-10-10 17:26:22 -04:00
|
|
|
mod emit;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod errors;
|
2020-05-11 17:33:36 -04:00
|
|
|
mod file_fetcher;
|
2021-12-16 05:45:41 -05:00
|
|
|
mod graph_util;
|
2020-05-11 17:33:36 -04:00
|
|
|
mod http_util;
|
2022-11-21 08:36:26 -05:00
|
|
|
mod js;
|
2020-12-07 05:46:39 -05:00
|
|
|
mod lsp;
|
2020-10-13 07:35:35 -04:00
|
|
|
mod module_loader;
|
2022-10-05 10:06:44 -04:00
|
|
|
mod napi;
|
2022-08-20 11:31:33 -04:00
|
|
|
mod node;
|
2022-08-10 15:23:58 -04:00
|
|
|
mod npm;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod ops;
|
2021-09-24 11:10:42 -04:00
|
|
|
mod proc_state;
|
2021-10-10 17:26:22 -04:00
|
|
|
mod resolver;
|
2020-11-30 14:35:12 -05:00
|
|
|
mod standalone;
|
2020-11-19 13:19:34 -05:00
|
|
|
mod tools;
|
2020-11-02 14:41:20 -05:00
|
|
|
mod tsc;
|
2022-11-28 17:28:54 -05:00
|
|
|
mod util;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod version;
|
2022-08-11 16:59:12 -04:00
|
|
|
mod worker;
|
2020-05-11 17:33:36 -04:00
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::flags_from_vec;
|
|
|
|
use crate::args::DenoSubcommand;
|
|
|
|
use crate::args::Flags;
|
2021-09-24 11:10:42 -04:00
|
|
|
use crate::proc_state::ProcState;
|
2023-02-15 11:30:54 -05:00
|
|
|
use crate::resolver::CliGraphResolver;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::display;
|
2023-01-24 23:03:03 -05:00
|
|
|
use crate::util::v8::get_v8_flags_from_env;
|
|
|
|
use crate::util::v8::init_v8_flags;
|
2022-06-27 16:54:09 -04:00
|
|
|
|
2022-06-29 11:51:11 -04:00
|
|
|
use args::CliOptions;
|
2022-12-09 09:40:48 -05:00
|
|
|
use deno_core::anyhow::Context;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-04-26 19:06:10 -04:00
|
|
|
use deno_core::error::JsError;
|
2021-09-12 12:04:17 -04:00
|
|
|
use deno_runtime::colors;
|
2022-09-02 16:53:23 -04:00
|
|
|
use deno_runtime::fmt_errors::format_js_error;
|
2022-07-11 13:02:23 -04:00
|
|
|
use deno_runtime::tokio_util::run_local;
|
2020-05-11 17:33:36 -04:00
|
|
|
use std::env;
|
|
|
|
use std::path::PathBuf;
|
2022-02-16 13:14:19 -05:00
|
|
|
|
2022-12-09 09:40:48 -05:00
|
|
|
async fn run_subcommand(flags: Flags) -> Result<i32, AnyError> {
|
2022-02-11 14:04:31 -05:00
|
|
|
match flags.subcommand.clone() {
|
2022-03-11 17:07:02 -05:00
|
|
|
DenoSubcommand::Bench(bench_flags) => {
|
2023-01-07 15:22:09 -05:00
|
|
|
let cli_options = CliOptions::from_flags(flags)?;
|
|
|
|
let bench_options = cli_options.resolve_bench_options(bench_flags)?;
|
|
|
|
if cli_options.watch_paths().is_some() {
|
|
|
|
tools::bench::run_benchmarks_with_watch(cli_options, bench_options)
|
|
|
|
.await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
} else {
|
2023-01-07 15:22:09 -05:00
|
|
|
tools::bench::run_benchmarks(cli_options, bench_options).await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
}
|
|
|
|
Ok(0)
|
2022-03-11 17:07:02 -05:00
|
|
|
}
|
2021-09-03 19:33:35 -04:00
|
|
|
DenoSubcommand::Bundle(bundle_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::bundle::bundle(flags, bundle_flags).await?;
|
|
|
|
Ok(0)
|
2021-09-03 19:33:35 -04:00
|
|
|
}
|
|
|
|
DenoSubcommand::Doc(doc_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::doc::print_docs(flags, doc_flags).await?;
|
|
|
|
Ok(0)
|
2021-09-03 19:33:35 -04:00
|
|
|
}
|
|
|
|
DenoSubcommand::Eval(eval_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::run::eval_command(flags, eval_flags).await
|
2021-02-21 11:58:32 -05:00
|
|
|
}
|
2021-09-03 19:33:35 -04:00
|
|
|
DenoSubcommand::Cache(cache_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
let ps = ProcState::build(flags).await?;
|
|
|
|
ps.load_and_type_check_files(&cache_flags.files).await?;
|
|
|
|
ps.cache_module_emits()?;
|
|
|
|
Ok(0)
|
2020-10-23 10:56:25 -04:00
|
|
|
}
|
2022-04-10 19:12:51 -04:00
|
|
|
DenoSubcommand::Check(check_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
let ps = ProcState::build(flags).await?;
|
|
|
|
ps.load_and_type_check_files(&check_flags.files).await?;
|
|
|
|
Ok(0)
|
2022-04-10 19:12:51 -04:00
|
|
|
}
|
2021-09-03 19:33:35 -04:00
|
|
|
DenoSubcommand::Compile(compile_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::standalone::compile(flags, compile_flags).await?;
|
|
|
|
Ok(0)
|
2021-04-26 13:28:38 -04:00
|
|
|
}
|
2021-09-03 19:33:35 -04:00
|
|
|
DenoSubcommand::Coverage(coverage_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::coverage::cover_files(flags, coverage_flags).await?;
|
|
|
|
Ok(0)
|
2020-07-08 10:50:12 -04:00
|
|
|
}
|
2021-09-03 19:33:35 -04:00
|
|
|
DenoSubcommand::Fmt(fmt_flags) => {
|
2023-01-07 15:22:09 -05:00
|
|
|
let cli_options = CliOptions::from_flags(flags)?;
|
|
|
|
let fmt_options = cli_options.resolve_fmt_options(fmt_flags)?;
|
|
|
|
tools::fmt::format(cli_options, fmt_options).await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
Ok(0)
|
2021-09-03 19:33:35 -04:00
|
|
|
}
|
2022-08-19 19:37:05 -04:00
|
|
|
DenoSubcommand::Init(init_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::init::init_project(init_flags).await?;
|
|
|
|
Ok(0)
|
2022-08-19 19:37:05 -04:00
|
|
|
}
|
2021-09-03 19:33:35 -04:00
|
|
|
DenoSubcommand::Info(info_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::info::info(flags, info_flags).await?;
|
|
|
|
Ok(0)
|
2021-09-03 19:33:35 -04:00
|
|
|
}
|
|
|
|
DenoSubcommand::Install(install_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::installer::install_command(flags, install_flags).await?;
|
|
|
|
Ok(0)
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
2021-09-30 11:38:07 -04:00
|
|
|
DenoSubcommand::Uninstall(uninstall_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::installer::uninstall(uninstall_flags.name, uninstall_flags.root)?;
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
DenoSubcommand::Lsp => {
|
|
|
|
lsp::start().await?;
|
|
|
|
Ok(0)
|
2021-09-30 11:38:07 -04:00
|
|
|
}
|
2021-09-03 19:33:35 -04:00
|
|
|
DenoSubcommand::Lint(lint_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
if lint_flags.rules {
|
|
|
|
tools::lint::print_rules_list(lint_flags.json);
|
|
|
|
} else {
|
2023-01-07 15:22:09 -05:00
|
|
|
let cli_options = CliOptions::from_flags(flags)?;
|
|
|
|
let lint_options = cli_options.resolve_lint_options(lint_flags)?;
|
|
|
|
tools::lint::lint(cli_options, lint_options).await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
}
|
|
|
|
Ok(0)
|
2021-09-03 19:33:35 -04:00
|
|
|
}
|
|
|
|
DenoSubcommand::Repl(repl_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::repl::run(flags, repl_flags).await
|
2021-09-03 19:33:35 -04:00
|
|
|
}
|
|
|
|
DenoSubcommand::Run(run_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
if run_flags.is_stdin() {
|
|
|
|
tools::run::run_from_stdin(flags).await
|
|
|
|
} else {
|
|
|
|
tools::run::run_script(flags, run_flags).await
|
|
|
|
}
|
2021-09-03 19:33:35 -04:00
|
|
|
}
|
2022-03-10 20:56:14 -05:00
|
|
|
DenoSubcommand::Task(task_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::task::execute_script(flags, task_flags).await
|
2022-03-10 20:56:14 -05:00
|
|
|
}
|
2021-09-03 19:33:35 -04:00
|
|
|
DenoSubcommand::Test(test_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
if let Some(ref coverage_dir) = flags.coverage_dir {
|
|
|
|
std::fs::create_dir_all(coverage_dir)
|
2023-01-27 10:43:16 -05:00
|
|
|
.with_context(|| format!("Failed creating: {coverage_dir}"))?;
|
2022-12-09 09:40:48 -05:00
|
|
|
// this is set in order to ensure spawned processes use the same
|
|
|
|
// coverage directory
|
|
|
|
env::set_var(
|
|
|
|
"DENO_UNSTABLE_COVERAGE_DIR",
|
|
|
|
PathBuf::from(coverage_dir).canonicalize()?,
|
|
|
|
);
|
|
|
|
}
|
2023-01-07 15:22:09 -05:00
|
|
|
let cli_options = CliOptions::from_flags(flags)?;
|
|
|
|
let test_options = cli_options.resolve_test_options(test_flags)?;
|
2022-12-09 09:40:48 -05:00
|
|
|
|
2023-01-07 15:22:09 -05:00
|
|
|
if cli_options.watch_paths().is_some() {
|
|
|
|
tools::test::run_tests_with_watch(cli_options, test_options).await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
} else {
|
2023-01-07 15:22:09 -05:00
|
|
|
tools::test::run_tests(cli_options, test_options).await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(0)
|
2021-09-03 19:33:35 -04:00
|
|
|
}
|
2021-12-21 09:49:27 -05:00
|
|
|
DenoSubcommand::Completions(completions_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
display::write_to_stdout_ignore_sigpipe(&completions_flags.buf)?;
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
DenoSubcommand::Types => {
|
|
|
|
let types = tsc::get_types_declaration_file_text(flags.unstable);
|
|
|
|
display::write_to_stdout_ignore_sigpipe(types.as_bytes())?;
|
|
|
|
Ok(0)
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
2021-09-03 19:33:35 -04:00
|
|
|
DenoSubcommand::Upgrade(upgrade_flags) => {
|
2022-12-12 21:30:44 -05:00
|
|
|
tools::upgrade::upgrade(flags, upgrade_flags).await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
Ok(0)
|
2021-09-03 19:33:35 -04:00
|
|
|
}
|
2022-02-16 13:14:19 -05:00
|
|
|
DenoSubcommand::Vendor(vendor_flags) => {
|
2022-12-09 09:40:48 -05:00
|
|
|
tools::vendor::vendor(flags, vendor_flags).await?;
|
|
|
|
Ok(0)
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
2020-11-26 09:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 08:49:05 -05:00
|
|
|
fn setup_panic_hook() {
|
|
|
|
// This function does two things inside of the panic hook:
|
|
|
|
// - Tokio does not exit the process when a task panics, so we define a custom
|
|
|
|
// panic hook to implement this behaviour.
|
|
|
|
// - We print a message to stderr to indicate that this is a bug in Deno, and
|
|
|
|
// should be reported to us.
|
2021-09-07 19:34:27 -04:00
|
|
|
let orig_hook = std::panic::take_hook();
|
|
|
|
std::panic::set_hook(Box::new(move |panic_info| {
|
2021-12-20 08:49:05 -05:00
|
|
|
eprintln!("\n============================================================");
|
|
|
|
eprintln!("Deno has panicked. This is a bug in Deno. Please report this");
|
|
|
|
eprintln!("at https://github.com/denoland/deno/issues/new.");
|
|
|
|
eprintln!("If you can reliably reproduce this panic, include the");
|
|
|
|
eprintln!("reproduction steps and re-run with the RUST_BACKTRACE=1 env");
|
|
|
|
eprintln!("var set and include the backtrace in your report.");
|
|
|
|
eprintln!();
|
2022-07-14 17:52:44 -04:00
|
|
|
eprintln!("Platform: {} {}", env::consts::OS, env::consts::ARCH);
|
2021-12-20 08:49:05 -05:00
|
|
|
eprintln!("Version: {}", version::deno());
|
2022-07-14 17:52:44 -04:00
|
|
|
eprintln!("Args: {:?}", env::args().collect::<Vec<_>>());
|
2021-12-20 08:49:05 -05:00
|
|
|
eprintln!();
|
2021-09-07 19:34:27 -04:00
|
|
|
orig_hook(panic_info);
|
|
|
|
std::process::exit(1);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-01-07 13:06:08 -05:00
|
|
|
fn unwrap_or_exit<T>(result: Result<T, AnyError>) -> T {
|
|
|
|
match result {
|
|
|
|
Ok(value) => value,
|
|
|
|
Err(error) => {
|
2023-01-27 10:43:16 -05:00
|
|
|
let mut error_string = format!("{error:?}");
|
2022-10-25 12:20:07 -04:00
|
|
|
let mut error_code = 1;
|
|
|
|
|
|
|
|
if let Some(e) = error.downcast_ref::<JsError>() {
|
|
|
|
error_string = format_js_error(e);
|
2022-11-25 17:00:28 -05:00
|
|
|
} else if let Some(e) = error.downcast_ref::<args::LockfileError>() {
|
2022-10-25 12:20:07 -04:00
|
|
|
error_string = e.to_string();
|
|
|
|
error_code = 10;
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:38:17 -05:00
|
|
|
eprintln!(
|
|
|
|
"{}: {}",
|
|
|
|
colors::red_bold("error"),
|
2022-04-26 19:06:10 -04:00
|
|
|
error_string.trim_start_matches("error: ")
|
2022-01-14 11:38:17 -05:00
|
|
|
);
|
2022-10-25 12:20:07 -04:00
|
|
|
std::process::exit(error_code);
|
2021-01-07 13:06:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
pub fn main() {
|
2021-12-20 08:49:05 -05:00
|
|
|
setup_panic_hook();
|
2021-10-25 11:16:16 -04:00
|
|
|
|
2022-11-28 17:28:54 -05:00
|
|
|
util::unix::raise_fd_limit();
|
|
|
|
util::windows::ensure_stdio_open();
|
2020-11-26 09:17:45 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
colors::enable_ansi(); // For Windows 10
|
2022-12-19 14:31:19 -05:00
|
|
|
deno_runtime::permissions::set_prompt_callbacks(
|
|
|
|
Box::new(util::draw_thread::DrawThread::hide),
|
|
|
|
Box::new(util::draw_thread::DrawThread::show),
|
|
|
|
);
|
2020-11-26 09:17:45 -05:00
|
|
|
|
|
|
|
let args: Vec<String> = env::args().collect();
|
2022-02-15 07:33:46 -05:00
|
|
|
|
2022-12-09 09:40:48 -05:00
|
|
|
let future = async move {
|
2022-02-15 07:33:46 -05:00
|
|
|
let standalone_res =
|
|
|
|
match standalone::extract_standalone(args.clone()).await {
|
|
|
|
Ok(Some((metadata, eszip))) => standalone::run(eszip, metadata).await,
|
|
|
|
Ok(None) => Ok(()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
};
|
|
|
|
// TODO(bartlomieju): doesn't handle exit code set by the runtime properly
|
|
|
|
unwrap_or_exit(standalone_res);
|
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
let flags = match flags_from_vec(args) {
|
2022-02-15 07:33:46 -05:00
|
|
|
Ok(flags) => flags,
|
|
|
|
Err(err @ clap::Error { .. })
|
2022-03-27 21:57:56 -04:00
|
|
|
if err.kind() == clap::ErrorKind::DisplayHelp
|
|
|
|
|| err.kind() == clap::ErrorKind::DisplayVersion =>
|
2022-02-15 07:33:46 -05:00
|
|
|
{
|
|
|
|
err.print().unwrap();
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
Err(err) => unwrap_or_exit(Err(AnyError::from(err))),
|
|
|
|
};
|
2023-01-24 23:03:03 -05:00
|
|
|
|
|
|
|
init_v8_flags(&flags.v8_flags, get_v8_flags_from_env());
|
2021-05-11 00:54:10 -04:00
|
|
|
|
2022-11-28 17:28:54 -05:00
|
|
|
util::logger::init(flags.log_level);
|
2022-02-15 07:33:46 -05:00
|
|
|
|
2022-12-09 09:40:48 -05:00
|
|
|
run_subcommand(flags).await
|
2022-02-15 07:33:46 -05:00
|
|
|
};
|
2020-05-11 17:33:36 -04:00
|
|
|
|
2022-12-09 09:40:48 -05:00
|
|
|
let exit_code = unwrap_or_exit(run_local(future));
|
2021-11-27 18:45:38 -05:00
|
|
|
|
2021-12-11 09:56:45 -05:00
|
|
|
std::process::exit(exit_code);
|
2018-06-15 19:43:23 -04:00
|
|
|
}
|