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;
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2019-02-07 20:07:20 -05:00
|
|
|
mod ansi;
|
2019-03-19 20:55:59 -04:00
|
|
|
pub mod cli_behavior;
|
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-10-31 14:11:10 -04:00
|
|
|
pub mod isolate;
|
2019-03-14 19:17:52 -04:00
|
|
|
pub mod isolate_state;
|
2018-12-06 23:05:36 -05:00
|
|
|
pub mod js_errors;
|
2019-02-08 21:36:10 -05:00
|
|
|
pub mod modules;
|
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-03-18 20:03:37 -04:00
|
|
|
mod startup_data;
|
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-01-09 12:59:46 -05:00
|
|
|
pub mod workers;
|
2018-07-04 14:50:28 -04:00
|
|
|
|
2019-03-19 20:55:59 -04:00
|
|
|
use crate::cli_behavior::CliBehavior;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::errors::RustOrJsError;
|
|
|
|
use crate::isolate::Isolate;
|
|
|
|
use crate::isolate_state::IsolateState;
|
|
|
|
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;
|
2018-11-29 22:03:00 -05:00
|
|
|
use std::sync::Arc;
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 19:43:23 -04:00
|
|
|
fn main() {
|
2019-02-07 16:19:50 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
ansi_term::enable_ansi_support().ok(); // For Windows 10
|
|
|
|
|
2018-07-26 17:37:09 -04:00
|
|
|
log::set_logger(&LOGGER).unwrap();
|
2018-09-22 01:03:24 -04:00
|
|
|
let args = env::args().collect();
|
2019-02-01 18:29:00 -05:00
|
|
|
let (mut flags, mut rest_argv, usage_string) = flags::set_flags(args)
|
|
|
|
.unwrap_or_else(|err| {
|
2018-10-24 00:02:43 -04:00
|
|
|
eprintln!("{}", err);
|
|
|
|
std::process::exit(1)
|
|
|
|
});
|
2018-11-06 01:41:39 -05:00
|
|
|
|
|
|
|
if flags.help {
|
|
|
|
println!("{}", &usage_string);
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
log::set_max_level(if flags.log_debug {
|
2019-01-29 10:37:27 -05:00
|
|
|
LevelFilter::Debug
|
2018-11-06 01:41:39 -05:00
|
|
|
} else {
|
2019-01-29 10:37:27 -05:00
|
|
|
LevelFilter::Warn
|
2018-11-06 01:41:39 -05:00
|
|
|
});
|
|
|
|
|
2019-02-01 18:29:00 -05:00
|
|
|
if flags.fmt {
|
2019-03-06 10:23:47 -05:00
|
|
|
rest_argv.insert(1, "https://deno.land/std/prettier/main.ts".to_string());
|
2019-02-08 15:59:38 -05:00
|
|
|
flags.allow_read = true;
|
2019-02-01 18:29:00 -05:00
|
|
|
flags.allow_write = true;
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:36:10 -05:00
|
|
|
let should_prefetch = flags.prefetch || flags.info;
|
2019-02-02 01:28:31 -05:00
|
|
|
let should_display_info = flags.info;
|
2019-01-15 12:19:58 -05:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
let state = Arc::new(IsolateState::new(flags, rest_argv, None));
|
|
|
|
let state_ = state.clone();
|
2019-03-18 20:03:37 -04:00
|
|
|
let startup_data = startup_data::deno_isolate_init();
|
2019-03-19 20:55:59 -04:00
|
|
|
let cli = CliBehavior::new(Some(startup_data), state_);
|
2019-03-14 19:17:52 -04:00
|
|
|
let mut isolate = Isolate::new(cli);
|
2019-01-09 12:59:46 -05:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
let main_future = lazy(move || {
|
2019-01-09 12:59:46 -05:00
|
|
|
// Setup runtime.
|
2019-03-14 19:17:52 -04:00
|
|
|
js_check(isolate.execute("denoMain()"));
|
2019-01-09 12:59:46 -05:00
|
|
|
|
2019-02-12 21:14:02 -05:00
|
|
|
// Execute main module.
|
2019-03-14 19:17:52 -04:00
|
|
|
if let Some(main_module) = state.main_module() {
|
2019-02-12 21:14:02 -05:00
|
|
|
debug!("main_module {}", main_module);
|
2019-03-14 19:17:52 -04:00
|
|
|
js_check(isolate.execute_mod(&main_module, should_prefetch));
|
2019-02-02 17:39:28 -05:00
|
|
|
if should_display_info {
|
|
|
|
// Display file info and exit. Do not run file
|
2019-03-14 19:17:52 -04:00
|
|
|
isolate.print_file_info(&main_module);
|
2019-02-02 17:39:28 -05:00
|
|
|
std::process::exit(0);
|
|
|
|
}
|
2019-01-09 12:59:46 -05:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
isolate.then(|result| {
|
|
|
|
js_check(result);
|
|
|
|
Ok(())
|
|
|
|
})
|
2018-09-18 14:53:16 -04:00
|
|
|
});
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
tokio_util::run(main_future);
|
2018-06-15 19:43:23 -04:00
|
|
|
}
|