Skip to content

Commit

Permalink
feat: print cache location when no arg in deno info (#2752)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored and ry committed Aug 12, 2019
1 parent 54982e9 commit 9bd473d
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 58 deletions.
25 changes: 20 additions & 5 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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)) => {
Expand Down Expand Up @@ -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]
Expand Down
134 changes: 82 additions & 52 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 || {
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions tests/041_info_flag.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DENO_DIR location: "[WILDCARD]"
Remote modules cache: "[WILDCARD]deps"
TypeScript compiler cache: "[WILDCARD]gen"
2 changes: 2 additions & 0 deletions tests/041_info_flag.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
args: info
output: tests/041_info_flag.out

0 comments on commit 9bd473d

Please sign in to comment.