2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-06 11:38:56 -05:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
2019-09-16 21:05:14 -04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
extern crate futures;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_json;
|
|
|
|
extern crate clap;
|
2020-01-05 11:56:18 -05:00
|
|
|
extern crate deno_core;
|
2019-09-16 21:05:14 -04:00
|
|
|
extern crate indexmap;
|
|
|
|
#[cfg(unix)]
|
|
|
|
extern crate nix;
|
|
|
|
extern crate rand;
|
2020-03-23 11:37:24 -04:00
|
|
|
extern crate regex;
|
|
|
|
extern crate reqwest;
|
2019-09-16 21:05:14 -04:00
|
|
|
extern crate serde;
|
|
|
|
extern crate serde_derive;
|
2019-11-04 10:38:52 -05:00
|
|
|
extern crate tokio;
|
2019-09-16 21:05:14 -04:00
|
|
|
extern crate url;
|
|
|
|
|
2019-11-03 10:39:27 -05:00
|
|
|
mod checksum;
|
2019-09-16 21:05:14 -04:00
|
|
|
pub mod colors;
|
|
|
|
pub mod compilers;
|
|
|
|
pub mod deno_dir;
|
|
|
|
pub mod diagnostics;
|
|
|
|
mod disk_cache;
|
2020-03-28 14:16:57 -04:00
|
|
|
mod doc;
|
2019-09-16 21:05:14 -04:00
|
|
|
mod file_fetcher;
|
|
|
|
pub mod flags;
|
2020-01-29 21:16:48 -05:00
|
|
|
mod fmt;
|
2019-09-16 21:05:14 -04:00
|
|
|
pub mod fmt_errors;
|
|
|
|
mod fs;
|
2020-04-01 19:41:32 -04:00
|
|
|
pub mod global_state;
|
2019-09-16 21:05:14 -04:00
|
|
|
mod global_timer;
|
2020-02-19 08:17:13 -05:00
|
|
|
pub mod http_cache;
|
2019-09-16 21:05:14 -04:00
|
|
|
mod http_util;
|
|
|
|
mod import_map;
|
2020-03-27 16:09:51 -04:00
|
|
|
mod inspector;
|
2020-01-31 11:34:50 -05:00
|
|
|
pub mod installer;
|
2019-10-04 20:28:51 -04:00
|
|
|
mod js;
|
2019-11-03 10:39:27 -05:00
|
|
|
mod lockfile;
|
2019-11-04 10:38:52 -05:00
|
|
|
mod metrics;
|
2019-09-16 21:05:14 -04:00
|
|
|
pub mod msg;
|
2020-02-23 14:51:29 -05:00
|
|
|
pub mod op_error;
|
2019-09-16 21:05:14 -04:00
|
|
|
pub mod ops;
|
|
|
|
pub mod permissions;
|
|
|
|
mod repl;
|
|
|
|
pub mod resolve_addr;
|
2020-01-24 08:15:31 -05:00
|
|
|
pub mod signal;
|
2019-09-16 21:05:14 -04:00
|
|
|
pub mod source_maps;
|
|
|
|
mod startup_data;
|
|
|
|
pub mod state;
|
2020-02-11 06:01:56 -05:00
|
|
|
mod test_runner;
|
2019-09-19 14:48:05 -04:00
|
|
|
pub mod test_util;
|
2019-09-16 21:05:14 -04:00
|
|
|
mod tokio_util;
|
2020-03-23 11:37:24 -04:00
|
|
|
mod upgrade;
|
2019-09-16 21:05:14 -04:00
|
|
|
pub mod version;
|
2020-01-21 03:49:47 -05:00
|
|
|
mod web_worker;
|
2019-09-16 21:05:14 -04:00
|
|
|
pub mod worker;
|
|
|
|
|
2020-04-03 12:35:03 -04:00
|
|
|
pub use dprint_plugin_typescript::swc_common;
|
|
|
|
pub use dprint_plugin_typescript::swc_ecma_ast;
|
|
|
|
pub use dprint_plugin_typescript::swc_ecma_parser;
|
|
|
|
|
2020-01-29 12:54:23 -05:00
|
|
|
use crate::compilers::TargetLib;
|
2020-04-07 13:47:06 -04:00
|
|
|
use crate::doc::parser::DocFileLoader;
|
2020-02-28 09:17:56 -05:00
|
|
|
use crate::file_fetcher::SourceFile;
|
2020-04-07 13:47:06 -04:00
|
|
|
use crate::file_fetcher::SourceFileFetcher;
|
2020-02-06 23:05:02 -05:00
|
|
|
use crate::global_state::GlobalState;
|
2020-02-28 09:17:56 -05:00
|
|
|
use crate::msg::MediaType;
|
2020-04-07 13:47:06 -04:00
|
|
|
use crate::op_error::OpError;
|
2019-11-14 12:10:25 -05:00
|
|
|
use crate::ops::io::get_stdio;
|
2020-04-03 13:40:11 -04:00
|
|
|
use crate::state::DebugType;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-01-21 11:50:06 -05:00
|
|
|
use crate::worker::MainWorker;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::v8_set_flags;
|
|
|
|
use deno_core::ErrBox;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2019-09-16 21:05:14 -04:00
|
|
|
use flags::DenoSubcommand;
|
2020-02-26 05:52:15 -05:00
|
|
|
use flags::Flags;
|
2020-02-18 10:08:18 -05:00
|
|
|
use futures::future::FutureExt;
|
2020-04-07 13:47:06 -04:00
|
|
|
use futures::Future;
|
2019-09-16 21:05:14 -04:00
|
|
|
use log::Level;
|
|
|
|
use log::Metadata;
|
|
|
|
use log::Record;
|
|
|
|
use std::env;
|
2020-02-18 10:08:18 -05:00
|
|
|
use std::io::Write;
|
2020-02-11 04:29:36 -05:00
|
|
|
use std::path::PathBuf;
|
2020-04-07 13:47:06 -04:00
|
|
|
use std::pin::Pin;
|
2020-03-23 11:37:24 -04:00
|
|
|
use upgrade::upgrade_command;
|
2020-02-18 10:08:18 -05:00
|
|
|
use url::Url;
|
2019-09-16 21:05:14 -04:00
|
|
|
|
|
|
|
static LOGGER: Logger = Logger;
|
|
|
|
|
2020-03-10 08:26:17 -04:00
|
|
|
// TODO(ry) Switch to env_logger or other standard crate.
|
2019-09-16 21:05:14 -04:00
|
|
|
struct Logger;
|
|
|
|
|
|
|
|
impl log::Log for Logger {
|
|
|
|
fn enabled(&self, metadata: &Metadata) -> bool {
|
|
|
|
metadata.level() <= log::max_level()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log(&self, record: &Record) {
|
|
|
|
if self.enabled(record.metadata()) {
|
|
|
|
let mut target = record.target().to_string();
|
|
|
|
|
|
|
|
if let Some(line_no) = record.line() {
|
|
|
|
target.push_str(":");
|
|
|
|
target.push_str(&line_no.to_string());
|
|
|
|
}
|
|
|
|
|
2020-03-10 08:26:17 -04:00
|
|
|
if record.level() >= Level::Info {
|
|
|
|
eprintln!("{}", record.args());
|
|
|
|
} else {
|
|
|
|
eprintln!("{} RS - {} - {}", record.level(), target, record.args());
|
|
|
|
}
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn flush(&self) {}
|
|
|
|
}
|
|
|
|
|
2020-03-28 14:16:57 -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-02-04 14:24:33 -05:00
|
|
|
fn create_main_worker(
|
2020-02-06 23:05:02 -05:00
|
|
|
global_state: GlobalState,
|
2020-02-04 14:24:33 -05:00
|
|
|
main_module: ModuleSpecifier,
|
2020-02-18 10:08:18 -05:00
|
|
|
) -> Result<MainWorker, ErrBox> {
|
2020-04-03 13:40:11 -04:00
|
|
|
let state = State::new(global_state, None, main_module, DebugType::Main)?;
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2019-11-14 12:10:25 -05:00
|
|
|
{
|
2020-02-18 10:08:18 -05:00
|
|
|
let mut s = state.borrow_mut();
|
2019-11-14 12:10:25 -05:00
|
|
|
let (stdin, stdout, stderr) = get_stdio();
|
2020-02-18 10:08:18 -05:00
|
|
|
s.resource_table.add("stdin", Box::new(stdin));
|
|
|
|
s.resource_table.add("stdout", Box::new(stdout));
|
|
|
|
s.resource_table.add("stderr", Box::new(stderr));
|
2019-11-14 12:10:25 -05:00
|
|
|
}
|
|
|
|
|
2020-02-18 10:08:18 -05:00
|
|
|
let mut worker = MainWorker::new(
|
|
|
|
"main".to_string(),
|
|
|
|
startup_data::deno_isolate_init(),
|
|
|
|
state,
|
2020-01-29 12:54:23 -05:00
|
|
|
);
|
2020-02-18 10:08:18 -05:00
|
|
|
worker.execute("bootstrapMainRuntime()")?;
|
|
|
|
Ok(worker)
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-02-06 23:05:02 -05:00
|
|
|
fn print_cache_info(state: &GlobalState) {
|
2019-09-16 21:05:14 -04:00
|
|
|
println!(
|
|
|
|
"{} {:?}",
|
|
|
|
colors::bold("DENO_DIR location:".to_string()),
|
|
|
|
state.dir.root
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{} {:?}",
|
|
|
|
colors::bold("Remote modules cache:".to_string()),
|
2020-02-19 08:17:13 -05:00
|
|
|
state.file_fetcher.http_cache.location
|
2019-09-16 21:05:14 -04:00
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{} {:?}",
|
|
|
|
colors::bold("TypeScript compiler cache:".to_string()),
|
|
|
|
state.dir.gen_cache.location
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-18 10:08:18 -05:00
|
|
|
// TODO(bartlomieju): this function de facto repeats
|
|
|
|
// whole compilation stack. Can this be done better somehow?
|
2020-01-21 11:50:06 -05:00
|
|
|
async fn print_file_info(
|
2020-02-03 18:08:44 -05:00
|
|
|
worker: &MainWorker,
|
2020-01-21 11:50:06 -05:00
|
|
|
module_specifier: ModuleSpecifier,
|
2020-02-18 10:08:18 -05:00
|
|
|
) -> Result<(), ErrBox> {
|
2020-02-08 14:34:31 -05:00
|
|
|
let global_state = worker.state.borrow().global_state.clone();
|
2019-09-16 21:05:14 -04:00
|
|
|
|
2020-02-18 10:08:18 -05:00
|
|
|
let out = global_state
|
2019-09-16 21:05:14 -04:00
|
|
|
.file_fetcher
|
2020-02-25 14:42:00 -05:00
|
|
|
.fetch_source_file(&module_specifier, None)
|
2020-02-18 10:08:18 -05:00
|
|
|
.await?;
|
|
|
|
|
2019-11-16 23:25:12 -05:00
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("local:".to_string()),
|
|
|
|
out.filename.to_str().unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("type:".to_string()),
|
|
|
|
msg::enum_name_media_type(out.media_type)
|
|
|
|
);
|
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
let module_specifier_ = module_specifier.clone();
|
2020-02-18 10:08:18 -05:00
|
|
|
global_state
|
2019-11-16 23:25:12 -05:00
|
|
|
.clone()
|
2020-02-03 18:08:44 -05:00
|
|
|
.fetch_compiled_module(module_specifier_, None, TargetLib::Main)
|
2020-02-18 10:08:18 -05:00
|
|
|
.await?;
|
|
|
|
|
2019-11-16 23:25:12 -05:00
|
|
|
if out.media_type == msg::MediaType::TypeScript
|
|
|
|
|| (out.media_type == msg::MediaType::JavaScript
|
2020-02-03 18:08:44 -05:00
|
|
|
&& global_state.ts_compiler.compile_js)
|
2019-11-16 23:25:12 -05:00
|
|
|
{
|
2020-02-03 18:08:44 -05:00
|
|
|
let compiled_source_file = global_state
|
2019-11-16 23:25:12 -05:00
|
|
|
.ts_compiler
|
|
|
|
.get_compiled_source_file(&out.url)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("compiled:".to_string()),
|
|
|
|
compiled_source_file.filename.to_str().unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
if let Ok(source_map) = global_state
|
2019-11-16 23:25:12 -05:00
|
|
|
.clone()
|
|
|
|
.ts_compiler
|
|
|
|
.get_source_map_file(&module_specifier)
|
|
|
|
{
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
colors::bold("map:".to_string()),
|
|
|
|
source_map.filename.to_str().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
if let Some(deps) = worker.isolate.modules.deps(&module_specifier) {
|
2019-11-16 23:25:12 -05:00
|
|
|
println!("{}{}", colors::bold("deps:\n".to_string()), deps.name);
|
|
|
|
if let Some(ref depsdeps) = deps.deps {
|
|
|
|
for d in depsdeps {
|
|
|
|
println!("{}", d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"{} cannot retrieve full dependency graph",
|
|
|
|
colors::bold("deps:".to_string()),
|
|
|
|
);
|
|
|
|
}
|
2020-02-18 10:08:18 -05:00
|
|
|
|
|
|
|
Ok(())
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-04-09 08:34:24 -04:00
|
|
|
fn get_types() -> String {
|
|
|
|
format!(
|
|
|
|
"{}\n{}\n{}",
|
|
|
|
crate::js::DENO_NS_LIB,
|
|
|
|
crate::js::SHARED_GLOBALS_LIB,
|
|
|
|
crate::js::WINDOW_LIB
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-18 10:08:18 -05:00
|
|
|
async fn info_command(
|
2020-02-26 05:52:15 -05:00
|
|
|
flags: Flags,
|
2020-02-18 10:08:18 -05:00
|
|
|
file: Option<String>,
|
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
let global_state = GlobalState::new(flags)?;
|
2019-09-16 21:05:14 -04:00
|
|
|
// If it was just "deno info" print location of caches and exit
|
2020-02-04 14:24:33 -05:00
|
|
|
if file.is_none() {
|
2020-02-18 10:08:18 -05:00
|
|
|
print_cache_info(&global_state);
|
|
|
|
return Ok(());
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
2020-02-04 14:24:33 -05:00
|
|
|
|
2020-02-18 10:08:18 -05:00
|
|
|
let main_module = ModuleSpecifier::resolve_url_or_path(&file.unwrap())?;
|
|
|
|
let mut worker = create_main_worker(global_state, main_module.clone())?;
|
|
|
|
worker.preload_module(&main_module).await?;
|
|
|
|
print_file_info(&worker, main_module.clone()).await
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-01-30 18:42:39 -05:00
|
|
|
async fn install_command(
|
2020-02-26 05:52:15 -05:00
|
|
|
flags: Flags,
|
2020-04-16 18:15:42 -04:00
|
|
|
root: Option<PathBuf>,
|
2020-01-30 18:42:39 -05:00
|
|
|
exe_name: String,
|
|
|
|
module_url: String,
|
|
|
|
args: Vec<String>,
|
2020-02-08 03:49:55 -05:00
|
|
|
force: bool,
|
2020-02-18 10:08:18 -05:00
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
// Firstly fetch and compile module, this step ensures that module exists.
|
2020-01-30 18:42:39 -05:00
|
|
|
let mut fetch_flags = flags.clone();
|
|
|
|
fetch_flags.reload = true;
|
2020-02-18 10:08:18 -05:00
|
|
|
let global_state = GlobalState::new(fetch_flags)?;
|
|
|
|
let main_module = ModuleSpecifier::resolve_url_or_path(&module_url)?;
|
|
|
|
let mut worker = create_main_worker(global_state, main_module.clone())?;
|
|
|
|
worker.preload_module(&main_module).await?;
|
2020-04-16 18:15:42 -04:00
|
|
|
installer::install(flags, root, &exe_name, &module_url, args, force)
|
2020-02-18 10:08:18 -05:00
|
|
|
.map_err(ErrBox::from)
|
2020-01-30 18:42:39 -05:00
|
|
|
}
|
|
|
|
|
2020-04-07 11:24:47 -04:00
|
|
|
async fn cache_command(flags: Flags, files: Vec<String>) -> Result<(), ErrBox> {
|
2020-02-04 14:24:33 -05:00
|
|
|
let main_module =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("./__$deno$fetch.ts").unwrap();
|
2020-02-18 10:08:18 -05:00
|
|
|
let global_state = GlobalState::new(flags)?;
|
2020-02-04 14:24:33 -05:00
|
|
|
let mut worker =
|
2020-02-18 10:08:18 -05:00
|
|
|
create_main_worker(global_state.clone(), main_module.clone())?;
|
2019-11-16 19:17:47 -05:00
|
|
|
|
2020-02-04 14:24:33 -05:00
|
|
|
for file in files {
|
2020-02-18 10:08:18 -05:00
|
|
|
let specifier = ModuleSpecifier::resolve_url_or_path(&file)?;
|
|
|
|
worker.preload_module(&specifier).await.map(|_| ())?;
|
2020-01-31 16:07:37 -05:00
|
|
|
}
|
|
|
|
|
2020-02-04 14:24:33 -05:00
|
|
|
if global_state.flags.lock_write {
|
|
|
|
if let Some(ref lockfile) = global_state.lockfile {
|
2020-01-30 11:28:51 -05:00
|
|
|
let g = lockfile.lock().unwrap();
|
2020-02-18 10:08:18 -05:00
|
|
|
g.write()?;
|
2020-01-30 11:28:51 -05:00
|
|
|
} else {
|
|
|
|
eprintln!("--lock flag must be specified when using --lock-write");
|
|
|
|
std::process::exit(11);
|
2020-01-26 13:43:59 -05:00
|
|
|
}
|
2020-01-30 11:28:51 -05:00
|
|
|
}
|
2020-02-18 10:08:18 -05:00
|
|
|
|
|
|
|
Ok(())
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-02-28 09:17:56 -05:00
|
|
|
async fn eval_command(
|
|
|
|
flags: Flags,
|
|
|
|
code: String,
|
|
|
|
as_typescript: bool,
|
|
|
|
) -> Result<(), ErrBox> {
|
2019-10-19 17:19:19 -04:00
|
|
|
// Force TypeScript compile.
|
|
|
|
let main_module =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("./__$deno$eval.ts").unwrap();
|
2020-02-18 10:08:18 -05:00
|
|
|
let global_state = GlobalState::new(flags)?;
|
|
|
|
let mut worker = create_main_worker(global_state, main_module.clone())?;
|
2020-02-28 09:17:56 -05:00
|
|
|
let main_module_url = main_module.as_url().to_owned();
|
|
|
|
// Create a dummy source file.
|
|
|
|
let source_file = SourceFile {
|
|
|
|
filename: main_module_url.to_file_path().unwrap(),
|
|
|
|
url: main_module_url,
|
|
|
|
types_url: None,
|
|
|
|
media_type: if as_typescript {
|
|
|
|
MediaType::TypeScript
|
|
|
|
} else {
|
|
|
|
MediaType::JavaScript
|
|
|
|
},
|
|
|
|
source_code: code.clone().into_bytes(),
|
|
|
|
};
|
|
|
|
// Save our fake file into file fetcher cache
|
|
|
|
// to allow module access by TS compiler (e.g. op_fetch_source_files)
|
|
|
|
worker
|
|
|
|
.state
|
|
|
|
.borrow()
|
|
|
|
.global_state
|
|
|
|
.file_fetcher
|
|
|
|
.save_source_file_in_cache(&main_module, source_file);
|
2019-11-16 19:17:47 -05:00
|
|
|
debug!("main_module {}", &main_module);
|
2020-02-28 09:17:56 -05:00
|
|
|
worker.execute_module(&main_module).await?;
|
2020-02-18 10:08:18 -05:00
|
|
|
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
|
|
|
(&mut *worker).await?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
|
|
|
Ok(())
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-02-04 14:24:33 -05:00
|
|
|
async fn bundle_command(
|
2020-02-26 05:52:15 -05:00
|
|
|
flags: Flags,
|
2020-02-04 14:24:33 -05:00
|
|
|
source_file: String,
|
2020-02-11 04:29:36 -05:00
|
|
|
out_file: Option<PathBuf>,
|
2020-02-18 10:08:18 -05:00
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
let module_name = ModuleSpecifier::resolve_url_or_path(&source_file)?;
|
|
|
|
let global_state = GlobalState::new(flags)?;
|
2020-02-25 14:42:00 -05:00
|
|
|
debug!(">>>>> bundle START");
|
2020-02-04 14:24:33 -05:00
|
|
|
let bundle_result = global_state
|
2020-01-30 11:28:51 -05:00
|
|
|
.ts_compiler
|
2020-02-25 14:42:00 -05:00
|
|
|
.bundle(global_state.clone(), module_name.to_string(), out_file)
|
2020-01-30 11:28:51 -05:00
|
|
|
.await;
|
2020-02-25 14:42:00 -05:00
|
|
|
debug!(">>>>> bundle END");
|
2020-02-18 10:08:18 -05:00
|
|
|
bundle_result
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:16:57 -04:00
|
|
|
async fn doc_command(
|
|
|
|
flags: Flags,
|
2020-04-09 08:34:24 -04:00
|
|
|
source_file: Option<String>,
|
2020-03-28 14:16:57 -04:00
|
|
|
json: bool,
|
|
|
|
maybe_filter: Option<String>,
|
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
let global_state = GlobalState::new(flags.clone())?;
|
2020-04-09 08:34:24 -04:00
|
|
|
let source_file = source_file.unwrap_or_else(|| "--builtin".to_string());
|
2020-03-28 14:16:57 -04:00
|
|
|
|
2020-04-07 13:47:06 -04:00
|
|
|
impl DocFileLoader for SourceFileFetcher {
|
|
|
|
fn load_source_code(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<String, OpError>>>> {
|
|
|
|
let specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path(specifier).expect("Bad specifier");
|
|
|
|
let fetcher = self.clone();
|
|
|
|
|
|
|
|
async move {
|
|
|
|
let source_file = fetcher.fetch_source_file(&specifier, None).await?;
|
|
|
|
String::from_utf8(source_file.source_code)
|
|
|
|
.map_err(|_| OpError::other("failed to parse".to_string()))
|
|
|
|
}
|
|
|
|
.boxed_local()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let loader = Box::new(global_state.file_fetcher.clone());
|
|
|
|
let doc_parser = doc::DocParser::new(loader);
|
2020-04-09 08:34:24 -04:00
|
|
|
|
|
|
|
let parse_result = if source_file == "--builtin" {
|
|
|
|
doc_parser.parse_source("lib.deno.d.ts", get_types().as_str())
|
|
|
|
} else {
|
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path(&source_file).unwrap();
|
|
|
|
doc_parser
|
|
|
|
.parse_with_reexports(&module_specifier.to_string())
|
|
|
|
.await
|
|
|
|
};
|
2020-03-28 14:16:57 -04:00
|
|
|
|
|
|
|
let doc_nodes = match parse_result {
|
|
|
|
Ok(nodes) => nodes,
|
|
|
|
Err(e) => {
|
2020-04-07 13:47:06 -04:00
|
|
|
eprintln!("{}", e);
|
2020-03-28 14:16:57 -04:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if json {
|
|
|
|
let writer = std::io::BufWriter::new(std::io::stdout());
|
|
|
|
serde_json::to_writer_pretty(writer, &doc_nodes).map_err(ErrBox::from)
|
|
|
|
} else {
|
|
|
|
let details = if let Some(filter) = maybe_filter {
|
|
|
|
let node = doc::find_node_by_name_recursively(doc_nodes, filter.clone());
|
|
|
|
if let Some(node) = node {
|
|
|
|
doc::printer::format_details(node)
|
|
|
|
} else {
|
|
|
|
eprintln!("Node {} was not found!", filter);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
doc::printer::format(doc_nodes)
|
|
|
|
};
|
|
|
|
|
|
|
|
write_to_stdout_ignore_sigpipe(details.as_bytes()).map_err(ErrBox::from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:52:15 -05:00
|
|
|
async fn run_repl(flags: Flags) -> Result<(), ErrBox> {
|
2020-02-04 14:24:33 -05:00
|
|
|
let main_module =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("./__$deno$repl.ts").unwrap();
|
2020-02-18 10:08:18 -05:00
|
|
|
let global_state = GlobalState::new(flags)?;
|
|
|
|
let mut worker = create_main_worker(global_state, main_module)?;
|
2020-01-30 11:28:51 -05:00
|
|
|
loop {
|
2020-02-18 10:08:18 -05:00
|
|
|
(&mut *worker).await?;
|
2020-01-30 11:28:51 -05:00
|
|
|
}
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:52:15 -05:00
|
|
|
async fn run_command(flags: Flags, script: String) -> Result<(), ErrBox> {
|
2020-02-18 10:08:18 -05:00
|
|
|
let global_state = GlobalState::new(flags.clone())?;
|
|
|
|
let main_module = ModuleSpecifier::resolve_url_or_path(&script).unwrap();
|
|
|
|
let mut worker =
|
|
|
|
create_main_worker(global_state.clone(), main_module.clone())?;
|
|
|
|
debug!("main_module {}", main_module);
|
|
|
|
worker.execute_module(&main_module).await?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
|
|
|
(&mut *worker).await?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('unload'))")?;
|
2020-02-04 14:24:33 -05:00
|
|
|
if global_state.flags.lock_write {
|
|
|
|
if let Some(ref lockfile) = global_state.lockfile {
|
2020-01-30 11:28:51 -05:00
|
|
|
let g = lockfile.lock().unwrap();
|
2020-02-18 10:08:18 -05:00
|
|
|
g.write()?;
|
2020-01-30 11:28:51 -05:00
|
|
|
} else {
|
|
|
|
eprintln!("--lock flag must be specified when using --lock-write");
|
|
|
|
std::process::exit(11);
|
2019-11-16 23:25:12 -05:00
|
|
|
}
|
2020-01-30 11:28:51 -05:00
|
|
|
}
|
2020-02-11 06:01:56 -05:00
|
|
|
Ok(())
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-02-11 06:01:56 -05:00
|
|
|
async fn test_command(
|
2020-02-26 05:52:15 -05:00
|
|
|
flags: Flags,
|
2020-02-11 06:01:56 -05:00
|
|
|
include: Option<Vec<String>>,
|
|
|
|
fail_fast: bool,
|
|
|
|
allow_none: bool,
|
2020-04-02 09:26:40 -04:00
|
|
|
filter: Option<String>,
|
2020-02-18 10:08:18 -05:00
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
let global_state = GlobalState::new(flags.clone())?;
|
2020-02-11 06:01:56 -05:00
|
|
|
let cwd = std::env::current_dir().expect("No current directory");
|
|
|
|
let include = include.unwrap_or_else(|| vec![".".to_string()]);
|
2020-02-18 10:08:18 -05:00
|
|
|
let test_modules = test_runner::prepare_test_modules_urls(include, &cwd)?;
|
2020-02-11 06:01:56 -05:00
|
|
|
|
|
|
|
if test_modules.is_empty() {
|
|
|
|
println!("No matching test modules found");
|
|
|
|
if !allow_none {
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2020-02-18 10:08:18 -05:00
|
|
|
return Ok(());
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let test_file_path = cwd.join(".deno.test.ts");
|
2020-02-18 10:08:18 -05:00
|
|
|
let test_file_url =
|
|
|
|
Url::from_file_path(&test_file_path).expect("Should be valid file url");
|
2020-04-02 09:26:40 -04:00
|
|
|
let test_file =
|
|
|
|
test_runner::render_test_file(test_modules, fail_fast, filter);
|
2020-02-18 10:08:18 -05:00
|
|
|
let main_module =
|
|
|
|
ModuleSpecifier::resolve_url(&test_file_url.to_string()).unwrap();
|
|
|
|
let mut worker =
|
|
|
|
create_main_worker(global_state.clone(), main_module.clone())?;
|
2020-03-19 09:26:47 -04:00
|
|
|
// Create a dummy source file.
|
|
|
|
let source_file = SourceFile {
|
|
|
|
filename: test_file_url.to_file_path().unwrap(),
|
|
|
|
url: test_file_url,
|
|
|
|
types_url: None,
|
|
|
|
media_type: MediaType::TypeScript,
|
|
|
|
source_code: test_file.clone().into_bytes(),
|
|
|
|
};
|
|
|
|
// Save our fake file into file fetcher cache
|
|
|
|
// to allow module access by TS compiler (e.g. op_fetch_source_files)
|
|
|
|
worker
|
|
|
|
.state
|
|
|
|
.borrow()
|
|
|
|
.global_state
|
|
|
|
.file_fetcher
|
|
|
|
.save_source_file_in_cache(&main_module, source_file);
|
2020-02-18 10:08:18 -05:00
|
|
|
let execute_result = worker.execute_module(&main_module).await;
|
|
|
|
execute_result?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('load'))")?;
|
|
|
|
(&mut *worker).await?;
|
|
|
|
worker.execute("window.dispatchEvent(new Event('unload'))")
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
pub fn main() {
|
2020-02-24 17:18:15 -05:00
|
|
|
#[cfg(windows)]
|
2020-02-24 19:30:17 -05:00
|
|
|
colors::enable_ansi(); // For Windows 10
|
2020-02-24 17:18:15 -05:00
|
|
|
|
2019-09-16 21:05:14 -04:00
|
|
|
log::set_logger(&LOGGER).unwrap();
|
|
|
|
let args: Vec<String> = env::args().collect();
|
2019-11-26 11:06:32 -05:00
|
|
|
let flags = flags::flags_from_vec(args);
|
2019-09-16 21:05:14 -04:00
|
|
|
|
|
|
|
if let Some(ref v8_flags) = flags.v8_flags {
|
2019-11-26 11:06:32 -05:00
|
|
|
let mut v8_flags_ = v8_flags.clone();
|
|
|
|
v8_flags_.insert(0, "UNUSED_BUT_NECESSARY_ARG0".to_string());
|
|
|
|
v8_set_flags(v8_flags_);
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let log_level = match flags.log_level {
|
|
|
|
Some(level) => level,
|
2020-03-10 08:26:17 -04:00
|
|
|
None => Level::Info, // Default log level
|
2019-09-16 21:05:14 -04:00
|
|
|
};
|
|
|
|
log::set_max_level(log_level.to_level_filter());
|
|
|
|
|
2020-02-18 10:08:18 -05:00
|
|
|
let fut = match flags.clone().subcommand {
|
|
|
|
DenoSubcommand::Bundle {
|
|
|
|
source_file,
|
|
|
|
out_file,
|
|
|
|
} => bundle_command(flags, source_file, out_file).boxed_local(),
|
2020-03-28 14:16:57 -04:00
|
|
|
DenoSubcommand::Doc {
|
|
|
|
source_file,
|
|
|
|
json,
|
|
|
|
filter,
|
|
|
|
} => doc_command(flags, source_file, json, filter).boxed_local(),
|
2020-02-28 09:17:56 -05:00
|
|
|
DenoSubcommand::Eval {
|
|
|
|
code,
|
|
|
|
as_typescript,
|
|
|
|
} => eval_command(flags, code, as_typescript).boxed_local(),
|
2020-04-07 11:24:47 -04:00
|
|
|
DenoSubcommand::Cache { files } => {
|
|
|
|
cache_command(flags, files).boxed_local()
|
2020-02-18 10:08:18 -05:00
|
|
|
}
|
|
|
|
DenoSubcommand::Fmt { check, files } => {
|
2020-02-26 05:50:53 -05:00
|
|
|
async move { fmt::format(files, check) }.boxed_local()
|
2020-02-18 10:08:18 -05:00
|
|
|
}
|
|
|
|
DenoSubcommand::Info { file } => info_command(flags, file).boxed_local(),
|
|
|
|
DenoSubcommand::Install {
|
2020-04-16 18:15:42 -04:00
|
|
|
root,
|
2020-02-18 10:08:18 -05:00
|
|
|
exe_name,
|
|
|
|
module_url,
|
|
|
|
args,
|
|
|
|
force,
|
2020-04-16 18:15:42 -04:00
|
|
|
} => install_command(flags, root, exe_name, module_url, args, force)
|
2020-02-18 10:08:18 -05:00
|
|
|
.boxed_local(),
|
|
|
|
DenoSubcommand::Repl => run_repl(flags).boxed_local(),
|
|
|
|
DenoSubcommand::Run { script } => run_command(flags, script).boxed_local(),
|
|
|
|
DenoSubcommand::Test {
|
|
|
|
fail_fast,
|
|
|
|
include,
|
|
|
|
allow_none,
|
2020-04-02 09:26:40 -04:00
|
|
|
filter,
|
|
|
|
} => {
|
|
|
|
test_command(flags, include, fail_fast, allow_none, filter).boxed_local()
|
|
|
|
}
|
2020-02-18 10:08:18 -05:00
|
|
|
DenoSubcommand::Completions { buf } => {
|
2020-03-28 14:16:57 -04:00
|
|
|
if let Err(e) = write_to_stdout_ignore_sigpipe(&buf) {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2020-02-18 10:08:18 -05:00
|
|
|
return;
|
2020-02-03 18:08:44 -05:00
|
|
|
}
|
2020-02-18 10:08:18 -05:00
|
|
|
DenoSubcommand::Types => {
|
2020-04-09 08:34:24 -04:00
|
|
|
let types = get_types();
|
2020-03-28 14:16:57 -04:00
|
|
|
if let Err(e) = write_to_stdout_ignore_sigpipe(types.as_bytes()) {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2020-02-18 10:08:18 -05:00
|
|
|
return;
|
|
|
|
}
|
2020-03-23 11:37:24 -04:00
|
|
|
DenoSubcommand::Upgrade { force, dry_run } => {
|
|
|
|
upgrade_command(dry_run, force).boxed_local()
|
|
|
|
}
|
2020-02-18 10:08:18 -05:00
|
|
|
_ => unreachable!(),
|
2020-02-03 18:08:44 -05:00
|
|
|
};
|
2020-02-18 10:08:18 -05:00
|
|
|
|
|
|
|
let result = tokio_util::run_basic(fut);
|
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("{}", err.to_string());
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|