From 9bd473d8ac9228e483bd26028dbe7ba88e971c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 12 Aug 2019 02:43:01 +0200 Subject: feat: print cache location when no arg in deno info (#2752) --- cli/flags.rs | 25 ++++++-- cli/main.rs | 134 ++++++++++++++++++++++++---------------- tests/022_info_flag.out | 14 ----- tests/022_info_flag.test | 4 -- tests/022_info_flag_script.out | 14 +++++ tests/022_info_flag_script.test | 4 ++ tests/041_info_flag.out | 3 + tests/041_info_flag.test | 2 + 8 files changed, 125 insertions(+), 75 deletions(-) delete mode 100644 tests/022_info_flag.out delete mode 100644 tests/022_info_flag.test create mode 100644 tests/022_info_flag_script.out create mode 100644 tests/022_info_flag_script.test create mode 100644 tests/041_info_flag.out create mode 100644 tests/041_info_flag.test diff --git a/cli/flags.rs b/cli/flags.rs index b91790651..6d83d0849 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 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, +) -> (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.out deleted file mode 100644 index 519e7cf6b..000000000 --- a/tests/022_info_flag.out +++ /dev/null @@ -1,14 +0,0 @@ -local: [WILDCARD]019_media_types.ts -type: TypeScript -compiled: [WILDCARD].js -map: [WILDCARD].js.map -deps: -http://127.0.0.1:4545/tests/019_media_types.ts - ├── http://localhost:4545/tests/subdir/mt_text_typescript.t1.ts - ├── http://localhost:4545/tests/subdir/mt_video_vdn.t2.ts - ├── http://localhost:4545/tests/subdir/mt_video_mp2t.t3.ts - ├── http://localhost:4545/tests/subdir/mt_application_x_typescript.t4.ts - ├── http://localhost:4545/tests/subdir/mt_text_javascript.j1.js - ├── http://localhost:4545/tests/subdir/mt_application_ecmascript.j2.js - ├── http://localhost:4545/tests/subdir/mt_text_ecmascript.j3.js - └── http://localhost:4545/tests/subdir/mt_application_x_javascript.j4.js diff --git a/tests/022_info_flag.test b/tests/022_info_flag.test deleted file mode 100644 index e58288ec5..000000000 --- a/tests/022_info_flag.test +++ /dev/null @@ -1,4 +0,0 @@ -# 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 diff --git a/tests/022_info_flag_script.out b/tests/022_info_flag_script.out new file mode 100644 index 000000000..519e7cf6b --- /dev/null +++ b/tests/022_info_flag_script.out @@ -0,0 +1,14 @@ +local: [WILDCARD]019_media_types.ts +type: TypeScript +compiled: [WILDCARD].js +map: [WILDCARD].js.map +deps: +http://127.0.0.1:4545/tests/019_media_types.ts + ├── http://localhost:4545/tests/subdir/mt_text_typescript.t1.ts + ├── http://localhost:4545/tests/subdir/mt_video_vdn.t2.ts + ├── http://localhost:4545/tests/subdir/mt_video_mp2t.t3.ts + ├── http://localhost:4545/tests/subdir/mt_application_x_typescript.t4.ts + ├── http://localhost:4545/tests/subdir/mt_text_javascript.j1.js + ├── http://localhost:4545/tests/subdir/mt_application_ecmascript.j2.js + ├── http://localhost:4545/tests/subdir/mt_text_ecmascript.j3.js + └── http://localhost:4545/tests/subdir/mt_application_x_javascript.j4.js diff --git a/tests/022_info_flag_script.test b/tests/022_info_flag_script.test new file mode 100644 index 000000000..ba11b9815 --- /dev/null +++ b/tests/022_info_flag_script.test @@ -0,0 +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_script.out diff --git a/tests/041_info_flag.out b/tests/041_info_flag.out new file mode 100644 index 000000000..c384fa892 --- /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 000000000..2cabb652c --- /dev/null +++ b/tests/041_info_flag.test @@ -0,0 +1,2 @@ +args: info +output: tests/041_info_flag.out -- cgit v1.2.3