diff options
Diffstat (limited to 'cli/main.rs')
-rw-r--r-- | cli/main.rs | 134 |
1 files changed, 82 insertions, 52 deletions
diff --git a/cli/main.rs b/cli/main.rs index ff757ed36..52d08ea9e 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -56,7 +56,6 @@ use deno::ErrBox; use deno::ModuleSpecifier; use flags::DenoFlags; use flags::DenoSubcommand; -use futures::future; use futures::lazy; use futures::Future; use log::Level; @@ -99,7 +98,62 @@ fn js_check(r: Result<(), ErrBox>) { } } -// TODO: we might want to rethink how this method works +fn create_worker_and_state( + flags: DenoFlags, + argv: Vec<String>, +) -> (Worker, ThreadSafeState) { + use crate::shell::Shell; + use std::sync::Arc; + use std::sync::Mutex; + let shell = Arc::new(Mutex::new(Shell::new())); + let progress = Progress::new(); + progress.set_callback(move |_done, _completed, _total, status, msg| { + if !status.is_empty() { + let mut s = shell.lock().unwrap(); + s.status(status, msg).expect("shell problem"); + } + }); + // TODO(kevinkassimo): maybe make include_deno_namespace also configurable? + let state = + ThreadSafeState::new(flags, argv, ops::op_selector_std, progress, true) + .unwrap(); + let worker = Worker::new( + "main".to_string(), + startup_data::deno_isolate_init(), + state.clone(), + ); + + (worker, state) +} + +fn types_command() { + let content = include_str!(concat!( + env!("GN_OUT_DIR"), + "/gen/cli/lib/lib.deno_runtime.d.ts" + )); + println!("{}", content); +} + +fn print_cache_info(worker: Worker) { + let state = worker.state; + + println!( + "{} {:?}", + ansi::bold("DENO_DIR location:".to_string()), + state.dir.root + ); + println!( + "{} {:?}", + ansi::bold("Remote modules cache:".to_string()), + state.dir.deps_cache.location + ); + println!( + "{} {:?}", + ansi::bold("TypeScript compiler cache:".to_string()), + state.dir.gen_cache.location + ); +} + pub fn print_file_info( worker: Worker, module_specifier: &ModuleSpecifier, @@ -181,48 +235,13 @@ pub fn print_file_info( }) } -fn create_worker_and_state( - flags: DenoFlags, - argv: Vec<String>, -) -> (Worker, ThreadSafeState) { - use crate::shell::Shell; - use std::sync::Arc; - use std::sync::Mutex; - let shell = Arc::new(Mutex::new(Shell::new())); - let progress = Progress::new(); - progress.set_callback(move |_done, _completed, _total, status, msg| { - if !status.is_empty() { - let mut s = shell.lock().unwrap(); - s.status(status, msg).expect("shell problem"); - } - }); - // TODO(kevinkassimo): maybe make include_deno_namespace also configurable? - let state = - ThreadSafeState::new(flags, argv, ops::op_selector_std, progress, true) - .unwrap(); - let worker = Worker::new( - "main".to_string(), - startup_data::deno_isolate_init(), - state.clone(), - ); - - (worker, state) -} - -fn types_command() { - let content = include_str!(concat!( - env!("GN_OUT_DIR"), - "/gen/cli/lib/lib.deno_runtime.d.ts" - )); - println!("{}", content); -} +fn info_command(flags: DenoFlags, argv: Vec<String>) { + let (mut worker, state) = create_worker_and_state(flags, argv.clone()); -fn fetch_or_info_command( - flags: DenoFlags, - argv: Vec<String>, - print_info: bool, -) { - let (mut worker, state) = create_worker_and_state(flags, argv); + // If it was just "deno info" print location of caches and exit + if argv.len() == 1 { + return print_cache_info(worker); + } let main_module = state.main_module().unwrap(); let main_future = lazy(move || { @@ -233,13 +252,7 @@ fn fetch_or_info_command( worker .execute_mod_async(&main_module, true) .map_err(print_err_and_exit) - .and_then(move |()| { - if print_info { - future::Either::A(print_file_info(worker, &main_module)) - } else { - future::Either::B(future::ok(worker)) - } - }) + .and_then(move |()| print_file_info(worker, &main_module)) .and_then(|worker| { worker.then(|result| { js_check(result); @@ -250,6 +263,23 @@ fn fetch_or_info_command( tokio_util::run(main_future); } +fn fetch_command(flags: DenoFlags, argv: Vec<String>) { + let (mut worker, state) = create_worker_and_state(flags, argv.clone()); + + let main_module = state.main_module().unwrap(); + let main_future = lazy(move || { + // Setup runtime. + js_check(worker.execute("denoMain()")); + debug!("main_module {}", main_module); + + worker.execute_mod_async(&main_module, true).then(|result| { + js_check(result); + Ok(()) + }) + }); + 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 @@ -391,8 +421,8 @@ fn main() { DenoSubcommand::Bundle => bundle_command(flags, argv), DenoSubcommand::Completions => {} DenoSubcommand::Eval => eval_command(flags, argv), - DenoSubcommand::Fetch => fetch_or_info_command(flags, argv, false), - DenoSubcommand::Info => fetch_or_info_command(flags, argv, true), + DenoSubcommand::Fetch => fetch_command(flags, argv), + DenoSubcommand::Info => info_command(flags, argv), DenoSubcommand::Install => run_script(flags, argv), DenoSubcommand::Repl => run_repl(flags, argv), DenoSubcommand::Run => run_script(flags, argv), |