提交 3650bae5 编写于 作者: K Kevin (Kun) "Kassimo" Qian 提交者: Ryan Dahl

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

上级 16ed1f25
......@@ -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<String>,
pub maybe_output_code: Option<String>,
pub maybe_source_map_filename: Option<String>,
pub maybe_source_map: Option<String>,
}
......@@ -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,
})
}
......
......@@ -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 {
......
......@@ -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();
......
......@@ -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();")
......
# 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
local: [WILDCARD]deps/http/127.0.0.1_PORT4545/tests/003_relative_import.ts
type: TypeScript
compiled: [WILDCARD].js
map: [WILDCARD].js.map
# 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
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册