diff --git a/cli/flags.rs b/cli/flags.rs index b917906517fa8c..6d83d0849471eb 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -259,8 +259,17 @@ The declaration file could be saved and used for typing information.", ), ).subcommand( SubCommand::with_name("info") - .about("Show source file related info") - .long_about("Show source file related info. + .about("Show info about cache or info related to source file") + .long_about("Show info about cache or info related to source file. + + deno info + +The following information is shown: + + DENO_DIR: location of directory containing Deno-related files + Remote modules cache: location of directory containing remote modules + TypeScript compiler cache: location of directory containing TS compiler output + deno info https://deno.land/std@v0.11/http/file_server.ts @@ -271,7 +280,7 @@ The following information is shown: compiled: TypeScript only. shown local path of compiled source code. map: TypeScript only. shown local path of source map. deps: Dependency tree of the source file.", - ).arg(Arg::with_name("file").takes_value(true).required(true)), + ).arg(Arg::with_name("file").takes_value(true).required(false)), ).subcommand( SubCommand::with_name("eval") .about("Eval script") @@ -748,8 +757,9 @@ pub fn flags_from_vec( DenoSubcommand::Run } ("info", Some(info_match)) => { - let file: &str = info_match.value_of("file").unwrap(); - argv.extend(vec![file.to_string()]); + if info_match.is_present("file") { + argv.push(info_match.value_of("file").unwrap().to_string()); + } DenoSubcommand::Info } ("install", Some(install_match)) => { @@ -1119,6 +1129,11 @@ mod tests { assert_eq!(flags, DenoFlags::default()); assert_eq!(subcommand, DenoSubcommand::Info); assert_eq!(argv, svec!["deno", "script.ts"]); + + let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "info"]); + assert_eq!(flags, DenoFlags::default()); + assert_eq!(subcommand, DenoSubcommand::Info); + assert_eq!(argv, svec!["deno"]); } #[test] diff --git a/cli/main.rs b/cli/main.rs index ff757ed3664ad4..52d08ea9e9c26f 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, +) -> (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, -) -> (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) { + let (mut worker, state) = create_worker_and_state(flags, argv.clone()); -fn fetch_or_info_command( - flags: DenoFlags, - argv: Vec, - 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) { + 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) { 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), diff --git a/tests/022_info_flag.out b/tests/022_info_flag_script.out similarity index 100% rename from tests/022_info_flag.out rename to tests/022_info_flag_script.out diff --git a/tests/022_info_flag.test b/tests/022_info_flag_script.test similarity index 81% rename from tests/022_info_flag.test rename to tests/022_info_flag_script.test index e58288ec566e48..ba11b9815026c4 100644 --- a/tests/022_info_flag.test +++ b/tests/022_info_flag_script.test @@ -1,4 +1,4 @@ # The output assumes 003_relative_import.ts has already been run earlier # and its output is cached to $DENO_DIR. args: info http://127.0.0.1:4545/tests/019_media_types.ts -output: tests/022_info_flag.out +output: tests/022_info_flag_script.out diff --git a/tests/041_info_flag.out b/tests/041_info_flag.out new file mode 100644 index 00000000000000..c384fa892df9bc --- /dev/null +++ b/tests/041_info_flag.out @@ -0,0 +1,3 @@ +DENO_DIR location: "[WILDCARD]" +Remote modules cache: "[WILDCARD]deps" +TypeScript compiler cache: "[WILDCARD]gen" diff --git a/tests/041_info_flag.test b/tests/041_info_flag.test new file mode 100644 index 00000000000000..2cabb652ce6afe --- /dev/null +++ b/tests/041_info_flag.test @@ -0,0 +1,2 @@ +args: info +output: tests/041_info_flag.out