2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-07-26 17:54:22 -04:00
|
|
|
#[macro_use]
|
2018-09-24 19:51:37 -04:00
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
2018-07-26 17:54:22 -04:00
|
|
|
extern crate log;
|
2018-10-26 21:34:42 -04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate futures;
|
2019-01-09 12:59:46 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_json;
|
2019-04-06 18:13:06 -04:00
|
|
|
extern crate clap;
|
|
|
|
extern crate deno;
|
2019-04-21 21:26:56 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
extern crate nix;
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2019-02-07 20:07:20 -05:00
|
|
|
mod ansi;
|
2019-01-09 12:59:46 -05:00
|
|
|
pub mod compiler;
|
2018-10-31 14:11:10 -04:00
|
|
|
pub mod deno_dir;
|
|
|
|
pub mod errors;
|
|
|
|
pub mod flags;
|
2018-07-26 17:54:22 -04:00
|
|
|
mod fs;
|
2019-03-10 15:37:05 -04:00
|
|
|
mod global_timer;
|
2018-10-25 19:14:04 -04:00
|
|
|
mod http_body;
|
2018-10-10 13:35:10 -04:00
|
|
|
mod http_util;
|
2018-12-06 23:05:36 -05:00
|
|
|
pub mod js_errors;
|
2018-10-31 14:11:10 -04:00
|
|
|
pub mod msg;
|
2018-11-02 20:09:10 -04:00
|
|
|
pub mod msg_util;
|
2018-10-03 20:48:02 -04:00
|
|
|
pub mod ops;
|
2018-10-31 14:11:10 -04:00
|
|
|
pub mod permissions;
|
2018-11-05 12:55:59 -05:00
|
|
|
mod repl;
|
2019-01-13 22:14:59 -05:00
|
|
|
pub mod resolve_addr;
|
2018-10-31 14:11:10 -04:00
|
|
|
pub mod resources;
|
2019-04-21 21:26:56 -04:00
|
|
|
mod signal;
|
2019-03-18 20:03:37 -04:00
|
|
|
mod startup_data;
|
2019-04-09 13:11:25 -04:00
|
|
|
pub mod state;
|
2018-09-18 14:53:16 -04:00
|
|
|
mod tokio_util;
|
2018-10-19 16:10:25 -04:00
|
|
|
mod tokio_write;
|
2018-10-31 14:11:10 -04:00
|
|
|
pub mod version;
|
2019-04-08 17:10:00 -04:00
|
|
|
pub mod worker;
|
2018-07-04 14:50:28 -04:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::errors::RustOrJsError;
|
2019-04-09 13:11:25 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
2019-04-16 15:13:42 -04:00
|
|
|
use crate::worker::root_specifier_to_url;
|
2019-04-08 17:10:00 -04:00
|
|
|
use crate::worker::Worker;
|
2019-04-21 11:34:18 -04:00
|
|
|
use deno::v8_set_flags;
|
|
|
|
use flags::DenoFlags;
|
2019-03-14 19:17:52 -04:00
|
|
|
use futures::lazy;
|
|
|
|
use futures::Future;
|
2019-01-29 10:37:27 -05:00
|
|
|
use log::{LevelFilter, Metadata, Record};
|
2018-07-25 21:27:27 -04:00
|
|
|
use std::env;
|
2019-04-21 11:34:18 -04:00
|
|
|
use std::path::Path;
|
2018-07-26 17:37:09 -04:00
|
|
|
|
|
|
|
static LOGGER: Logger = Logger;
|
|
|
|
|
|
|
|
struct Logger;
|
|
|
|
|
|
|
|
impl log::Log for Logger {
|
2019-01-29 10:37:27 -05:00
|
|
|
fn enabled(&self, metadata: &Metadata) -> bool {
|
2018-08-07 13:33:36 -04:00
|
|
|
metadata.level() <= log::max_level()
|
2018-07-26 17:37:09 -04:00
|
|
|
}
|
|
|
|
|
2019-01-29 10:37:27 -05:00
|
|
|
fn log(&self, record: &Record) {
|
2018-07-26 17:37:09 -04:00
|
|
|
if self.enabled(record.metadata()) {
|
2018-08-17 16:34:30 -04:00
|
|
|
println!("{} RS - {}", record.level(), record.args());
|
2018-07-26 17:37:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn flush(&self) {}
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
fn print_err_and_exit(err: RustOrJsError) {
|
2018-12-10 17:50:41 -05:00
|
|
|
eprintln!("{}", err.to_string());
|
2018-12-06 23:05:36 -05:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
fn js_check<E>(r: Result<(), E>)
|
|
|
|
where
|
|
|
|
E: Into<RustOrJsError>,
|
|
|
|
{
|
|
|
|
if let Err(err) = r {
|
|
|
|
print_err_and_exit(err.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
// TODO(ry) Move this to main.rs
|
|
|
|
pub fn print_file_info(worker: &Worker, url: &str) {
|
|
|
|
let maybe_out =
|
|
|
|
worker::fetch_module_meta_data_and_maybe_compile(&worker.state, url, ".");
|
|
|
|
if let Err(err) = maybe_out {
|
|
|
|
println!("{}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let out = maybe_out.unwrap();
|
|
|
|
|
|
|
|
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() {
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
ansi::bold("compiled:".to_string()),
|
|
|
|
out.maybe_output_code_filename.as_ref().unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.maybe_source_map_filename.is_some() {
|
|
|
|
println!(
|
|
|
|
"{} {}",
|
|
|
|
ansi::bold("map:".to_string()),
|
|
|
|
out.maybe_source_map_filename.as_ref().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-19 11:18:46 -04:00
|
|
|
if let Some(deps) = worker.modules.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-04-16 15:13:42 -04:00
|
|
|
}
|
2019-04-19 11:18:46 -04:00
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"{} cannot retrieve full dependency graph",
|
|
|
|
ansi::bold("deps:".to_string()),
|
|
|
|
);
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
fn create_worker_and_state(
|
|
|
|
flags: DenoFlags,
|
|
|
|
argv: Vec<String>,
|
|
|
|
) -> (Worker, ThreadSafeState) {
|
|
|
|
let state = ThreadSafeState::new(flags, argv, ops::op_selector_std);
|
|
|
|
let worker = Worker::new(
|
|
|
|
"main".to_string(),
|
|
|
|
startup_data::deno_isolate_init(),
|
|
|
|
state.clone(),
|
|
|
|
);
|
2019-02-07 16:19:50 -05:00
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
(worker, state)
|
|
|
|
}
|
2018-11-06 01:41:39 -05:00
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
fn types_command() {
|
|
|
|
let p = Path::new(concat!(
|
|
|
|
env!("GN_OUT_DIR"),
|
|
|
|
"/gen/cli/lib/lib.deno_runtime.d.ts"
|
|
|
|
));
|
|
|
|
let content_bytes = std::fs::read(p).unwrap();
|
|
|
|
let content = std::str::from_utf8(&content_bytes[..]).unwrap();
|
|
|
|
println!("{}", content);
|
|
|
|
}
|
2018-11-06 01:41:39 -05:00
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
fn prefetch_or_info_command(
|
|
|
|
flags: DenoFlags,
|
|
|
|
argv: Vec<String>,
|
|
|
|
print_info: bool,
|
|
|
|
) {
|
|
|
|
let (mut worker, state) = create_worker_and_state(flags, argv);
|
2019-02-01 18:29:00 -05:00
|
|
|
|
2019-04-21 11:34:18 -04: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 12:19:58 -05:00
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
let main_url = root_specifier_to_url(&main_module).unwrap();
|
|
|
|
|
|
|
|
worker
|
|
|
|
.execute_mod_async(&main_url, true)
|
|
|
|
.and_then(move |worker| {
|
|
|
|
if print_info {
|
|
|
|
print_file_info(&worker, &main_module);
|
|
|
|
}
|
|
|
|
worker.then(|result| {
|
|
|
|
js_check(result);
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}).map_err(|(err, _worker)| print_err_and_exit(err))
|
|
|
|
});
|
|
|
|
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 13:11:25 -04:00
|
|
|
);
|
2019-01-09 12:59:46 -05:00
|
|
|
|
2019-04-21 11:34:18 -04: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 15:13:42 -04:00
|
|
|
js_check(result);
|
|
|
|
Ok(())
|
2019-04-21 11:34:18 -04:00
|
|
|
}).map_err(|(err, _worker): (RustOrJsError, Worker)| {
|
|
|
|
print_err_and_exit(err)
|
2019-04-16 15:13:42 -04:00
|
|
|
})
|
2019-04-21 11:34:18 -04: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);
|
|
|
|
|
|
|
|
let main_url = root_specifier_to_url(&main_module).unwrap();
|
|
|
|
|
|
|
|
worker
|
|
|
|
.execute_mod_async(&main_url, false)
|
|
|
|
.and_then(move |worker| {
|
|
|
|
worker.then(|result| {
|
2019-04-16 15:13:42 -04:00
|
|
|
js_check(result);
|
|
|
|
Ok(())
|
|
|
|
})
|
2019-04-21 11:34:18 -04:00
|
|
|
}).map_err(|(err, _worker)| print_err_and_exit(err))
|
|
|
|
});
|
|
|
|
tokio_util::run(main_future);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fmt_command(mut flags: DenoFlags, mut argv: Vec<String>) {
|
|
|
|
argv.insert(1, "https://deno.land/std/prettier/main.ts".to_string());
|
|
|
|
flags.allow_read = true;
|
|
|
|
flags.allow_write = true;
|
|
|
|
run_script(flags, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
let cli_app = flags::create_cli_app();
|
|
|
|
let matches = cli_app.get_matches_from(args);
|
|
|
|
let flags = flags::parse_flags(matches.clone());
|
|
|
|
let mut argv: Vec<String> = vec!["deno".to_string()];
|
|
|
|
|
|
|
|
if flags.v8_help {
|
|
|
|
// show v8 help and exit
|
|
|
|
v8_set_flags(vec!["--help".to_string()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
match &flags.v8_flags {
|
|
|
|
Some(v8_flags) => {
|
|
|
|
v8_set_flags(v8_flags.clone());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
|
|
|
|
log::set_max_level(if flags.log_debug {
|
|
|
|
LevelFilter::Debug
|
|
|
|
} else {
|
|
|
|
LevelFilter::Warn
|
|
|
|
});
|
|
|
|
|
|
|
|
match matches.subcommand() {
|
|
|
|
("types", Some(_)) => {
|
|
|
|
types_command();
|
|
|
|
}
|
|
|
|
("eval", Some(eval_match)) => {
|
|
|
|
let code: &str = eval_match.value_of("code").unwrap();
|
|
|
|
argv.extend(vec![code.to_string()]);
|
|
|
|
eval_command(flags, argv);
|
|
|
|
}
|
|
|
|
("info", Some(info_match)) => {
|
|
|
|
let file: &str = info_match.value_of("file").unwrap();
|
|
|
|
argv.extend(vec![file.to_string()]);
|
|
|
|
prefetch_or_info_command(flags, argv, true);
|
|
|
|
}
|
|
|
|
("prefetch", Some(prefetch_match)) => {
|
|
|
|
let file: &str = prefetch_match.value_of("file").unwrap();
|
|
|
|
argv.extend(vec![file.to_string()]);
|
|
|
|
prefetch_or_info_command(flags, argv, false);
|
|
|
|
}
|
|
|
|
("fmt", Some(fmt_match)) => {
|
|
|
|
let files: Vec<String> = fmt_match
|
|
|
|
.values_of("files")
|
|
|
|
.unwrap()
|
|
|
|
.map(String::from)
|
|
|
|
.collect();
|
|
|
|
argv.extend(files);
|
|
|
|
fmt_command(flags, argv);
|
|
|
|
}
|
|
|
|
(script, Some(script_match)) => {
|
|
|
|
argv.extend(vec![script.to_string()]);
|
|
|
|
// check if there are any extra arguments that should
|
|
|
|
// be passed to script
|
|
|
|
if script_match.is_present("") {
|
|
|
|
let script_args: Vec<String> = script_match
|
|
|
|
.values_of("")
|
|
|
|
.unwrap()
|
|
|
|
.map(String::from)
|
|
|
|
.collect();
|
|
|
|
argv.extend(script_args);
|
|
|
|
}
|
|
|
|
run_script(flags, argv);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
run_repl(flags, argv);
|
|
|
|
}
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
2018-06-15 19:43:23 -04:00
|
|
|
}
|