2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2019-09-03 22:12:21 -04:00
|
|
|
|
2020-09-14 08:27:44 -04:00
|
|
|
mod ast;
|
2021-02-15 21:50:27 -05:00
|
|
|
mod auth_tokens;
|
2020-05-11 17:33:36 -04:00
|
|
|
mod checksum;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod colors;
|
2021-05-10 12:16:39 -04:00
|
|
|
mod config_file;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod deno_dir;
|
|
|
|
mod diagnostics;
|
2020-05-23 09:22:08 -04:00
|
|
|
mod diff;
|
2020-05-11 17:33:36 -04:00
|
|
|
mod disk_cache;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod errors;
|
2020-05-11 17:33:36 -04:00
|
|
|
mod file_fetcher;
|
2020-09-11 12:19:49 -04:00
|
|
|
mod file_watcher;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod flags;
|
2020-06-26 08:09:02 -04:00
|
|
|
mod flags_allow_net;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod fmt_errors;
|
2020-11-16 14:48:50 -05:00
|
|
|
mod fs_util;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod http_cache;
|
2020-05-11 17:33:36 -04:00
|
|
|
mod http_util;
|
|
|
|
mod import_map;
|
2020-09-07 09:59:47 -04:00
|
|
|
mod info;
|
2020-05-11 17:33:36 -04:00
|
|
|
mod lockfile;
|
2021-05-11 00:54:10 -04:00
|
|
|
mod logger;
|
2020-12-07 05:46:39 -05:00
|
|
|
mod lsp;
|
2020-09-15 10:33:14 -04:00
|
|
|
mod media_type;
|
2020-11-02 14:41:20 -05:00
|
|
|
mod module_graph;
|
2020-10-13 07:35:35 -04:00
|
|
|
mod module_loader;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod ops;
|
2020-10-13 07:35:35 -04:00
|
|
|
mod program_state;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod source_maps;
|
2020-09-24 18:31:17 -04:00
|
|
|
mod specifier_handler;
|
2020-11-30 14:35:12 -05:00
|
|
|
mod standalone;
|
2020-08-03 17:39:48 -04:00
|
|
|
mod text_encoding;
|
2020-05-11 17:33:36 -04:00
|
|
|
mod tokio_util;
|
2020-11-19 13:19:34 -05:00
|
|
|
mod tools;
|
2020-11-02 14:41:20 -05:00
|
|
|
mod tsc;
|
2021-04-13 10:24:45 -04:00
|
|
|
mod unix_util;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod version;
|
2020-05-11 17:33:36 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
use crate::file_fetcher::File;
|
2021-05-10 02:06:13 -04:00
|
|
|
use crate::file_watcher::ResolutionResult;
|
2020-11-30 14:35:12 -05:00
|
|
|
use crate::flags::DenoSubcommand;
|
|
|
|
use crate::flags::Flags;
|
2020-12-11 12:49:26 -05:00
|
|
|
use crate::fmt_errors::PrettyJsError;
|
2020-09-15 10:33:14 -04:00
|
|
|
use crate::media_type::MediaType;
|
2021-05-10 02:06:13 -04:00
|
|
|
use crate::module_graph::GraphBuilder;
|
|
|
|
use crate::module_graph::Module;
|
2020-12-11 12:49:26 -05:00
|
|
|
use crate::module_loader::CliModuleLoader;
|
2020-10-13 07:35:35 -04:00
|
|
|
use crate::program_state::ProgramState;
|
2020-12-11 12:49:26 -05:00
|
|
|
use crate::source_maps::apply_source_map;
|
2020-10-19 23:10:42 -04:00
|
|
|
use crate::specifier_handler::FetchHandler;
|
2020-11-30 14:35:12 -05:00
|
|
|
use crate::tools::installer::infer_name_from_url;
|
2020-10-23 07:05:41 -04:00
|
|
|
use deno_core::error::generic_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::future::FutureExt;
|
|
|
|
use deno_core::futures::Future;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_url_or_path;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
2020-05-11 17:33:36 -04:00
|
|
|
use deno_core::v8_set_flags;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
|
|
|
|
use deno_runtime::permissions::Permissions;
|
|
|
|
use deno_runtime::web_worker::WebWorker;
|
|
|
|
use deno_runtime::web_worker::WebWorkerOptions;
|
|
|
|
use deno_runtime::worker::MainWorker;
|
|
|
|
use deno_runtime::worker::WorkerOptions;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
|
|
|
use log::info;
|
2021-05-10 02:06:13 -04:00
|
|
|
use std::collections::HashSet;
|
2020-05-11 17:33:36 -04:00
|
|
|
use std::env;
|
2020-06-11 10:58:09 -04:00
|
|
|
use std::io::Read;
|
2020-05-11 17:33:36 -04:00
|
|
|
use std::io::Write;
|
2020-08-07 18:51:59 -04:00
|
|
|
use std::iter::once;
|
2020-05-11 17:33:36 -04:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::pin::Pin;
|
2020-10-11 22:25:27 -04:00
|
|
|
use std::rc::Rc;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::sync::Arc;
|
2020-12-29 23:17:17 -05:00
|
|
|
use std::sync::Mutex;
|
2021-05-10 02:06:13 -04:00
|
|
|
use tools::test_runner;
|
2020-05-11 17:33:36 -04:00
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
fn create_web_worker_callback(
|
|
|
|
program_state: Arc<ProgramState>,
|
|
|
|
) -> Arc<CreateWebWorkerCb> {
|
|
|
|
Arc::new(move |args| {
|
|
|
|
let global_state_ = program_state.clone();
|
|
|
|
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)
|
|
|
|
});
|
|
|
|
|
|
|
|
let attach_inspector = program_state.maybe_inspector_server.is_some()
|
2020-12-21 08:04:25 -05:00
|
|
|
|| program_state.coverage_dir.is_some();
|
2020-12-11 12:49:26 -05:00
|
|
|
let maybe_inspector_server = program_state.maybe_inspector_server.clone();
|
|
|
|
|
2021-01-06 15:31:16 -05:00
|
|
|
let module_loader = CliModuleLoader::new_for_worker(
|
|
|
|
program_state.clone(),
|
|
|
|
args.parent_permissions.clone(),
|
|
|
|
);
|
2020-12-11 12:49:26 -05:00
|
|
|
let create_web_worker_cb =
|
|
|
|
create_web_worker_callback(program_state.clone());
|
|
|
|
|
|
|
|
let options = WebWorkerOptions {
|
|
|
|
args: program_state.flags.argv.clone(),
|
|
|
|
apply_source_maps: true,
|
|
|
|
debug_flag: program_state
|
|
|
|
.flags
|
|
|
|
.log_level
|
|
|
|
.map_or(false, |l| l == log::Level::Debug),
|
|
|
|
unstable: program_state.flags.unstable,
|
2021-01-04 18:15:52 -05:00
|
|
|
ca_data: program_state.ca_data.clone(),
|
2021-01-07 21:08:51 -05:00
|
|
|
user_agent: version::get_user_agent(),
|
2020-12-11 12:49:26 -05:00
|
|
|
seed: program_state.flags.seed,
|
|
|
|
module_loader,
|
|
|
|
create_web_worker_cb,
|
|
|
|
js_error_create_fn: Some(js_error_create_fn),
|
|
|
|
use_deno_namespace: args.use_deno_namespace,
|
|
|
|
attach_inspector,
|
|
|
|
maybe_inspector_server,
|
2020-12-11 18:36:18 -05:00
|
|
|
runtime_version: version::deno(),
|
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
|
|
|
no_color: !colors::use_color(),
|
2020-12-13 13:45:53 -05:00
|
|
|
get_error_class_fn: Some(&crate::errors::get_error_class_name),
|
2021-04-07 09:22:14 -04:00
|
|
|
blob_url_store: program_state.blob_url_store.clone(),
|
2021-05-22 12:08:24 -04:00
|
|
|
broadcast_channel: program_state.broadcast_channel.clone(),
|
2020-12-11 12:49:26 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut worker = WebWorker::from_options(
|
|
|
|
args.name,
|
|
|
|
args.permissions,
|
|
|
|
args.main_module,
|
|
|
|
args.worker_id,
|
|
|
|
&options,
|
|
|
|
);
|
|
|
|
|
|
|
|
// This block registers additional ops and state that
|
|
|
|
// are only available in the CLI
|
|
|
|
{
|
|
|
|
let js_runtime = &mut worker.js_runtime;
|
|
|
|
js_runtime
|
|
|
|
.op_state()
|
|
|
|
.borrow_mut()
|
|
|
|
.put::<Arc<ProgramState>>(program_state.clone());
|
|
|
|
// Applies source maps - works in conjuction with `js_error_create_fn`
|
|
|
|
// above
|
|
|
|
ops::errors::init(js_runtime);
|
|
|
|
if args.use_deno_namespace {
|
|
|
|
ops::runtime_compiler::init(js_runtime);
|
|
|
|
}
|
2021-04-25 16:00:05 -04:00
|
|
|
js_runtime.sync_ops_cache();
|
2020-12-11 12:49:26 -05:00
|
|
|
}
|
|
|
|
worker.bootstrap(&options);
|
|
|
|
|
|
|
|
worker
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_main_worker(
|
|
|
|
program_state: &Arc<ProgramState>,
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: Permissions,
|
2021-04-25 17:38:59 -04:00
|
|
|
enable_testing: bool,
|
2020-12-11 12:49:26 -05:00
|
|
|
) -> MainWorker {
|
|
|
|
let module_loader = CliModuleLoader::new(program_state.clone());
|
|
|
|
|
|
|
|
let global_state_ = program_state.clone();
|
|
|
|
|
|
|
|
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)
|
|
|
|
});
|
|
|
|
|
|
|
|
let attach_inspector = program_state.maybe_inspector_server.is_some()
|
|
|
|
|| program_state.flags.repl
|
2020-12-21 08:04:25 -05:00
|
|
|
|| program_state.coverage_dir.is_some();
|
2020-12-11 12:49:26 -05:00
|
|
|
let maybe_inspector_server = program_state.maybe_inspector_server.clone();
|
|
|
|
let should_break_on_first_statement =
|
|
|
|
program_state.flags.inspect_brk.is_some();
|
|
|
|
|
|
|
|
let create_web_worker_cb = create_web_worker_callback(program_state.clone());
|
|
|
|
|
|
|
|
let options = WorkerOptions {
|
|
|
|
apply_source_maps: true,
|
|
|
|
args: program_state.flags.argv.clone(),
|
|
|
|
debug_flag: program_state
|
|
|
|
.flags
|
|
|
|
.log_level
|
|
|
|
.map_or(false, |l| l == log::Level::Debug),
|
|
|
|
unstable: program_state.flags.unstable,
|
2021-01-04 18:15:52 -05:00
|
|
|
ca_data: program_state.ca_data.clone(),
|
2021-01-07 21:08:51 -05:00
|
|
|
user_agent: version::get_user_agent(),
|
2020-12-11 12:49:26 -05:00
|
|
|
seed: program_state.flags.seed,
|
|
|
|
js_error_create_fn: Some(js_error_create_fn),
|
|
|
|
create_web_worker_cb,
|
|
|
|
attach_inspector,
|
|
|
|
maybe_inspector_server,
|
|
|
|
should_break_on_first_statement,
|
|
|
|
module_loader,
|
2020-12-11 18:36:18 -05:00
|
|
|
runtime_version: version::deno(),
|
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
|
|
|
no_color: !colors::use_color(),
|
2020-12-13 13:45:53 -05:00
|
|
|
get_error_class_fn: Some(&crate::errors::get_error_class_name),
|
2021-01-07 13:06:08 -05:00
|
|
|
location: program_state.flags.location.clone(),
|
2021-05-27 01:23:12 -04:00
|
|
|
origin_storage_dir: program_state.flags.location.clone().map(|loc| {
|
2021-05-10 06:02:47 -04:00
|
|
|
program_state
|
|
|
|
.dir
|
|
|
|
.root
|
|
|
|
.clone()
|
2021-05-27 01:23:12 -04:00
|
|
|
// TODO(@crowlKats): change to origin_data for 2.0
|
2021-05-10 06:02:47 -04:00
|
|
|
.join("location_data")
|
|
|
|
.join(checksum::gen(&[loc.to_string().as_bytes()]))
|
|
|
|
}),
|
2021-04-07 09:22:14 -04:00
|
|
|
blob_url_store: program_state.blob_url_store.clone(),
|
2021-05-22 12:08:24 -04:00
|
|
|
broadcast_channel: program_state.broadcast_channel.clone(),
|
2020-12-11 12:49:26 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut worker = MainWorker::from_options(main_module, permissions, &options);
|
|
|
|
|
|
|
|
// This block registers additional ops and state that
|
|
|
|
// are only available in the CLI
|
|
|
|
{
|
|
|
|
let js_runtime = &mut worker.js_runtime;
|
|
|
|
js_runtime
|
|
|
|
.op_state()
|
|
|
|
.borrow_mut()
|
|
|
|
.put::<Arc<ProgramState>>(program_state.clone());
|
|
|
|
// Applies source maps - works in conjuction with `js_error_create_fn`
|
|
|
|
// above
|
|
|
|
ops::errors::init(js_runtime);
|
|
|
|
ops::runtime_compiler::init(js_runtime);
|
2021-04-25 17:38:59 -04:00
|
|
|
|
|
|
|
if enable_testing {
|
2021-04-28 14:17:04 -04:00
|
|
|
ops::testing::init(js_runtime);
|
2021-04-25 17:38:59 -04:00
|
|
|
}
|
|
|
|
|
2021-04-25 16:00:05 -04:00
|
|
|
js_runtime.sync_ops_cache();
|
2020-12-11 12:49:26 -05:00
|
|
|
}
|
|
|
|
worker.bootstrap(&options);
|
|
|
|
|
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
2021-02-25 10:24:05 -05:00
|
|
|
pub fn write_to_stdout_ignore_sigpipe(
|
|
|
|
bytes: &[u8],
|
|
|
|
) -> Result<(), std::io::Error> {
|
2020-05-11 17:33:36 -04: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 10:24:05 -05:00
|
|
|
pub fn write_json_to_stdout<T>(value: &T) -> Result<(), AnyError>
|
2020-07-08 10:50:12 -04:00
|
|
|
where
|
|
|
|
T: ?Sized + serde::ser::Serialize,
|
|
|
|
{
|
2021-06-03 14:49:02 -04: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 10:50:12 -04:00
|
|
|
}
|
|
|
|
|
2020-08-18 12:30:13 -04:00
|
|
|
fn print_cache_info(
|
2020-10-13 07:35:35 -04:00
|
|
|
state: &Arc<ProgramState>,
|
2020-08-18 12:30:13 -04:00
|
|
|
json: bool,
|
2021-05-27 01:23:12 -04:00
|
|
|
location: Option<deno_core::url::Url>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-07-08 10:50:12 -04:00
|
|
|
let deno_dir = &state.dir.root;
|
2020-11-05 19:38:21 -05:00
|
|
|
let modules_cache = &state.file_fetcher.get_http_cache_location();
|
2020-07-08 10:50:12 -04:00
|
|
|
let typescript_cache = &state.dir.gen_cache.location;
|
2021-04-08 21:27:27 -04:00
|
|
|
let registry_cache =
|
|
|
|
&state.dir.root.join(lsp::language_server::REGISTRIES_PATH);
|
2021-05-27 01:23:12 -04: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 10:50:12 -04:00
|
|
|
if json {
|
2021-05-27 01:23:12 -04:00
|
|
|
let mut output = json!({
|
|
|
|
"denoDir": deno_dir,
|
|
|
|
"modulesCache": modules_cache,
|
|
|
|
"typescriptCache": typescript_cache,
|
|
|
|
"registryCache": registry_cache,
|
|
|
|
"originStorage": origin_dir,
|
2020-07-08 10:50:12 -04:00
|
|
|
});
|
2021-05-27 01:23:12 -04:00
|
|
|
|
|
|
|
if location.is_some() {
|
|
|
|
output["localStorage"] =
|
|
|
|
serde_json::to_value(origin_dir.join("local_storage"))?;
|
|
|
|
}
|
|
|
|
|
2020-07-08 10:50:12 -04: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-08 21:27:27 -04:00
|
|
|
colors::bold("Emitted modules cache:"),
|
2020-07-08 10:50:12 -04:00
|
|
|
typescript_cache
|
|
|
|
);
|
2021-04-08 21:27:27 -04:00
|
|
|
println!(
|
|
|
|
"{} {:?}",
|
|
|
|
colors::bold("Language server registries cache:"),
|
|
|
|
registry_cache,
|
|
|
|
);
|
2021-05-27 01:23:12 -04: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 10:50:12 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 10:24:05 -05:00
|
|
|
pub fn get_types(unstable: bool) -> String {
|
2021-06-10 10:18:13 -04:00
|
|
|
let mut types = vec![
|
2021-01-05 20:38:23 -05:00
|
|
|
crate::tsc::DENO_NS_LIB,
|
2021-03-12 15:23:59 -05: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 10:17:18 -05:00
|
|
|
crate::tsc::DENO_URL_LIB,
|
2021-01-05 20:38:23 -05:00
|
|
|
crate::tsc::DENO_WEB_LIB,
|
|
|
|
crate::tsc::DENO_FETCH_LIB,
|
2021-03-01 05:31:13 -05:00
|
|
|
crate::tsc::DENO_WEBGPU_LIB,
|
2021-01-06 10:57:28 -05:00
|
|
|
crate::tsc::DENO_WEBSOCKET_LIB,
|
2021-05-10 06:02:47 -04:00
|
|
|
crate::tsc::DENO_WEBSTORAGE_LIB,
|
2021-03-26 03:43:58 -04:00
|
|
|
crate::tsc::DENO_CRYPTO_LIB,
|
2021-05-22 12:08:24 -04:00
|
|
|
crate::tsc::DENO_BROADCAST_CHANNEL_LIB,
|
2021-01-05 20:38:23 -05:00
|
|
|
crate::tsc::SHARED_GLOBALS_LIB,
|
|
|
|
crate::tsc::WINDOW_LIB,
|
2021-06-10 10:18:13 -04:00
|
|
|
];
|
2020-08-04 10:08:41 -04:00
|
|
|
|
2020-05-11 17:33:36 -04:00
|
|
|
if unstable {
|
2021-06-10 10:18:13 -04:00
|
|
|
types.push(crate::tsc::UNSTABLE_NS_LIB);
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
2020-08-04 10:08:41 -04:00
|
|
|
|
2021-06-10 10:18:13 -04:00
|
|
|
types.join("\n")
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
async fn compile_command(
|
|
|
|
flags: Flags,
|
|
|
|
source_file: String,
|
2020-12-01 09:11:02 -05:00
|
|
|
output: Option<PathBuf>,
|
2021-01-04 18:15:52 -05:00
|
|
|
args: Vec<String>,
|
2021-01-18 21:40:22 -05:00
|
|
|
target: Option<String>,
|
2020-11-30 14:35:12 -05:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let debug = flags.log_level == Some(log::Level::Debug);
|
|
|
|
|
2021-01-07 21:08:51 -05:00
|
|
|
let run_flags =
|
|
|
|
tools::standalone::compile_to_runtime_flags(flags.clone(), args)?;
|
2021-01-04 18:15:52 -05:00
|
|
|
|
2021-02-17 13:47:18 -05:00
|
|
|
let module_specifier = resolve_url_or_path(&source_file)?;
|
2021-02-17 08:32:57 -05:00
|
|
|
let program_state = ProgramState::build(flags.clone()).await?;
|
2021-01-18 21:40:22 -05:00
|
|
|
let deno_dir = &program_state.dir;
|
2020-11-30 14:35:12 -05:00
|
|
|
|
2020-12-01 09:11:02 -05:00
|
|
|
let output = output.or_else(|| {
|
2021-02-17 13:47:18 -05:00
|
|
|
infer_name_from_url(&module_specifier).map(PathBuf::from)
|
2020-12-01 09:11:02 -05:00
|
|
|
}).ok_or_else(|| generic_error(
|
|
|
|
"An executable name was not provided. One could not be inferred from the URL. Aborting.",
|
|
|
|
))?;
|
2020-11-30 14:35:12 -05:00
|
|
|
|
|
|
|
let module_graph = create_module_graph_and_maybe_check(
|
|
|
|
module_specifier.clone(),
|
|
|
|
program_state.clone(),
|
|
|
|
debug,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
info!(
|
|
|
|
"{} {}",
|
|
|
|
colors::green("Bundle"),
|
|
|
|
module_specifier.to_string()
|
|
|
|
);
|
2021-05-10 12:16:39 -04:00
|
|
|
let bundle_str =
|
|
|
|
bundle_module_graph(module_graph, program_state.clone(), flags, debug)?;
|
2020-11-30 14:35:12 -05:00
|
|
|
|
|
|
|
info!(
|
|
|
|
"{} {}",
|
|
|
|
colors::green("Compile"),
|
|
|
|
module_specifier.to_string()
|
|
|
|
);
|
2021-01-18 21:40:22 -05:00
|
|
|
|
2021-04-26 13:28:38 -04:00
|
|
|
// Select base binary based on target
|
2021-01-18 21:40:22 -05:00
|
|
|
let original_binary =
|
2021-04-26 13:28:38 -04:00
|
|
|
tools::standalone::get_base_binary(deno_dir, target.clone()).await?;
|
2021-01-18 21:40:22 -05:00
|
|
|
|
|
|
|
let final_bin = tools::standalone::create_standalone_binary(
|
|
|
|
original_binary,
|
2021-01-07 21:08:51 -05:00
|
|
|
bundle_str,
|
|
|
|
run_flags,
|
2021-01-18 21:40:22 -05:00
|
|
|
)?;
|
2020-11-30 14:35:12 -05:00
|
|
|
|
2020-12-01 09:11:02 -05:00
|
|
|
info!("{} {}", colors::green("Emit"), output.display());
|
2020-11-30 14:35:12 -05:00
|
|
|
|
2021-03-07 08:20:01 -05:00
|
|
|
tools::standalone::write_standalone_binary(output.clone(), target, final_bin)
|
|
|
|
.await?;
|
2021-01-18 21:40:22 -05:00
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:33:36 -04:00
|
|
|
async fn info_command(
|
|
|
|
flags: Flags,
|
2020-10-11 22:25:27 -04:00
|
|
|
maybe_specifier: Option<String>,
|
2020-07-08 10:50:12 -04:00
|
|
|
json: bool,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-05-27 01:23:12 -04:00
|
|
|
let location = flags.location.clone();
|
2021-02-17 08:32:57 -05:00
|
|
|
let program_state = ProgramState::build(flags).await?;
|
2020-10-11 22:25:27 -04:00
|
|
|
if let Some(specifier) = maybe_specifier {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path(&specifier)?;
|
2020-12-29 23:17:17 -05:00
|
|
|
let handler = Arc::new(Mutex::new(specifier_handler::FetchHandler::new(
|
2020-10-13 07:35:35 -04:00
|
|
|
&program_state,
|
2020-10-22 20:50:15 -04:00
|
|
|
// info accesses dynamically imported modules just for their information
|
|
|
|
// so we allow access to all of them.
|
2020-10-11 22:25:27 -04:00
|
|
|
Permissions::allow_all(),
|
2021-05-17 03:44:38 -04:00
|
|
|
Permissions::allow_all(),
|
2020-10-11 22:25:27 -04:00
|
|
|
)?));
|
2020-11-02 14:41:20 -05:00
|
|
|
let mut builder = module_graph::GraphBuilder::new(
|
2020-10-11 22:25:27 -04:00
|
|
|
handler,
|
2020-10-13 07:35:35 -04:00
|
|
|
program_state.maybe_import_map.clone(),
|
2020-10-23 17:01:54 -04:00
|
|
|
program_state.lockfile.clone(),
|
2020-10-11 22:25:27 -04:00
|
|
|
);
|
2020-10-22 20:50:15 -04:00
|
|
|
builder.add(&specifier, false).await?;
|
2020-10-23 17:01:54 -04:00
|
|
|
let graph = builder.get_graph();
|
2020-10-11 22:25:27 -04:00
|
|
|
let info = graph.info()?;
|
2020-09-07 09:59:47 -04:00
|
|
|
|
|
|
|
if json {
|
2021-03-01 06:49:58 -05:00
|
|
|
write_json_to_stdout(&json!(info))
|
2020-09-07 09:59:47 -04:00
|
|
|
} else {
|
2021-03-01 06:49:58 -05:00
|
|
|
write_to_stdout_ignore_sigpipe(info.to_string().as_bytes())
|
|
|
|
.map_err(|err| err.into())
|
2020-09-07 09:59:47 -04:00
|
|
|
}
|
2020-10-11 22:25:27 -04:00
|
|
|
} else {
|
|
|
|
// If it was just "deno info" print location of caches and exit
|
2021-05-27 01:23:12 -04:00
|
|
|
print_cache_info(&program_state, json, location)
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn install_command(
|
|
|
|
flags: Flags,
|
|
|
|
module_url: String,
|
|
|
|
args: Vec<String>,
|
|
|
|
name: Option<String>,
|
|
|
|
root: Option<PathBuf>,
|
|
|
|
force: bool,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-10-19 15:19:20 -04:00
|
|
|
let mut preload_flags = flags.clone();
|
|
|
|
preload_flags.inspect = None;
|
|
|
|
preload_flags.inspect_brk = None;
|
2020-12-13 13:45:53 -05:00
|
|
|
let permissions = Permissions::from_options(&preload_flags.clone().into());
|
2021-02-17 08:32:57 -05:00
|
|
|
let program_state = ProgramState::build(preload_flags).await?;
|
2021-02-17 13:47:18 -05:00
|
|
|
let main_module = resolve_url_or_path(&module_url)?;
|
2020-11-02 17:37:55 -05:00
|
|
|
let mut worker =
|
2021-04-25 17:38:59 -04:00
|
|
|
create_main_worker(&program_state, main_module.clone(), permissions, false);
|
2020-09-20 08:05:11 -04:00
|
|
|
// First, fetch and compile the module; this step ensures that the module exists.
|
2020-09-16 11:56:06 -04:00
|
|
|
worker.preload_module(&main_module).await?;
|
2020-11-19 13:19:34 -05:00
|
|
|
tools::installer::install(flags, &module_url, args, name, root, force)
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
|
|
|
|
2021-06-22 21:48:01 -04:00
|
|
|
async fn lsp_command() -> Result<(), AnyError> {
|
|
|
|
lsp::start().await
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2020-06-12 10:42:12 -04:00
|
|
|
async fn lint_command(
|
2021-04-27 06:44:36 -04:00
|
|
|
_flags: Flags,
|
2020-10-21 07:12:01 -04:00
|
|
|
files: Vec<PathBuf>,
|
2020-06-12 10:42:12 -04:00
|
|
|
list_rules: bool,
|
2020-10-21 07:12:01 -04:00
|
|
|
ignore: Vec<PathBuf>,
|
2020-08-13 11:30:46 -04:00
|
|
|
json: bool,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-06-12 10:42:12 -04:00
|
|
|
if list_rules {
|
2020-11-19 13:19:34 -05:00
|
|
|
tools::lint::print_rules_list(json);
|
2020-06-12 10:42:12 -04:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:19:34 -05:00
|
|
|
tools::lint::lint_files(files, ignore, json).await
|
2020-06-08 08:06:20 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
async fn cache_command(
|
|
|
|
flags: Flags,
|
|
|
|
files: Vec<String>,
|
|
|
|
) -> Result<(), AnyError> {
|
2020-11-01 21:51:56 -05:00
|
|
|
let lib = if flags.unstable {
|
2020-11-02 14:41:20 -05:00
|
|
|
module_graph::TypeLib::UnstableDenoWindow
|
2020-11-01 21:51:56 -05:00
|
|
|
} else {
|
2020-11-02 14:41:20 -05:00
|
|
|
module_graph::TypeLib::DenoWindow
|
2020-11-01 21:51:56 -05:00
|
|
|
};
|
2021-02-17 08:32:57 -05:00
|
|
|
let program_state = ProgramState::build(flags).await?;
|
2020-05-11 17:33:36 -04:00
|
|
|
|
|
|
|
for file in files {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path(&file)?;
|
2020-10-29 09:19:55 -04:00
|
|
|
program_state
|
|
|
|
.prepare_module_load(
|
|
|
|
specifier,
|
2020-11-01 21:51:56 -05:00
|
|
|
lib.clone(),
|
2020-10-29 09:19:55 -04:00
|
|
|
Permissions::allow_all(),
|
2021-05-17 03:44:38 -04:00
|
|
|
Permissions::allow_all(),
|
2020-10-29 09:19:55 -04:00
|
|
|
false,
|
|
|
|
program_state.maybe_import_map.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn eval_command(
|
|
|
|
flags: Flags,
|
|
|
|
code: String,
|
2021-02-21 11:58:32 -05:00
|
|
|
ext: String,
|
2020-05-21 10:35:36 -04:00
|
|
|
print: bool,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-05-11 17:33:36 -04:00
|
|
|
// Force TypeScript compile.
|
2021-02-17 13:47:18 -05:00
|
|
|
let main_module = resolve_url_or_path("./$deno$eval.ts").unwrap();
|
2020-12-13 13:45:53 -05:00
|
|
|
let permissions = Permissions::from_options(&flags.clone().into());
|
2021-02-17 08:32:57 -05:00
|
|
|
let program_state = ProgramState::build(flags).await?;
|
2020-11-02 17:37:55 -05:00
|
|
|
let mut worker =
|
2021-04-25 17:38:59 -04:00
|
|
|
create_main_worker(&program_state, main_module.clone(), permissions, false);
|
2020-05-11 17:33:36 -04:00
|
|
|
// Create a dummy source file.
|
2020-05-21 10:35:36 -04:00
|
|
|
let source_code = if print {
|
2020-08-04 10:08:41 -04:00
|
|
|
format!("console.log({})", code)
|
2020-05-21 10:35:36 -04:00
|
|
|
} else {
|
2020-08-04 10:08:41 -04:00
|
|
|
code
|
2020-05-21 10:35:36 -04:00
|
|
|
}
|
|
|
|
.into_bytes();
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let file = File {
|
2021-02-17 13:47:18 -05:00
|
|
|
local: main_module.clone().to_file_path().unwrap(),
|
2020-11-05 19:38:21 -05:00
|
|
|
maybe_types: None,
|
2021-02-21 11:58:32 -05:00
|
|
|
media_type: if ext.as_str() == "ts" {
|
2020-05-11 17:33:36 -04:00
|
|
|
MediaType::TypeScript
|
2021-02-21 11:58:32 -05:00
|
|
|
} else if ext.as_str() == "tsx" {
|
2021-03-25 14:17:37 -04:00
|
|
|
MediaType::Tsx
|
2021-02-21 11:58:32 -05:00
|
|
|
} else if ext.as_str() == "js" {
|
2020-05-11 17:33:36 -04:00
|
|
|
MediaType::JavaScript
|
2021-02-21 11:58:32 -05:00
|
|
|
} else {
|
2021-03-25 14:17:37 -04:00
|
|
|
MediaType::Jsx
|
2020-05-11 17:33:36 -04:00
|
|
|
},
|
2020-11-05 19:38:21 -05:00
|
|
|
source: String::from_utf8(source_code)?,
|
2021-02-17 13:47:18 -05:00
|
|
|
specifier: main_module.clone(),
|
2020-05-11 17:33:36 -04:00
|
|
|
};
|
2020-11-05 19:38:21 -05:00
|
|
|
|
2020-05-11 17:33:36 -04:00
|
|
|
// Save our fake file into file fetcher cache
|
2020-08-04 10:08:41 -04:00
|
|
|
// to allow module access by TS compiler.
|
2020-11-05 19:38:21 -05:00
|
|
|
program_state.file_fetcher.insert_cached(file);
|
2020-05-11 17:33:36 -04:00
|
|
|
debug!("main_module {}", &main_module);
|
|
|
|
worker.execute_module(&main_module).await?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
2021-05-26 15:07:12 -04:00
|
|
|
worker.run_event_loop(false).await?;
|
2020-05-11 17:33:36 -04:00
|
|
|
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
async fn create_module_graph_and_maybe_check(
|
|
|
|
module_specifier: ModuleSpecifier,
|
|
|
|
program_state: Arc<ProgramState>,
|
|
|
|
debug: bool,
|
|
|
|
) -> Result<module_graph::Graph, AnyError> {
|
2020-12-29 23:17:17 -05:00
|
|
|
let handler = Arc::new(Mutex::new(FetchHandler::new(
|
2020-11-30 14:35:12 -05:00
|
|
|
&program_state,
|
|
|
|
// when bundling, dynamic imports are only access for their type safety,
|
|
|
|
// therefore we will allow the graph to access any module.
|
|
|
|
Permissions::allow_all(),
|
2021-05-17 03:44:38 -04:00
|
|
|
Permissions::allow_all(),
|
2020-11-30 14:35:12 -05:00
|
|
|
)?));
|
|
|
|
let mut builder = module_graph::GraphBuilder::new(
|
|
|
|
handler,
|
|
|
|
program_state.maybe_import_map.clone(),
|
|
|
|
program_state.lockfile.clone(),
|
|
|
|
);
|
|
|
|
builder.add(&module_specifier, false).await?;
|
|
|
|
let module_graph = builder.get_graph();
|
|
|
|
|
|
|
|
if !program_state.flags.no_check {
|
|
|
|
// TODO(@kitsonk) support bundling for workers
|
|
|
|
let lib = if program_state.flags.unstable {
|
|
|
|
module_graph::TypeLib::UnstableDenoWindow
|
|
|
|
} else {
|
|
|
|
module_graph::TypeLib::DenoWindow
|
|
|
|
};
|
|
|
|
let result_info =
|
|
|
|
module_graph.clone().check(module_graph::CheckOptions {
|
|
|
|
debug,
|
|
|
|
emit: false,
|
|
|
|
lib,
|
2021-05-10 12:16:39 -04:00
|
|
|
maybe_config_file: program_state.maybe_config_file.clone(),
|
2020-11-30 14:35:12 -05:00
|
|
|
reload: program_state.flags.reload,
|
2021-06-19 10:14:43 -04:00
|
|
|
..Default::default()
|
2020-11-30 14:35:12 -05:00
|
|
|
})?;
|
|
|
|
|
|
|
|
debug!("{}", result_info.stats);
|
|
|
|
if let Some(ignored_options) = result_info.maybe_ignored_options {
|
|
|
|
eprintln!("{}", ignored_options);
|
|
|
|
}
|
|
|
|
if !result_info.diagnostics.is_empty() {
|
|
|
|
return Err(generic_error(result_info.diagnostics.to_string()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(module_graph)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bundle_module_graph(
|
|
|
|
module_graph: module_graph::Graph,
|
2021-05-10 12:16:39 -04:00
|
|
|
program_state: Arc<ProgramState>,
|
2020-11-30 14:35:12 -05:00
|
|
|
flags: Flags,
|
|
|
|
debug: bool,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let (bundle, stats, maybe_ignored_options) =
|
|
|
|
module_graph.bundle(module_graph::BundleOptions {
|
|
|
|
debug,
|
2021-05-10 12:16:39 -04:00
|
|
|
maybe_config_file: program_state.maybe_config_file.clone(),
|
2020-11-30 14:35:12 -05:00
|
|
|
})?;
|
|
|
|
match maybe_ignored_options {
|
|
|
|
Some(ignored_options) if flags.no_check => {
|
|
|
|
eprintln!("{}", ignored_options);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
debug!("{}", stats);
|
|
|
|
Ok(bundle)
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:33:36 -04:00
|
|
|
async fn bundle_command(
|
|
|
|
flags: Flags,
|
|
|
|
source_file: String,
|
|
|
|
out_file: Option<PathBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-11-22 15:45:44 -05:00
|
|
|
let debug = flags.log_level == Some(log::Level::Debug);
|
2020-05-18 06:59:29 -04:00
|
|
|
|
2021-05-10 02:06:13 -04:00
|
|
|
let resolver = |_| {
|
2020-11-22 15:45:44 -05:00
|
|
|
let flags = flags.clone();
|
2020-11-28 09:18:13 -05:00
|
|
|
let source_file1 = source_file.clone();
|
|
|
|
let source_file2 = source_file.clone();
|
2020-11-22 15:45:44 -05:00
|
|
|
async move {
|
2021-02-17 13:47:18 -05:00
|
|
|
let module_specifier = resolve_url_or_path(&source_file1)?;
|
2020-11-22 15:45:44 -05:00
|
|
|
|
|
|
|
debug!(">>>>> bundle START");
|
2021-02-17 08:32:57 -05:00
|
|
|
let program_state = ProgramState::build(flags.clone()).await?;
|
2020-11-22 15:45:44 -05:00
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
let module_graph = create_module_graph_and_maybe_check(
|
|
|
|
module_specifier,
|
|
|
|
program_state.clone(),
|
|
|
|
debug,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-05-20 10:25:40 -04:00
|
|
|
|
2020-11-22 15:45:44 -05:00
|
|
|
let mut paths_to_watch: Vec<PathBuf> = module_graph
|
|
|
|
.get_modules()
|
|
|
|
.iter()
|
2021-02-17 13:47:18 -05:00
|
|
|
.filter_map(|specifier| specifier.to_file_path().ok())
|
2020-11-22 15:45:44 -05:00
|
|
|
.collect();
|
2020-05-29 10:32:15 -04:00
|
|
|
|
2020-11-22 15:45:44 -05:00
|
|
|
if let Some(import_map) = program_state.flags.import_map_path.as_ref() {
|
|
|
|
paths_to_watch
|
|
|
|
.push(fs_util::resolve_from_cwd(std::path::Path::new(import_map))?);
|
|
|
|
}
|
2020-10-23 07:05:41 -04:00
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
Ok((paths_to_watch, module_graph, program_state))
|
2020-10-19 23:10:42 -04:00
|
|
|
}
|
2020-11-28 09:18:13 -05:00
|
|
|
.map(move |result| match result {
|
2021-05-10 12:16:39 -04:00
|
|
|
Ok((paths_to_watch, module_graph, program_state)) => {
|
|
|
|
ResolutionResult::Restart {
|
|
|
|
paths_to_watch,
|
|
|
|
result: Ok((program_state, module_graph)),
|
|
|
|
}
|
|
|
|
}
|
2021-05-10 02:06:13 -04:00
|
|
|
Err(e) => ResolutionResult::Restart {
|
|
|
|
paths_to_watch: vec![PathBuf::from(source_file2)],
|
|
|
|
result: Err(e),
|
2020-11-28 09:18:13 -05:00
|
|
|
},
|
|
|
|
})
|
2020-11-22 15:45:44 -05:00
|
|
|
};
|
2020-10-19 23:10:42 -04:00
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
let operation = |(program_state, module_graph): (
|
|
|
|
Arc<ProgramState>,
|
|
|
|
module_graph::Graph,
|
|
|
|
)| {
|
2020-11-22 15:45:44 -05:00
|
|
|
let flags = flags.clone();
|
|
|
|
let out_file = out_file.clone();
|
|
|
|
async move {
|
2021-05-10 02:06:13 -04:00
|
|
|
info!("{} {}", colors::green("Bundle"), module_graph.info()?.root);
|
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
let output =
|
|
|
|
bundle_module_graph(module_graph, program_state, flags, debug)?;
|
2020-11-22 15:45:44 -05:00
|
|
|
|
|
|
|
debug!(">>>>> bundle END");
|
|
|
|
|
|
|
|
if let Some(out_file) = out_file.as_ref() {
|
|
|
|
let output_bytes = output.as_bytes();
|
|
|
|
let output_len = output_bytes.len();
|
|
|
|
fs_util::write_file(out_file, output_bytes, 0o644)?;
|
|
|
|
info!(
|
|
|
|
"{} {:?} ({})",
|
|
|
|
colors::green("Emit"),
|
|
|
|
out_file,
|
|
|
|
colors::gray(&info::human_size(output_len as f64))
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
println!("{}", output);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if flags.watch {
|
2021-05-10 02:06:13 -04:00
|
|
|
file_watcher::watch_func(resolver, operation, "Bundle").await?;
|
2020-05-29 10:32:15 -04:00
|
|
|
} else {
|
2021-05-10 02:06:13 -04:00
|
|
|
let module_graph =
|
|
|
|
if let ResolutionResult::Restart { result, .. } = resolver(None).await {
|
|
|
|
result?
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
};
|
2020-11-22 15:45:44 -05:00
|
|
|
operation(module_graph).await?;
|
2020-05-29 10:32:15 -04:00
|
|
|
}
|
2020-11-22 15:45:44 -05:00
|
|
|
|
2020-05-29 10:32:15 -04:00
|
|
|
Ok(())
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn doc_command(
|
|
|
|
flags: Flags,
|
|
|
|
source_file: Option<String>,
|
|
|
|
json: bool,
|
|
|
|
maybe_filter: Option<String>,
|
2020-07-12 08:16:33 -04:00
|
|
|
private: bool,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-02-25 10:24:05 -05:00
|
|
|
tools::doc::print_docs(flags, source_file, json, maybe_filter, private).await
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
|
|
|
|
2020-11-22 15:45:44 -05:00
|
|
|
async fn format_command(
|
|
|
|
flags: Flags,
|
|
|
|
args: Vec<PathBuf>,
|
|
|
|
ignore: Vec<PathBuf>,
|
|
|
|
check: bool,
|
2021-01-19 12:39:35 -05:00
|
|
|
ext: String,
|
2020-11-22 15:45:44 -05:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
2021-01-19 12:39:35 -05:00
|
|
|
return tools::fmt::format_stdin(check, ext);
|
2020-11-22 15:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tools::fmt::format(args, ignore, check, flags.watch).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
async fn run_repl(flags: Flags) -> Result<(), AnyError> {
|
2021-02-17 13:47:18 -05:00
|
|
|
let main_module = resolve_url_or_path("./$deno$repl.ts").unwrap();
|
2020-12-13 13:45:53 -05:00
|
|
|
let permissions = Permissions::from_options(&flags.clone().into());
|
2021-02-17 08:32:57 -05:00
|
|
|
let program_state = ProgramState::build(flags).await?;
|
2020-11-02 17:37:55 -05:00
|
|
|
let mut worker =
|
2021-04-25 17:38:59 -04:00
|
|
|
create_main_worker(&program_state, main_module.clone(), permissions, false);
|
2021-05-26 15:07:12 -04:00
|
|
|
worker.run_event_loop(false).await?;
|
2020-10-01 19:14:55 -04:00
|
|
|
|
2020-11-19 13:19:34 -05:00
|
|
|
tools::repl::run(&program_state, worker).await
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
|
2021-02-17 08:32:57 -05:00
|
|
|
let program_state = ProgramState::build(flags.clone()).await?;
|
2020-12-13 13:45:53 -05:00
|
|
|
let permissions = Permissions::from_options(&flags.clone().into());
|
2021-02-17 13:47:18 -05:00
|
|
|
let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap();
|
2020-12-11 12:49:26 -05:00
|
|
|
let mut worker = create_main_worker(
|
|
|
|
&program_state.clone(),
|
|
|
|
main_module.clone(),
|
|
|
|
permissions,
|
2021-04-25 17:38:59 -04:00
|
|
|
false,
|
2020-12-11 12:49:26 -05:00
|
|
|
);
|
2020-09-11 12:19:49 -04:00
|
|
|
|
|
|
|
let mut source = Vec::new();
|
|
|
|
std::io::stdin().read_to_end(&mut source)?;
|
|
|
|
// Create a dummy source file.
|
2020-11-05 19:38:21 -05:00
|
|
|
let source_file = File {
|
2021-02-17 13:47:18 -05:00
|
|
|
local: main_module.clone().to_file_path().unwrap(),
|
2020-11-05 19:38:21 -05:00
|
|
|
maybe_types: None,
|
2020-09-11 12:19:49 -04:00
|
|
|
media_type: MediaType::TypeScript,
|
2020-11-05 19:38:21 -05:00
|
|
|
source: String::from_utf8(source)?,
|
|
|
|
specifier: main_module.clone(),
|
2020-06-11 10:58:09 -04:00
|
|
|
};
|
2020-09-11 12:19:49 -04:00
|
|
|
// Save our fake file into file fetcher cache
|
|
|
|
// to allow module access by TS compiler
|
2020-11-05 19:38:21 -05:00
|
|
|
program_state.file_fetcher.insert_cached(source_file);
|
2020-09-11 12:19:49 -04:00
|
|
|
|
|
|
|
debug!("main_module {}", main_module);
|
|
|
|
worker.execute_module(&main_module).await?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
2021-05-26 15:07:12 -04:00
|
|
|
worker.run_event_loop(false).await?;
|
2020-09-11 12:19:49 -04:00
|
|
|
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
|
2021-05-10 02:06:13 -04:00
|
|
|
let resolver = |_| {
|
2020-11-28 09:18:13 -05:00
|
|
|
let script1 = script.clone();
|
|
|
|
let script2 = script.clone();
|
2020-11-22 15:45:44 -05:00
|
|
|
let flags = flags.clone();
|
|
|
|
async move {
|
2021-02-17 13:47:18 -05:00
|
|
|
let main_module = resolve_url_or_path(&script1)?;
|
2021-02-17 08:32:57 -05:00
|
|
|
let program_state = ProgramState::build(flags).await?;
|
2020-12-29 23:17:17 -05:00
|
|
|
let handler = Arc::new(Mutex::new(FetchHandler::new(
|
2020-11-22 15:45:44 -05:00
|
|
|
&program_state,
|
|
|
|
Permissions::allow_all(),
|
2021-05-17 03:44:38 -04:00
|
|
|
Permissions::allow_all(),
|
2020-11-22 15:45:44 -05:00
|
|
|
)?));
|
|
|
|
let mut builder = module_graph::GraphBuilder::new(
|
|
|
|
handler,
|
|
|
|
program_state.maybe_import_map.clone(),
|
|
|
|
program_state.lockfile.clone(),
|
|
|
|
);
|
|
|
|
builder.add(&main_module, false).await?;
|
|
|
|
let module_graph = builder.get_graph();
|
|
|
|
|
|
|
|
// Find all local files in graph
|
|
|
|
let mut paths_to_watch: Vec<PathBuf> = module_graph
|
|
|
|
.get_modules()
|
|
|
|
.iter()
|
2021-02-17 13:47:18 -05:00
|
|
|
.filter_map(|specifier| specifier.to_file_path().ok())
|
2020-11-22 15:45:44 -05:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
if let Some(import_map) = program_state.flags.import_map_path.as_ref() {
|
|
|
|
paths_to_watch
|
|
|
|
.push(fs_util::resolve_from_cwd(std::path::Path::new(import_map))?);
|
|
|
|
}
|
2020-09-11 12:19:49 -04:00
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
Ok((paths_to_watch, main_module, program_state))
|
2020-11-22 15:45:44 -05:00
|
|
|
}
|
2020-11-28 09:18:13 -05:00
|
|
|
.map(move |result| match result {
|
2021-05-10 12:16:39 -04:00
|
|
|
Ok((paths_to_watch, module_info, program_state)) => {
|
|
|
|
ResolutionResult::Restart {
|
|
|
|
paths_to_watch,
|
|
|
|
result: Ok((program_state, module_info)),
|
|
|
|
}
|
|
|
|
}
|
2021-05-10 02:06:13 -04:00
|
|
|
Err(e) => ResolutionResult::Restart {
|
|
|
|
paths_to_watch: vec![PathBuf::from(script2)],
|
|
|
|
result: Err(e),
|
2020-11-28 09:18:13 -05:00
|
|
|
},
|
|
|
|
})
|
2020-11-22 15:45:44 -05:00
|
|
|
};
|
2020-09-23 07:56:16 -04:00
|
|
|
|
2021-05-10 12:16:39 -04:00
|
|
|
let operation =
|
|
|
|
|(program_state, main_module): (Arc<ProgramState>, ModuleSpecifier)| {
|
|
|
|
let flags = flags.clone();
|
|
|
|
let permissions = Permissions::from_options(&flags.into());
|
|
|
|
async move {
|
|
|
|
let main_module = main_module.clone();
|
|
|
|
let mut worker = create_main_worker(
|
|
|
|
&program_state,
|
|
|
|
main_module.clone(),
|
|
|
|
permissions,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
debug!("main_module {}", main_module);
|
|
|
|
worker.execute_module(&main_module).await?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
2021-05-26 15:07:12 -04:00
|
|
|
worker.run_event_loop(false).await?;
|
2021-05-10 12:16:39 -04:00
|
|
|
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
2020-11-22 15:45:44 -05:00
|
|
|
|
2021-05-10 02:06:13 -04:00
|
|
|
file_watcher::watch_func(resolver, operation, "Process").await
|
2020-09-11 12:19:49 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
async fn run_command(flags: Flags, script: String) -> Result<(), AnyError> {
|
2020-09-11 12:19:49 -04:00
|
|
|
// Read script content from stdin
|
|
|
|
if script == "-" {
|
|
|
|
return run_from_stdin(flags).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags.watch {
|
|
|
|
return run_with_watch(flags, script).await;
|
|
|
|
}
|
2020-06-11 10:58:09 -04:00
|
|
|
|
2021-02-17 13:47:18 -05:00
|
|
|
let main_module = resolve_url_or_path(&script)?;
|
2021-02-17 08:32:57 -05:00
|
|
|
let program_state = ProgramState::build(flags.clone()).await?;
|
2020-12-13 13:45:53 -05:00
|
|
|
let permissions = Permissions::from_options(&flags.clone().into());
|
2020-11-02 17:37:55 -05:00
|
|
|
let mut worker =
|
2021-04-25 17:38:59 -04:00
|
|
|
create_main_worker(&program_state, main_module.clone(), permissions, false);
|
2020-12-28 10:51:26 -05:00
|
|
|
|
|
|
|
let mut maybe_coverage_collector =
|
|
|
|
if let Some(ref coverage_dir) = program_state.coverage_dir {
|
2021-05-26 11:47:33 -04:00
|
|
|
let session = worker.create_inspector_session().await;
|
2020-12-28 10:51:26 -05:00
|
|
|
|
|
|
|
let coverage_dir = PathBuf::from(coverage_dir);
|
|
|
|
let mut coverage_collector =
|
|
|
|
tools::coverage::CoverageCollector::new(coverage_dir, session);
|
2021-05-31 10:53:49 -04:00
|
|
|
worker
|
|
|
|
.with_event_loop(coverage_collector.start_collecting().boxed_local())
|
|
|
|
.await?;
|
2020-12-28 10:51:26 -05:00
|
|
|
Some(coverage_collector)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-05-11 17:33:36 -04:00
|
|
|
debug!("main_module {}", main_module);
|
|
|
|
worker.execute_module(&main_module).await?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
2021-05-26 15:07:12 -04:00
|
|
|
worker
|
|
|
|
.run_event_loop(maybe_coverage_collector.is_none())
|
|
|
|
.await?;
|
2020-05-11 17:33:36 -04:00
|
|
|
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
2020-12-28 10:51:26 -05:00
|
|
|
|
|
|
|
if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
|
2021-05-31 10:53:49 -04:00
|
|
|
worker
|
|
|
|
.with_event_loop(coverage_collector.stop_collecting().boxed_local())
|
|
|
|
.await?;
|
2020-12-28 10:51:26 -05:00
|
|
|
}
|
2020-05-11 17:33:36 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:27:51 -05:00
|
|
|
async fn coverage_command(
|
|
|
|
flags: Flags,
|
|
|
|
files: Vec<PathBuf>,
|
|
|
|
ignore: Vec<PathBuf>,
|
|
|
|
include: Vec<String>,
|
|
|
|
exclude: Vec<String>,
|
|
|
|
lcov: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if files.is_empty() {
|
|
|
|
println!("No matching coverage profiles found");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
tools::coverage::cover_files(
|
|
|
|
flags.clone(),
|
|
|
|
files,
|
|
|
|
ignore,
|
|
|
|
include,
|
|
|
|
exclude,
|
|
|
|
lcov,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-05-11 17:33:36 -04:00
|
|
|
async fn test_command(
|
|
|
|
flags: Flags,
|
|
|
|
include: Option<Vec<String>>,
|
2020-11-22 08:06:51 -05:00
|
|
|
no_run: bool,
|
2021-05-10 19:54:39 -04:00
|
|
|
doc: bool,
|
2020-05-11 17:33:36 -04:00
|
|
|
fail_fast: bool,
|
|
|
|
quiet: bool,
|
|
|
|
allow_none: bool,
|
|
|
|
filter: Option<String>,
|
2021-04-28 14:17:04 -04:00
|
|
|
concurrent_jobs: usize,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-12-21 08:04:25 -05:00
|
|
|
if let Some(ref coverage_dir) = flags.coverage_dir {
|
2021-05-21 09:57:00 -04:00
|
|
|
std::fs::create_dir_all(&coverage_dir)?;
|
2021-05-19 09:33:22 -04:00
|
|
|
env::set_var(
|
|
|
|
"DENO_UNSTABLE_COVERAGE_DIR",
|
|
|
|
PathBuf::from(coverage_dir).canonicalize()?,
|
|
|
|
);
|
2020-12-21 08:04:25 -05:00
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2021-05-10 19:54:39 -04:00
|
|
|
// TODO(caspervonb) move this chunk into tools::test_runner.
|
|
|
|
|
2021-05-10 02:06:13 -04:00
|
|
|
let program_state = ProgramState::build(flags.clone()).await?;
|
|
|
|
|
|
|
|
let include = include.unwrap_or_else(|| vec![".".to_string()]);
|
|
|
|
let cwd = std::env::current_dir().expect("No current directory");
|
|
|
|
|
|
|
|
let permissions = Permissions::from_options(&flags.clone().into());
|
|
|
|
let lib = if flags.unstable {
|
|
|
|
module_graph::TypeLib::UnstableDenoWindow
|
|
|
|
} else {
|
|
|
|
module_graph::TypeLib::DenoWindow
|
|
|
|
};
|
|
|
|
|
|
|
|
if flags.watch {
|
|
|
|
let handler = Arc::new(Mutex::new(FetchHandler::new(
|
|
|
|
&program_state,
|
|
|
|
Permissions::allow_all(),
|
2021-05-17 03:44:38 -04:00
|
|
|
Permissions::allow_all(),
|
2021-05-10 02:06:13 -04:00
|
|
|
)?));
|
|
|
|
|
|
|
|
let paths_to_watch: Vec<_> = include.iter().map(PathBuf::from).collect();
|
|
|
|
|
2021-05-10 19:54:39 -04:00
|
|
|
// TODO(caspervonb) clean this up.
|
2021-05-10 02:06:13 -04:00
|
|
|
let resolver = |changed: Option<Vec<PathBuf>>| {
|
2021-05-10 19:54:39 -04:00
|
|
|
let doc_modules_result = test_runner::collect_test_module_specifiers(
|
|
|
|
include.clone(),
|
|
|
|
&cwd,
|
|
|
|
fs_util::is_supported_ext,
|
|
|
|
);
|
|
|
|
|
|
|
|
let test_modules_result = test_runner::collect_test_module_specifiers(
|
|
|
|
include.clone(),
|
|
|
|
&cwd,
|
|
|
|
tools::test_runner::is_supported,
|
|
|
|
);
|
|
|
|
|
2021-05-10 02:06:13 -04:00
|
|
|
let paths_to_watch = paths_to_watch.clone();
|
|
|
|
let paths_to_watch_clone = paths_to_watch.clone();
|
|
|
|
|
|
|
|
let handler = handler.clone();
|
|
|
|
let program_state = program_state.clone();
|
|
|
|
let files_changed = changed.is_some();
|
|
|
|
async move {
|
2021-05-10 19:54:39 -04:00
|
|
|
let doc_modules = if doc { doc_modules_result? } else { Vec::new() };
|
|
|
|
|
2021-05-10 02:06:13 -04:00
|
|
|
let test_modules = test_modules_result?;
|
|
|
|
|
|
|
|
let mut paths_to_watch = paths_to_watch_clone;
|
|
|
|
let mut modules_to_reload = if files_changed {
|
|
|
|
Vec::new()
|
|
|
|
} else {
|
|
|
|
test_modules
|
|
|
|
.iter()
|
|
|
|
.filter_map(|url| deno_core::resolve_url(url.as_str()).ok())
|
|
|
|
.collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut builder = GraphBuilder::new(
|
|
|
|
handler,
|
|
|
|
program_state.maybe_import_map.clone(),
|
|
|
|
program_state.lockfile.clone(),
|
|
|
|
);
|
|
|
|
for specifier in test_modules.iter() {
|
|
|
|
builder.add(specifier, false).await?;
|
|
|
|
}
|
|
|
|
let graph = builder.get_graph();
|
|
|
|
|
2021-05-10 19:54:39 -04:00
|
|
|
for specifier in doc_modules {
|
|
|
|
if let Ok(path) = specifier.to_file_path() {
|
|
|
|
paths_to_watch.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 02:06:13 -04:00
|
|
|
for specifier in test_modules {
|
|
|
|
fn get_dependencies<'a>(
|
|
|
|
graph: &'a module_graph::Graph,
|
|
|
|
module: &'a Module,
|
|
|
|
// This needs to be accessible to skip getting dependencies if they're already there,
|
|
|
|
// otherwise this will cause a stack overflow with circular dependencies
|
|
|
|
output: &mut HashSet<&'a ModuleSpecifier>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
for dep in module.dependencies.values() {
|
|
|
|
if let Some(specifier) = &dep.maybe_code {
|
|
|
|
if !output.contains(specifier) {
|
|
|
|
output.insert(specifier);
|
|
|
|
|
|
|
|
get_dependencies(
|
|
|
|
&graph,
|
|
|
|
graph.get_specifier(specifier)?,
|
|
|
|
output,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(specifier) = &dep.maybe_type {
|
|
|
|
if !output.contains(specifier) {
|
|
|
|
output.insert(specifier);
|
|
|
|
|
|
|
|
get_dependencies(
|
|
|
|
&graph,
|
|
|
|
graph.get_specifier(specifier)?,
|
|
|
|
output,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test module and all it's dependencies
|
|
|
|
let mut modules = HashSet::new();
|
|
|
|
modules.insert(&specifier);
|
|
|
|
get_dependencies(
|
|
|
|
&graph,
|
|
|
|
graph.get_specifier(&specifier)?,
|
|
|
|
&mut modules,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
paths_to_watch.extend(
|
|
|
|
modules
|
|
|
|
.iter()
|
|
|
|
.filter_map(|specifier| specifier.to_file_path().ok()),
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(changed) = &changed {
|
|
|
|
for path in changed.iter().filter_map(|path| {
|
|
|
|
deno_core::resolve_url_or_path(&path.to_string_lossy()).ok()
|
|
|
|
}) {
|
|
|
|
if modules.contains(&&path) {
|
|
|
|
modules_to_reload.push(specifier);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((paths_to_watch, modules_to_reload))
|
|
|
|
}
|
|
|
|
.map(move |result| {
|
|
|
|
if files_changed
|
|
|
|
&& matches!(result, Ok((_, ref modules)) if modules.is_empty())
|
|
|
|
{
|
|
|
|
ResolutionResult::Ignore
|
|
|
|
} else {
|
|
|
|
match result {
|
|
|
|
Ok((paths_to_watch, modules_to_reload)) => {
|
|
|
|
ResolutionResult::Restart {
|
|
|
|
paths_to_watch,
|
|
|
|
result: Ok(modules_to_reload),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => ResolutionResult::Restart {
|
|
|
|
paths_to_watch,
|
|
|
|
result: Err(e),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
file_watcher::watch_func(
|
|
|
|
resolver,
|
|
|
|
|modules_to_reload| {
|
|
|
|
test_runner::run_tests(
|
|
|
|
program_state.clone(),
|
|
|
|
permissions.clone(),
|
|
|
|
lib.clone(),
|
2021-05-10 19:54:39 -04:00
|
|
|
modules_to_reload.clone(),
|
2021-05-10 02:06:13 -04:00
|
|
|
modules_to_reload,
|
|
|
|
no_run,
|
|
|
|
fail_fast,
|
|
|
|
quiet,
|
|
|
|
true,
|
|
|
|
filter.clone(),
|
|
|
|
concurrent_jobs,
|
|
|
|
)
|
|
|
|
.map(|res| res.map(|_| ()))
|
|
|
|
},
|
|
|
|
"Test",
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
} else {
|
2021-05-10 19:54:39 -04:00
|
|
|
let doc_modules = if doc {
|
|
|
|
test_runner::collect_test_module_specifiers(
|
|
|
|
include.clone(),
|
|
|
|
&cwd,
|
|
|
|
fs_util::is_supported_ext,
|
|
|
|
)?
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
let test_modules = test_runner::collect_test_module_specifiers(
|
|
|
|
include.clone(),
|
|
|
|
&cwd,
|
|
|
|
tools::test_runner::is_supported,
|
|
|
|
)?;
|
2021-05-10 02:06:13 -04:00
|
|
|
|
|
|
|
let failed = test_runner::run_tests(
|
|
|
|
program_state.clone(),
|
|
|
|
permissions,
|
|
|
|
lib,
|
2021-05-10 19:54:39 -04:00
|
|
|
doc_modules,
|
2021-05-10 02:06:13 -04:00
|
|
|
test_modules,
|
|
|
|
no_run,
|
|
|
|
fail_fast,
|
|
|
|
quiet,
|
|
|
|
allow_none,
|
|
|
|
filter,
|
|
|
|
concurrent_jobs,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
if failed {
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 09:01:30 -04:00
|
|
|
|
|
|
|
Ok(())
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05: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 18:15:52 -05:00
|
|
|
// Keep in sync with `standalone.rs`.
|
2020-11-26 09:17:45 -05: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 18:51:59 -04:00
|
|
|
}
|
2020-11-26 09:17:45 -05: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 17:33:36 -04:00
|
|
|
}
|
2020-11-26 09:17:45 -05:00
|
|
|
}
|
2020-05-11 17:33:36 -04:00
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
fn get_subcommand(
|
|
|
|
flags: Flags,
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> {
|
|
|
|
match flags.clone().subcommand {
|
2020-05-11 17:33:36 -04:00
|
|
|
DenoSubcommand::Bundle {
|
|
|
|
source_file,
|
|
|
|
out_file,
|
|
|
|
} => bundle_command(flags, source_file, out_file).boxed_local(),
|
|
|
|
DenoSubcommand::Doc {
|
|
|
|
source_file,
|
|
|
|
json,
|
|
|
|
filter,
|
2020-07-12 08:16:33 -04:00
|
|
|
private,
|
|
|
|
} => doc_command(flags, source_file, json, filter, private).boxed_local(),
|
2021-02-21 11:58:32 -05:00
|
|
|
DenoSubcommand::Eval { print, code, ext } => {
|
|
|
|
eval_command(flags, code, ext, print).boxed_local()
|
|
|
|
}
|
2020-10-23 10:56:25 -04:00
|
|
|
DenoSubcommand::Cache { files } => {
|
|
|
|
cache_command(flags, files).boxed_local()
|
|
|
|
}
|
2020-11-30 14:35:12 -05:00
|
|
|
DenoSubcommand::Compile {
|
|
|
|
source_file,
|
2020-12-01 09:11:02 -05:00
|
|
|
output,
|
2021-01-04 18:15:52 -05:00
|
|
|
args,
|
2021-01-18 21:40:22 -05:00
|
|
|
target,
|
2021-04-26 13:28:38 -04:00
|
|
|
} => {
|
|
|
|
compile_command(flags, source_file, output, args, target).boxed_local()
|
|
|
|
}
|
2021-02-24 09:27:51 -05:00
|
|
|
DenoSubcommand::Coverage {
|
|
|
|
files,
|
|
|
|
ignore,
|
|
|
|
include,
|
|
|
|
exclude,
|
|
|
|
lcov,
|
|
|
|
} => coverage_command(flags, files, ignore, include, exclude, lcov)
|
|
|
|
.boxed_local(),
|
2020-07-30 12:09:08 -04:00
|
|
|
DenoSubcommand::Fmt {
|
|
|
|
check,
|
|
|
|
files,
|
|
|
|
ignore,
|
2021-01-19 12:39:35 -05:00
|
|
|
ext,
|
|
|
|
} => format_command(flags, files, ignore, check, ext).boxed_local(),
|
2020-07-08 10:50:12 -04:00
|
|
|
DenoSubcommand::Info { file, json } => {
|
|
|
|
info_command(flags, file, json).boxed_local()
|
|
|
|
}
|
2020-05-11 17:33:36 -04:00
|
|
|
DenoSubcommand::Install {
|
|
|
|
module_url,
|
|
|
|
args,
|
|
|
|
name,
|
|
|
|
root,
|
|
|
|
force,
|
|
|
|
} => {
|
|
|
|
install_command(flags, module_url, args, name, root, force).boxed_local()
|
|
|
|
}
|
2021-06-22 21:48:01 -04:00
|
|
|
DenoSubcommand::Lsp => lsp_command().boxed_local(),
|
2020-08-12 09:47:44 -04:00
|
|
|
DenoSubcommand::Lint {
|
|
|
|
files,
|
|
|
|
rules,
|
|
|
|
ignore,
|
2020-08-13 11:30:46 -04:00
|
|
|
json,
|
|
|
|
} => lint_command(flags, files, rules, ignore, json).boxed_local(),
|
2020-05-11 17:33:36 -04:00
|
|
|
DenoSubcommand::Repl => run_repl(flags).boxed_local(),
|
|
|
|
DenoSubcommand::Run { script } => run_command(flags, script).boxed_local(),
|
|
|
|
DenoSubcommand::Test {
|
2020-11-22 08:06:51 -05:00
|
|
|
no_run,
|
2021-05-10 19:54:39 -04:00
|
|
|
doc,
|
2020-05-11 17:33:36 -04:00
|
|
|
fail_fast,
|
|
|
|
quiet,
|
|
|
|
include,
|
|
|
|
allow_none,
|
|
|
|
filter,
|
2021-04-28 14:17:04 -04:00
|
|
|
concurrent_jobs,
|
|
|
|
} => test_command(
|
|
|
|
flags,
|
|
|
|
include,
|
|
|
|
no_run,
|
2021-05-10 19:54:39 -04:00
|
|
|
doc,
|
2021-04-28 14:17:04 -04:00
|
|
|
fail_fast,
|
|
|
|
quiet,
|
|
|
|
allow_none,
|
|
|
|
filter,
|
|
|
|
concurrent_jobs,
|
|
|
|
)
|
|
|
|
.boxed_local(),
|
2020-05-11 17:33:36 -04:00
|
|
|
DenoSubcommand::Completions { buf } => {
|
|
|
|
if let Err(e) = write_to_stdout_ignore_sigpipe(&buf) {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2020-11-26 09:17:45 -05:00
|
|
|
std::process::exit(0);
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
|
|
|
DenoSubcommand::Types => {
|
|
|
|
let types = get_types(flags.unstable);
|
|
|
|
if let Err(e) = write_to_stdout_ignore_sigpipe(types.as_bytes()) {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2020-11-26 09:17:45 -05:00
|
|
|
std::process::exit(0);
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
|
|
|
DenoSubcommand::Upgrade {
|
|
|
|
force,
|
|
|
|
dry_run,
|
2020-11-29 14:00:35 -05:00
|
|
|
canary,
|
2020-05-11 17:33:36 -04:00
|
|
|
version,
|
2020-07-06 18:21:26 -04:00
|
|
|
output,
|
2020-07-05 23:58:23 -04:00
|
|
|
ca_file,
|
2020-11-29 14:00:35 -05:00
|
|
|
} => tools::upgrade::upgrade_command(
|
|
|
|
dry_run, force, canary, version, output, ca_file,
|
|
|
|
)
|
|
|
|
.boxed_local(),
|
2020-11-26 09:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) => {
|
2021-03-18 20:02:29 -04:00
|
|
|
eprintln!("{}: {:?}", colors::red_bold("error"), error);
|
2021-01-07 13:06:08 -05:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
pub fn main() {
|
|
|
|
#[cfg(windows)]
|
|
|
|
colors::enable_ansi(); // For Windows 10
|
2021-04-13 10:24:45 -04:00
|
|
|
unix_util::raise_fd_limit();
|
2020-11-26 09:17:45 -05:00
|
|
|
|
|
|
|
let args: Vec<String> = env::args().collect();
|
2021-01-07 21:08:51 -05:00
|
|
|
let standalone_res = match standalone::extract_standalone(args.clone()) {
|
|
|
|
Ok(Some((metadata, bundle))) => {
|
|
|
|
tokio_util::run_basic(standalone::run(bundle, metadata))
|
|
|
|
}
|
|
|
|
Ok(None) => Ok(()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
};
|
|
|
|
if let Err(err) = standalone_res {
|
2020-11-30 14:35:12 -05:00
|
|
|
eprintln!("{}: {}", colors::red_bold("error"), err.to_string());
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2020-11-26 09:17:45 -05:00
|
|
|
|
2021-01-09 07:08:03 -05:00
|
|
|
let flags = match flags::flags_from_vec(args) {
|
|
|
|
Ok(flags) => flags,
|
|
|
|
Err(err @ clap::Error { .. })
|
|
|
|
if err.kind == clap::ErrorKind::HelpDisplayed
|
|
|
|
|| err.kind == clap::ErrorKind::VersionDisplayed =>
|
|
|
|
{
|
|
|
|
err.write_to(&mut std::io::stdout()).unwrap();
|
2021-01-18 19:56:24 -05:00
|
|
|
std::io::stdout().write_all(b"\n").unwrap();
|
2021-01-09 07:08:03 -05:00
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
Err(err) => unwrap_or_exit(Err(AnyError::from(err))),
|
|
|
|
};
|
2020-12-06 12:19:21 -05:00
|
|
|
if !flags.v8_flags.is_empty() {
|
|
|
|
init_v8_flags(&*flags.v8_flags);
|
2020-11-26 09:17:45 -05:00
|
|
|
}
|
2021-05-11 00:54:10 -04:00
|
|
|
|
|
|
|
logger::init(flags.log_level);
|
2020-05-11 17:33:36 -04:00
|
|
|
|
2021-01-07 13:06:08 -05:00
|
|
|
unwrap_or_exit(tokio_util::run_basic(get_subcommand(flags)));
|
2018-06-15 19:43:23 -04:00
|
|
|
}
|