mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
Add deno eval subcommand (#2102)
This commit is contained in:
parent
d3bd5879c3
commit
591b5e4a7d
5 changed files with 75 additions and 43 deletions
82
cli/flags.rs
82
cli/flags.rs
|
@ -25,6 +25,7 @@ pub struct DenoFlags {
|
||||||
pub prefetch: bool,
|
pub prefetch: bool,
|
||||||
pub info: bool,
|
pub info: bool,
|
||||||
pub fmt: bool,
|
pub fmt: bool,
|
||||||
|
pub eval: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<ArgMatches<'a>> for DenoFlags {
|
impl<'a> From<ArgMatches<'a>> for DenoFlags {
|
||||||
|
@ -82,28 +83,26 @@ impl<'a> From<ArgMatches<'a>> for DenoFlags {
|
||||||
if matches.is_present("fmt") {
|
if matches.is_present("fmt") {
|
||||||
flags.fmt = true;
|
flags.fmt = true;
|
||||||
}
|
}
|
||||||
|
if matches.is_present("eval") {
|
||||||
|
flags.eval = true;
|
||||||
|
}
|
||||||
|
|
||||||
flags
|
flags
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
|
static ENV_VARIABLES_HELP: &str = "ENVIRONMENT VARIABLES:
|
||||||
pub fn set_flags(
|
|
||||||
args: Vec<String>,
|
|
||||||
) -> Result<(DenoFlags, Vec<String>), String> {
|
|
||||||
let app_settings: Vec<AppSettings> = vec![
|
|
||||||
AppSettings::AllowExternalSubcommands,
|
|
||||||
AppSettings::DisableHelpSubcommand,
|
|
||||||
];
|
|
||||||
|
|
||||||
let env_variables_help = "ENVIRONMENT VARIABLES:
|
|
||||||
DENO_DIR Set deno's base directory
|
DENO_DIR Set deno's base directory
|
||||||
NO_COLOR Set to disable color";
|
NO_COLOR Set to disable color";
|
||||||
|
|
||||||
let clap_app = App::new("deno")
|
fn create_cli_app<'a, 'b>() -> App<'a, 'b> {
|
||||||
|
let cli_app = App::new("deno")
|
||||||
|
.bin_name("deno")
|
||||||
.global_settings(&[AppSettings::ColorNever])
|
.global_settings(&[AppSettings::ColorNever])
|
||||||
.settings(&app_settings[..])
|
.settings(&[
|
||||||
.after_help(env_variables_help)
|
AppSettings::AllowExternalSubcommands,
|
||||||
|
AppSettings::DisableHelpSubcommand,
|
||||||
|
]).after_help(ENV_VARIABLES_HELP)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("version")
|
Arg::with_name("version")
|
||||||
.short("v")
|
.short("v")
|
||||||
|
@ -171,18 +170,25 @@ pub fn set_flags(
|
||||||
.long("prefetch")
|
.long("prefetch")
|
||||||
.help("Prefetch the dependencies"),
|
.help("Prefetch the dependencies"),
|
||||||
).subcommand(
|
).subcommand(
|
||||||
// TODO(bartlomieju): version is not handled properly
|
|
||||||
SubCommand::with_name("info")
|
SubCommand::with_name("info")
|
||||||
|
.setting(AppSettings::DisableVersion)
|
||||||
.about("Show source file related info")
|
.about("Show source file related info")
|
||||||
.arg(Arg::with_name("file").takes_value(true).required(true)),
|
.arg(Arg::with_name("file").takes_value(true).required(true)),
|
||||||
).subcommand(
|
).subcommand(
|
||||||
// TODO(bartlomieju): version is not handled properly
|
SubCommand::with_name("eval")
|
||||||
SubCommand::with_name("fmt").about("Format files").arg(
|
.setting(AppSettings::DisableVersion)
|
||||||
Arg::with_name("files")
|
.about("Eval script")
|
||||||
.takes_value(true)
|
.arg(Arg::with_name("code").takes_value(true).required(true)),
|
||||||
.multiple(true)
|
).subcommand(
|
||||||
.required(true),
|
SubCommand::with_name("fmt")
|
||||||
),
|
.setting(AppSettings::DisableVersion)
|
||||||
|
.about("Format files")
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("files")
|
||||||
|
.takes_value(true)
|
||||||
|
.multiple(true)
|
||||||
|
.required(true),
|
||||||
|
),
|
||||||
).subcommand(
|
).subcommand(
|
||||||
// this is a fake subcommand - it's used in conjunction with
|
// this is a fake subcommand - it's used in conjunction with
|
||||||
// AppSettings:AllowExternalSubcommand to treat it as an
|
// AppSettings:AllowExternalSubcommand to treat it as an
|
||||||
|
@ -190,43 +196,49 @@ pub fn set_flags(
|
||||||
SubCommand::with_name("<script>").about("Script to run"),
|
SubCommand::with_name("<script>").about("Script to run"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let matches = clap_app.get_matches_from(args);
|
cli_app
|
||||||
|
}
|
||||||
|
|
||||||
// TODO(bartomieju): compatibility with old "opts" approach - to be refactored
|
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
|
||||||
let mut rest: Vec<String> = vec![String::from("deno")];
|
pub fn set_flags(
|
||||||
|
args: Vec<String>,
|
||||||
|
) -> Result<(DenoFlags, Vec<String>), String> {
|
||||||
|
let mut rest_argv: Vec<String> = vec!["deno".to_string()];
|
||||||
|
let cli_app = create_cli_app();
|
||||||
|
let matches = cli_app.get_matches_from(args);
|
||||||
|
|
||||||
match matches.subcommand() {
|
match matches.subcommand() {
|
||||||
|
("eval", Some(info_match)) => {
|
||||||
|
let code: &str = info_match.value_of("code").unwrap();
|
||||||
|
rest_argv.extend(vec![code.to_string()]);
|
||||||
|
}
|
||||||
("info", Some(info_match)) => {
|
("info", Some(info_match)) => {
|
||||||
// TODO(bartlomieju): it still relies on `is_present("info")` check
|
|
||||||
// in `set_recognized_flags`
|
|
||||||
let file: &str = info_match.value_of("file").unwrap();
|
let file: &str = info_match.value_of("file").unwrap();
|
||||||
rest.extend(vec![file.to_string()]);
|
rest_argv.extend(vec![file.to_string()]);
|
||||||
}
|
}
|
||||||
("fmt", Some(fmt_match)) => {
|
("fmt", Some(fmt_match)) => {
|
||||||
// TODO(bartlomieju): it still relies on `is_present("fmt")` check
|
|
||||||
// in `set_recognized_flags`
|
|
||||||
let files: Vec<String> = fmt_match
|
let files: Vec<String> = fmt_match
|
||||||
.values_of("files")
|
.values_of("files")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
.collect();
|
.collect();
|
||||||
rest.extend(files);
|
rest_argv.extend(files);
|
||||||
}
|
}
|
||||||
(script, Some(script_match)) => {
|
(script, Some(script_match)) => {
|
||||||
rest.extend(vec![script.to_string()]);
|
rest_argv.extend(vec![script.to_string()]);
|
||||||
// check if there are any extra arguments
|
// check if there are any extra arguments that should
|
||||||
|
// be passed to script
|
||||||
if script_match.is_present("") {
|
if script_match.is_present("") {
|
||||||
let script_args: Vec<String> = script_match
|
let script_args: Vec<String> = script_match
|
||||||
.values_of("")
|
.values_of("")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
.collect();
|
.collect();
|
||||||
rest.extend(script_args);
|
rest_argv.extend(script_args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
// TODO: end
|
|
||||||
|
|
||||||
if matches.is_present("v8-options") {
|
if matches.is_present("v8-options") {
|
||||||
// display v8 help and exit
|
// display v8 help and exit
|
||||||
|
@ -246,7 +258,7 @@ pub fn set_flags(
|
||||||
}
|
}
|
||||||
|
|
||||||
let flags = DenoFlags::from(matches);
|
let flags = DenoFlags::from(matches);
|
||||||
Ok((flags, rest))
|
Ok((flags, rest_argv))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
32
cli/main.rs
32
cli/main.rs
|
@ -112,14 +112,30 @@ fn main() {
|
||||||
// Setup runtime.
|
// Setup runtime.
|
||||||
js_check(main_worker.execute("denoMain()"));
|
js_check(main_worker.execute("denoMain()"));
|
||||||
|
|
||||||
// Execute main module.
|
if state.flags.eval {
|
||||||
if let Some(main_module) = state.main_module() {
|
// Wrap provided script in async function so asynchronous methods
|
||||||
debug!("main_module {}", main_module);
|
// work. This is required until top-level await is not supported.
|
||||||
js_check(main_worker.execute_mod(&main_module, should_prefetch));
|
let js_source = format!(
|
||||||
if should_display_info {
|
"async function _topLevelWrapper(){{
|
||||||
// Display file info and exit. Do not run file
|
{}
|
||||||
main_worker.print_file_info(&main_module);
|
}}
|
||||||
std::process::exit(0);
|
_topLevelWrapper();
|
||||||
|
",
|
||||||
|
&state.argv[1]
|
||||||
|
);
|
||||||
|
// ATM imports in `deno eval` are not allowed
|
||||||
|
// TODO Support ES modules once Worker supports evaluating anonymous modules.
|
||||||
|
js_check(main_worker.execute(&js_source));
|
||||||
|
} else {
|
||||||
|
// Execute main module.
|
||||||
|
if let Some(main_module) = state.main_module() {
|
||||||
|
debug!("main_module {}", main_module);
|
||||||
|
js_check(main_worker.execute_mod(&main_module, should_prefetch));
|
||||||
|
if should_display_info {
|
||||||
|
// Display file info and exit. Do not run file
|
||||||
|
main_worker.print_file_info(&main_module);
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
tests/029_eval.out
Normal file
1
tests/029_eval.out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
hello
|
2
tests/029_eval.test
Normal file
2
tests/029_eval.test
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
args: eval console.log("hello")
|
||||||
|
output: tests/029_eval.out
|
|
@ -562,6 +562,7 @@ OPTIONS:
|
||||||
|
|
||||||
SUBCOMMANDS:
|
SUBCOMMANDS:
|
||||||
<script> Script to run
|
<script> Script to run
|
||||||
|
eval Eval script
|
||||||
fmt Format files
|
fmt Format files
|
||||||
info Show source file related info
|
info Show source file related info
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue