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;
|
|
|
|
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 deno_error;
|
|
|
|
pub mod diagnostics;
|
|
|
|
mod disk_cache;
|
|
|
|
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;
|
2019-11-04 10:38:52 -05:00
|
|
|
mod global_state;
|
2019-09-16 21:05:14 -04:00
|
|
|
mod global_timer;
|
|
|
|
mod http_util;
|
|
|
|
mod import_map;
|
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;
|
|
|
|
pub mod ops;
|
|
|
|
pub mod permissions;
|
|
|
|
mod progress;
|
|
|
|
mod repl;
|
|
|
|
pub mod resolve_addr;
|
|
|
|
mod shell;
|
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;
|
|
|
|
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-01-29 12:54:23 -05:00
|
|
|
use crate::compilers::TargetLib;
|
2019-09-25 10:46:58 -04:00
|
|
|
use crate::deno_error::js_check;
|
2019-11-26 23:25:14 -05:00
|
|
|
use crate::deno_error::{print_err_and_exit, print_msg_and_exit};
|
2020-02-11 06:01:56 -05:00
|
|
|
use crate::fs as deno_fs;
|
2020-02-06 23:05:02 -05:00
|
|
|
use crate::global_state::GlobalState;
|
2019-11-14 12:10:25 -05:00
|
|
|
use crate::ops::io::get_stdio;
|
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::DenoFlags;
|
|
|
|
use flags::DenoSubcommand;
|
|
|
|
use log::Level;
|
|
|
|
use log::Metadata;
|
|
|
|
use log::Record;
|
|
|
|
use std::env;
|
2020-02-11 06:01:56 -05:00
|
|
|
use std::fs as std_fs;
|
2020-02-11 04:29:36 -05:00
|
|
|
use std::path::PathBuf;
|
2019-09-16 21:05:14 -04:00
|
|
|
|
|
|
|
static LOGGER: Logger = Logger;
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("{} RS - {} - {}", record.level(), target, record.args());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn flush(&self) {}
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:05:02 -05:00
|
|
|
fn create_global_state(flags: DenoFlags) -> GlobalState {
|
|
|
|
GlobalState::new(flags)
|
2019-09-25 10:46:58 -04:00
|
|
|
.map_err(deno_error::print_err_and_exit)
|
2020-02-04 14:24:33 -05:00
|
|
|
.unwrap()
|
|
|
|
}
|
2019-09-16 21:05:14 -04:00
|
|
|
|
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,
|
|
|
|
) -> MainWorker {
|
2020-02-08 14:34:31 -05:00
|
|
|
let state = State::new(global_state, None, main_module)
|
2020-02-04 14:24:33 -05:00
|
|
|
.map_err(deno_error::print_err_and_exit)
|
|
|
|
.unwrap();
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2019-11-14 12:10:25 -05:00
|
|
|
let state_ = state.clone();
|
|
|
|
{
|
2020-02-08 14:34:31 -05:00
|
|
|
let mut state = state_.borrow_mut();
|
2019-11-14 12:10:25 -05:00
|
|
|
let (stdin, stdout, stderr) = get_stdio();
|
2020-02-08 14:34:31 -05:00
|
|
|
state.resource_table.add("stdin", Box::new(stdin));
|
|
|
|
state.resource_table.add("stdout", Box::new(stdout));
|
|
|
|
state.resource_table.add("stderr", Box::new(stderr));
|
2019-11-14 12:10:25 -05:00
|
|
|
}
|
|
|
|
|
2020-02-05 02:40:38 -05:00
|
|
|
MainWorker::new("main".to_string(), startup_data::deno_isolate_init(), state)
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn types_command() {
|
2020-02-07 03:30:05 -05:00
|
|
|
let types = format!(
|
2020-01-29 12:54:23 -05:00
|
|
|
"{}\n{}\n{}",
|
|
|
|
crate::js::DENO_NS_LIB,
|
|
|
|
crate::js::SHARED_GLOBALS_LIB,
|
|
|
|
crate::js::WINDOW_LIB
|
|
|
|
);
|
2020-02-07 03:30:05 -05:00
|
|
|
use std::io::Write;
|
|
|
|
let _r = std::io::stdout().write_all(types.as_bytes());
|
|
|
|
// TODO(ry) Only ignore SIGPIPE. Currently ignoring all errors.
|
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()),
|
|
|
|
state.dir.deps_cache.location
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{} {:?}",
|
|
|
|
colors::bold("TypeScript compiler cache:".to_string()),
|
|
|
|
state.dir.gen_cache.location
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-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-03 18:08:44 -05:00
|
|
|
let maybe_source_file = global_state
|
2019-09-16 21:05:14 -04:00
|
|
|
.file_fetcher
|
2019-11-25 09:33:23 -05:00
|
|
|
.fetch_source_file_async(&module_specifier, None)
|
2019-11-16 23:25:12 -05:00
|
|
|
.await;
|
|
|
|
if let Err(err) = maybe_source_file {
|
|
|
|
println!("{}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let out = maybe_source_file.unwrap();
|
|
|
|
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();
|
|
|
|
let maybe_compiled = 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)
|
2019-11-16 23:25:12 -05:00
|
|
|
.await;
|
|
|
|
if let Err(e) = maybe_compiled {
|
|
|
|
debug!("compiler error exiting!");
|
|
|
|
eprintln!("\n{}", e.to_string());
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
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()),
|
|
|
|
);
|
|
|
|
}
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-02-04 14:24:33 -05:00
|
|
|
async fn info_command(flags: DenoFlags, file: Option<String>) {
|
|
|
|
let global_state = create_global_state(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() {
|
|
|
|
return print_cache_info(&global_state);
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
// Setup runtime.
|
2020-02-04 14:24:33 -05:00
|
|
|
let main_module = ModuleSpecifier::resolve_url_or_path(&file.unwrap())
|
|
|
|
.expect("Bad specifier");
|
|
|
|
let mut worker = create_main_worker(global_state, main_module.clone());
|
|
|
|
|
|
|
|
// TODO(bartlomieju): not needed?
|
2020-01-21 09:53:29 -05:00
|
|
|
js_check(worker.execute("bootstrapMainRuntime()"));
|
2019-11-16 19:17:47 -05:00
|
|
|
|
2020-01-30 11:28:51 -05:00
|
|
|
let main_result = worker.execute_mod_async(&main_module, None, true).await;
|
|
|
|
if let Err(e) = main_result {
|
|
|
|
print_err_and_exit(e);
|
|
|
|
}
|
2020-02-03 18:08:44 -05:00
|
|
|
print_file_info(&worker, main_module.clone()).await;
|
|
|
|
let result = (&mut *worker).await;
|
2020-01-30 11:28:51 -05:00
|
|
|
js_check(result);
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-01-30 18:42:39 -05:00
|
|
|
async fn install_command(
|
|
|
|
flags: DenoFlags,
|
2020-02-11 04:29:36 -05:00
|
|
|
dir: 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-01-30 18:42:39 -05:00
|
|
|
) {
|
|
|
|
// Firstly fetch and compile module, this
|
|
|
|
// ensures the module exists.
|
|
|
|
let mut fetch_flags = flags.clone();
|
|
|
|
fetch_flags.reload = true;
|
2020-02-04 14:24:33 -05:00
|
|
|
fetch_command(fetch_flags, vec![module_url.to_string()]).await;
|
2020-01-30 18:42:39 -05:00
|
|
|
|
|
|
|
let install_result =
|
2020-02-08 03:49:55 -05:00
|
|
|
installer::install(flags, dir, &exe_name, &module_url, args, force);
|
2020-01-30 18:42:39 -05:00
|
|
|
if let Err(e) = install_result {
|
|
|
|
print_msg_and_exit(&e.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 14:24:33 -05:00
|
|
|
async fn fetch_command(flags: DenoFlags, files: Vec<String>) {
|
|
|
|
let main_module =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("./__$deno$fetch.ts").unwrap();
|
|
|
|
let global_state = create_global_state(flags);
|
|
|
|
let mut worker =
|
|
|
|
create_main_worker(global_state.clone(), main_module.clone());
|
2019-09-16 21:05:14 -04:00
|
|
|
|
2020-02-04 14:24:33 -05:00
|
|
|
// TODO(bartlomieju): not needed?
|
2020-01-21 09:53:29 -05:00
|
|
|
js_check(worker.execute("bootstrapMainRuntime()"));
|
2019-11-16 19:17:47 -05:00
|
|
|
|
2020-02-04 14:24:33 -05:00
|
|
|
for file in files {
|
|
|
|
let specifier = ModuleSpecifier::resolve_url_or_path(&file).unwrap();
|
|
|
|
let result = worker.execute_mod_async(&specifier, None, true).await;
|
|
|
|
js_check(result);
|
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();
|
|
|
|
if let Err(e) = g.write() {
|
|
|
|
print_err_and_exit(ErrBox::from(e));
|
2020-01-26 13:43:59 -05:00
|
|
|
}
|
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
|
|
|
}
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-02-04 14:24:33 -05:00
|
|
|
async fn eval_command(flags: DenoFlags, code: String) {
|
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-04 14:24:33 -05:00
|
|
|
let global_state = create_global_state(flags);
|
|
|
|
let mut worker = create_main_worker(global_state, main_module.clone());
|
2019-09-16 21:05:14 -04:00
|
|
|
|
2020-01-21 09:53:29 -05:00
|
|
|
js_check(worker.execute("bootstrapMainRuntime()"));
|
2019-11-16 19:17:47 -05:00
|
|
|
debug!("main_module {}", &main_module);
|
2019-10-19 17:19:19 -04:00
|
|
|
|
2020-01-30 11:28:51 -05:00
|
|
|
let exec_result = worker
|
2020-02-04 14:24:33 -05:00
|
|
|
.execute_mod_async(&main_module, Some(code), false)
|
2020-01-30 11:28:51 -05:00
|
|
|
.await;
|
|
|
|
if let Err(e) = exec_result {
|
|
|
|
print_err_and_exit(e);
|
|
|
|
}
|
|
|
|
js_check(worker.execute("window.dispatchEvent(new Event('load'))"));
|
2020-02-03 18:08:44 -05:00
|
|
|
let result = (&mut *worker).await;
|
2020-01-30 11:28:51 -05:00
|
|
|
js_check(result);
|
2020-02-03 18:08:44 -05:00
|
|
|
js_check(worker.execute("window.dispatchEvent(new Event('unload'))"));
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-02-04 14:24:33 -05:00
|
|
|
async fn bundle_command(
|
|
|
|
flags: DenoFlags,
|
|
|
|
source_file: String,
|
2020-02-11 04:29:36 -05:00
|
|
|
out_file: Option<PathBuf>,
|
2020-02-04 14:24:33 -05:00
|
|
|
) {
|
2019-09-16 21:05:14 -04:00
|
|
|
debug!(">>>>> bundle_async START");
|
2020-02-04 14:24:33 -05:00
|
|
|
let source_file_specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path(&source_file).expect("Bad specifier");
|
|
|
|
let global_state = create_global_state(flags);
|
|
|
|
let mut worker =
|
|
|
|
create_main_worker(global_state.clone(), source_file_specifier.clone());
|
|
|
|
|
2019-10-14 17:46:27 -04:00
|
|
|
// NOTE: we need to poll `worker` otherwise TS compiler worker won't run properly
|
2020-02-03 18:08:44 -05:00
|
|
|
let result = (&mut *worker).await;
|
2020-01-30 11:28:51 -05:00
|
|
|
js_check(result);
|
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-04 14:24:33 -05:00
|
|
|
.bundle_async(
|
|
|
|
global_state.clone(),
|
|
|
|
source_file_specifier.to_string(),
|
|
|
|
out_file,
|
|
|
|
)
|
2020-01-30 11:28:51 -05:00
|
|
|
.await;
|
|
|
|
if let Err(err) = bundle_result {
|
|
|
|
debug!("diagnostics returned, exiting!");
|
|
|
|
eprintln!("");
|
|
|
|
print_err_and_exit(err);
|
|
|
|
}
|
|
|
|
debug!(">>>>> bundle_async END");
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-01-30 11:28:51 -05:00
|
|
|
async fn run_repl(flags: DenoFlags) {
|
2020-02-04 14:24:33 -05:00
|
|
|
let main_module =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("./__$deno$repl.ts").unwrap();
|
|
|
|
let global_state = create_global_state(flags);
|
|
|
|
let mut worker = create_main_worker(global_state, main_module);
|
2020-01-21 09:53:29 -05:00
|
|
|
js_check(worker.execute("bootstrapMainRuntime()"));
|
2020-01-30 11:28:51 -05:00
|
|
|
loop {
|
2020-02-03 18:08:44 -05:00
|
|
|
let result = (&mut *worker).await;
|
2020-01-30 11:28:51 -05:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("{}", err.to_string());
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
2020-01-30 11:28:51 -05:00
|
|
|
}
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|
|
|
|
|
2020-02-11 06:01:56 -05:00
|
|
|
async fn run_command(flags: DenoFlags, script: String) {
|
|
|
|
let global_state = create_global_state(flags.clone());
|
|
|
|
let result = run_script(global_state.clone(), script).await;
|
|
|
|
if let Err(err) = result {
|
2020-01-30 11:28:51 -05:00
|
|
|
print_err_and_exit(err);
|
|
|
|
}
|
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();
|
|
|
|
if let Err(e) = g.write() {
|
|
|
|
print_err_and_exit(ErrBox::from(e));
|
2019-11-16 19:17:47 -05:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_script(
|
|
|
|
global_state: GlobalState,
|
|
|
|
script: String,
|
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
let main_module = ModuleSpecifier::resolve_url_or_path(&script).unwrap();
|
|
|
|
let mut worker =
|
|
|
|
create_main_worker(global_state.clone(), main_module.clone());
|
|
|
|
// Setup runtime.
|
|
|
|
worker.execute("bootstrapMainRuntime()")?;
|
|
|
|
debug!("main_module {}", main_module);
|
|
|
|
worker.execute_mod_async(&main_module, None, false).await?;
|
|
|
|
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-11 04:29:36 -05:00
|
|
|
async fn fmt_command(files: Option<Vec<PathBuf>>, check: bool) {
|
2020-02-09 05:19:05 -05:00
|
|
|
if let Err(err) = fmt::format_files(files, check) {
|
|
|
|
print_err_and_exit(err);
|
|
|
|
}
|
2020-01-29 21:16:48 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 06:01:56 -05:00
|
|
|
async fn test_command(
|
|
|
|
flags: DenoFlags,
|
|
|
|
include: Option<Vec<String>>,
|
|
|
|
fail_fast: bool,
|
|
|
|
_quiet: bool,
|
|
|
|
allow_none: bool,
|
|
|
|
) {
|
|
|
|
let global_state = create_global_state(flags.clone());
|
|
|
|
let cwd = std::env::current_dir().expect("No current directory");
|
|
|
|
let include = include.unwrap_or_else(|| vec![".".to_string()]);
|
|
|
|
let res = test_runner::prepare_test_modules_urls(include, cwd.clone());
|
|
|
|
|
|
|
|
let test_modules = match res {
|
|
|
|
Ok(modules) => modules,
|
|
|
|
Err(e) => return print_err_and_exit(e),
|
|
|
|
};
|
|
|
|
if test_modules.is_empty() {
|
|
|
|
println!("No matching test modules found");
|
|
|
|
if !allow_none {
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let test_file = test_runner::render_test_file(test_modules, fail_fast);
|
|
|
|
let test_file_path = cwd.join(".deno.test.ts");
|
|
|
|
deno_fs::write_file(&test_file_path, test_file.as_bytes(), 0o666)
|
|
|
|
.expect("Can't write test file");
|
|
|
|
|
|
|
|
let mut flags = flags.clone();
|
|
|
|
flags
|
|
|
|
.argv
|
|
|
|
.push(test_file_path.to_string_lossy().to_string());
|
|
|
|
|
|
|
|
let result =
|
|
|
|
run_script(global_state, test_file_path.to_string_lossy().to_string())
|
|
|
|
.await;
|
|
|
|
std_fs::remove_file(&test_file_path).expect("Failed to remove temp file");
|
|
|
|
if let Err(err) = result {
|
|
|
|
print_err_and_exit(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
pub fn main() {
|
2019-09-16 21:05:14 -04:00
|
|
|
#[cfg(windows)]
|
|
|
|
ansi_term::enable_ansi_support().ok(); // For Windows 10
|
|
|
|
|
|
|
|
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,
|
|
|
|
None => Level::Warn,
|
|
|
|
};
|
|
|
|
log::set_max_level(log_level.to_level_filter());
|
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
let fut = async move {
|
|
|
|
match flags.clone().subcommand {
|
2020-02-04 14:24:33 -05:00
|
|
|
DenoSubcommand::Bundle {
|
|
|
|
source_file,
|
|
|
|
out_file,
|
|
|
|
} => bundle_command(flags, source_file, out_file).await,
|
|
|
|
DenoSubcommand::Completions { buf } => {
|
|
|
|
print!("{}", std::str::from_utf8(&buf).unwrap());
|
|
|
|
}
|
|
|
|
DenoSubcommand::Eval { code } => eval_command(flags, code).await,
|
|
|
|
DenoSubcommand::Fetch { files } => fetch_command(flags, files).await,
|
2020-02-03 18:08:44 -05:00
|
|
|
DenoSubcommand::Format { check, files } => {
|
|
|
|
fmt_command(files, check).await
|
|
|
|
}
|
2020-02-04 14:24:33 -05:00
|
|
|
DenoSubcommand::Info { file } => info_command(flags, file).await,
|
2020-02-03 18:08:44 -05:00
|
|
|
DenoSubcommand::Install {
|
|
|
|
dir,
|
|
|
|
exe_name,
|
|
|
|
module_url,
|
|
|
|
args,
|
2020-02-08 03:49:55 -05:00
|
|
|
force,
|
|
|
|
} => install_command(flags, dir, exe_name, module_url, args, force).await,
|
2020-02-03 18:08:44 -05:00
|
|
|
DenoSubcommand::Repl => run_repl(flags).await,
|
2020-02-11 06:01:56 -05:00
|
|
|
DenoSubcommand::Run { script } => run_command(flags, script).await,
|
|
|
|
DenoSubcommand::Test {
|
|
|
|
quiet,
|
|
|
|
fail_fast,
|
|
|
|
include,
|
|
|
|
allow_none,
|
|
|
|
} => test_command(flags, include, fail_fast, quiet, allow_none).await,
|
2020-02-03 18:08:44 -05:00
|
|
|
DenoSubcommand::Types => types_command(),
|
|
|
|
_ => panic!("bad subcommand"),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
tokio_util::run_basic(fut);
|
2019-09-16 21:05:14 -04:00
|
|
|
}
|