2020-05-11 17:33:36 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-09-03 22:12:21 -04:00
|
|
|
|
2020-09-18 12:31:30 -04:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
2020-05-11 17:33:36 -04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
2020-09-14 08:27:44 -04:00
|
|
|
mod ast;
|
2020-05-11 17:33:36 -04:00
|
|
|
mod checksum;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod colors;
|
|
|
|
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 inspector;
|
|
|
|
mod js;
|
|
|
|
mod lockfile;
|
2020-09-15 10:33:14 -04:00
|
|
|
mod media_type;
|
2020-05-11 17:33:36 -04:00
|
|
|
mod metrics;
|
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;
|
|
|
|
mod permissions;
|
2020-10-13 07:35:35 -04:00
|
|
|
mod program_state;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod resolve_addr;
|
|
|
|
mod signal;
|
|
|
|
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;
|
2020-08-31 14:12:24 -04:00
|
|
|
mod tsc_config;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod version;
|
2020-11-26 09:17:45 -05:00
|
|
|
mod web_worker;
|
2020-10-12 07:25:25 -04:00
|
|
|
mod worker;
|
2020-05-11 17:33:36 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
use crate::file_fetcher::File;
|
|
|
|
use crate::file_fetcher::FileFetcher;
|
2020-11-28 09:18:13 -05:00
|
|
|
use crate::file_watcher::ModuleResolutionResult;
|
2020-11-30 14:35:12 -05:00
|
|
|
use crate::flags::DenoSubcommand;
|
|
|
|
use crate::flags::Flags;
|
|
|
|
use crate::import_map::ImportMap;
|
2020-09-15 10:33:14 -04:00
|
|
|
use crate::media_type::MediaType;
|
2020-05-11 17:33:36 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-11-30 14:35:12 -05:00
|
|
|
use crate::program_state::exit_unstable;
|
2020-10-13 07:35:35 -04:00
|
|
|
use crate::program_state::ProgramState;
|
2020-10-19 23:10:42 -04:00
|
|
|
use crate::specifier_handler::FetchHandler;
|
2020-11-30 14:35:12 -05:00
|
|
|
use crate::standalone::create_standalone_binary;
|
|
|
|
use crate::tools::installer::infer_name_from_url;
|
2020-05-11 17:33:36 -04:00
|
|
|
use crate::worker::MainWorker;
|
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;
|
|
|
|
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-08-19 13:13:43 -04:00
|
|
|
use deno_doc as doc;
|
|
|
|
use deno_doc::parser::DocFileLoader;
|
2020-05-11 17:33:36 -04:00
|
|
|
use log::Level;
|
2020-09-17 14:38:57 -04:00
|
|
|
use log::LevelFilter;
|
2020-10-11 22:25:27 -04:00
|
|
|
use std::cell::RefCell;
|
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-05-11 17:33:36 -04:00
|
|
|
|
|
|
|
fn write_to_stdout_ignore_sigpipe(bytes: &[u8]) -> Result<(), std::io::Error> {
|
|
|
|
use std::io::ErrorKind;
|
|
|
|
|
|
|
|
match std::io::stdout().write_all(bytes) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(e) => match e.kind() {
|
|
|
|
ErrorKind::BrokenPipe => Ok(()),
|
|
|
|
_ => Err(e),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
fn write_json_to_stdout<T>(value: &T) -> Result<(), AnyError>
|
2020-07-08 10:50:12 -04:00
|
|
|
where
|
|
|
|
T: ?Sized + serde::ser::Serialize,
|
|
|
|
{
|
|
|
|
let writer = std::io::BufWriter::new(std::io::stdout());
|
2020-09-14 12:48:57 -04:00
|
|
|
serde_json::to_writer_pretty(writer, value).map_err(AnyError::from)
|
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,
|
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;
|
|
|
|
if json {
|
|
|
|
let output = json!({
|
|
|
|
"denoDir": deno_dir,
|
|
|
|
"modulesCache": modules_cache,
|
|
|
|
"typescriptCache": typescript_cache,
|
|
|
|
});
|
|
|
|
write_json_to_stdout(&output)
|
|
|
|
} else {
|
|
|
|
println!("{} {:?}", colors::bold("DENO_DIR location:"), deno_dir);
|
|
|
|
println!(
|
|
|
|
"{} {:?}",
|
|
|
|
colors::bold("Remote modules cache:"),
|
|
|
|
modules_cache
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{} {:?}",
|
|
|
|
colors::bold("TypeScript compiler cache:"),
|
|
|
|
typescript_cache
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:33:36 -04:00
|
|
|
fn get_types(unstable: bool) -> String {
|
2020-08-04 10:08:41 -04:00
|
|
|
let mut types = format!(
|
2020-09-18 09:20:55 -04:00
|
|
|
"{}\n{}\n{}\n{}\n{}",
|
2020-08-04 10:08:41 -04:00
|
|
|
crate::js::DENO_NS_LIB,
|
2020-08-07 10:55:02 -04:00
|
|
|
crate::js::DENO_WEB_LIB,
|
2020-09-18 09:20:55 -04:00
|
|
|
crate::js::DENO_FETCH_LIB,
|
2020-08-04 10:08:41 -04:00
|
|
|
crate::js::SHARED_GLOBALS_LIB,
|
|
|
|
crate::js::WINDOW_LIB,
|
|
|
|
);
|
|
|
|
|
2020-05-11 17:33:36 -04:00
|
|
|
if unstable {
|
2020-08-04 10:08:41 -04:00
|
|
|
types.push_str(&format!("\n{}", crate::js::UNSTABLE_NS_LIB,));
|
2020-05-11 17:33:36 -04:00
|
|
|
}
|
2020-08-04 10:08:41 -04:00
|
|
|
|
|
|
|
types
|
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>,
|
2020-11-30 14:35:12 -05:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if !flags.unstable {
|
|
|
|
exit_unstable("compile");
|
|
|
|
}
|
|
|
|
|
|
|
|
let debug = flags.log_level == Some(log::Level::Debug);
|
|
|
|
|
|
|
|
let module_specifier = ModuleSpecifier::resolve_url_or_path(&source_file)?;
|
|
|
|
let program_state = ProgramState::new(flags.clone())?;
|
|
|
|
|
2020-12-01 09:11:02 -05:00
|
|
|
let output = output.or_else(|| {
|
|
|
|
infer_name_from_url(module_specifier.as_url()).map(PathBuf::from)
|
|
|
|
}).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()
|
|
|
|
);
|
|
|
|
let bundle_str = bundle_module_graph(module_graph, flags, debug)?;
|
|
|
|
|
|
|
|
info!(
|
|
|
|
"{} {}",
|
|
|
|
colors::green("Compile"),
|
|
|
|
module_specifier.to_string()
|
|
|
|
);
|
2020-12-01 09:11:02 -05:00
|
|
|
create_standalone_binary(bundle_str.as_bytes().to_vec(), output.clone())
|
2020-11-30 14:35:12 -05:00
|
|
|
.await?;
|
|
|
|
|
2020-12-01 09:11:02 -05:00
|
|
|
info!("{} {}", colors::green("Emit"), output.display());
|
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> {
|
2020-07-08 10:50:12 -04:00
|
|
|
if json && !flags.unstable {
|
|
|
|
exit_unstable("--json");
|
|
|
|
}
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = ProgramState::new(flags)?;
|
2020-10-11 22:25:27 -04:00
|
|
|
if let Some(specifier) = maybe_specifier {
|
|
|
|
let specifier = ModuleSpecifier::resolve_url_or_path(&specifier)?;
|
|
|
|
let handler = Rc::new(RefCell::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(),
|
|
|
|
)?));
|
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 {
|
2020-10-11 22:25:27 -04:00
|
|
|
write_json_to_stdout(&json!(info))?;
|
2020-09-07 09:59:47 -04:00
|
|
|
} else {
|
2020-11-09 09:38:29 -05:00
|
|
|
write_to_stdout_ignore_sigpipe(info.to_string().as_bytes())?;
|
2020-09-07 09:59:47 -04:00
|
|
|
}
|
2020-10-11 22:25:27 -04:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
// If it was just "deno info" print location of caches and exit
|
2020-10-13 07:35:35 -04:00
|
|
|
print_cache_info(&program_state, json)
|
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-11-02 17:37:55 -05:00
|
|
|
let permissions = Permissions::from_flags(&preload_flags);
|
2020-10-19 15:19:20 -04:00
|
|
|
let program_state = ProgramState::new(preload_flags)?;
|
2020-09-16 11:56:06 -04:00
|
|
|
let main_module = ModuleSpecifier::resolve_url_or_path(&module_url)?;
|
2020-11-02 17:37:55 -05:00
|
|
|
let mut worker =
|
|
|
|
MainWorker::new(&program_state, main_module.clone(), permissions);
|
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
|
|
|
}
|
|
|
|
|
2020-06-12 10:42:12 -04:00
|
|
|
async fn lint_command(
|
2020-10-26 08:36:13 -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-10-26 08:36:13 -04:00
|
|
|
if !flags.unstable {
|
|
|
|
exit_unstable("lint");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = ProgramState::new(flags)?;
|
2020-05-11 17:33:36 -04:00
|
|
|
|
|
|
|
for file in files {
|
|
|
|
let specifier = ModuleSpecifier::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(),
|
|
|
|
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,
|
|
|
|
as_typescript: bool,
|
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.
|
|
|
|
let main_module =
|
2020-09-12 05:33:51 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path("./$deno$eval.ts").unwrap();
|
2020-11-02 17:37:55 -05:00
|
|
|
let permissions = Permissions::from_flags(&flags);
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = ProgramState::new(flags)?;
|
2020-11-02 17:37:55 -05:00
|
|
|
let mut worker =
|
|
|
|
MainWorker::new(&program_state, main_module.clone(), permissions);
|
2020-05-11 17:33:36 -04:00
|
|
|
let main_module_url = main_module.as_url().to_owned();
|
|
|
|
// 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 {
|
|
|
|
local: main_module_url.to_file_path().unwrap(),
|
|
|
|
maybe_types: None,
|
2020-05-11 17:33:36 -04:00
|
|
|
media_type: if as_typescript {
|
|
|
|
MediaType::TypeScript
|
|
|
|
} else {
|
|
|
|
MediaType::JavaScript
|
|
|
|
},
|
2020-11-05 19:38:21 -05:00
|
|
|
source: String::from_utf8(source_code)?,
|
|
|
|
specifier: ModuleSpecifier::from(main_module_url),
|
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'))")?;
|
2020-10-11 07:20:40 -04:00
|
|
|
worker.run_event_loop().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> {
|
|
|
|
let handler = Rc::new(RefCell::new(FetchHandler::new(
|
|
|
|
&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(),
|
|
|
|
)?));
|
|
|
|
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,
|
|
|
|
maybe_config_path: program_state.flags.config_path.clone(),
|
|
|
|
reload: program_state.flags.reload,
|
|
|
|
})?;
|
|
|
|
|
|
|
|
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,
|
|
|
|
flags: Flags,
|
|
|
|
debug: bool,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let (bundle, stats, maybe_ignored_options) =
|
|
|
|
module_graph.bundle(module_graph::BundleOptions {
|
|
|
|
debug,
|
|
|
|
maybe_config_path: flags.config_path,
|
|
|
|
})?;
|
|
|
|
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
|
|
|
|
2020-11-22 15:45:44 -05:00
|
|
|
let module_resolver = || {
|
|
|
|
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 {
|
|
|
|
let module_specifier =
|
2020-11-28 09:18:13 -05:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&source_file1)?;
|
2020-11-22 15:45:44 -05:00
|
|
|
|
|
|
|
debug!(">>>>> bundle START");
|
|
|
|
let program_state = ProgramState::new(flags.clone())?;
|
|
|
|
|
|
|
|
info!(
|
|
|
|
"{} {}",
|
|
|
|
colors::green("Bundle"),
|
|
|
|
module_specifier.to_string()
|
|
|
|
);
|
|
|
|
|
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()
|
|
|
|
.filter_map(|specifier| specifier.as_url().to_file_path().ok())
|
|
|
|
.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
|
|
|
|
2020-11-22 15:45:44 -05:00
|
|
|
Ok((paths_to_watch, module_graph))
|
2020-10-19 23:10:42 -04:00
|
|
|
}
|
2020-11-28 09:18:13 -05:00
|
|
|
.map(move |result| match result {
|
|
|
|
Ok((paths_to_watch, module_graph)) => ModuleResolutionResult::Success {
|
|
|
|
paths_to_watch,
|
|
|
|
module_info: module_graph,
|
|
|
|
},
|
|
|
|
Err(e) => ModuleResolutionResult::Fail {
|
|
|
|
source_path: PathBuf::from(source_file2),
|
|
|
|
error: e,
|
|
|
|
},
|
|
|
|
})
|
2020-11-22 15:45:44 -05:00
|
|
|
.boxed_local()
|
|
|
|
};
|
2020-10-19 23:10:42 -04:00
|
|
|
|
2020-11-22 15:45:44 -05:00
|
|
|
let operation = |module_graph: module_graph::Graph| {
|
|
|
|
let flags = flags.clone();
|
|
|
|
let out_file = out_file.clone();
|
|
|
|
async move {
|
2020-11-30 14:35:12 -05:00
|
|
|
let output = bundle_module_graph(module_graph, 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(())
|
|
|
|
}
|
|
|
|
.boxed_local()
|
|
|
|
};
|
|
|
|
|
|
|
|
if flags.watch {
|
|
|
|
file_watcher::watch_func_with_module_resolution(
|
|
|
|
module_resolver,
|
|
|
|
operation,
|
|
|
|
"Bundle",
|
|
|
|
)
|
|
|
|
.await?;
|
2020-05-29 10:32:15 -04:00
|
|
|
} else {
|
2020-11-28 09:18:13 -05:00
|
|
|
let module_graph = match module_resolver().await {
|
|
|
|
ModuleResolutionResult::Fail { error, .. } => return Err(error),
|
|
|
|
ModuleResolutionResult::Success { module_info, .. } => module_info,
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
2020-10-11 19:05:46 -04:00
|
|
|
struct DocLoader {
|
2020-11-05 19:38:21 -05:00
|
|
|
fetcher: FileFetcher,
|
2020-10-11 19:05:46 -04:00
|
|
|
maybe_import_map: Option<ImportMap>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocFileLoader for DocLoader {
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
) -> Result<String, doc::DocError> {
|
|
|
|
let maybe_resolved =
|
|
|
|
if let Some(import_map) = self.maybe_import_map.as_ref() {
|
|
|
|
import_map
|
|
|
|
.resolve(specifier, referrer)
|
|
|
|
.map_err(|e| doc::DocError::Resolve(e.to_string()))?
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let resolved_specifier = if let Some(resolved) = maybe_resolved {
|
|
|
|
resolved
|
|
|
|
} else {
|
|
|
|
ModuleSpecifier::resolve_import(specifier, referrer)
|
|
|
|
.map_err(|e| doc::DocError::Resolve(e.to_string()))?
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(resolved_specifier.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_source_code(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<String, doc::DocError>>>> {
|
|
|
|
let fetcher = self.fetcher.clone();
|
|
|
|
let specifier = ModuleSpecifier::resolve_url_or_path(specifier)
|
|
|
|
.expect("Expected valid specifier");
|
|
|
|
async move {
|
|
|
|
let source_file = fetcher
|
2020-11-05 19:38:21 -05:00
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-10-11 19:05:46 -04:00
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
|
|
|
doc::DocError::Io(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
e.to_string(),
|
|
|
|
))
|
|
|
|
})?;
|
2020-11-05 19:38:21 -05:00
|
|
|
Ok(source_file.source)
|
2020-10-11 19:05:46 -04:00
|
|
|
}
|
|
|
|
.boxed_local()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = ProgramState::new(flags.clone())?;
|
2020-05-11 17:33:36 -04:00
|
|
|
let source_file = source_file.unwrap_or_else(|| "--builtin".to_string());
|
|
|
|
|
2020-10-11 19:05:46 -04:00
|
|
|
let loader = Box::new(DocLoader {
|
2020-10-13 07:35:35 -04:00
|
|
|
fetcher: program_state.file_fetcher.clone(),
|
|
|
|
maybe_import_map: program_state.maybe_import_map.clone(),
|
2020-10-11 19:05:46 -04:00
|
|
|
});
|
2020-07-12 08:16:33 -04:00
|
|
|
let doc_parser = doc::DocParser::new(loader, private);
|
2020-05-11 17:33:36 -04:00
|
|
|
|
|
|
|
let parse_result = if source_file == "--builtin" {
|
2020-09-15 10:33:14 -04:00
|
|
|
let syntax = ast::get_syntax(&MediaType::Dts);
|
2020-08-19 13:13:43 -04:00
|
|
|
doc_parser.parse_source(
|
|
|
|
"lib.deno.d.ts",
|
|
|
|
syntax,
|
|
|
|
get_types(flags.unstable).as_str(),
|
|
|
|
)
|
2020-05-11 17:33:36 -04:00
|
|
|
} else {
|
2020-08-23 09:48:35 -04:00
|
|
|
let path = PathBuf::from(&source_file);
|
2020-09-14 08:27:44 -04:00
|
|
|
let media_type = MediaType::from(&path);
|
|
|
|
let syntax = ast::get_syntax(&media_type);
|
2020-05-11 17:33:36 -04:00
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path(&source_file).unwrap();
|
|
|
|
doc_parser
|
2020-08-19 13:13:43 -04:00
|
|
|
.parse_with_reexports(&module_specifier.to_string(), syntax)
|
2020-05-11 17:33:36 -04:00
|
|
|
.await
|
|
|
|
};
|
|
|
|
|
2020-08-10 11:41:19 -04:00
|
|
|
let mut doc_nodes = match parse_result {
|
2020-05-11 17:33:36 -04:00
|
|
|
Ok(nodes) => nodes,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if json {
|
2020-07-08 10:50:12 -04:00
|
|
|
write_json_to_stdout(&doc_nodes)
|
2020-05-11 17:33:36 -04:00
|
|
|
} else {
|
2020-08-10 11:41:19 -04:00
|
|
|
doc_nodes.retain(|doc_node| doc_node.kind != doc::DocNodeKind::Import);
|
2020-05-11 17:33:36 -04:00
|
|
|
let details = if let Some(filter) = maybe_filter {
|
2020-06-09 08:12:47 -04:00
|
|
|
let nodes =
|
|
|
|
doc::find_nodes_by_name_recursively(doc_nodes, filter.clone());
|
|
|
|
if nodes.is_empty() {
|
2020-05-11 17:33:36 -04:00
|
|
|
eprintln!("Node {} was not found!", filter);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2020-08-19 13:13:43 -04:00
|
|
|
format!(
|
|
|
|
"{}",
|
|
|
|
doc::DocPrinter::new(&nodes, colors::use_color(), private)
|
|
|
|
)
|
2020-05-11 17:33:36 -04:00
|
|
|
} else {
|
2020-08-19 13:13:43 -04:00
|
|
|
format!(
|
|
|
|
"{}",
|
|
|
|
doc::DocPrinter::new(&doc_nodes, colors::use_color(), private)
|
|
|
|
)
|
2020-05-11 17:33:36 -04:00
|
|
|
};
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
write_to_stdout_ignore_sigpipe(details.as_bytes()).map_err(AnyError::from)
|
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,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
|
|
|
return tools::fmt::format_stdin(check);
|
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
2020-05-11 17:33:36 -04:00
|
|
|
let main_module =
|
2020-09-12 05:33:51 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path("./$deno$repl.ts").unwrap();
|
2020-11-02 17:37:55 -05:00
|
|
|
let permissions = Permissions::from_flags(&flags);
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = ProgramState::new(flags)?;
|
2020-11-02 17:37:55 -05:00
|
|
|
let mut worker =
|
|
|
|
MainWorker::new(&program_state, main_module.clone(), permissions);
|
2020-10-11 07:20:40 -04:00
|
|
|
worker.run_event_loop().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> {
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = ProgramState::new(flags.clone())?;
|
2020-11-02 17:37:55 -05:00
|
|
|
let permissions = Permissions::from_flags(&flags);
|
2020-09-11 12:19:49 -04:00
|
|
|
let main_module =
|
2020-09-12 05:33:51 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path("./$deno$stdin.ts").unwrap();
|
2020-11-02 17:37:55 -05:00
|
|
|
let mut worker =
|
|
|
|
MainWorker::new(&program_state.clone(), main_module.clone(), permissions);
|
2020-09-11 12:19:49 -04:00
|
|
|
|
|
|
|
let mut source = Vec::new();
|
|
|
|
std::io::stdin().read_to_end(&mut source)?;
|
|
|
|
let main_module_url = main_module.as_url().to_owned();
|
|
|
|
// Create a dummy source file.
|
2020-11-05 19:38:21 -05:00
|
|
|
let source_file = File {
|
|
|
|
local: main_module_url.to_file_path().unwrap(),
|
|
|
|
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'))")?;
|
2020-10-11 07:20:40 -04:00
|
|
|
worker.run_event_loop().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> {
|
2020-11-22 15:45:44 -05:00
|
|
|
let module_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 {
|
2020-11-28 09:18:13 -05:00
|
|
|
let main_module = ModuleSpecifier::resolve_url_or_path(&script1)?;
|
2020-11-22 15:45:44 -05:00
|
|
|
let program_state = ProgramState::new(flags)?;
|
|
|
|
let handler = Rc::new(RefCell::new(FetchHandler::new(
|
|
|
|
&program_state,
|
|
|
|
Permissions::allow_all(),
|
|
|
|
)?));
|
|
|
|
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()
|
|
|
|
.filter_map(|specifier| specifier.as_url().to_file_path().ok())
|
|
|
|
.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
|
|
|
|
2020-11-22 15:45:44 -05:00
|
|
|
Ok((paths_to_watch, main_module))
|
|
|
|
}
|
2020-11-28 09:18:13 -05:00
|
|
|
.map(move |result| match result {
|
|
|
|
Ok((paths_to_watch, module_info)) => ModuleResolutionResult::Success {
|
|
|
|
paths_to_watch,
|
|
|
|
module_info,
|
|
|
|
},
|
|
|
|
Err(e) => ModuleResolutionResult::Fail {
|
|
|
|
source_path: PathBuf::from(script2),
|
|
|
|
error: e,
|
|
|
|
},
|
|
|
|
})
|
2020-11-22 15:45:44 -05:00
|
|
|
.boxed_local()
|
|
|
|
};
|
2020-09-23 07:56:16 -04:00
|
|
|
|
2020-11-22 15:45:44 -05:00
|
|
|
let operation = |main_module: ModuleSpecifier| {
|
|
|
|
let flags = flags.clone();
|
2020-11-02 17:37:55 -05:00
|
|
|
let permissions = Permissions::from_flags(&flags);
|
2020-09-11 12:19:49 -04:00
|
|
|
async move {
|
2020-11-22 15:45:44 -05:00
|
|
|
let main_module = main_module.clone();
|
|
|
|
let program_state = ProgramState::new(flags)?;
|
|
|
|
let mut worker =
|
|
|
|
MainWorker::new(&program_state, main_module.clone(), permissions);
|
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'))")?;
|
2020-10-11 07:20:40 -04:00
|
|
|
worker.run_event_loop().await?;
|
2020-09-11 12:19:49 -04:00
|
|
|
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
.boxed_local()
|
2020-11-22 15:45:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
file_watcher::watch_func_with_module_resolution(
|
|
|
|
module_resolver,
|
|
|
|
operation,
|
|
|
|
"Process",
|
|
|
|
)
|
2020-09-11 12:19:49 -04:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-09-11 12:19:49 -04:00
|
|
|
let main_module = ModuleSpecifier::resolve_url_or_path(&script)?;
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = ProgramState::new(flags.clone())?;
|
2020-11-02 17:37:55 -05:00
|
|
|
let permissions = Permissions::from_flags(&flags);
|
|
|
|
let mut worker =
|
|
|
|
MainWorker::new(&program_state, main_module.clone(), permissions);
|
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'))")?;
|
2020-10-11 07:20:40 -04:00
|
|
|
worker.run_event_loop().await?;
|
2020-05-11 17:33:36 -04:00
|
|
|
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn test_command(
|
|
|
|
flags: Flags,
|
|
|
|
include: Option<Vec<String>>,
|
2020-11-22 08:06:51 -05:00
|
|
|
no_run: bool,
|
2020-05-11 17:33:36 -04:00
|
|
|
fail_fast: bool,
|
|
|
|
quiet: bool,
|
|
|
|
allow_none: bool,
|
|
|
|
filter: Option<String>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-10-13 07:35:35 -04:00
|
|
|
let program_state = ProgramState::new(flags.clone())?;
|
2020-11-02 17:37:55 -05:00
|
|
|
let permissions = Permissions::from_flags(&flags);
|
2020-05-11 17:33:36 -04:00
|
|
|
let cwd = std::env::current_dir().expect("No current directory");
|
|
|
|
let include = include.unwrap_or_else(|| vec![".".to_string()]);
|
2020-11-19 13:19:34 -05:00
|
|
|
let test_modules =
|
|
|
|
tools::test_runner::prepare_test_modules_urls(include, &cwd)?;
|
2020-05-11 17:33:36 -04:00
|
|
|
|
|
|
|
if test_modules.is_empty() {
|
|
|
|
println!("No matching test modules found");
|
|
|
|
if !allow_none {
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-11-22 08:06:51 -05:00
|
|
|
let main_module = ModuleSpecifier::resolve_path("$deno$test.ts")?;
|
2020-05-11 17:33:36 -04:00
|
|
|
// Create a dummy source file.
|
2020-11-05 19:38:21 -05:00
|
|
|
let source_file = File {
|
2020-11-22 08:06:51 -05:00
|
|
|
local: main_module.as_url().to_file_path().unwrap(),
|
2020-11-05 19:38:21 -05:00
|
|
|
maybe_types: None,
|
2020-05-11 17:33:36 -04:00
|
|
|
media_type: MediaType::TypeScript,
|
2020-11-22 08:06:51 -05:00
|
|
|
source: tools::test_runner::render_test_file(
|
|
|
|
test_modules.clone(),
|
|
|
|
fail_fast,
|
|
|
|
quiet,
|
|
|
|
filter,
|
|
|
|
),
|
|
|
|
specifier: main_module.clone(),
|
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(source_file);
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2020-11-22 08:06:51 -05:00
|
|
|
if no_run {
|
|
|
|
let lib = if flags.unstable {
|
|
|
|
module_graph::TypeLib::UnstableDenoWindow
|
|
|
|
} else {
|
|
|
|
module_graph::TypeLib::DenoWindow
|
|
|
|
};
|
|
|
|
program_state
|
|
|
|
.prepare_module_load(
|
|
|
|
main_module.clone(),
|
|
|
|
lib,
|
|
|
|
Permissions::allow_all(),
|
|
|
|
false,
|
|
|
|
program_state.maybe_import_map.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut worker =
|
|
|
|
MainWorker::new(&program_state, main_module.clone(), permissions);
|
|
|
|
|
2020-09-28 06:22:29 -04:00
|
|
|
let mut maybe_coverage_collector = if flags.coverage {
|
2020-10-11 07:20:40 -04:00
|
|
|
let session = worker.create_inspector_session();
|
2020-11-19 13:19:34 -05:00
|
|
|
let mut coverage_collector =
|
|
|
|
tools::coverage::CoverageCollector::new(session);
|
2020-09-13 09:01:30 -04:00
|
|
|
coverage_collector.start_collecting().await?;
|
|
|
|
|
|
|
|
Some(coverage_collector)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-05-11 17:33:36 -04:00
|
|
|
let execute_result = worker.execute_module(&main_module).await;
|
|
|
|
execute_result?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
2020-10-11 07:20:40 -04:00
|
|
|
worker.run_event_loop().await?;
|
2020-09-13 09:01:30 -04:00
|
|
|
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
2020-10-11 07:20:40 -04:00
|
|
|
worker.run_event_loop().await?;
|
2020-09-13 09:01:30 -04:00
|
|
|
|
|
|
|
if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
|
2020-09-23 14:12:24 -04:00
|
|
|
let coverages = coverage_collector.collect().await?;
|
2020-09-13 09:01:30 -04:00
|
|
|
coverage_collector.stop_collecting().await?;
|
|
|
|
|
2020-11-19 13:19:34 -05:00
|
|
|
let filtered_coverages = tools::coverage::filter_script_coverages(
|
|
|
|
coverages,
|
2020-11-22 08:06:51 -05:00
|
|
|
main_module.as_url().clone(),
|
2020-11-19 13:19:34 -05:00
|
|
|
test_modules,
|
|
|
|
);
|
2020-09-13 09:01:30 -04:00
|
|
|
|
2020-11-19 13:19:34 -05:00
|
|
|
let mut coverage_reporter =
|
|
|
|
tools::coverage::PrettyCoverageReporter::new(quiet);
|
2020-09-23 14:12:24 -04:00
|
|
|
for coverage in filtered_coverages {
|
|
|
|
coverage_reporter.visit_coverage(&coverage);
|
|
|
|
}
|
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");
|
|
|
|
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 init_logger(maybe_level: Option<Level>) {
|
|
|
|
let log_level = match maybe_level {
|
2020-05-11 17:33:36 -04:00
|
|
|
Some(level) => level,
|
|
|
|
None => Level::Info, // Default log level
|
|
|
|
};
|
2020-09-03 16:16:49 -04:00
|
|
|
env_logger::Builder::from_env(
|
|
|
|
env_logger::Env::default()
|
|
|
|
.default_filter_or(log_level.to_level_filter().to_string()),
|
|
|
|
)
|
2020-09-17 14:38:57 -04:00
|
|
|
// https://github.com/denoland/deno/issues/6641
|
|
|
|
.filter_module("rustyline", LevelFilter::Off)
|
2020-09-03 16:16:49 -04:00
|
|
|
.format(|buf, record| {
|
|
|
|
let mut target = record.target().to_string();
|
|
|
|
if let Some(line_no) = record.line() {
|
2020-09-14 18:05:17 -04:00
|
|
|
target.push(':');
|
2020-09-03 16:16:49 -04:00
|
|
|
target.push_str(&line_no.to_string());
|
|
|
|
}
|
2020-09-22 11:08:10 -04:00
|
|
|
if record.level() <= Level::Info {
|
|
|
|
// Print ERROR, WARN, INFO logs as they are
|
2020-09-03 16:16:49 -04:00
|
|
|
writeln!(buf, "{}", record.args())
|
|
|
|
} else {
|
2020-09-22 11:08:10 -04:00
|
|
|
// Add prefix to DEBUG or TRACE logs
|
2020-09-03 16:16:49 -04:00
|
|
|
writeln!(
|
|
|
|
buf,
|
|
|
|
"{} RS - {} - {}",
|
|
|
|
record.level(),
|
|
|
|
target,
|
|
|
|
record.args()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.init();
|
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(),
|
2020-05-11 17:33:36 -04:00
|
|
|
DenoSubcommand::Eval {
|
2020-05-21 10:35:36 -04:00
|
|
|
print,
|
2020-05-11 17:33:36 -04:00
|
|
|
code,
|
|
|
|
as_typescript,
|
2020-05-21 10:35:36 -04:00
|
|
|
} => eval_command(flags, code, as_typescript, 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,
|
|
|
|
} => compile_command(flags, source_file, output).boxed_local(),
|
2020-07-30 12:09:08 -04:00
|
|
|
DenoSubcommand::Fmt {
|
|
|
|
check,
|
|
|
|
files,
|
|
|
|
ignore,
|
2020-11-22 15:45:44 -05:00
|
|
|
} => format_command(flags, files, ignore, check).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()
|
|
|
|
}
|
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,
|
2020-05-11 17:33:36 -04:00
|
|
|
fail_fast,
|
|
|
|
quiet,
|
|
|
|
include,
|
|
|
|
allow_none,
|
|
|
|
filter,
|
2020-11-22 08:06:51 -05:00
|
|
|
} => {
|
|
|
|
test_command(flags, include, no_run, fail_fast, quiet, allow_none, filter)
|
|
|
|
.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
#[cfg(windows)]
|
|
|
|
colors::enable_ansi(); // For Windows 10
|
|
|
|
|
|
|
|
let args: Vec<String> = env::args().collect();
|
2020-11-30 14:35:12 -05:00
|
|
|
if let Err(err) = standalone::try_run_standalone_binary(args.clone()) {
|
|
|
|
eprintln!("{}: {}", colors::red_bold("error"), err.to_string());
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2020-11-26 09:17:45 -05:00
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
let flags = flags::flags_from_vec(args);
|
2020-11-26 09:17:45 -05:00
|
|
|
if let Some(ref v8_flags) = flags.v8_flags {
|
|
|
|
init_v8_flags(v8_flags);
|
|
|
|
}
|
|
|
|
init_logger(flags.log_level);
|
2020-05-11 17:33:36 -04:00
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
let subcommand_future = get_subcommand(flags);
|
|
|
|
let result = tokio_util::run_basic(subcommand_future);
|
2020-05-11 17:33:36 -04:00
|
|
|
if let Err(err) = result {
|
2020-11-12 16:49:59 -05:00
|
|
|
eprintln!("{}: {}", colors::red_bold("error"), err.to_string());
|
2020-05-11 17:33:36 -04:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2018-06-15 19:43:23 -04:00
|
|
|
}
|