2022-01-08 03:09:52 +00:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2019-09-04 02:12:21 +00:00
|
|
|
|
2021-02-16 02:50:27 +00:00
|
|
|
mod auth_tokens;
|
2021-10-10 21:26:22 +00:00
|
|
|
mod cache;
|
2022-02-07 16:05:49 +00:00
|
|
|
mod cdp;
|
2020-05-11 21:33:36 +00:00
|
|
|
mod checksum;
|
2021-10-04 23:35:55 +00:00
|
|
|
mod compat;
|
2021-05-10 16:16:39 +00:00
|
|
|
mod config_file;
|
2020-10-12 11:25:25 +00:00
|
|
|
mod deno_dir;
|
|
|
|
mod diagnostics;
|
2020-05-23 13:22:08 +00:00
|
|
|
mod diff;
|
2020-05-11 21:33:36 +00:00
|
|
|
mod disk_cache;
|
2021-10-10 21:26:22 +00:00
|
|
|
mod emit;
|
2020-10-12 11:25:25 +00:00
|
|
|
mod errors;
|
2020-05-11 21:33:36 +00:00
|
|
|
mod file_fetcher;
|
2020-09-11 16:19:49 +00:00
|
|
|
mod file_watcher;
|
2020-10-12 11:25:25 +00:00
|
|
|
mod flags;
|
2020-06-26 12:09:02 +00:00
|
|
|
mod flags_allow_net;
|
2020-10-12 11:25:25 +00:00
|
|
|
mod fmt_errors;
|
2020-11-16 19:48:50 +00:00
|
|
|
mod fs_util;
|
2021-12-16 10:45:41 +00:00
|
|
|
mod graph_util;
|
2020-10-12 11:25:25 +00:00
|
|
|
mod http_cache;
|
2020-05-11 21:33:36 +00:00
|
|
|
mod http_util;
|
|
|
|
mod lockfile;
|
2021-05-11 04:54:10 +00:00
|
|
|
mod logger;
|
2020-12-07 10:46:39 +00:00
|
|
|
mod lsp;
|
2020-10-13 11:35:35 +00:00
|
|
|
mod module_loader;
|
2020-10-12 11:25:25 +00:00
|
|
|
mod ops;
|
2021-09-24 15:10:42 +00:00
|
|
|
mod proc_state;
|
2021-10-10 21:26:22 +00:00
|
|
|
mod resolver;
|
2020-10-12 11:25:25 +00:00
|
|
|
mod source_maps;
|
2020-11-30 19:35:12 +00:00
|
|
|
mod standalone;
|
2020-08-03 21:39:48 +00:00
|
|
|
mod text_encoding;
|
2020-11-19 18:19:34 +00:00
|
|
|
mod tools;
|
2020-11-02 19:41:20 +00:00
|
|
|
mod tsc;
|
2021-04-13 14:24:45 +00:00
|
|
|
mod unix_util;
|
2020-10-12 11:25:25 +00:00
|
|
|
mod version;
|
2021-10-25 15:16:16 +00:00
|
|
|
mod windows_util;
|
2020-05-11 21:33:36 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
use crate::file_fetcher::File;
|
2021-05-10 06:06:13 +00:00
|
|
|
use crate::file_watcher::ResolutionResult;
|
2021-09-03 23:33:35 +00:00
|
|
|
use crate::flags::BundleFlags;
|
|
|
|
use crate::flags::CacheFlags;
|
2021-11-29 22:23:30 +00:00
|
|
|
use crate::flags::CheckFlag;
|
2021-09-03 23:33:35 +00:00
|
|
|
use crate::flags::CompileFlags;
|
|
|
|
use crate::flags::CompletionsFlags;
|
|
|
|
use crate::flags::CoverageFlags;
|
2020-11-30 19:35:12 +00:00
|
|
|
use crate::flags::DenoSubcommand;
|
2021-09-03 23:33:35 +00:00
|
|
|
use crate::flags::DocFlags;
|
|
|
|
use crate::flags::EvalFlags;
|
2020-11-30 19:35:12 +00:00
|
|
|
use crate::flags::Flags;
|
2021-09-03 23:33:35 +00:00
|
|
|
use crate::flags::FmtFlags;
|
|
|
|
use crate::flags::InfoFlags;
|
|
|
|
use crate::flags::InstallFlags;
|
|
|
|
use crate::flags::LintFlags;
|
|
|
|
use crate::flags::ReplFlags;
|
|
|
|
use crate::flags::RunFlags;
|
|
|
|
use crate::flags::TestFlags;
|
2021-09-30 15:38:07 +00:00
|
|
|
use crate::flags::UninstallFlags;
|
2021-09-03 23:33:35 +00:00
|
|
|
use crate::flags::UpgradeFlags;
|
2020-12-11 17:49:26 +00:00
|
|
|
use crate::fmt_errors::PrettyJsError;
|
2021-12-16 10:45:41 +00:00
|
|
|
use crate::graph_util::graph_lock_or_exit;
|
|
|
|
use crate::graph_util::graph_valid;
|
2020-12-11 17:49:26 +00:00
|
|
|
use crate::module_loader::CliModuleLoader;
|
2021-09-24 15:10:42 +00:00
|
|
|
use crate::proc_state::ProcState;
|
2021-10-10 21:26:22 +00:00
|
|
|
use crate::resolver::ImportMapResolver;
|
2021-11-09 01:26:39 +00:00
|
|
|
use crate::resolver::JsxResolver;
|
2020-12-11 17:49:26 +00:00
|
|
|
use crate::source_maps::apply_source_map;
|
2020-11-30 19:35:12 +00:00
|
|
|
use crate::tools::installer::infer_name_from_url;
|
2021-09-07 14:39:32 +00:00
|
|
|
use deno_ast::MediaType;
|
2020-10-23 11:05:41 +00:00
|
|
|
use deno_core::error::generic_error;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 16:36:37 +00:00
|
|
|
use deno_core::futures::future::FutureExt;
|
2022-02-11 12:41:56 +00:00
|
|
|
use deno_core::futures::future::LocalFutureObj;
|
2020-09-21 16:36:37 +00:00
|
|
|
use deno_core::futures::Future;
|
2021-06-21 23:45:41 +00:00
|
|
|
use deno_core::located_script_name;
|
2021-12-16 10:45:41 +00:00
|
|
|
use deno_core::parking_lot::RwLock;
|
2021-02-17 18:47:18 +00:00
|
|
|
use deno_core::resolve_url_or_path;
|
2020-09-21 16:36:37 +00:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
2020-05-11 21:33:36 +00:00
|
|
|
use deno_core::v8_set_flags;
|
2021-12-29 13:30:08 +00:00
|
|
|
use deno_core::Extension;
|
2020-05-11 21:33:36 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
2021-09-12 16:04:17 +00:00
|
|
|
use deno_runtime::colors;
|
2020-12-13 18:45:53 +00:00
|
|
|
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
|
2022-02-11 12:41:56 +00:00
|
|
|
use deno_runtime::ops::worker_host::PreloadModuleCb;
|
2020-12-13 18:45:53 +00:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2021-10-21 11:05:43 +00:00
|
|
|
use deno_runtime::tokio_util::run_basic;
|
2020-12-13 18:45:53 +00:00
|
|
|
use deno_runtime::web_worker::WebWorker;
|
|
|
|
use deno_runtime::web_worker::WebWorkerOptions;
|
|
|
|
use deno_runtime::worker::MainWorker;
|
|
|
|
use deno_runtime::worker::WorkerOptions;
|
2021-10-05 20:41:14 +00:00
|
|
|
use deno_runtime::BootstrapOptions;
|
2021-03-26 16:34:25 +00:00
|
|
|
use log::debug;
|
|
|
|
use log::info;
|
2020-05-11 21:33:36 +00:00
|
|
|
use std::env;
|
2020-06-11 14:58:09 +00:00
|
|
|
use std::io::Read;
|
2020-05-11 21:33:36 +00:00
|
|
|
use std::io::Write;
|
2020-08-07 22:51:59 +00:00
|
|
|
use std::iter::once;
|
2020-05-11 21:33:36 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::pin::Pin;
|
2020-10-12 02:25:27 +00:00
|
|
|
use std::rc::Rc;
|
2020-08-18 16:30:13 +00:00
|
|
|
use std::sync::Arc;
|
2020-05-11 21:33:36 +00:00
|
|
|
|
2022-02-11 12:41:56 +00:00
|
|
|
fn create_web_worker_preload_module_callback(
|
|
|
|
ps: ProcState,
|
|
|
|
) -> Arc<PreloadModuleCb> {
|
|
|
|
let compat = ps.flags.compat;
|
|
|
|
|
|
|
|
Arc::new(move |mut worker| {
|
|
|
|
let fut = async move {
|
|
|
|
if compat {
|
|
|
|
worker.execute_side_module(&compat::GLOBAL_URL).await?;
|
|
|
|
worker.execute_side_module(&compat::MODULE_URL).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(worker)
|
|
|
|
};
|
|
|
|
LocalFutureObj::new(Box::new(fut))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:10:42 +00:00
|
|
|
fn create_web_worker_callback(ps: ProcState) -> Arc<CreateWebWorkerCb> {
|
2020-12-11 17:49:26 +00:00
|
|
|
Arc::new(move |args| {
|
2021-09-24 15:10:42 +00:00
|
|
|
let global_state_ = ps.clone();
|
2020-12-11 17:49:26 +00:00
|
|
|
let js_error_create_fn = Rc::new(move |core_js_error| {
|
|
|
|
let source_mapped_error =
|
|
|
|
apply_source_map(&core_js_error, global_state_.clone());
|
|
|
|
PrettyJsError::create(source_mapped_error)
|
|
|
|
});
|
|
|
|
|
2021-09-24 15:10:42 +00:00
|
|
|
let maybe_inspector_server = ps.maybe_inspector_server.clone();
|
2020-12-11 17:49:26 +00:00
|
|
|
|
2021-01-06 20:31:16 +00:00
|
|
|
let module_loader = CliModuleLoader::new_for_worker(
|
2021-09-24 15:10:42 +00:00
|
|
|
ps.clone(),
|
2021-01-06 20:31:16 +00:00
|
|
|
args.parent_permissions.clone(),
|
|
|
|
);
|
2021-09-24 15:10:42 +00:00
|
|
|
let create_web_worker_cb = create_web_worker_callback(ps.clone());
|
2022-02-11 12:41:56 +00:00
|
|
|
let preload_module_cb =
|
|
|
|
create_web_worker_preload_module_callback(ps.clone());
|
2020-12-11 17:49:26 +00:00
|
|
|
|
2021-12-29 13:30:08 +00:00
|
|
|
let extensions = ops::cli_exts(ps.clone(), args.use_deno_namespace);
|
|
|
|
|
2020-12-11 17:49:26 +00:00
|
|
|
let options = WebWorkerOptions {
|
2021-10-05 20:41:14 +00:00
|
|
|
bootstrap: BootstrapOptions {
|
|
|
|
args: ps.flags.argv.clone(),
|
|
|
|
apply_source_maps: true,
|
|
|
|
cpu_count: num_cpus::get(),
|
|
|
|
debug_flag: ps
|
|
|
|
.flags
|
|
|
|
.log_level
|
|
|
|
.map_or(false, |l| l == log::Level::Debug),
|
|
|
|
enable_testing_features: ps.flags.enable_testing_features,
|
|
|
|
location: Some(args.main_module.clone()),
|
|
|
|
no_color: !colors::use_color(),
|
|
|
|
runtime_version: version::deno(),
|
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
|
|
|
unstable: ps.flags.unstable,
|
|
|
|
},
|
2021-12-29 13:30:08 +00:00
|
|
|
extensions,
|
2021-09-24 15:10:42 +00:00
|
|
|
unsafely_ignore_certificate_errors: ps
|
2021-08-09 14:53:21 +00:00
|
|
|
.flags
|
2021-08-10 11:19:45 +00:00
|
|
|
.unsafely_ignore_certificate_errors
|
2021-08-09 14:53:21 +00:00
|
|
|
.clone(),
|
2021-09-24 15:10:42 +00:00
|
|
|
root_cert_store: ps.root_cert_store.clone(),
|
2021-01-08 02:08:51 +00:00
|
|
|
user_agent: version::get_user_agent(),
|
2021-09-24 15:10:42 +00:00
|
|
|
seed: ps.flags.seed,
|
2020-12-11 17:49:26 +00:00
|
|
|
module_loader,
|
|
|
|
create_web_worker_cb,
|
2022-02-11 12:41:56 +00:00
|
|
|
preload_module_cb,
|
2020-12-11 17:49:26 +00:00
|
|
|
js_error_create_fn: Some(js_error_create_fn),
|
|
|
|
use_deno_namespace: args.use_deno_namespace,
|
2021-08-16 12:29:54 +00:00
|
|
|
worker_type: args.worker_type,
|
2020-12-11 17:49:26 +00:00
|
|
|
maybe_inspector_server,
|
2020-12-13 18:45:53 +00:00
|
|
|
get_error_class_fn: Some(&crate::errors::get_error_class_name),
|
2021-09-24 15:10:42 +00:00
|
|
|
blob_store: ps.blob_store.clone(),
|
|
|
|
broadcast_channel: ps.broadcast_channel.clone(),
|
|
|
|
shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
|
2021-09-29 08:47:24 +00:00
|
|
|
compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
|
2021-12-11 14:56:45 +00:00
|
|
|
maybe_exit_code: args.maybe_exit_code,
|
2020-12-11 17:49:26 +00:00
|
|
|
};
|
|
|
|
|
2021-12-29 13:30:08 +00:00
|
|
|
WebWorker::bootstrap_from_options(
|
2020-12-11 17:49:26 +00:00
|
|
|
args.name,
|
|
|
|
args.permissions,
|
|
|
|
args.main_module,
|
|
|
|
args.worker_id,
|
2021-10-05 20:41:14 +00:00
|
|
|
options,
|
2021-12-29 13:30:08 +00:00
|
|
|
)
|
2020-12-11 17:49:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_main_worker(
|
2021-09-24 15:10:42 +00:00
|
|
|
ps: &ProcState,
|
2020-12-11 17:49:26 +00:00
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: Permissions,
|
2021-12-29 13:30:08 +00:00
|
|
|
mut custom_extensions: Vec<Extension>,
|
2020-12-11 17:49:26 +00:00
|
|
|
) -> MainWorker {
|
2021-09-24 15:10:42 +00:00
|
|
|
let module_loader = CliModuleLoader::new(ps.clone());
|
2020-12-11 17:49:26 +00:00
|
|
|
|
2021-09-24 15:10:42 +00:00
|
|
|
let global_state_ = ps.clone();
|
2020-12-11 17:49:26 +00:00
|
|
|
|
|
|
|
let js_error_create_fn = Rc::new(move |core_js_error| {
|
|
|
|
let source_mapped_error =
|
|
|
|
apply_source_map(&core_js_error, global_state_.clone());
|
|
|
|
PrettyJsError::create(source_mapped_error)
|
|
|
|
});
|
|
|
|
|
2021-09-24 15:10:42 +00:00
|
|
|
let maybe_inspector_server = ps.maybe_inspector_server.clone();
|
|
|
|
let should_break_on_first_statement = ps.flags.inspect_brk.is_some();
|
2020-12-11 17:49:26 +00:00
|
|
|
|
2021-09-24 15:10:42 +00:00
|
|
|
let create_web_worker_cb = create_web_worker_callback(ps.clone());
|
2022-02-11 12:41:56 +00:00
|
|
|
let web_worker_preload_module_cb =
|
|
|
|
create_web_worker_preload_module_callback(ps.clone());
|
2020-12-11 17:49:26 +00:00
|
|
|
|
2021-10-27 00:10:27 +00:00
|
|
|
let maybe_storage_key = if let Some(location) = &ps.flags.location {
|
|
|
|
// if a location is set, then the ascii serialization of the location is
|
|
|
|
// used, unless the origin is opaque, and then no storage origin is set, as
|
|
|
|
// we can't expect the origin to be reproducible
|
|
|
|
let storage_origin = location.origin().ascii_serialization();
|
|
|
|
if storage_origin == "null" {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(storage_origin)
|
|
|
|
}
|
|
|
|
} else if let Some(config_file) = &ps.maybe_config_file {
|
|
|
|
// otherwise we will use the path to the config file
|
2021-11-24 20:14:19 +00:00
|
|
|
Some(config_file.specifier.to_string())
|
2021-10-27 00:10:27 +00:00
|
|
|
} else {
|
|
|
|
// otherwise we will use the path to the main module
|
|
|
|
Some(main_module.to_string())
|
|
|
|
};
|
|
|
|
|
|
|
|
let origin_storage_dir = maybe_storage_key.map(|key| {
|
|
|
|
ps.dir
|
|
|
|
.root
|
|
|
|
// TODO(@crowlKats): change to origin_data for 2.0
|
|
|
|
.join("location_data")
|
|
|
|
.join(checksum::gen(&[key.as_bytes()]))
|
|
|
|
});
|
|
|
|
|
2021-12-29 13:30:08 +00:00
|
|
|
let mut extensions = ops::cli_exts(ps.clone(), true);
|
|
|
|
extensions.append(&mut custom_extensions);
|
|
|
|
|
2020-12-11 17:49:26 +00:00
|
|
|
let options = WorkerOptions {
|
2021-10-05 20:41:14 +00:00
|
|
|
bootstrap: BootstrapOptions {
|
|
|
|
apply_source_maps: true,
|
|
|
|
args: ps.flags.argv.clone(),
|
|
|
|
cpu_count: num_cpus::get(),
|
|
|
|
debug_flag: ps.flags.log_level.map_or(false, |l| l == log::Level::Debug),
|
|
|
|
enable_testing_features: ps.flags.enable_testing_features,
|
|
|
|
location: ps.flags.location.clone(),
|
|
|
|
no_color: !colors::use_color(),
|
|
|
|
runtime_version: version::deno(),
|
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
|
|
|
unstable: ps.flags.unstable,
|
|
|
|
},
|
2021-12-29 13:30:08 +00:00
|
|
|
extensions,
|
2021-09-24 15:10:42 +00:00
|
|
|
unsafely_ignore_certificate_errors: ps
|
2021-08-09 14:53:21 +00:00
|
|
|
.flags
|
2021-08-10 11:19:45 +00:00
|
|
|
.unsafely_ignore_certificate_errors
|
2021-08-09 14:53:21 +00:00
|
|
|
.clone(),
|
2021-09-24 15:10:42 +00:00
|
|
|
root_cert_store: ps.root_cert_store.clone(),
|
2021-01-08 02:08:51 +00:00
|
|
|
user_agent: version::get_user_agent(),
|
2021-09-24 15:10:42 +00:00
|
|
|
seed: ps.flags.seed,
|
2020-12-11 17:49:26 +00:00
|
|
|
js_error_create_fn: Some(js_error_create_fn),
|
|
|
|
create_web_worker_cb,
|
2022-02-11 12:41:56 +00:00
|
|
|
web_worker_preload_module_cb,
|
2020-12-11 17:49:26 +00:00
|
|
|
maybe_inspector_server,
|
|
|
|
should_break_on_first_statement,
|
|
|
|
module_loader,
|
2020-12-13 18:45:53 +00:00
|
|
|
get_error_class_fn: Some(&crate::errors::get_error_class_name),
|
2021-10-27 00:10:27 +00:00
|
|
|
origin_storage_dir,
|
2021-09-24 15:10:42 +00:00
|
|
|
blob_store: ps.blob_store.clone(),
|
|
|
|
broadcast_channel: ps.broadcast_channel.clone(),
|
|
|
|
shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
|
2021-09-29 08:47:24 +00:00
|
|
|
compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
|
2020-12-11 17:49:26 +00:00
|
|
|
};
|
|
|
|
|
2021-12-29 13:30:08 +00:00
|
|
|
MainWorker::bootstrap_from_options(main_module, permissions, options)
|
2020-12-11 17:49:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 15:24:05 +00:00
|
|
|
pub fn write_to_stdout_ignore_sigpipe(
|
|
|
|
bytes: &[u8],
|
|
|
|
) -> Result<(), std::io::Error> {
|
2020-05-11 21:33:36 +00:00
|
|
|
use std::io::ErrorKind;
|
|
|
|
|
|
|
|
match std::io::stdout().write_all(bytes) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(e) => match e.kind() {
|
|
|
|
ErrorKind::BrokenPipe => Ok(()),
|
|
|
|
_ => Err(e),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 15:24:05 +00:00
|
|
|
pub fn write_json_to_stdout<T>(value: &T) -> Result<(), AnyError>
|
2020-07-08 14:50:12 +00:00
|
|
|
where
|
|
|
|
T: ?Sized + serde::ser::Serialize,
|
|
|
|
{
|
2021-06-03 18:49:02 +00:00
|
|
|
let mut writer = std::io::BufWriter::new(std::io::stdout());
|
|
|
|
serde_json::to_writer_pretty(&mut writer, value)?;
|
|
|
|
writeln!(&mut writer)?;
|
|
|
|
Ok(())
|
2020-07-08 14:50:12 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 16:30:13 +00:00
|
|
|
fn print_cache_info(
|
2021-09-24 15:10:42 +00:00
|
|
|
state: &ProcState,
|
2020-08-18 16:30:13 +00:00
|
|
|
json: bool,
|
2021-10-10 21:26:22 +00:00
|
|
|
location: Option<&deno_core::url::Url>,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<(), AnyError> {
|
2020-07-08 14:50:12 +00:00
|
|
|
let deno_dir = &state.dir.root;
|
2020-11-06 00:38:21 +00:00
|
|
|
let modules_cache = &state.file_fetcher.get_http_cache_location();
|
2020-07-08 14:50:12 +00:00
|
|
|
let typescript_cache = &state.dir.gen_cache.location;
|
2021-04-09 01:27:27 +00:00
|
|
|
let registry_cache =
|
|
|
|
&state.dir.root.join(lsp::language_server::REGISTRIES_PATH);
|
2021-05-27 05:23:12 +00:00
|
|
|
let mut origin_dir = state.dir.root.join("location_data");
|
|
|
|
|
|
|
|
if let Some(location) = &location {
|
|
|
|
origin_dir =
|
|
|
|
origin_dir.join(&checksum::gen(&[location.to_string().as_bytes()]));
|
|
|
|
}
|
|
|
|
|
2020-07-08 14:50:12 +00:00
|
|
|
if json {
|
2021-05-27 05:23:12 +00:00
|
|
|
let mut output = json!({
|
|
|
|
"denoDir": deno_dir,
|
|
|
|
"modulesCache": modules_cache,
|
|
|
|
"typescriptCache": typescript_cache,
|
|
|
|
"registryCache": registry_cache,
|
|
|
|
"originStorage": origin_dir,
|
2020-07-08 14:50:12 +00:00
|
|
|
});
|
2021-05-27 05:23:12 +00:00
|
|
|
|
|
|
|
if location.is_some() {
|
|
|
|
output["localStorage"] =
|
|
|
|
serde_json::to_value(origin_dir.join("local_storage"))?;
|
|
|
|
}
|
|
|
|
|
2020-07-08 14:50:12 +00:00
|
|
|
write_json_to_stdout(&output)
|
|
|
|
} else {
|
|
|
|
println!("{} {:?}", colors::bold("DENO_DIR location:"), deno_dir);
|
|
|
|
println!(
|
|
|
|
"{} {:?}",
|
|
|
|
colors::bold("Remote modules cache:"),
|
|
|
|
modules_cache
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{} {:?}",
|
2021-04-09 01:27:27 +00:00
|
|
|
colors::bold("Emitted modules cache:"),
|
2020-07-08 14:50:12 +00:00
|
|
|
typescript_cache
|
|
|
|
);
|
2021-04-09 01:27:27 +00:00
|
|
|
println!(
|
|
|
|
"{} {:?}",
|
|
|
|
colors::bold("Language server registries cache:"),
|
|
|
|
registry_cache,
|
|
|
|
);
|
2021-05-27 05:23:12 +00:00
|
|
|
println!("{} {:?}", colors::bold("Origin storage:"), origin_dir);
|
|
|
|
if location.is_some() {
|
|
|
|
println!(
|
|
|
|
"{} {:?}",
|
|
|
|
colors::bold("Local Storage:"),
|
|
|
|
origin_dir.join("local_storage"),
|
|
|
|
);
|
|
|
|
}
|
2020-07-08 14:50:12 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 15:24:05 +00:00
|
|
|
pub fn get_types(unstable: bool) -> String {
|
2021-06-10 14:18:13 +00:00
|
|
|
let mut types = vec![
|
2021-01-06 01:38:23 +00:00
|
|
|
crate::tsc::DENO_NS_LIB,
|
2021-03-12 20:23:59 +00:00
|
|
|
crate::tsc::DENO_CONSOLE_LIB,
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 15:17:18 +00:00
|
|
|
crate::tsc::DENO_URL_LIB,
|
2021-01-06 01:38:23 +00:00
|
|
|
crate::tsc::DENO_WEB_LIB,
|
|
|
|
crate::tsc::DENO_FETCH_LIB,
|
2021-03-01 10:31:13 +00:00
|
|
|
crate::tsc::DENO_WEBGPU_LIB,
|
2021-01-06 15:57:28 +00:00
|
|
|
crate::tsc::DENO_WEBSOCKET_LIB,
|
2021-05-10 10:02:47 +00:00
|
|
|
crate::tsc::DENO_WEBSTORAGE_LIB,
|
2021-03-26 07:43:58 +00:00
|
|
|
crate::tsc::DENO_CRYPTO_LIB,
|
2021-05-22 16:08:24 +00:00
|
|
|
crate::tsc::DENO_BROADCAST_CHANNEL_LIB,
|
2021-06-28 23:43:03 +00:00
|
|
|
crate::tsc::DENO_NET_LIB,
|
2021-01-06 01:38:23 +00:00
|
|
|
crate::tsc::SHARED_GLOBALS_LIB,
|
|
|
|
crate::tsc::WINDOW_LIB,
|
2021-06-10 14:18:13 +00:00
|
|
|
];
|
2020-08-04 14:08:41 +00:00
|
|
|
|
2020-05-11 21:33:36 +00:00
|
|
|
if unstable {
|
2021-06-10 14:18:13 +00:00
|
|
|
types.push(crate::tsc::UNSTABLE_NS_LIB);
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
2020-08-04 14:08:41 +00:00
|
|
|
|
2021-06-10 14:18:13 +00:00
|
|
|
types.join("\n")
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 19:35:12 +00:00
|
|
|
async fn compile_command(
|
|
|
|
flags: Flags,
|
2021-09-03 23:33:35 +00:00
|
|
|
compile_flags: CompileFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2020-11-30 19:35:12 +00:00
|
|
|
let debug = flags.log_level == Some(log::Level::Debug);
|
|
|
|
|
2022-02-11 19:04:31 +00:00
|
|
|
let run_flags =
|
|
|
|
tools::standalone::compile_to_runtime_flags(&flags, compile_flags.args)?;
|
2021-01-04 23:15:52 +00:00
|
|
|
|
2021-09-03 23:33:35 +00:00
|
|
|
let module_specifier = resolve_url_or_path(&compile_flags.source_file)?;
|
2022-02-11 19:04:31 +00:00
|
|
|
let ps = ProcState::build(Arc::new(flags)).await?;
|
2021-09-24 15:10:42 +00:00
|
|
|
let deno_dir = &ps.dir;
|
2020-11-30 19:35:12 +00:00
|
|
|
|
2022-01-11 04:24:39 +00:00
|
|
|
let output = compile_flags.output.and_then(|output| {
|
|
|
|
if fs_util::path_has_trailing_slash(&output) {
|
|
|
|
let infer_file_name = infer_name_from_url(&module_specifier).map(PathBuf::from)?;
|
|
|
|
Some(output.join(infer_file_name))
|
|
|
|
} else {
|
|
|
|
Some(output)
|
|
|
|
}
|
|
|
|
}).or_else(|| {
|
2021-02-17 18:47:18 +00:00
|
|
|
infer_name_from_url(&module_specifier).map(PathBuf::from)
|
2020-12-01 14:11:02 +00:00
|
|
|
}).ok_or_else(|| generic_error(
|
|
|
|
"An executable name was not provided. One could not be inferred from the URL. Aborting.",
|
|
|
|
))?;
|
2020-11-30 19:35:12 +00:00
|
|
|
|
2022-02-15 12:33:46 +00:00
|
|
|
let graph = Arc::try_unwrap(
|
|
|
|
create_graph_and_maybe_check(module_specifier.clone(), &ps, debug).await?,
|
|
|
|
)
|
|
|
|
.map_err(|_| {
|
|
|
|
generic_error("There should only be one reference to ModuleGraph")
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let eszip = eszip::EszipV2::from_graph(graph, Default::default())?;
|
2020-11-30 19:35:12 +00:00
|
|
|
|
|
|
|
info!(
|
|
|
|
"{} {}",
|
|
|
|
colors::green("Compile"),
|
|
|
|
module_specifier.to_string()
|
|
|
|
);
|
2021-01-19 02:40:22 +00:00
|
|
|
|
2021-04-26 17:28:38 +00:00
|
|
|
// Select base binary based on target
|
2021-01-19 02:40:22 +00:00
|
|
|
let original_binary =
|
2021-09-03 23:33:35 +00:00
|
|
|
tools::standalone::get_base_binary(deno_dir, compile_flags.target.clone())
|
|
|
|
.await?;
|
2021-01-19 02:40:22 +00:00
|
|
|
|
|
|
|
let final_bin = tools::standalone::create_standalone_binary(
|
|
|
|
original_binary,
|
2022-02-15 12:33:46 +00:00
|
|
|
eszip,
|
|
|
|
module_specifier.clone(),
|
2021-01-08 02:08:51 +00:00
|
|
|
run_flags,
|
2021-01-19 02:40:22 +00:00
|
|
|
)?;
|
2020-11-30 19:35:12 +00:00
|
|
|
|
2020-12-01 14:11:02 +00:00
|
|
|
info!("{} {}", colors::green("Emit"), output.display());
|
2020-11-30 19:35:12 +00:00
|
|
|
|
2021-09-03 23:33:35 +00:00
|
|
|
tools::standalone::write_standalone_binary(
|
|
|
|
output.clone(),
|
|
|
|
compile_flags.target,
|
|
|
|
final_bin,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-01-19 02:40:22 +00:00
|
|
|
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2020-11-30 19:35:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:33:36 +00:00
|
|
|
async fn info_command(
|
|
|
|
flags: Flags,
|
2021-09-03 23:33:35 +00:00
|
|
|
info_flags: InfoFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2022-02-11 19:04:31 +00:00
|
|
|
let ps = ProcState::build(Arc::new(flags)).await?;
|
2021-09-03 23:33:35 +00:00
|
|
|
if let Some(specifier) = info_flags.file {
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url_or_path(&specifier)?;
|
2021-10-10 21:26:22 +00:00
|
|
|
let mut cache = cache::FetchCacher::new(
|
|
|
|
ps.dir.gen_cache.clone(),
|
|
|
|
ps.file_fetcher.clone(),
|
2020-10-12 02:25:27 +00:00
|
|
|
Permissions::allow_all(),
|
2021-05-17 07:44:38 +00:00
|
|
|
Permissions::allow_all(),
|
2020-10-12 02:25:27 +00:00
|
|
|
);
|
2021-10-10 21:26:22 +00:00
|
|
|
let maybe_locker = lockfile::as_maybe_locker(ps.lockfile.clone());
|
2021-11-09 01:26:39 +00:00
|
|
|
let maybe_import_map_resolver =
|
|
|
|
ps.maybe_import_map.clone().map(ImportMapResolver::new);
|
|
|
|
let maybe_jsx_resolver = ps
|
|
|
|
.maybe_config_file
|
|
|
|
.as_ref()
|
|
|
|
.map(|cf| {
|
|
|
|
cf.to_maybe_jsx_import_source_module()
|
|
|
|
.map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone()))
|
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
let maybe_resolver = if maybe_jsx_resolver.is_some() {
|
|
|
|
maybe_jsx_resolver.as_ref().map(|jr| jr.as_resolver())
|
|
|
|
} else {
|
|
|
|
maybe_import_map_resolver
|
|
|
|
.as_ref()
|
|
|
|
.map(|im| im.as_resolver())
|
|
|
|
};
|
2021-10-10 21:26:22 +00:00
|
|
|
let graph = deno_graph::create_graph(
|
2022-01-31 22:33:57 +00:00
|
|
|
vec![(specifier, deno_graph::ModuleKind::Esm)],
|
2021-10-10 21:26:22 +00:00
|
|
|
false,
|
|
|
|
None,
|
|
|
|
&mut cache,
|
2021-11-09 01:26:39 +00:00
|
|
|
maybe_resolver,
|
2021-10-10 21:26:22 +00:00
|
|
|
maybe_locker,
|
|
|
|
None,
|
2022-01-13 16:58:00 +00:00
|
|
|
None,
|
2021-10-10 21:26:22 +00:00
|
|
|
)
|
|
|
|
.await;
|
2020-09-07 13:59:47 +00:00
|
|
|
|
2021-09-03 23:33:35 +00:00
|
|
|
if info_flags.json {
|
2021-12-11 14:56:45 +00:00
|
|
|
write_json_to_stdout(&json!(graph))?;
|
2020-09-07 13:59:47 +00:00
|
|
|
} else {
|
2021-12-11 14:56:45 +00:00
|
|
|
write_to_stdout_ignore_sigpipe(graph.to_string().as_bytes())?;
|
2020-09-07 13:59:47 +00:00
|
|
|
}
|
2020-10-12 02:25:27 +00:00
|
|
|
} else {
|
|
|
|
// If it was just "deno info" print location of caches and exit
|
2021-12-11 14:56:45 +00:00
|
|
|
print_cache_info(&ps, info_flags.json, ps.flags.location.as_ref())?;
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn install_command(
|
|
|
|
flags: Flags,
|
2021-09-03 23:33:35 +00:00
|
|
|
install_flags: InstallFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2020-10-19 19:19:20 +00:00
|
|
|
let mut preload_flags = flags.clone();
|
|
|
|
preload_flags.inspect = None;
|
|
|
|
preload_flags.inspect_brk = None;
|
2022-02-11 19:04:31 +00:00
|
|
|
let permissions =
|
|
|
|
Permissions::from_options(&preload_flags.permissions_options());
|
|
|
|
let ps = ProcState::build(Arc::new(preload_flags)).await?;
|
2021-09-03 23:33:35 +00:00
|
|
|
let main_module = resolve_url_or_path(&install_flags.module_url)?;
|
2020-11-02 22:37:55 +00:00
|
|
|
let mut worker =
|
2021-12-29 13:30:08 +00:00
|
|
|
create_main_worker(&ps, main_module.clone(), permissions, vec![]);
|
2020-09-20 12:05:11 +00:00
|
|
|
// First, fetch and compile the module; this step ensures that the module exists.
|
2021-09-18 01:44:53 +00:00
|
|
|
worker.preload_module(&main_module, true).await?;
|
2021-12-21 14:49:27 +00:00
|
|
|
tools::installer::install(flags, install_flags)?;
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 15:38:07 +00:00
|
|
|
async fn uninstall_command(
|
|
|
|
uninstall_flags: UninstallFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
|
|
|
tools::installer::uninstall(uninstall_flags.name, uninstall_flags.root)?;
|
|
|
|
Ok(0)
|
2021-09-30 15:38:07 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 14:56:45 +00:00
|
|
|
async fn lsp_command() -> Result<i32, AnyError> {
|
|
|
|
lsp::start().await?;
|
|
|
|
Ok(0)
|
2020-12-07 10:46:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:42:12 +00:00
|
|
|
async fn lint_command(
|
2021-09-03 15:01:58 +00:00
|
|
|
flags: Flags,
|
2021-09-03 23:33:35 +00:00
|
|
|
lint_flags: LintFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2021-09-03 23:33:35 +00:00
|
|
|
if lint_flags.rules {
|
|
|
|
tools::lint::print_rules_list(lint_flags.json);
|
2021-12-11 14:56:45 +00:00
|
|
|
return Ok(0);
|
2020-06-12 14:42:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 16:39:39 +00:00
|
|
|
tools::lint::lint(flags, lint_flags).await?;
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2020-06-08 12:06:20 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 16:48:57 +00:00
|
|
|
async fn cache_command(
|
|
|
|
flags: Flags,
|
2021-09-03 23:33:35 +00:00
|
|
|
cache_flags: CacheFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2020-11-02 02:51:56 +00:00
|
|
|
let lib = if flags.unstable {
|
2021-10-10 21:26:22 +00:00
|
|
|
emit::TypeLib::UnstableDenoWindow
|
2020-11-02 02:51:56 +00:00
|
|
|
} else {
|
2021-10-10 21:26:22 +00:00
|
|
|
emit::TypeLib::DenoWindow
|
2020-11-02 02:51:56 +00:00
|
|
|
};
|
2022-02-11 19:04:31 +00:00
|
|
|
let ps = ProcState::build(Arc::new(flags)).await?;
|
2020-05-11 21:33:36 +00:00
|
|
|
|
2021-09-03 23:33:35 +00:00
|
|
|
for file in cache_flags.files {
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url_or_path(&file)?;
|
2021-09-24 15:10:42 +00:00
|
|
|
ps.prepare_module_load(
|
2022-01-31 22:33:57 +00:00
|
|
|
vec![(specifier, deno_graph::ModuleKind::Esm)],
|
2021-10-10 21:26:22 +00:00
|
|
|
false,
|
2021-09-24 15:10:42 +00:00
|
|
|
lib.clone(),
|
|
|
|
Permissions::allow_all(),
|
|
|
|
Permissions::allow_all(),
|
2021-11-15 23:25:52 +00:00
|
|
|
false,
|
2021-09-24 15:10:42 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn eval_command(
|
|
|
|
flags: Flags,
|
2021-09-03 23:33:35 +00:00
|
|
|
eval_flags: EvalFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2021-10-10 21:26:22 +00:00
|
|
|
// 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)).unwrap();
|
2022-02-11 19:04:31 +00:00
|
|
|
let permissions = Permissions::from_options(&flags.permissions_options());
|
|
|
|
let ps = ProcState::build(Arc::new(flags)).await?;
|
2020-11-02 22:37:55 +00:00
|
|
|
let mut worker =
|
2021-12-29 13:30:08 +00:00
|
|
|
create_main_worker(&ps, main_module.clone(), permissions, vec![]);
|
2020-05-11 21:33:36 +00:00
|
|
|
// Create a dummy source file.
|
2021-09-03 23:33:35 +00:00
|
|
|
let source_code = if eval_flags.print {
|
|
|
|
format!("console.log({})", eval_flags.code)
|
2020-05-21 14:35:36 +00:00
|
|
|
} else {
|
2021-09-03 23:33:35 +00:00
|
|
|
eval_flags.code
|
2020-05-21 14:35:36 +00:00
|
|
|
}
|
|
|
|
.into_bytes();
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let file = File {
|
2021-02-17 18:47:18 +00:00
|
|
|
local: main_module.clone().to_file_path().unwrap(),
|
2020-11-06 00:38:21 +00:00
|
|
|
maybe_types: None,
|
2021-10-10 21:26:22 +00:00
|
|
|
media_type: MediaType::Unknown,
|
2021-09-07 14:39:32 +00:00
|
|
|
source: Arc::new(String::from_utf8(source_code)?),
|
2021-02-17 18:47:18 +00:00
|
|
|
specifier: main_module.clone(),
|
2021-09-02 15:38:19 +00:00
|
|
|
maybe_headers: None,
|
2020-05-11 21:33:36 +00:00
|
|
|
};
|
2020-11-06 00:38:21 +00:00
|
|
|
|
2020-05-11 21:33:36 +00:00
|
|
|
// Save our fake file into file fetcher cache
|
2020-08-04 14:08:41 +00:00
|
|
|
// to allow module access by TS compiler.
|
2021-09-24 15:10:42 +00:00
|
|
|
ps.file_fetcher.insert_cached(file);
|
2020-05-11 21:33:36 +00:00
|
|
|
debug!("main_module {}", &main_module);
|
2022-02-11 19:04:31 +00:00
|
|
|
if ps.flags.compat {
|
2021-10-10 21:26:22 +00:00
|
|
|
worker.execute_side_module(&compat::GLOBAL_URL).await?;
|
2021-10-06 17:07:04 +00:00
|
|
|
}
|
2021-09-18 01:44:53 +00:00
|
|
|
worker.execute_main_module(&main_module).await?;
|
2021-12-21 14:49:27 +00:00
|
|
|
worker.dispatch_load_event(&located_script_name!())?;
|
2021-05-26 19:07:12 +00:00
|
|
|
worker.run_event_loop(false).await?;
|
2021-12-21 14:49:27 +00:00
|
|
|
worker.dispatch_unload_event(&located_script_name!())?;
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 21:26:22 +00:00
|
|
|
async fn create_graph_and_maybe_check(
|
|
|
|
root: ModuleSpecifier,
|
|
|
|
ps: &ProcState,
|
2020-11-30 19:35:12 +00:00
|
|
|
debug: bool,
|
2021-10-10 21:26:22 +00:00
|
|
|
) -> Result<Arc<deno_graph::ModuleGraph>, AnyError> {
|
|
|
|
let mut cache = cache::FetchCacher::new(
|
|
|
|
ps.dir.gen_cache.clone(),
|
|
|
|
ps.file_fetcher.clone(),
|
2020-11-30 19:35:12 +00:00
|
|
|
Permissions::allow_all(),
|
2021-05-17 07:44:38 +00:00
|
|
|
Permissions::allow_all(),
|
2020-11-30 19:35:12 +00:00
|
|
|
);
|
2021-10-10 21:26:22 +00:00
|
|
|
let maybe_locker = lockfile::as_maybe_locker(ps.lockfile.clone());
|
2021-11-09 01:26:39 +00:00
|
|
|
let maybe_imports = if let Some(config_file) = &ps.maybe_config_file {
|
|
|
|
config_file.to_maybe_imports()?
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let maybe_import_map_resolver =
|
|
|
|
ps.maybe_import_map.clone().map(ImportMapResolver::new);
|
|
|
|
let maybe_jsx_resolver = ps
|
2021-10-10 21:26:22 +00:00
|
|
|
.maybe_config_file
|
|
|
|
.as_ref()
|
2021-11-09 01:26:39 +00:00
|
|
|
.map(|cf| {
|
|
|
|
cf.to_maybe_jsx_import_source_module()
|
|
|
|
.map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone()))
|
|
|
|
})
|
2021-10-10 21:26:22 +00:00
|
|
|
.flatten();
|
2021-11-09 01:26:39 +00:00
|
|
|
let maybe_resolver = if maybe_jsx_resolver.is_some() {
|
|
|
|
maybe_jsx_resolver.as_ref().map(|jr| jr.as_resolver())
|
|
|
|
} else {
|
|
|
|
maybe_import_map_resolver
|
|
|
|
.as_ref()
|
|
|
|
.map(|im| im.as_resolver())
|
|
|
|
};
|
2021-10-10 21:26:22 +00:00
|
|
|
let graph = Arc::new(
|
|
|
|
deno_graph::create_graph(
|
2022-01-31 22:33:57 +00:00
|
|
|
vec![(root, deno_graph::ModuleKind::Esm)],
|
2021-10-10 21:26:22 +00:00
|
|
|
false,
|
|
|
|
maybe_imports,
|
|
|
|
&mut cache,
|
2021-11-09 01:26:39 +00:00
|
|
|
maybe_resolver,
|
2021-10-10 21:26:22 +00:00
|
|
|
maybe_locker,
|
|
|
|
None,
|
2022-01-13 16:58:00 +00:00
|
|
|
None,
|
2021-10-10 21:26:22 +00:00
|
|
|
)
|
|
|
|
.await,
|
|
|
|
);
|
|
|
|
|
2021-12-22 13:25:06 +00:00
|
|
|
let check_js = ps
|
|
|
|
.maybe_config_file
|
|
|
|
.as_ref()
|
|
|
|
.map(|cf| cf.get_check_js())
|
|
|
|
.unwrap_or(false);
|
|
|
|
graph_valid(&graph, ps.flags.check != CheckFlag::None, check_js)?;
|
2021-12-16 10:45:41 +00:00
|
|
|
graph_lock_or_exit(&graph);
|
2020-11-30 19:35:12 +00:00
|
|
|
|
2021-11-29 22:23:30 +00:00
|
|
|
if ps.flags.check != CheckFlag::None {
|
2021-09-24 15:10:42 +00:00
|
|
|
let lib = if ps.flags.unstable {
|
2021-10-10 21:26:22 +00:00
|
|
|
emit::TypeLib::UnstableDenoWindow
|
2020-11-30 19:35:12 +00:00
|
|
|
} else {
|
2021-10-10 21:26:22 +00:00
|
|
|
emit::TypeLib::DenoWindow
|
2020-11-30 19:35:12 +00:00
|
|
|
};
|
2021-10-10 21:26:22 +00:00
|
|
|
let (ts_config, maybe_ignored_options) = emit::get_ts_config(
|
|
|
|
emit::ConfigType::Check {
|
|
|
|
tsc_emit: false,
|
2020-11-30 19:35:12 +00:00
|
|
|
lib,
|
2021-10-10 21:26:22 +00:00
|
|
|
},
|
|
|
|
ps.maybe_config_file.as_ref(),
|
|
|
|
None,
|
|
|
|
)?;
|
|
|
|
if let Some(ignored_options) = maybe_ignored_options {
|
2020-11-30 19:35:12 +00:00
|
|
|
eprintln!("{}", ignored_options);
|
|
|
|
}
|
2021-11-24 20:14:19 +00:00
|
|
|
let maybe_config_specifier =
|
|
|
|
ps.maybe_config_file.as_ref().map(|cf| cf.specifier.clone());
|
2021-10-10 21:26:22 +00:00
|
|
|
let check_result = emit::check_and_maybe_emit(
|
2021-12-16 10:45:41 +00:00
|
|
|
&graph.roots,
|
|
|
|
Arc::new(RwLock::new(graph.as_ref().into())),
|
2021-10-10 21:26:22 +00:00
|
|
|
&mut cache,
|
|
|
|
emit::CheckOptions {
|
2021-11-29 22:23:30 +00:00
|
|
|
check: ps.flags.check.clone(),
|
2021-10-10 21:26:22 +00:00
|
|
|
debug,
|
|
|
|
emit_with_diagnostics: false,
|
|
|
|
maybe_config_specifier,
|
|
|
|
ts_config,
|
2021-12-16 10:45:41 +00:00
|
|
|
log_checks: true,
|
2021-11-23 21:20:30 +00:00
|
|
|
reload: ps.flags.reload,
|
2021-12-16 10:45:41 +00:00
|
|
|
reload_exclusions: Default::default(),
|
2021-10-10 21:26:22 +00:00
|
|
|
},
|
|
|
|
)?;
|
|
|
|
debug!("{}", check_result.stats);
|
|
|
|
if !check_result.diagnostics.is_empty() {
|
|
|
|
return Err(check_result.diagnostics.into());
|
2020-11-30 19:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 21:26:22 +00:00
|
|
|
Ok(graph)
|
2020-11-30 19:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bundle_module_graph(
|
2021-10-10 21:26:22 +00:00
|
|
|
graph: &deno_graph::ModuleGraph,
|
|
|
|
ps: &ProcState,
|
|
|
|
flags: &Flags,
|
|
|
|
) -> Result<(String, Option<String>), AnyError> {
|
2022-01-31 22:33:57 +00:00
|
|
|
info!("{} {}", colors::green("Bundle"), graph.roots[0].0);
|
2021-10-10 21:26:22 +00:00
|
|
|
|
|
|
|
let (ts_config, maybe_ignored_options) = emit::get_ts_config(
|
|
|
|
emit::ConfigType::Bundle,
|
|
|
|
ps.maybe_config_file.as_ref(),
|
|
|
|
None,
|
|
|
|
)?;
|
2021-11-29 22:23:30 +00:00
|
|
|
if flags.check == CheckFlag::None {
|
2021-10-10 21:26:22 +00:00
|
|
|
if let Some(ignored_options) = maybe_ignored_options {
|
2020-11-30 19:35:12 +00:00
|
|
|
eprintln!("{}", ignored_options);
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 21:26:22 +00:00
|
|
|
|
|
|
|
emit::bundle(
|
|
|
|
graph,
|
|
|
|
emit::BundleOptions {
|
|
|
|
bundle_type: emit::BundleType::Module,
|
|
|
|
ts_config,
|
2022-01-12 12:05:06 +00:00
|
|
|
emit_ignore_directives: true,
|
2021-10-10 21:26:22 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A function that converts a float to a string the represents a human
|
|
|
|
/// readable version of that number.
|
|
|
|
fn human_size(size: f64) -> String {
|
|
|
|
let negative = if size.is_sign_positive() { "" } else { "-" };
|
|
|
|
let size = size.abs();
|
|
|
|
let units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
|
|
if size < 1_f64 {
|
|
|
|
return format!("{}{}{}", negative, size, "B");
|
|
|
|
}
|
|
|
|
let delimiter = 1024_f64;
|
|
|
|
let exponent = std::cmp::min(
|
|
|
|
(size.ln() / delimiter.ln()).floor() as i32,
|
|
|
|
(units.len() - 1) as i32,
|
|
|
|
);
|
|
|
|
let pretty_bytes = format!("{:.2}", size / delimiter.powi(exponent))
|
|
|
|
.parse::<f64>()
|
|
|
|
.unwrap()
|
|
|
|
* 1_f64;
|
|
|
|
let unit = units[exponent as usize];
|
|
|
|
format!("{}{}{}", negative, pretty_bytes, unit)
|
2020-11-30 19:35:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:33:36 +00:00
|
|
|
async fn bundle_command(
|
|
|
|
flags: Flags,
|
2021-09-03 23:33:35 +00:00
|
|
|
bundle_flags: BundleFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2020-11-22 20:45:44 +00:00
|
|
|
let debug = flags.log_level == Some(log::Level::Debug);
|
2022-02-11 19:04:31 +00:00
|
|
|
let flags = Arc::new(flags);
|
2021-05-10 06:06:13 +00:00
|
|
|
let resolver = |_| {
|
2020-11-22 20:45:44 +00:00
|
|
|
let flags = flags.clone();
|
2021-09-03 23:33:35 +00:00
|
|
|
let source_file1 = bundle_flags.source_file.clone();
|
|
|
|
let source_file2 = bundle_flags.source_file.clone();
|
2020-11-22 20:45:44 +00:00
|
|
|
async move {
|
2021-02-17 18:47:18 +00:00
|
|
|
let module_specifier = resolve_url_or_path(&source_file1)?;
|
2020-11-22 20:45:44 +00:00
|
|
|
|
|
|
|
debug!(">>>>> bundle START");
|
2022-02-11 19:04:31 +00:00
|
|
|
let ps = ProcState::build(flags).await?;
|
2020-11-22 20:45:44 +00:00
|
|
|
|
2021-10-10 21:26:22 +00:00
|
|
|
let graph =
|
|
|
|
create_graph_and_maybe_check(module_specifier, &ps, debug).await?;
|
2020-05-20 14:25:40 +00:00
|
|
|
|
2021-10-10 21:26:22 +00:00
|
|
|
let mut paths_to_watch: Vec<PathBuf> = graph
|
|
|
|
.specifiers()
|
2020-11-22 20:45:44 +00:00
|
|
|
.iter()
|
2021-10-10 21:26:22 +00:00
|
|
|
.filter_map(|(_, r)| {
|
|
|
|
r.as_ref()
|
|
|
|
.ok()
|
2022-01-31 22:33:57 +00:00
|
|
|
.map(|(s, _, _)| s.to_file_path().ok())
|
2021-10-10 21:26:22 +00:00
|
|
|
.flatten()
|
|
|
|
})
|
2020-11-22 20:45:44 +00:00
|
|
|
.collect();
|
2020-05-29 14:32:15 +00:00
|
|
|
|
2021-09-24 15:10:42 +00:00
|
|
|
if let Some(import_map) = ps.flags.import_map_path.as_ref() {
|
2020-11-22 20:45:44 +00:00
|
|
|
paths_to_watch
|
|
|
|
.push(fs_util::resolve_from_cwd(std::path::Path::new(import_map))?);
|
|
|
|
}
|
2020-10-23 11:05:41 +00:00
|
|
|
|
2021-10-10 21:26:22 +00:00
|
|
|
Ok((paths_to_watch, graph, ps))
|
2020-10-20 03:10:42 +00:00
|
|
|
}
|
2020-11-28 14:18:13 +00:00
|
|
|
.map(move |result| match result {
|
2021-10-10 21:26:22 +00:00
|
|
|
Ok((paths_to_watch, graph, ps)) => ResolutionResult::Restart {
|
2021-09-24 15:10:42 +00:00
|
|
|
paths_to_watch,
|
2021-10-10 21:26:22 +00:00
|
|
|
result: Ok((ps, graph)),
|
2021-09-24 15:10:42 +00:00
|
|
|
},
|
2021-05-10 06:06:13 +00:00
|
|
|
Err(e) => ResolutionResult::Restart {
|
|
|
|
paths_to_watch: vec![PathBuf::from(source_file2)],
|
|
|
|
result: Err(e),
|
2020-11-28 14:18:13 +00:00
|
|
|
},
|
|
|
|
})
|
2020-11-22 20:45:44 +00:00
|
|
|
};
|
2020-10-20 03:10:42 +00:00
|
|
|
|
2021-10-10 21:26:22 +00:00
|
|
|
let operation = |(ps, graph): (ProcState, Arc<deno_graph::ModuleGraph>)| {
|
2021-09-03 23:33:35 +00:00
|
|
|
let out_file = bundle_flags.out_file.clone();
|
2020-11-22 20:45:44 +00:00
|
|
|
async move {
|
2021-10-10 21:26:22 +00:00
|
|
|
let (bundle_emit, maybe_bundle_map) =
|
2022-02-11 19:04:31 +00:00
|
|
|
bundle_module_graph(graph.as_ref(), &ps, &ps.flags)?;
|
2020-11-22 20:45:44 +00:00
|
|
|
debug!(">>>>> bundle END");
|
|
|
|
|
|
|
|
if let Some(out_file) = out_file.as_ref() {
|
2021-10-10 21:26:22 +00:00
|
|
|
let output_bytes = bundle_emit.as_bytes();
|
2020-11-22 20:45:44 +00:00
|
|
|
let output_len = output_bytes.len();
|
|
|
|
fs_util::write_file(out_file, output_bytes, 0o644)?;
|
|
|
|
info!(
|
|
|
|
"{} {:?} ({})",
|
|
|
|
colors::green("Emit"),
|
|
|
|
out_file,
|
2021-10-10 21:26:22 +00:00
|
|
|
colors::gray(human_size(output_len as f64))
|
2020-11-22 20:45:44 +00:00
|
|
|
);
|
2021-10-10 21:26:22 +00:00
|
|
|
if let Some(bundle_map) = maybe_bundle_map {
|
|
|
|
let map_bytes = bundle_map.as_bytes();
|
|
|
|
let map_len = map_bytes.len();
|
|
|
|
let ext = if let Some(curr_ext) = out_file.extension() {
|
|
|
|
format!("{}.map", curr_ext.to_string_lossy())
|
|
|
|
} else {
|
|
|
|
"map".to_string()
|
|
|
|
};
|
|
|
|
let map_out_file = out_file.with_extension(ext);
|
|
|
|
fs_util::write_file(&map_out_file, map_bytes, 0o644)?;
|
|
|
|
info!(
|
|
|
|
"{} {:?} ({})",
|
|
|
|
colors::green("Emit"),
|
|
|
|
map_out_file,
|
|
|
|
colors::gray(human_size(map_len as f64))
|
|
|
|
);
|
|
|
|
}
|
2020-11-22 20:45:44 +00:00
|
|
|
} else {
|
2021-10-10 21:26:22 +00:00
|
|
|
println!("{}", bundle_emit);
|
2020-11-22 20:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-15 21:04:43 +00:00
|
|
|
if flags.watch.is_some() {
|
2022-01-31 16:39:39 +00:00
|
|
|
file_watcher::watch_func(
|
|
|
|
resolver,
|
|
|
|
operation,
|
|
|
|
file_watcher::PrintConfig {
|
|
|
|
job_name: "Bundle".to_string(),
|
|
|
|
clear_screen: !flags.no_clear_screen,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2020-05-29 14:32:15 +00:00
|
|
|
} else {
|
2021-05-10 06:06:13 +00:00
|
|
|
let module_graph =
|
|
|
|
if let ResolutionResult::Restart { result, .. } = resolver(None).await {
|
|
|
|
result?
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
};
|
2020-11-22 20:45:44 +00:00
|
|
|
operation(module_graph).await?;
|
2020-05-29 14:32:15 +00:00
|
|
|
}
|
2020-11-22 20:45:44 +00:00
|
|
|
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn doc_command(
|
|
|
|
flags: Flags,
|
2021-09-03 23:33:35 +00:00
|
|
|
doc_flags: DocFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2021-12-20 21:29:02 +00:00
|
|
|
tools::doc::print_docs(flags, doc_flags).await?;
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 20:45:44 +00:00
|
|
|
async fn format_command(
|
|
|
|
flags: Flags,
|
2021-09-03 23:33:35 +00:00
|
|
|
fmt_flags: FmtFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2022-02-11 19:04:31 +00:00
|
|
|
let ps = ProcState::build(Arc::new(flags)).await?;
|
2021-09-24 15:10:42 +00:00
|
|
|
let maybe_fmt_config = if let Some(config_file) = &ps.maybe_config_file {
|
|
|
|
config_file.to_fmt_config()?
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2021-09-13 18:19:10 +00:00
|
|
|
|
2021-09-03 23:33:35 +00:00
|
|
|
if fmt_flags.files.len() == 1 && fmt_flags.files[0].to_string_lossy() == "-" {
|
2021-12-11 14:56:45 +00:00
|
|
|
tools::fmt::format_stdin(
|
2021-09-13 20:06:45 +00:00
|
|
|
fmt_flags,
|
2021-09-13 18:19:10 +00:00
|
|
|
maybe_fmt_config.map(|c| c.options).unwrap_or_default(),
|
2021-12-11 14:56:45 +00:00
|
|
|
)?;
|
|
|
|
return Ok(0);
|
2020-11-22 20:45:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 19:04:31 +00:00
|
|
|
tools::fmt::format(ps.flags.as_ref(), fmt_flags, maybe_fmt_config).await?;
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2020-11-22 20:45:44 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 14:56:45 +00:00
|
|
|
async fn repl_command(
|
|
|
|
flags: Flags,
|
|
|
|
repl_flags: ReplFlags,
|
|
|
|
) -> Result<i32, AnyError> {
|
2021-02-17 18:47:18 +00:00
|
|
|
let main_module = resolve_url_or_path("./$deno$repl.ts").unwrap();
|
2022-02-11 19:04:31 +00:00
|
|
|
let permissions = Permissions::from_options(&flags.permissions_options());
|
|
|
|
let ps = ProcState::build(Arc::new(flags)).await?;
|
2020-11-02 22:37:55 +00:00
|
|
|
let mut worker =
|
2021-12-29 13:30:08 +00:00
|
|
|
create_main_worker(&ps, main_module.clone(), permissions, vec![]);
|
2022-02-11 19:04:31 +00:00
|
|
|
if ps.flags.compat {
|
2021-10-10 21:26:22 +00:00
|
|
|
worker.execute_side_module(&compat::GLOBAL_URL).await?;
|
2021-11-24 15:55:10 +00:00
|
|
|
compat::add_global_require(&mut worker.js_runtime, main_module.as_str())?;
|
2022-01-03 19:10:17 +00:00
|
|
|
worker.run_event_loop(false).await?;
|
|
|
|
compat::setup_builtin_modules(&mut worker.js_runtime)?;
|
2021-10-08 15:11:33 +00:00
|
|
|
}
|
2021-05-26 19:07:12 +00:00
|
|
|
worker.run_event_loop(false).await?;
|
2020-10-01 23:14:55 +00:00
|
|
|
|
2021-09-24 15:10:42 +00:00
|
|
|
tools::repl::run(&ps, worker, repl_flags.eval).await
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 14:56:45 +00:00
|
|
|
async fn run_from_stdin(flags: Flags) -> Result<i32, AnyError> {
|
2022-02-11 19:04:31 +00:00
|
|
|
let ps = ProcState::build(Arc::new(flags)).await?;
|
|
|
|
let permissions = Permissions::from_options(&ps.flags.permissions_options());
|
2021-02-17 18:47:18 +00:00
|
|
|
let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap();
|
2021-09-24 15:10:42 +00:00
|
|
|
let mut worker =
|
2021-12-29 13:30:08 +00:00
|
|
|
create_main_worker(&ps.clone(), main_module.clone(), permissions, vec![]);
|
2020-09-11 16:19:49 +00:00
|
|
|
|
|
|
|
let mut source = Vec::new();
|
|
|
|
std::io::stdin().read_to_end(&mut source)?;
|
|
|
|
// Create a dummy source file.
|
2020-11-06 00:38:21 +00:00
|
|
|
let source_file = File {
|
2021-02-17 18:47:18 +00:00
|
|
|
local: main_module.clone().to_file_path().unwrap(),
|
2020-11-06 00:38:21 +00:00
|
|
|
maybe_types: None,
|
2020-09-11 16:19:49 +00:00
|
|
|
media_type: MediaType::TypeScript,
|
2021-09-07 14:39:32 +00:00
|
|
|
source: Arc::new(String::from_utf8(source)?),
|
2020-11-06 00:38:21 +00:00
|
|
|
specifier: main_module.clone(),
|
2021-09-02 15:38:19 +00:00
|
|
|
maybe_headers: None,
|
2020-06-11 14:58:09 +00:00
|
|
|
};
|
2020-09-11 16:19:49 +00:00
|
|
|
// Save our fake file into file fetcher cache
|
|
|
|
// to allow module access by TS compiler
|
2021-09-24 15:10:42 +00:00
|
|
|
ps.file_fetcher.insert_cached(source_file);
|
2020-09-11 16:19:49 +00:00
|
|
|
|
|
|
|
debug!("main_module {}", main_module);
|
2022-02-11 19:04:31 +00:00
|
|
|
if ps.flags.compat {
|
2021-10-10 21:26:22 +00:00
|
|
|
worker.execute_side_module(&compat::GLOBAL_URL).await?;
|
2021-10-06 17:07:04 +00:00
|
|
|
}
|
2021-09-18 01:44:53 +00:00
|
|
|
worker.execute_main_module(&main_module).await?;
|
2021-12-21 14:49:27 +00:00
|
|
|
worker.dispatch_load_event(&located_script_name!())?;
|
2021-05-26 19:07:12 +00:00
|
|
|
worker.run_event_loop(false).await?;
|
2021-12-21 14:49:27 +00:00
|
|
|
worker.dispatch_unload_event(&located_script_name!())?;
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(worker.get_exit_code())
|
2020-09-11 16:19:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 14:56:45 +00:00
|
|
|
// 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> {
|
2022-02-11 19:04:31 +00:00
|
|
|
let flags = Arc::new(flags);
|
2021-05-10 06:06:13 +00:00
|
|
|
let resolver = |_| {
|
2020-11-28 14:18:13 +00:00
|
|
|
let script1 = script.clone();
|
|
|
|
let script2 = script.clone();
|
2020-11-22 20:45:44 +00:00
|
|
|
let flags = flags.clone();
|
2021-12-15 21:04:43 +00:00
|
|
|
let watch_flag = flags.watch.clone();
|
2020-11-22 20:45:44 +00:00
|
|
|
async move {
|
2021-02-17 18:47:18 +00:00
|
|
|
let main_module = resolve_url_or_path(&script1)?;
|
2021-09-24 15:10:42 +00:00
|
|
|
let ps = ProcState::build(flags).await?;
|
2021-10-10 21:26:22 +00:00
|
|
|
let mut cache = cache::FetchCacher::new(
|
|
|
|
ps.dir.gen_cache.clone(),
|
|
|
|
ps.file_fetcher.clone(),
|
2020-11-22 20:45:44 +00:00
|
|
|
Permissions::allow_all(),
|
2021-05-17 07:44:38 +00:00
|
|
|
Permissions::allow_all(),
|
2020-11-22 20:45:44 +00:00
|
|
|
);
|
2021-10-10 21:26:22 +00:00
|
|
|
let maybe_locker = lockfile::as_maybe_locker(ps.lockfile.clone());
|
2021-11-09 01:26:39 +00:00
|
|
|
let maybe_imports = if let Some(config_file) = &ps.maybe_config_file {
|
|
|
|
config_file.to_maybe_imports()?
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let maybe_import_map_resolver =
|
|
|
|
ps.maybe_import_map.clone().map(ImportMapResolver::new);
|
|
|
|
let maybe_jsx_resolver = ps
|
2021-10-10 21:26:22 +00:00
|
|
|
.maybe_config_file
|
|
|
|
.as_ref()
|
2021-11-09 01:26:39 +00:00
|
|
|
.map(|cf| {
|
|
|
|
cf.to_maybe_jsx_import_source_module()
|
|
|
|
.map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone()))
|
|
|
|
})
|
2021-10-10 21:26:22 +00:00
|
|
|
.flatten();
|
2021-11-09 01:26:39 +00:00
|
|
|
let maybe_resolver = if maybe_jsx_resolver.is_some() {
|
|
|
|
maybe_jsx_resolver.as_ref().map(|jr| jr.as_resolver())
|
|
|
|
} else {
|
|
|
|
maybe_import_map_resolver
|
|
|
|
.as_ref()
|
|
|
|
.map(|im| im.as_resolver())
|
|
|
|
};
|
2021-10-10 21:26:22 +00:00
|
|
|
let graph = deno_graph::create_graph(
|
2022-01-31 22:33:57 +00:00
|
|
|
vec![(main_module.clone(), deno_graph::ModuleKind::Esm)],
|
2021-10-10 21:26:22 +00:00
|
|
|
false,
|
|
|
|
maybe_imports,
|
|
|
|
&mut cache,
|
2021-11-09 01:26:39 +00:00
|
|
|
maybe_resolver,
|
2021-10-10 21:26:22 +00:00
|
|
|
maybe_locker,
|
|
|
|
None,
|
2022-01-13 16:58:00 +00:00
|
|
|
None,
|
2021-10-10 21:26:22 +00:00
|
|
|
)
|
|
|
|
.await;
|
2021-12-22 13:25:06 +00:00
|
|
|
let check_js = ps
|
|
|
|
.maybe_config_file
|
|
|
|
.as_ref()
|
|
|
|
.map(|cf| cf.get_check_js())
|
|
|
|
.unwrap_or(false);
|
|
|
|
graph_valid(&graph, ps.flags.check != flags::CheckFlag::None, check_js)?;
|
2020-11-22 20:45:44 +00:00
|
|
|
|
|
|
|
// Find all local files in graph
|
2021-10-10 21:26:22 +00:00
|
|
|
let mut paths_to_watch: Vec<PathBuf> = graph
|
|
|
|
.specifiers()
|
2020-11-22 20:45:44 +00:00
|
|
|
.iter()
|
2021-10-10 21:26:22 +00:00
|
|
|
.filter_map(|(_, r)| {
|
|
|
|
r.as_ref()
|
|
|
|
.ok()
|
2022-01-31 22:33:57 +00:00
|
|
|
.map(|(s, _, _)| s.to_file_path().ok())
|
2021-10-10 21:26:22 +00:00
|
|
|
.flatten()
|
|
|
|
})
|
2020-11-22 20:45:44 +00:00
|
|
|
.collect();
|
|
|
|
|
2021-12-15 21:04:43 +00:00
|
|
|
// Add the extra files listed in the watch flag
|
|
|
|
if let Some(watch_paths) = watch_flag {
|
|
|
|
paths_to_watch.extend(watch_paths);
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:10:42 +00:00
|
|
|
if let Some(import_map) = ps.flags.import_map_path.as_ref() {
|
2020-11-22 20:45:44 +00:00
|
|
|
paths_to_watch
|
|
|
|
.push(fs_util::resolve_from_cwd(std::path::Path::new(import_map))?);
|
|
|
|
}
|
2020-09-11 16:19:49 +00:00
|
|
|
|
2021-09-24 15:10:42 +00:00
|
|
|
Ok((paths_to_watch, main_module, ps))
|
2020-11-22 20:45:44 +00:00
|
|
|
}
|
2020-11-28 14:18:13 +00:00
|
|
|
.map(move |result| match result {
|
2021-09-24 15:10:42 +00:00
|
|
|
Ok((paths_to_watch, module_info, ps)) => ResolutionResult::Restart {
|
|
|
|
paths_to_watch,
|
|
|
|
result: Ok((ps, module_info)),
|
|
|
|
},
|
2021-05-10 06:06:13 +00:00
|
|
|
Err(e) => ResolutionResult::Restart {
|
|
|
|
paths_to_watch: vec![PathBuf::from(script2)],
|
|
|
|
result: Err(e),
|
2020-11-28 14:18:13 +00:00
|
|
|
},
|
|
|
|
})
|
2020-11-22 20:45:44 +00:00
|
|
|
};
|
2020-09-23 11:56:16 +00:00
|
|
|
|
2021-08-24 20:34:09 +00:00
|
|
|
/// The FileWatcherModuleExecutor provides module execution with safe dispatching of life-cycle events by tracking the
|
|
|
|
/// state of any pending events and emitting accordingly on drop in the case of a future
|
|
|
|
/// cancellation.
|
|
|
|
struct FileWatcherModuleExecutor {
|
|
|
|
worker: MainWorker,
|
|
|
|
pending_unload: bool,
|
2021-10-06 17:07:04 +00:00
|
|
|
compat: bool,
|
2021-08-24 20:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FileWatcherModuleExecutor {
|
2021-10-06 17:07:04 +00:00
|
|
|
pub fn new(worker: MainWorker, compat: bool) -> FileWatcherModuleExecutor {
|
2021-08-24 20:34:09 +00:00
|
|
|
FileWatcherModuleExecutor {
|
|
|
|
worker,
|
|
|
|
pending_unload: false,
|
2021-10-06 17:07:04 +00:00
|
|
|
compat,
|
2021-08-24 20:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute the given main module emitting load and unload events before and after execution
|
|
|
|
/// respectively.
|
|
|
|
pub async fn execute(
|
|
|
|
&mut self,
|
|
|
|
main_module: &ModuleSpecifier,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-10-06 17:07:04 +00:00
|
|
|
if self.compat {
|
2021-10-10 21:26:22 +00:00
|
|
|
self.worker.execute_side_module(&compat::GLOBAL_URL).await?;
|
2021-10-06 17:07:04 +00:00
|
|
|
}
|
2021-09-18 01:44:53 +00:00
|
|
|
self.worker.execute_main_module(main_module).await?;
|
2021-12-21 14:49:27 +00:00
|
|
|
self.worker.dispatch_load_event(&located_script_name!())?;
|
2021-08-24 20:34:09 +00:00
|
|
|
self.pending_unload = true;
|
|
|
|
|
|
|
|
let result = self.worker.run_event_loop(false).await;
|
|
|
|
self.pending_unload = false;
|
|
|
|
|
|
|
|
if let Err(err) = result {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
|
2021-12-21 14:49:27 +00:00
|
|
|
self.worker.dispatch_unload_event(&located_script_name!())?;
|
2021-08-24 20:34:09 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for FileWatcherModuleExecutor {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.pending_unload {
|
|
|
|
self
|
|
|
|
.worker
|
2021-12-21 14:49:27 +00:00
|
|
|
.dispatch_unload_event(&located_script_name!())
|
2021-08-24 20:34:09 +00:00
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:10:42 +00:00
|
|
|
let operation = |(ps, main_module): (ProcState, ModuleSpecifier)| {
|
|
|
|
let flags = flags.clone();
|
2022-02-11 19:04:31 +00:00
|
|
|
let permissions = Permissions::from_options(&flags.permissions_options());
|
2021-09-24 15:10:42 +00:00
|
|
|
async move {
|
|
|
|
// We make use an module executor guard to ensure that unload is always fired when an
|
|
|
|
// operation is called.
|
2021-10-06 17:07:04 +00:00
|
|
|
let mut executor = FileWatcherModuleExecutor::new(
|
2021-12-29 13:30:08 +00:00
|
|
|
create_main_worker(&ps, main_module.clone(), permissions, vec![]),
|
2021-10-06 17:07:04 +00:00
|
|
|
flags.compat,
|
|
|
|
);
|
2021-09-24 15:10:42 +00:00
|
|
|
|
|
|
|
executor.execute(&main_module).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
2020-11-22 20:45:44 +00:00
|
|
|
|
2022-01-31 16:39:39 +00:00
|
|
|
file_watcher::watch_func(
|
|
|
|
resolver,
|
|
|
|
operation,
|
|
|
|
file_watcher::PrintConfig {
|
|
|
|
job_name: "Process".to_string(),
|
|
|
|
clear_screen: !flags.no_clear_screen,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2020-09-11 16:19:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 23:33:35 +00:00
|
|
|
async fn run_command(
|
|
|
|
flags: Flags,
|
|
|
|
run_flags: RunFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2020-09-11 16:19:49 +00:00
|
|
|
// Read script content from stdin
|
2021-09-03 23:33:35 +00:00
|
|
|
if run_flags.script == "-" {
|
2020-09-11 16:19:49 +00:00
|
|
|
return run_from_stdin(flags).await;
|
|
|
|
}
|
|
|
|
|
2021-12-15 21:04:43 +00:00
|
|
|
if flags.watch.is_some() {
|
2021-09-03 23:33:35 +00:00
|
|
|
return run_with_watch(flags, run_flags.script).await;
|
2020-09-11 16:19:49 +00:00
|
|
|
}
|
2020-06-11 14:58:09 +00:00
|
|
|
|
2021-10-18 17:36:28 +00:00
|
|
|
// TODO(bartlomieju): it should not be resolved here if we're in compat mode
|
|
|
|
// because it might be a bare specifier
|
|
|
|
// 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
|
2021-09-03 23:33:35 +00:00
|
|
|
let main_module = resolve_url_or_path(&run_flags.script)?;
|
2022-02-11 19:04:31 +00:00
|
|
|
let ps = ProcState::build(Arc::new(flags)).await?;
|
|
|
|
let permissions = Permissions::from_options(&ps.flags.permissions_options());
|
2020-11-02 22:37:55 +00:00
|
|
|
let mut worker =
|
2021-12-29 13:30:08 +00:00
|
|
|
create_main_worker(&ps, main_module.clone(), permissions, vec![]);
|
2020-12-28 15:51:26 +00:00
|
|
|
|
|
|
|
let mut maybe_coverage_collector =
|
2021-09-24 15:10:42 +00:00
|
|
|
if let Some(ref coverage_dir) = ps.coverage_dir {
|
2021-05-26 15:47:33 +00:00
|
|
|
let session = worker.create_inspector_session().await;
|
2020-12-28 15:51:26 +00:00
|
|
|
|
|
|
|
let coverage_dir = PathBuf::from(coverage_dir);
|
|
|
|
let mut coverage_collector =
|
|
|
|
tools::coverage::CoverageCollector::new(coverage_dir, session);
|
2021-05-31 14:53:49 +00:00
|
|
|
worker
|
|
|
|
.with_event_loop(coverage_collector.start_collecting().boxed_local())
|
|
|
|
.await?;
|
2020-12-28 15:51:26 +00:00
|
|
|
Some(coverage_collector)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-05-11 21:33:36 +00:00
|
|
|
debug!("main_module {}", main_module);
|
2021-10-18 17:36:28 +00:00
|
|
|
|
2022-02-11 19:04:31 +00:00
|
|
|
if ps.flags.compat {
|
2021-10-18 17:36:28 +00:00
|
|
|
// TODO(bartlomieju): fix me
|
|
|
|
assert_eq!(main_module.scheme(), "file");
|
|
|
|
|
|
|
|
// Set up Node globals
|
2021-10-10 21:26:22 +00:00
|
|
|
worker.execute_side_module(&compat::GLOBAL_URL).await?;
|
2021-10-18 17:36:28 +00:00
|
|
|
// And `module` module that we'll use for checking which
|
|
|
|
// loader to use and potentially load CJS module with.
|
|
|
|
// This allows to skip permission check for `--allow-net`
|
|
|
|
// which would otherwise be requested by dynamically importing
|
|
|
|
// this file.
|
|
|
|
worker.execute_side_module(&compat::MODULE_URL).await?;
|
|
|
|
|
2021-11-01 18:46:07 +00:00
|
|
|
let use_esm_loader = compat::check_if_should_use_esm_loader(&main_module)?;
|
2021-10-18 17:36:28 +00:00
|
|
|
|
|
|
|
if use_esm_loader {
|
|
|
|
// ES module execution in Node compatiblity mode
|
|
|
|
worker.execute_main_module(&main_module).await?;
|
|
|
|
} else {
|
|
|
|
// CJS module execution in Node compatiblity mode
|
|
|
|
compat::load_cjs_module(
|
|
|
|
&mut worker.js_runtime,
|
|
|
|
&main_module.to_file_path().unwrap().display().to_string(),
|
2021-12-30 16:18:30 +00:00
|
|
|
true,
|
2021-10-18 17:36:28 +00:00
|
|
|
)?;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Regular ES module execution
|
|
|
|
worker.execute_main_module(&main_module).await?;
|
2021-10-06 17:07:04 +00:00
|
|
|
}
|
2021-10-18 17:36:28 +00:00
|
|
|
|
2021-12-21 14:49:27 +00:00
|
|
|
worker.dispatch_load_event(&located_script_name!())?;
|
2021-05-26 19:07:12 +00:00
|
|
|
worker
|
|
|
|
.run_event_loop(maybe_coverage_collector.is_none())
|
|
|
|
.await?;
|
2021-12-21 14:49:27 +00:00
|
|
|
worker.dispatch_unload_event(&located_script_name!())?;
|
2020-12-28 15:51:26 +00:00
|
|
|
|
|
|
|
if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
|
2021-05-31 14:53:49 +00:00
|
|
|
worker
|
|
|
|
.with_event_loop(coverage_collector.stop_collecting().boxed_local())
|
|
|
|
.await?;
|
2020-12-28 15:51:26 +00:00
|
|
|
}
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(worker.get_exit_code())
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 14:27:51 +00:00
|
|
|
async fn coverage_command(
|
|
|
|
flags: Flags,
|
2021-09-03 23:33:35 +00:00
|
|
|
coverage_flags: CoverageFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2021-09-03 23:33:35 +00:00
|
|
|
if coverage_flags.files.is_empty() {
|
2021-07-15 16:28:14 +00:00
|
|
|
return Err(generic_error("No matching coverage profiles found"));
|
2021-02-24 14:27:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 21:29:02 +00:00
|
|
|
tools::coverage::cover_files(flags, coverage_flags).await?;
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2021-02-24 14:27:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:33:36 +00:00
|
|
|
async fn test_command(
|
|
|
|
flags: Flags,
|
2021-09-03 23:33:35 +00:00
|
|
|
test_flags: TestFlags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Result<i32, AnyError> {
|
2020-12-21 13:04:25 +00:00
|
|
|
if let Some(ref coverage_dir) = flags.coverage_dir {
|
2021-05-21 13:57:00 +00:00
|
|
|
std::fs::create_dir_all(&coverage_dir)?;
|
2021-05-19 13:33:22 +00:00
|
|
|
env::set_var(
|
|
|
|
"DENO_UNSTABLE_COVERAGE_DIR",
|
|
|
|
PathBuf::from(coverage_dir).canonicalize()?,
|
|
|
|
);
|
2020-12-21 13:04:25 +00:00
|
|
|
}
|
2020-09-13 13:01:30 +00:00
|
|
|
|
2021-12-15 21:04:43 +00:00
|
|
|
if flags.watch.is_some() {
|
2021-12-20 21:29:02 +00:00
|
|
|
tools::test::run_tests_with_watch(flags, test_flags).await?;
|
|
|
|
} else {
|
|
|
|
tools::test::run_tests(flags, test_flags).await?;
|
2021-05-10 06:06:13 +00:00
|
|
|
}
|
2020-09-13 13:01:30 +00:00
|
|
|
|
2021-12-11 14:56:45 +00:00
|
|
|
Ok(0)
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 14:49:27 +00:00
|
|
|
async fn completions_command(
|
|
|
|
_flags: Flags,
|
|
|
|
completions_flags: CompletionsFlags,
|
|
|
|
) -> Result<i32, AnyError> {
|
|
|
|
write_to_stdout_ignore_sigpipe(&completions_flags.buf)?;
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn types_command(flags: Flags) -> Result<i32, AnyError> {
|
|
|
|
let types = get_types(flags.unstable);
|
|
|
|
write_to_stdout_ignore_sigpipe(types.as_bytes())?;
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn upgrade_command(
|
|
|
|
_flags: Flags,
|
|
|
|
upgrade_flags: UpgradeFlags,
|
|
|
|
) -> Result<i32, AnyError> {
|
|
|
|
tools::upgrade::upgrade(upgrade_flags).await?;
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
2020-11-26 14:17:45 +00:00
|
|
|
fn init_v8_flags(v8_flags: &[String]) {
|
|
|
|
let v8_flags_includes_help = v8_flags
|
|
|
|
.iter()
|
|
|
|
.any(|flag| flag == "-help" || flag == "--help");
|
2021-01-04 23:15:52 +00:00
|
|
|
// Keep in sync with `standalone.rs`.
|
2020-11-26 14:17:45 +00:00
|
|
|
let v8_flags = once("UNUSED_BUT_NECESSARY_ARG0".to_owned())
|
|
|
|
.chain(v8_flags.iter().cloned())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let unrecognized_v8_flags = v8_set_flags(v8_flags)
|
|
|
|
.into_iter()
|
|
|
|
.skip(1)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if !unrecognized_v8_flags.is_empty() {
|
|
|
|
for f in unrecognized_v8_flags {
|
|
|
|
eprintln!("error: V8 did not recognize flag '{}'", f);
|
2020-08-07 22:51:59 +00:00
|
|
|
}
|
2020-11-26 14:17:45 +00:00
|
|
|
eprintln!("\nFor a list of V8 flags, use '--v8-flags=--help'");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
if v8_flags_includes_help {
|
|
|
|
std::process::exit(0);
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
2020-11-26 14:17:45 +00:00
|
|
|
}
|
2020-05-11 21:33:36 +00:00
|
|
|
|
2020-11-26 14:17:45 +00:00
|
|
|
fn get_subcommand(
|
|
|
|
flags: Flags,
|
2021-12-11 14:56:45 +00:00
|
|
|
) -> Pin<Box<dyn Future<Output = Result<i32, AnyError>>>> {
|
2022-02-11 19:04:31 +00:00
|
|
|
match flags.subcommand.clone() {
|
2021-09-03 23:33:35 +00:00
|
|
|
DenoSubcommand::Bundle(bundle_flags) => {
|
|
|
|
bundle_command(flags, bundle_flags).boxed_local()
|
|
|
|
}
|
|
|
|
DenoSubcommand::Doc(doc_flags) => {
|
|
|
|
doc_command(flags, doc_flags).boxed_local()
|
|
|
|
}
|
|
|
|
DenoSubcommand::Eval(eval_flags) => {
|
|
|
|
eval_command(flags, eval_flags).boxed_local()
|
2021-02-21 16:58:32 +00:00
|
|
|
}
|
2021-09-03 23:33:35 +00:00
|
|
|
DenoSubcommand::Cache(cache_flags) => {
|
|
|
|
cache_command(flags, cache_flags).boxed_local()
|
2020-10-23 14:56:25 +00:00
|
|
|
}
|
2021-09-03 23:33:35 +00:00
|
|
|
DenoSubcommand::Compile(compile_flags) => {
|
|
|
|
compile_command(flags, compile_flags).boxed_local()
|
2021-04-26 17:28:38 +00:00
|
|
|
}
|
2021-09-03 23:33:35 +00:00
|
|
|
DenoSubcommand::Coverage(coverage_flags) => {
|
|
|
|
coverage_command(flags, coverage_flags).boxed_local()
|
2020-07-08 14:50:12 +00:00
|
|
|
}
|
2021-09-03 23:33:35 +00:00
|
|
|
DenoSubcommand::Fmt(fmt_flags) => {
|
|
|
|
format_command(flags, fmt_flags).boxed_local()
|
|
|
|
}
|
|
|
|
DenoSubcommand::Info(info_flags) => {
|
|
|
|
info_command(flags, info_flags).boxed_local()
|
|
|
|
}
|
|
|
|
DenoSubcommand::Install(install_flags) => {
|
|
|
|
install_command(flags, install_flags).boxed_local()
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
2021-09-30 15:38:07 +00:00
|
|
|
DenoSubcommand::Uninstall(uninstall_flags) => {
|
|
|
|
uninstall_command(uninstall_flags).boxed_local()
|
|
|
|
}
|
2021-06-23 01:48:01 +00:00
|
|
|
DenoSubcommand::Lsp => lsp_command().boxed_local(),
|
2021-09-03 23:33:35 +00:00
|
|
|
DenoSubcommand::Lint(lint_flags) => {
|
|
|
|
lint_command(flags, lint_flags).boxed_local()
|
|
|
|
}
|
|
|
|
DenoSubcommand::Repl(repl_flags) => {
|
2021-12-11 14:56:45 +00:00
|
|
|
repl_command(flags, repl_flags).boxed_local()
|
2021-09-03 23:33:35 +00:00
|
|
|
}
|
|
|
|
DenoSubcommand::Run(run_flags) => {
|
|
|
|
run_command(flags, run_flags).boxed_local()
|
|
|
|
}
|
|
|
|
DenoSubcommand::Test(test_flags) => {
|
|
|
|
test_command(flags, test_flags).boxed_local()
|
|
|
|
}
|
2021-12-21 14:49:27 +00:00
|
|
|
DenoSubcommand::Completions(completions_flags) => {
|
|
|
|
completions_command(flags, completions_flags).boxed_local()
|
2020-05-11 21:33:36 +00:00
|
|
|
}
|
2021-12-21 14:49:27 +00:00
|
|
|
DenoSubcommand::Types => types_command(flags).boxed_local(),
|
2021-09-03 23:33:35 +00:00
|
|
|
DenoSubcommand::Upgrade(upgrade_flags) => {
|
2021-12-21 14:49:27 +00:00
|
|
|
upgrade_command(flags, upgrade_flags).boxed_local()
|
2021-09-03 23:33:35 +00:00
|
|
|
}
|
2020-11-26 14:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 13:49:05 +00: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 23:34:27 +00:00
|
|
|
let orig_hook = std::panic::take_hook();
|
|
|
|
std::panic::set_hook(Box::new(move |panic_info| {
|
2021-12-20 13:49:05 +00: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!();
|
|
|
|
eprintln!(
|
|
|
|
"Platform: {} {}",
|
|
|
|
std::env::consts::OS,
|
|
|
|
std::env::consts::ARCH
|
|
|
|
);
|
|
|
|
eprintln!("Version: {}", version::deno());
|
|
|
|
eprintln!("Args: {:?}", std::env::args().collect::<Vec<_>>());
|
|
|
|
eprintln!();
|
2021-09-07 23:34:27 +00:00
|
|
|
orig_hook(panic_info);
|
|
|
|
std::process::exit(1);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-01-07 18:06:08 +00:00
|
|
|
fn unwrap_or_exit<T>(result: Result<T, AnyError>) -> T {
|
|
|
|
match result {
|
|
|
|
Ok(value) => value,
|
|
|
|
Err(error) => {
|
2022-01-14 16:38:17 +00:00
|
|
|
eprintln!(
|
|
|
|
"{}: {}",
|
|
|
|
colors::red_bold("error"),
|
|
|
|
format!("{:?}", error).trim_start_matches("error: ")
|
|
|
|
);
|
2021-01-07 18:06:08 +00:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 14:17:45 +00:00
|
|
|
pub fn main() {
|
2021-12-20 13:49:05 +00:00
|
|
|
setup_panic_hook();
|
2021-10-25 15:16:16 +00:00
|
|
|
|
|
|
|
unix_util::raise_fd_limit();
|
|
|
|
windows_util::ensure_stdio_open();
|
2020-11-26 14:17:45 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
colors::enable_ansi(); // For Windows 10
|
|
|
|
|
|
|
|
let args: Vec<String> = env::args().collect();
|
2022-02-15 12:33:46 +00:00
|
|
|
|
|
|
|
let exit_code = async move {
|
|
|
|
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);
|
|
|
|
|
|
|
|
let flags = match flags::flags_from_vec(args) {
|
|
|
|
Ok(flags) => flags,
|
|
|
|
Err(err @ clap::Error { .. })
|
|
|
|
if err.kind == clap::ErrorKind::DisplayHelp
|
|
|
|
|| err.kind == clap::ErrorKind::DisplayVersion =>
|
|
|
|
{
|
|
|
|
err.print().unwrap();
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
Err(err) => unwrap_or_exit(Err(AnyError::from(err))),
|
|
|
|
};
|
|
|
|
if !flags.v8_flags.is_empty() {
|
|
|
|
init_v8_flags(&*flags.v8_flags);
|
2021-01-09 12:08:03 +00:00
|
|
|
}
|
2021-05-11 04:54:10 +00:00
|
|
|
|
2022-02-15 12:33:46 +00:00
|
|
|
logger::init(flags.log_level);
|
|
|
|
|
|
|
|
let exit_code = get_subcommand(flags).await;
|
|
|
|
|
|
|
|
exit_code
|
|
|
|
};
|
2020-05-11 21:33:36 +00:00
|
|
|
|
2022-02-15 12:33:46 +00:00
|
|
|
let exit_code = unwrap_or_exit(run_basic(exit_code));
|
2021-11-27 23:45:38 +00:00
|
|
|
|
2021-12-11 14:56:45 +00:00
|
|
|
std::process::exit(exit_code);
|
2018-06-15 23:43:23 +00:00
|
|
|
}
|