Add --info flag to display file info (compiled code/source map) (#1647)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2019-02-01 22:28:31 -08:00 committed by Ryan Dahl
parent 16ed1f2545
commit 3650bae5f6
9 changed files with 69 additions and 0 deletions

View file

@ -24,7 +24,9 @@ pub struct CodeFetchOutput {
pub filename: String, pub filename: String,
pub media_type: msg::MediaType, pub media_type: msg::MediaType,
pub source_code: String, pub source_code: String,
pub maybe_output_code_filename: Option<String>,
pub maybe_output_code: Option<String>, pub maybe_output_code: Option<String>,
pub maybe_source_map_filename: Option<String>,
pub maybe_source_map: Option<String>, pub maybe_source_map: Option<String>,
} }
@ -70,7 +72,9 @@ impl CodeFetchOutput {
filename, filename,
media_type: msg::MediaType::JavaScript, // TODO media_type: msg::MediaType::JavaScript, // TODO
source_code, source_code,
maybe_output_code_filename: None,
maybe_output_code, maybe_output_code,
maybe_source_map_filename: None,
maybe_source_map, maybe_source_map,
}) })
} }

View file

@ -180,7 +180,9 @@ impl DenoDir {
filename: filename.to_string(), filename: filename.to_string(),
media_type: map_content_type(&p, Some(&content_type)), media_type: map_content_type(&p, Some(&content_type)),
source_code: source, source_code: source,
maybe_output_code_filename: None,
maybe_output_code: None, maybe_output_code: None,
maybe_source_map_filename: None,
maybe_source_map: None, maybe_source_map: None,
})); }));
} else { } else {
@ -219,7 +221,9 @@ impl DenoDir {
filename: filename.to_string(), filename: filename.to_string(),
media_type: map_content_type(&p, maybe_content_type_str), media_type: map_content_type(&p, maybe_content_type_str),
source_code, source_code,
maybe_output_code_filename: None,
maybe_output_code: None, maybe_output_code: None,
maybe_source_map_filename: None,
maybe_source_map: None, maybe_source_map: None,
})) }))
} }
@ -307,6 +311,8 @@ impl DenoDir {
return Ok(out); return Ok(out);
} }
let (output_code_filename, output_source_map_filename) =
self.cache_path(&out.filename, &out.source_code);
let result = let result =
self.load_cache(out.filename.as_str(), out.source_code.as_str()); self.load_cache(out.filename.as_str(), out.source_code.as_str());
match result { match result {
@ -322,7 +328,13 @@ impl DenoDir {
filename: out.filename, filename: out.filename,
media_type: out.media_type, media_type: out.media_type,
source_code: out.source_code, 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_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), maybe_source_map: Some(source_map),
}), }),
} }
@ -419,6 +431,28 @@ impl DenoDir {
debug!("module_name: {}, filename: {}", module_name, filename); debug!("module_name: {}, filename: {}", module_name, filename);
Ok((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 { impl SourceMapGetter for DenoDir {

View file

@ -29,6 +29,7 @@ pub struct DenoFlags {
pub allow_run: bool, pub allow_run: bool,
pub types: bool, pub types: bool,
pub prefetch: bool, pub prefetch: bool,
pub info: bool,
} }
pub fn get_usage(opts: &Options) -> String { pub fn get_usage(opts: &Options) -> String {
@ -111,6 +112,9 @@ fn set_recognized_flags(
if matches.opt_present("prefetch") { if matches.opt_present("prefetch") {
flags.prefetch = true; flags.prefetch = true;
} }
if matches.opt_present("info") {
flags.info = true;
}
if !matches.free.is_empty() { if !matches.free.is_empty() {
rest.extend(matches.free); rest.extend(matches.free);
@ -147,6 +151,7 @@ pub fn set_flags(
opts.optflag("", "v8-options", "Print V8 command line options."); opts.optflag("", "v8-options", "Print V8 command line options.");
opts.optflag("", "types", "Print runtime TypeScript declarations."); opts.optflag("", "types", "Print runtime TypeScript declarations.");
opts.optflag("", "prefetch", "Prefetch the dependencies."); opts.optflag("", "prefetch", "Prefetch the dependencies.");
opts.optflag("", "info", "Show source file related info");
let mut flags = DenoFlags::default(); let mut flags = DenoFlags::default();

View file

@ -81,12 +81,22 @@ fn main() {
}); });
let should_prefetch = flags.prefetch; let should_prefetch = flags.prefetch;
let should_display_info = flags.info;
let state = Arc::new(isolate::IsolateState::new(flags, rest_argv, None)); let state = Arc::new(isolate::IsolateState::new(flags, rest_argv, None));
let snapshot = snapshot::deno_snapshot(); let snapshot = snapshot::deno_snapshot();
let mut isolate = isolate::Isolate::new(snapshot, state, ops::dispatch); let mut isolate = isolate::Isolate::new(snapshot, state, ops::dispatch);
tokio_util::init(|| { 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. // Setup runtime.
isolate isolate
.execute("denoMain();") .execute("denoMain();")

View file

@ -0,0 +1 @@
Hello

View file

@ -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

4
tests/022_info_flag.out Normal file
View file

@ -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

4
tests/022_info_flag.test Normal file
View file

@ -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

View file

@ -20,6 +20,9 @@ def read_test(file_name):
lines = test_file.splitlines() lines = test_file.splitlines()
test_dict = {} test_dict = {}
for line in lines: for line in lines:
if line.strip().startswith("#"):
# skip comments
continue
key, value = re.split(r":\s+", line) key, value = re.split(r":\s+", line)
test_dict[key] = value test_dict[key] = value
return test_dict return test_dict