Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): reorganize main.rs and split workers #8495

Merged
merged 6 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 46 additions & 33 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod tools;
mod tsc;
mod tsc_config;
mod version;
mod web_worker;
mod worker;

use crate::file_fetcher::File;
Expand Down Expand Up @@ -792,37 +793,31 @@ async fn test_command(
Ok(())
}

pub fn main() {
#[cfg(windows)]
colors::enable_ansi(); // For Windows 10

let args: Vec<String> = env::args().collect();
let flags = flags::flags_from_vec(args);

if let Some(ref v8_flags) = flags.v8_flags {
let v8_flags_includes_help = v8_flags
.iter()
.any(|flag| flag == "-help" || flag == "--help");
let v8_flags = once("UNUSED_BUT_NECESSARY_ARG0".to_owned())
.chain(v8_flags.iter().cloned())
.collect::<Vec<_>>();
let unrecognized_v8_flags = v8_set_flags(v8_flags)
.into_iter()
.skip(1)
.collect::<Vec<_>>();
if !unrecognized_v8_flags.is_empty() {
for f in unrecognized_v8_flags {
eprintln!("error: V8 did not recognize flag '{}'", f);
}
eprintln!("\nFor a list of V8 flags, use '--v8-flags=--help'");
std::process::exit(1);
}
if v8_flags_includes_help {
std::process::exit(0);
fn init_v8_flags(v8_flags: &[String]) {
let v8_flags_includes_help = v8_flags
.iter()
.any(|flag| flag == "-help" || flag == "--help");
let v8_flags = once("UNUSED_BUT_NECESSARY_ARG0".to_owned())
.chain(v8_flags.iter().cloned())
.collect::<Vec<_>>();
let unrecognized_v8_flags = v8_set_flags(v8_flags)
.into_iter()
.skip(1)
.collect::<Vec<_>>();
if !unrecognized_v8_flags.is_empty() {
for f in unrecognized_v8_flags {
eprintln!("error: V8 did not recognize flag '{}'", f);
}
eprintln!("\nFor a list of V8 flags, use '--v8-flags=--help'");
std::process::exit(1);
}
if v8_flags_includes_help {
std::process::exit(0);
}
}

let log_level = match flags.log_level {
fn init_logger(maybe_level: Option<Level>) {
let log_level = match maybe_level {
Some(level) => level,
None => Level::Info, // Default log level
};
Expand Down Expand Up @@ -853,8 +848,12 @@ pub fn main() {
}
})
.init();
}

let fut = match flags.clone().subcommand {
fn get_subcommand(
flags: Flags,
) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> {
match flags.clone().subcommand {
DenoSubcommand::Bundle {
source_file,
out_file,
Expand Down Expand Up @@ -914,15 +913,15 @@ pub fn main() {
eprintln!("{}", e);
std::process::exit(1);
}
return;
std::process::exit(0);
}
DenoSubcommand::Types => {
let types = get_types(flags.unstable);
if let Err(e) = write_to_stdout_ignore_sigpipe(types.as_bytes()) {
eprintln!("{}", e);
std::process::exit(1);
}
return;
std::process::exit(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fine for now, but ideally we would architect it so that exit is not called deeper inside the system

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I'm still looking for proper patterns

}
DenoSubcommand::Upgrade {
force,
Expand All @@ -934,9 +933,23 @@ pub fn main() {
tools::upgrade::upgrade_command(dry_run, force, version, output, ca_file)
.boxed_local()
}
};
}
}

pub fn main() {
#[cfg(windows)]
colors::enable_ansi(); // For Windows 10

let args: Vec<String> = env::args().collect();
let flags = flags::flags_from_vec(args);

if let Some(ref v8_flags) = flags.v8_flags {
init_v8_flags(v8_flags);
}
init_logger(flags.log_level);

let result = tokio_util::run_basic(fut);
let subcommand_future = get_subcommand(flags);
let result = tokio_util::run_basic(subcommand_future);
if let Err(err) = result {
eprintln!("{}: {}", colors::red_bold("error"), err.to_string());
std::process::exit(1);
Expand Down
4 changes: 2 additions & 2 deletions cli/ops/web_worker.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use crate::worker::WebWorkerHandle;
use crate::worker::WorkerEvent;
use crate::web_worker::WebWorkerHandle;
use crate::web_worker::WorkerEvent;
use deno_core::futures::channel::mpsc;
use deno_core::serde_json::json;

Expand Down
6 changes: 3 additions & 3 deletions cli/ops/worker_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::ops::io::get_stdio;
use crate::permissions::Permissions;
use crate::program_state::ProgramState;
use crate::tokio_util::create_basic_runtime;
use crate::worker::WebWorker;
use crate::worker::WebWorkerHandle;
use crate::worker::WorkerEvent;
use crate::web_worker::WebWorker;
use crate::web_worker::WebWorkerHandle;
use crate::web_worker::WorkerEvent;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::error::JsError;
Expand Down
19 changes: 9 additions & 10 deletions cli/tools/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::colors;
use crate::inspector::InspectorSession;
use crate::program_state::ProgramState;
use crate::worker::MainWorker;
use crate::worker::Worker;
use deno_core::error::AnyError;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
Expand Down Expand Up @@ -280,7 +279,7 @@ impl Highlighter for LineHighlighter {
}

async fn post_message_and_poll(
worker: &mut Worker,
worker: &mut MainWorker,
session: &mut InspectorSession,
method: &str,
params: Option<Value>,
Expand All @@ -305,7 +304,7 @@ async fn post_message_and_poll(
}

async fn read_line_and_poll(
worker: &mut Worker,
worker: &mut MainWorker,
session: &mut InspectorSession,
message_rx: &Receiver<(String, Option<Value>)>,
response_tx: &Sender<Result<Value, AnyError>>,
Expand Down Expand Up @@ -425,7 +424,7 @@ pub async fn run(

let history_file = program_state.dir.root.join("deno_history.txt");

post_message_and_poll(&mut *worker, &mut session, "Runtime.enable", None)
post_message_and_poll(&mut worker, &mut session, "Runtime.enable", None)
.await?;

// Enabling the runtime domain will always send trigger one executionContextCreated for each
Expand Down Expand Up @@ -474,7 +473,7 @@ pub async fn run(

while !is_closing(&mut worker, &mut session, context_id).await? {
let line = read_line_and_poll(
&mut *worker,
&mut worker,
&mut session,
&message_rx,
&response_tx,
Expand All @@ -495,7 +494,7 @@ pub async fn run(
};

let evaluate_response = post_message_and_poll(
&mut *worker,
&mut worker,
&mut session,
"Runtime.evaluate",
Some(json!({
Expand All @@ -513,7 +512,7 @@ pub async fn run(
&& wrapped_line != line
{
post_message_and_poll(
&mut *worker,
&mut worker,
&mut session,
"Runtime.evaluate",
Some(json!({
Expand All @@ -533,7 +532,7 @@ pub async fn run(

if evaluate_exception_details.is_some() {
post_message_and_poll(
&mut *worker,
&mut worker,
&mut session,
"Runtime.callFunctionOn",
Some(json!({
Expand All @@ -546,7 +545,7 @@ pub async fn run(
).await?;
} else {
post_message_and_poll(
&mut *worker,
&mut worker,
&mut session,
"Runtime.callFunctionOn",
Some(json!({
Expand All @@ -564,7 +563,7 @@ pub async fn run(
// Deno.inspectArgs.
let inspect_response =
post_message_and_poll(
&mut *worker,
&mut worker,
&mut session,
"Runtime.callFunctionOn",
Some(json!({
Expand Down
Loading