From 3650bae5f6565cb6de2706512df145c86e8525cd Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Fri, 1 Feb 2019 22:28:31 -0800 Subject: [PATCH] Add --info flag to display file info (compiled code/source map) (#1647) --- src/compiler.rs | 4 ++++ src/deno_dir.rs | 34 ++++++++++++++++++++++++++++++++++ src/flags.rs | 5 +++++ src/main.rs | 10 ++++++++++ tests/021_info_flag_setup.out | 1 + tests/021_info_flag_setup.test | 4 ++++ tests/022_info_flag.out | 4 ++++ tests/022_info_flag.test | 4 ++++ tools/integration_tests.py | 3 +++ 9 files changed, 69 insertions(+) create mode 100644 tests/021_info_flag_setup.out create mode 100644 tests/021_info_flag_setup.test create mode 100644 tests/022_info_flag.out create mode 100644 tests/022_info_flag.test diff --git a/src/compiler.rs b/src/compiler.rs index db8d3ecd5b..3b5d57ad0c 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -24,7 +24,9 @@ pub struct CodeFetchOutput { pub filename: String, pub media_type: msg::MediaType, pub source_code: String, + pub maybe_output_code_filename: Option, pub maybe_output_code: Option, + pub maybe_source_map_filename: Option, pub maybe_source_map: Option, } @@ -70,7 +72,9 @@ impl CodeFetchOutput { filename, media_type: msg::MediaType::JavaScript, // TODO source_code, + maybe_output_code_filename: None, maybe_output_code, + maybe_source_map_filename: None, maybe_source_map, }) } diff --git a/src/deno_dir.rs b/src/deno_dir.rs index 6fbe90f680..1a87a7d697 100644 --- a/src/deno_dir.rs +++ b/src/deno_dir.rs @@ -180,7 +180,9 @@ impl DenoDir { filename: filename.to_string(), media_type: map_content_type(&p, Some(&content_type)), source_code: source, + maybe_output_code_filename: None, maybe_output_code: None, + maybe_source_map_filename: None, maybe_source_map: None, })); } else { @@ -219,7 +221,9 @@ impl DenoDir { filename: filename.to_string(), media_type: map_content_type(&p, maybe_content_type_str), source_code, + maybe_output_code_filename: None, maybe_output_code: None, + maybe_source_map_filename: None, maybe_source_map: None, })) } @@ -307,6 +311,8 @@ impl DenoDir { return Ok(out); } + let (output_code_filename, output_source_map_filename) = + self.cache_path(&out.filename, &out.source_code); let result = self.load_cache(out.filename.as_str(), out.source_code.as_str()); match result { @@ -322,7 +328,13 @@ impl DenoDir { filename: out.filename, media_type: out.media_type, source_code: out.source_code, + maybe_output_code_filename: output_code_filename + .to_str() + .map(|s| s.to_string()), maybe_output_code: Some(output_code), + maybe_source_map_filename: output_source_map_filename + .to_str() + .map(|s| s.to_string()), maybe_source_map: Some(source_map), }), } @@ -419,6 +431,28 @@ impl DenoDir { debug!("module_name: {}, filename: {}", module_name, filename); Ok((module_name, filename)) } + + pub fn print_file_info(self: &Self, filename: String) { + let maybe_out = self.code_fetch(&filename, "."); + if maybe_out.is_err() { + println!("{}", maybe_out.unwrap_err()); + return; + } + let out = maybe_out.unwrap(); + + println!("local: {}", &(out.filename)); + println!("type: {}", msg::enum_name_media_type(out.media_type)); + if out.maybe_output_code_filename.is_some() { + println!( + "compiled: {}", + out.maybe_output_code_filename.as_ref().unwrap(), + ); + } + if out.maybe_source_map_filename.is_some() { + println!("map: {}", out.maybe_source_map_filename.as_ref().unwrap()); + } + // TODO print deps. + } } impl SourceMapGetter for DenoDir { diff --git a/src/flags.rs b/src/flags.rs index 7a400319c3..ecc60922e1 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -29,6 +29,7 @@ pub struct DenoFlags { pub allow_run: bool, pub types: bool, pub prefetch: bool, + pub info: bool, } pub fn get_usage(opts: &Options) -> String { @@ -111,6 +112,9 @@ fn set_recognized_flags( if matches.opt_present("prefetch") { flags.prefetch = true; } + if matches.opt_present("info") { + flags.info = true; + } if !matches.free.is_empty() { rest.extend(matches.free); @@ -147,6 +151,7 @@ pub fn set_flags( opts.optflag("", "v8-options", "Print V8 command line options."); opts.optflag("", "types", "Print runtime TypeScript declarations."); opts.optflag("", "prefetch", "Prefetch the dependencies."); + opts.optflag("", "info", "Show source file related info"); let mut flags = DenoFlags::default(); diff --git a/src/main.rs b/src/main.rs index 1b93ea86d8..d50a68ad59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,12 +81,22 @@ fn main() { }); let should_prefetch = flags.prefetch; + let should_display_info = flags.info; let state = Arc::new(isolate::IsolateState::new(flags, rest_argv, None)); let snapshot = snapshot::deno_snapshot(); let mut isolate = isolate::Isolate::new(snapshot, state, ops::dispatch); tokio_util::init(|| { + // Requires tokio + if should_display_info { + isolate + .state + .dir + .print_file_info(isolate.state.argv[1].clone()); + std::process::exit(0); + } + // Setup runtime. isolate .execute("denoMain();") diff --git a/tests/021_info_flag_setup.out b/tests/021_info_flag_setup.out new file mode 100644 index 0000000000..e965047ad7 --- /dev/null +++ b/tests/021_info_flag_setup.out @@ -0,0 +1 @@ +Hello diff --git a/tests/021_info_flag_setup.test b/tests/021_info_flag_setup.test new file mode 100644 index 0000000000..eaded08400 --- /dev/null +++ b/tests/021_info_flag_setup.test @@ -0,0 +1,4 @@ +# This is used to make sure code for remote source is compiled +# such that 022_info_flag.test would function correctly +args: --reload http://127.0.0.1:4545/tests/003_relative_import.ts +output: tests/021_info_flag_setup.out diff --git a/tests/022_info_flag.out b/tests/022_info_flag.out new file mode 100644 index 0000000000..9421387a69 --- /dev/null +++ b/tests/022_info_flag.out @@ -0,0 +1,4 @@ +local: [WILDCARD]deps/http/127.0.0.1_PORT4545/tests/003_relative_import.ts +type: TypeScript +compiled: [WILDCARD].js +map: [WILDCARD].js.map diff --git a/tests/022_info_flag.test b/tests/022_info_flag.test new file mode 100644 index 0000000000..e2132a85fe --- /dev/null +++ b/tests/022_info_flag.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/003_relative_import.ts +output: tests/022_info_flag.out diff --git a/tools/integration_tests.py b/tools/integration_tests.py index cca4a75ca4..a6392bae6d 100755 --- a/tools/integration_tests.py +++ b/tools/integration_tests.py @@ -20,6 +20,9 @@ def read_test(file_name): lines = test_file.splitlines() test_dict = {} for line in lines: + if line.strip().startswith("#"): + # skip comments + continue key, value = re.split(r":\s+", line) test_dict[key] = value return test_dict