2019-01-02 00:58:40 +00:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-07-26 21:54:22 +00:00
|
|
|
#[macro_use]
|
2018-09-24 23:51:37 +00:00
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
2018-07-26 21:54:22 +00:00
|
|
|
extern crate log;
|
2018-10-27 01:34:42 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate futures;
|
2019-01-09 17:59:46 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_json;
|
2019-04-06 22:13:06 +00:00
|
|
|
extern crate clap;
|
|
|
|
extern crate deno;
|
2019-06-09 13:08:20 +00:00
|
|
|
extern crate indexmap;
|
2019-04-22 01:26:56 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
extern crate nix;
|
2019-05-17 18:03:01 +00:00
|
|
|
extern crate rand;
|
2018-07-26 21:54:22 +00:00
|
|
|
|
2019-02-08 01:07:20 +00:00
|
|
|
mod ansi;
|
2019-01-09 17:59:46 +00:00
|
|
|
pub mod compiler;
|
2018-10-31 18:11:10 +00:00
|
|
|
pub mod deno_dir;
|
2019-06-04 13:03:56 +00:00
|
|
|
pub mod diagnostics;
|
2019-05-03 04:06:43 +00:00
|
|
|
mod dispatch_minimal;
|
2018-10-31 18:11:10 +00:00
|
|
|
pub mod errors;
|
|
|
|
pub mod flags;
|
2018-07-26 21:54:22 +00:00
|
|
|
mod fs;
|
2019-03-10 19:37:05 +00:00
|
|
|
mod global_timer;
|
2018-10-25 23:14:04 +00:00
|
|
|
mod http_body;
|
2018-10-10 17:35:10 +00:00
|
|
|
mod http_util;
|
2019-06-09 13:08:20 +00:00
|
|
|
mod import_map;
|
2018-12-07 04:05:36 +00:00
|
|
|
pub mod js_errors;
|
2018-10-31 18:11:10 +00:00
|
|
|
pub mod msg;
|
2018-11-03 00:09:10 +00:00
|
|
|
pub mod msg_util;
|
2018-10-04 00:48:02 +00:00
|
|
|
pub mod ops;
|
2018-10-31 18:11:10 +00:00
|
|
|
pub mod permissions;
|
2019-05-11 14:23:19 +00:00
|
|
|
mod progress;
|
2018-11-05 17:55:59 +00:00
|
|
|
mod repl;
|
2019-01-14 03:14:59 +00:00
|
|
|
pub mod resolve_addr;
|
2018-10-31 18:11:10 +00:00
|
|
|
pub mod resources;
|
2019-04-22 01:26:56 +00:00
|
|
|
mod signal;
|
2019-03-19 00:03:37 +00:00
|
|
|
mod startup_data;
|
2019-04-09 17:11:25 +00:00
|
|
|
pub mod state;
|
2018-09-18 18:53:16 +00:00
|
|
|
mod tokio_util;
|
2018-10-19 20:10:25 +00:00
|
|
|
mod tokio_write;
|
2018-10-31 18:11:10 +00:00
|
|
|
pub mod version;
|
2019-04-08 21:10:00 +00:00
|
|
|
pub mod worker;
|
2018-07-04 18:50:28 +00:00
|
|
|
|
2019-06-08 18:42:28 +00:00
|
|
|
use crate::compiler::bundle_async;
|
2019-03-14 23:17:52 +00:00
|
|
|
use crate::errors::RustOrJsError;
|
2019-05-11 14:23:19 +00:00
|
|
|
use crate::progress::Progress;
|
2019-04-09 17:11:25 +00:00
|
|
|
use crate::state::ThreadSafeState;
|
2019-04-08 21:10:00 +00:00
|
|
|
use crate::worker::Worker;
|
2019-04-21 15:34:18 +00:00
|
|
|
use deno::v8_set_flags;
|
2019-06-12 23:55:59 +00:00
|
|
|
use deno::ModuleSpecifier;
|
2019-04-21 15:34:18 +00:00
|
|
|
use flags::DenoFlags;
|
2019-04-29 23:43:06 +00:00
|
|
|
use flags::DenoSubcommand;
|
2019-06-08 11:10:12 +00:00
|
|
|
use futures::future;
|
2019-03-14 23:17:52 +00:00
|
|
|
use futures::lazy;
|
|
|
|
use futures::Future;
|
2019-01-29 15:37:27 +00:00
|
|
|
use log::{LevelFilter, Metadata, Record};
|
2018-07-26 01:27:27 +00:00
|
|
|
use std::env;
|
2018-07-26 21:37:09 +00:00
|
|
|
|
|
|
|
static LOGGER: Logger = Logger;
|
|
|
|
|
|
|
|
struct Logger;
|
|
|
|
|
|
|
|
impl log::Log for Logger {
|
2019-01-29 15:37:27 +00:00
|
|
|
fn enabled(&self, metadata: &Metadata) -> bool {
|
2018-08-07 17:33:36 +00:00
|
|
|
metadata.level() <= log::max_level()
|
2018-07-26 21:37:09 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 15:37:27 +00:00
|
|
|
fn log(&self, record: &Record) {
|
2018-07-26 21:37:09 +00:00
|
|
|
if self.enabled(record.metadata()) {
|
2019-05-25 16:23:47 +00:00
|
|
|
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());
|
2018-07-26 21:37:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn flush(&self) {}
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:17:52 +00:00
|
|
|
fn print_err_and_exit(err: RustOrJsError) {
|
2018-12-10 22:50:41 +00:00
|
|
|
eprintln!("{}", err.to_string());
|
2018-12-07 04:05:36 +00:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:17:52 +00:00
|
|
|
fn js_check<E>(r: Result<(), E>)
|
|
|
|
where
|
|
|
|
E: Into<RustOrJsError>,
|
|
|
|
{
|
|
|
|
if let Err(err) = r {
|
|
|
|
print_err_and_exit(err.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 10:03:04 +00:00
|
|
|
pub fn print_file_info(
|
|
|
|
worker: Worker,
|
2019-06-12 19:00:08 +00:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-06-08 10:03:04 +00:00
|
|
|
) -> impl Future<Item = Worker, Error = ()> {
|
2019-06-12 19:00:08 +00:00
|
|
|
state::fetch_module_meta_data_and_maybe_compile_async(
|
|
|
|
&worker.state,
|
|
|
|
module_specifier,
|
|
|
|
).and_then(move |out| {
|
|
|
|
println!("{} {}", ansi::bold("local:".to_string()), &(out.filename));
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
ansi::bold("type:".to_string()),
|
|
|
|
msg::enum_name_media_type(out.media_type)
|
|
|
|
);
|
|
|
|
|
|
|
|
if out.maybe_output_code_filename.is_some() {
|
2019-06-08 10:03:04 +00:00
|
|
|
println!(
|
|
|
|
"{} {}",
|
2019-06-12 19:00:08 +00:00
|
|
|
ansi::bold("compiled:".to_string()),
|
|
|
|
out.maybe_output_code_filename.as_ref().unwrap(),
|
2019-06-08 10:03:04 +00:00
|
|
|
);
|
2019-06-12 19:00:08 +00:00
|
|
|
}
|
2019-04-16 19:13:42 +00:00
|
|
|
|
2019-06-12 19:00:08 +00:00
|
|
|
if out.maybe_source_map_filename.is_some() {
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
ansi::bold("map:".to_string()),
|
|
|
|
out.maybe_source_map_filename.as_ref().unwrap()
|
|
|
|
);
|
|
|
|
}
|
2019-06-08 11:10:12 +00:00
|
|
|
|
2019-06-12 19:00:08 +00:00
|
|
|
if let Some(deps) =
|
|
|
|
worker.state.modules.lock().unwrap().deps(&out.module_name)
|
|
|
|
{
|
|
|
|
println!("{}{}", ansi::bold("deps:\n".to_string()), deps.name);
|
|
|
|
if let Some(ref depsdeps) = deps.deps {
|
|
|
|
for d in depsdeps {
|
|
|
|
println!("{}", d);
|
2019-06-08 10:03:04 +00:00
|
|
|
}
|
2019-04-19 15:18:46 +00:00
|
|
|
}
|
2019-06-12 19:00:08 +00:00
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"{} cannot retrieve full dependency graph",
|
|
|
|
ansi::bold("deps:".to_string()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(worker)
|
|
|
|
}).map_err(|err| println!("{}", err))
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 15:34:18 +00:00
|
|
|
fn create_worker_and_state(
|
|
|
|
flags: DenoFlags,
|
|
|
|
argv: Vec<String>,
|
|
|
|
) -> (Worker, ThreadSafeState) {
|
2019-05-11 14:23:19 +00:00
|
|
|
let progress = Progress::new();
|
|
|
|
progress.set_callback(|done, completed, total, msg| {
|
2019-05-17 16:35:29 +00:00
|
|
|
if !done {
|
2019-05-11 14:23:19 +00:00
|
|
|
eprint!("\r[{}/{}] {}", completed, total, msg);
|
|
|
|
eprint!("\x1B[K"); // Clear to end of line.
|
2019-05-17 16:35:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// print empty line only if progress bar was used
|
|
|
|
if done && total > 0 {
|
|
|
|
eprintln!();
|
2019-05-11 14:23:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
let state = ThreadSafeState::new(flags, argv, ops::op_selector_std, progress);
|
2019-04-21 15:34:18 +00:00
|
|
|
let worker = Worker::new(
|
|
|
|
"main".to_string(),
|
|
|
|
startup_data::deno_isolate_init(),
|
|
|
|
state.clone(),
|
|
|
|
);
|
2019-02-07 21:19:50 +00:00
|
|
|
|
2019-04-21 15:34:18 +00:00
|
|
|
(worker, state)
|
|
|
|
}
|
2018-11-06 06:41:39 +00:00
|
|
|
|
2019-04-21 15:34:18 +00:00
|
|
|
fn types_command() {
|
2019-04-25 18:27:30 +00:00
|
|
|
let content = include_str!(concat!(
|
2019-04-21 15:34:18 +00:00
|
|
|
env!("GN_OUT_DIR"),
|
|
|
|
"/gen/cli/lib/lib.deno_runtime.d.ts"
|
|
|
|
));
|
|
|
|
println!("{}", content);
|
|
|
|
}
|
2018-11-06 06:41:39 +00:00
|
|
|
|
2019-04-25 17:47:33 +00:00
|
|
|
fn fetch_or_info_command(
|
2019-04-21 15:34:18 +00:00
|
|
|
flags: DenoFlags,
|
|
|
|
argv: Vec<String>,
|
|
|
|
print_info: bool,
|
|
|
|
) {
|
|
|
|
let (mut worker, state) = create_worker_and_state(flags, argv);
|
2019-02-01 23:29:00 +00:00
|
|
|
|
2019-04-21 15:34:18 +00:00
|
|
|
let main_module = state.main_module().unwrap();
|
|
|
|
let main_future = lazy(move || {
|
|
|
|
// Setup runtime.
|
|
|
|
js_check(worker.execute("denoMain()"));
|
|
|
|
debug!("main_module {}", main_module);
|
2019-01-15 17:19:58 +00:00
|
|
|
|
2019-04-21 15:34:18 +00:00
|
|
|
worker
|
2019-06-12 19:00:08 +00:00
|
|
|
.execute_mod_async(&main_module, true)
|
2019-06-08 11:10:12 +00:00
|
|
|
.map_err(print_err_and_exit)
|
2019-06-05 20:35:38 +00:00
|
|
|
.and_then(move |()| {
|
2019-04-21 15:34:18 +00:00
|
|
|
if print_info {
|
2019-06-08 10:03:04 +00:00
|
|
|
future::Either::A(print_file_info(worker, &main_module))
|
|
|
|
} else {
|
|
|
|
future::Either::B(future::ok(worker))
|
2019-04-21 15:34:18 +00:00
|
|
|
}
|
2019-06-08 10:03:04 +00:00
|
|
|
}).and_then(|worker| {
|
2019-04-21 15:34:18 +00:00
|
|
|
worker.then(|result| {
|
|
|
|
js_check(result);
|
|
|
|
Ok(())
|
|
|
|
})
|
2019-06-08 11:10:12 +00:00
|
|
|
})
|
2019-04-21 15:34:18 +00:00
|
|
|
});
|
|
|
|
tokio_util::run(main_future);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eval_command(flags: DenoFlags, argv: Vec<String>) {
|
|
|
|
let (mut worker, state) = create_worker_and_state(flags, argv);
|
|
|
|
// Wrap provided script in async function so asynchronous methods
|
|
|
|
// work. This is required until top-level await is not supported.
|
|
|
|
let js_source = format!(
|
|
|
|
"async function _topLevelWrapper(){{
|
|
|
|
{}
|
|
|
|
}}
|
|
|
|
_topLevelWrapper();
|
|
|
|
",
|
|
|
|
&state.argv[1]
|
2019-04-09 17:11:25 +00:00
|
|
|
);
|
2019-01-09 17:59:46 +00:00
|
|
|
|
2019-04-21 15:34:18 +00:00
|
|
|
let main_future = lazy(move || {
|
|
|
|
js_check(worker.execute("denoMain()"));
|
|
|
|
// ATM imports in `deno eval` are not allowed
|
|
|
|
// TODO Support ES modules once Worker supports evaluating anonymous modules.
|
|
|
|
js_check(worker.execute(&js_source));
|
|
|
|
worker.then(|result| {
|
|
|
|
js_check(result);
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
});
|
|
|
|
tokio_util::run(main_future);
|
|
|
|
}
|
|
|
|
|
2019-05-03 20:24:09 +00:00
|
|
|
fn xeval_command(flags: DenoFlags, argv: Vec<String>) {
|
|
|
|
let xeval_replvar = flags.xeval_replvar.clone().unwrap();
|
|
|
|
let (mut worker, state) = create_worker_and_state(flags, argv);
|
|
|
|
let xeval_source = format!(
|
|
|
|
"window._xevalWrapper = async function ({}){{
|
|
|
|
{}
|
|
|
|
}}",
|
|
|
|
&xeval_replvar, &state.argv[1]
|
|
|
|
);
|
|
|
|
|
|
|
|
let main_future = lazy(move || {
|
|
|
|
// Setup runtime.
|
|
|
|
js_check(worker.execute(&xeval_source));
|
|
|
|
js_check(worker.execute("denoMain()"));
|
|
|
|
worker
|
|
|
|
.then(|result| {
|
|
|
|
js_check(result);
|
|
|
|
Ok(())
|
|
|
|
}).map_err(|(err, _worker): (RustOrJsError, Worker)| {
|
|
|
|
print_err_and_exit(err)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
tokio_util::run(main_future);
|
|
|
|
}
|
|
|
|
|
2019-06-08 18:42:28 +00:00
|
|
|
fn bundle_command(flags: DenoFlags, argv: Vec<String>) {
|
|
|
|
let (mut _worker, state) = create_worker_and_state(flags, argv);
|
|
|
|
|
|
|
|
let main_module = state.main_module().unwrap();
|
|
|
|
assert!(state.argv.len() >= 3);
|
|
|
|
let out_file = state.argv[2].clone();
|
|
|
|
debug!(">>>>> bundle_async START");
|
2019-06-12 19:00:08 +00:00
|
|
|
let bundle_future = bundle_async(state, main_module.to_string(), out_file)
|
2019-06-08 18:42:28 +00:00
|
|
|
.map_err(|e| {
|
|
|
|
debug!("diagnostics returned, exiting!");
|
|
|
|
eprintln!("\n{}", e.to_string());
|
|
|
|
std::process::exit(1);
|
|
|
|
}).and_then(move |_| {
|
|
|
|
debug!(">>>>> bundle_async END");
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
tokio_util::run(bundle_future);
|
|
|
|
}
|
|
|
|
|
2019-04-21 15:34:18 +00:00
|
|
|
fn run_repl(flags: DenoFlags, argv: Vec<String>) {
|
|
|
|
let (mut worker, _state) = create_worker_and_state(flags, argv);
|
|
|
|
|
|
|
|
// REPL situation.
|
|
|
|
let main_future = lazy(move || {
|
|
|
|
// Setup runtime.
|
|
|
|
js_check(worker.execute("denoMain()"));
|
|
|
|
worker
|
|
|
|
.then(|result| {
|
2019-04-16 19:13:42 +00:00
|
|
|
js_check(result);
|
|
|
|
Ok(())
|
2019-04-21 15:34:18 +00:00
|
|
|
}).map_err(|(err, _worker): (RustOrJsError, Worker)| {
|
|
|
|
print_err_and_exit(err)
|
2019-04-16 19:13:42 +00:00
|
|
|
})
|
2019-04-21 15:34:18 +00:00
|
|
|
});
|
|
|
|
tokio_util::run(main_future);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_script(flags: DenoFlags, argv: Vec<String>) {
|
|
|
|
let (mut worker, state) = create_worker_and_state(flags, argv);
|
|
|
|
|
|
|
|
let main_module = state.main_module().unwrap();
|
|
|
|
// Normal situation of executing a module.
|
|
|
|
let main_future = lazy(move || {
|
|
|
|
// Setup runtime.
|
|
|
|
js_check(worker.execute("denoMain()"));
|
|
|
|
debug!("main_module {}", main_module);
|
|
|
|
|
|
|
|
worker
|
2019-06-12 19:00:08 +00:00
|
|
|
.execute_mod_async(&main_module, false)
|
2019-06-05 20:35:38 +00:00
|
|
|
.and_then(move |()| {
|
2019-04-21 15:34:18 +00:00
|
|
|
worker.then(|result| {
|
2019-04-16 19:13:42 +00:00
|
|
|
js_check(result);
|
|
|
|
Ok(())
|
|
|
|
})
|
2019-06-05 20:35:38 +00:00
|
|
|
}).map_err(print_err_and_exit)
|
2019-04-21 15:34:18 +00:00
|
|
|
});
|
|
|
|
tokio_util::run(main_future);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
#[cfg(windows)]
|
|
|
|
ansi_term::enable_ansi_support().ok(); // For Windows 10
|
|
|
|
|
|
|
|
log::set_logger(&LOGGER).unwrap();
|
|
|
|
let args: Vec<String> = env::args().collect();
|
2019-04-29 23:43:06 +00:00
|
|
|
let (flags, subcommand, argv) = flags::flags_from_vec(args);
|
2019-04-21 15:34:18 +00:00
|
|
|
|
2019-05-03 00:37:02 +00:00
|
|
|
if let Some(ref v8_flags) = flags.v8_flags {
|
|
|
|
v8_set_flags(v8_flags.clone());
|
|
|
|
}
|
2019-04-21 15:34:18 +00:00
|
|
|
|
|
|
|
log::set_max_level(if flags.log_debug {
|
|
|
|
LevelFilter::Debug
|
|
|
|
} else {
|
|
|
|
LevelFilter::Warn
|
|
|
|
});
|
|
|
|
|
2019-04-29 23:43:06 +00:00
|
|
|
match subcommand {
|
2019-06-08 18:42:28 +00:00
|
|
|
DenoSubcommand::Bundle => bundle_command(flags, argv),
|
2019-04-29 23:43:06 +00:00
|
|
|
DenoSubcommand::Eval => eval_command(flags, argv),
|
|
|
|
DenoSubcommand::Fetch => fetch_or_info_command(flags, argv, false),
|
|
|
|
DenoSubcommand::Info => fetch_or_info_command(flags, argv, true),
|
2019-06-14 17:05:06 +00:00
|
|
|
DenoSubcommand::Install => run_script(flags, argv),
|
2019-04-29 23:43:06 +00:00
|
|
|
DenoSubcommand::Repl => run_repl(flags, argv),
|
|
|
|
DenoSubcommand::Run => run_script(flags, argv),
|
|
|
|
DenoSubcommand::Types => types_command(),
|
2019-05-03 22:48:50 +00:00
|
|
|
DenoSubcommand::Version => run_repl(flags, argv),
|
2019-05-03 20:24:09 +00:00
|
|
|
DenoSubcommand::Xeval => xeval_command(flags, argv),
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
2018-06-15 23:43:23 +00:00
|
|
|
}
|