1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 08:39:09 -05:00
denoland-deno/cli/main.rs

140 lines
3 KiB
Rust
Raw Normal View History

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]
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;
#[macro_use]
extern crate serde_json;
2018-07-26 17:54:22 -04:00
2019-02-07 20:07:20 -05:00
mod ansi;
pub mod cli_behavior;
pub mod compiler;
pub mod deno_dir;
pub mod errors;
pub mod flags;
2018-07-26 17:54:22 -04:00
mod fs;
mod global_timer;
mod http_body;
mod http_util;
pub mod isolate;
pub mod isolate_state;
pub mod js_errors;
pub mod modules;
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;
pub mod permissions;
mod repl;
pub mod resolve_addr;
pub mod resources;
mod startup_data;
mod tokio_util;
mod tokio_write;
pub mod version;
pub mod workers;
2018-07-04 14:50:28 -04:00
use crate::cli_behavior::CliBehavior;
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;
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()
}
2019-01-29 10:37:27 -05:00
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
println!("{} RS - {}", record.level(), record.args());
}
}
fn flush(&self) {}
}
fn print_err_and_exit(err: RustOrJsError) {
2018-12-10 17:50:41 -05:00
eprintln!("{}", err.to_string());
std::process::exit(1);
}
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() {
#[cfg(windows)]
ansi_term::enable_ansi_support().ok(); // For Windows 10
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| {
eprintln!("{}", err);
std::process::exit(1)
});
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
} else {
2019-01-29 10:37:27 -05:00
LevelFilter::Warn
});
2019-02-01 18:29:00 -05:00
if flags.fmt {
rest_argv.insert(1, "https://deno.land/std/prettier/main.ts".to_string());
flags.allow_read = true;
2019-02-01 18:29:00 -05:00
flags.allow_write = true;
}
let should_prefetch = flags.prefetch || flags.info;
let should_display_info = flags.info;
let state = Arc::new(IsolateState::new(flags, rest_argv, None));
let state_ = state.clone();
let startup_data = startup_data::deno_isolate_init();
let cli = CliBehavior::new(Some(startup_data), state_);
let mut isolate = Isolate::new(cli);
let main_future = lazy(move || {
// Setup runtime.
js_check(isolate.execute("denoMain()"));
2019-02-12 21:14:02 -05:00
// Execute main module.
if let Some(main_module) = state.main_module() {
2019-02-12 21:14:02 -05:00
debug!("main_module {}", main_module);
js_check(isolate.execute_mod(&main_module, should_prefetch));
if should_display_info {
// Display file info and exit. Do not run file
isolate.print_file_info(&main_module);
std::process::exit(0);
}
}
isolate.then(|result| {
js_check(result);
Ok(())
})
});
tokio_util::run(main_future);
2018-06-15 19:43:23 -04:00
}