0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

refactor: remove repeated code in main.rs (#6954)

This commit is contained in:
Bartek Iwańczuk 2020-08-04 16:08:41 +02:00 committed by GitHub
parent 2cd1fe8edf
commit 55ea9c7e85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -286,22 +286,18 @@ async fn print_file_info(
} }
fn get_types(unstable: bool) -> String { fn get_types(unstable: bool) -> String {
let mut types = format!(
"{}\n{}\n{}",
crate::js::DENO_NS_LIB,
crate::js::SHARED_GLOBALS_LIB,
crate::js::WINDOW_LIB,
);
if unstable { if unstable {
format!( types.push_str(&format!("\n{}", crate::js::UNSTABLE_NS_LIB,));
"{}\n{}\n{}\n{}",
crate::js::DENO_NS_LIB,
crate::js::SHARED_GLOBALS_LIB,
crate::js::WINDOW_LIB,
crate::js::UNSTABLE_NS_LIB,
)
} else {
format!(
"{}\n{}\n{}",
crate::js::DENO_NS_LIB,
crate::js::SHARED_GLOBALS_LIB,
crate::js::WINDOW_LIB,
)
} }
types
} }
async fn info_command( async fn info_command(
@ -348,20 +344,9 @@ async fn lint_command(
files: Vec<String>, files: Vec<String>,
list_rules: bool, list_rules: bool,
) -> Result<(), ErrBox> { ) -> Result<(), ErrBox> {
let global_state = GlobalState::new(flags)?; if !flags.unstable {
exit_unstable("lint");
// TODO(bartlomieju): refactor, it's non-sense to create }
// state just to perform unstable check...
use crate::state::State;
let state = State::new(
global_state,
None,
ModuleSpecifier::resolve_url("file:///dummy.ts").unwrap(),
None,
true,
)?;
state.check_unstable("lint");
if list_rules { if list_rules {
lint::print_rules_list(); lint::print_rules_list();
@ -396,13 +381,14 @@ async fn eval_command(
let main_module = let main_module =
ModuleSpecifier::resolve_url_or_path("./__$deno$eval.ts").unwrap(); ModuleSpecifier::resolve_url_or_path("./__$deno$eval.ts").unwrap();
let global_state = GlobalState::new(flags)?; let global_state = GlobalState::new(flags)?;
let mut worker = MainWorker::create(global_state, main_module.clone())?; let mut worker =
MainWorker::create(global_state.clone(), main_module.clone())?;
let main_module_url = main_module.as_url().to_owned(); let main_module_url = main_module.as_url().to_owned();
// Create a dummy source file. // Create a dummy source file.
let source_code = if print { let source_code = if print {
"console.log(".to_string() + &code + ")" format!("console.log({})", code)
} else { } else {
code.clone() code
} }
.into_bytes(); .into_bytes();
@ -418,11 +404,8 @@ async fn eval_command(
source_code: TextDocument::new(source_code, Some("utf-8")), source_code: TextDocument::new(source_code, Some("utf-8")),
}; };
// Save our fake file into file fetcher cache // Save our fake file into file fetcher cache
// to allow module access by TS compiler (e.g. op_fetch_source_files) // to allow module access by TS compiler.
worker global_state
.state
.borrow()
.global_state
.file_fetcher .file_fetcher
.save_source_file_in_cache(&main_module, source_file); .save_source_file_in_cache(&main_module, source_file);
debug!("main_module {}", &main_module); debug!("main_module {}", &main_module);
@ -520,11 +503,11 @@ async fn doc_command(
&self, &self,
specifier: &str, specifier: &str,
) -> Pin<Box<dyn Future<Output = Result<String, OpError>>>> { ) -> Pin<Box<dyn Future<Output = Result<String, OpError>>>> {
let specifier =
ModuleSpecifier::resolve_url_or_path(specifier).expect("Bad specifier");
let fetcher = self.clone(); let fetcher = self.clone();
let specifier = specifier.to_string();
async move { async move {
let specifier = ModuleSpecifier::resolve_url_or_path(&specifier)
.map_err(OpError::from)?;
let source_file = fetcher let source_file = fetcher
.fetch_source_file(&specifier, None, Permissions::allow_all()) .fetch_source_file(&specifier, None, Permissions::allow_all())
.await?; .await?;
@ -606,11 +589,8 @@ async fn run_command(flags: Flags, script: String) -> Result<(), ErrBox> {
source_code: source.into(), source_code: source.into(),
}; };
// Save our fake file into file fetcher cache // Save our fake file into file fetcher cache
// to allow module access by TS compiler (e.g. op_fetch_source_files) // to allow module access by TS compiler
worker global_state
.state
.borrow()
.global_state
.file_fetcher .file_fetcher
.save_source_file_in_cache(&main_module, source_file); .save_source_file_in_cache(&main_module, source_file);
}; };
@ -665,11 +645,8 @@ async fn test_command(
), ),
}; };
// Save our fake file into file fetcher cache // Save our fake file into file fetcher cache
// to allow module access by TS compiler (e.g. op_fetch_source_files) // to allow module access by TS compiler
worker global_state
.state
.borrow()
.global_state
.file_fetcher .file_fetcher
.save_source_file_in_cache(&main_module, source_file); .save_source_file_in_cache(&main_module, source_file);
let execute_result = worker.execute_module(&main_module).await; let execute_result = worker.execute_module(&main_module).await;