diff options
author | Naju Mancheril <najumancheril@users.noreply.github.com> | 2022-04-20 08:16:37 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-20 14:16:37 +0200 |
commit | 3833d37b15e1e8380efd1a9eea956a8b33745555 (patch) | |
tree | 1b4e14b138ad7623feec3ca483988cbf44d5db0b /cli/flags.rs | |
parent | ae479b1036630970c0d0aaf67cbb500a3c7ed622 (diff) |
feat(repl): add "--eval-file" flag to execute a script file on startup (#14247)
This commit adds support for "--eval-file" in "deno repl" subcommand.
This flag can be used to pass paths or URLs to files, that will be executed
on REPL startup. All files will be executed in the same context as the REPL
(ie. as "plain old scripts", not ES modules), sharing the global scope.
This feature allows to implement custom REPLs on top of Deno's REPL.
Diffstat (limited to 'cli/flags.rs')
-rw-r--r-- | cli/flags.rs | 76 |
1 files changed, 71 insertions, 5 deletions
diff --git a/cli/flags.rs b/cli/flags.rs index cdd90fa33..b0e4b32ed 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -144,6 +144,7 @@ pub struct LintFlags { #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub struct ReplFlags { + pub eval_files: Option<Vec<String>>, pub eval: Option<String>, } @@ -216,7 +217,10 @@ pub enum DenoSubcommand { impl Default for DenoSubcommand { fn default() -> DenoSubcommand { - DenoSubcommand::Repl(ReplFlags { eval: None }) + DenoSubcommand::Repl(ReplFlags { + eval_files: None, + eval: None, + }) } } @@ -556,7 +560,13 @@ where Some(("uninstall", m)) => uninstall_parse(&mut flags, m), Some(("upgrade", m)) => upgrade_parse(&mut flags, m), Some(("vendor", m)) => vendor_parse(&mut flags, m), - _ => handle_repl_flags(&mut flags, ReplFlags { eval: None }), + _ => handle_repl_flags( + &mut flags, + ReplFlags { + eval_files: None, + eval: None, + }, + ), } Ok(flags) @@ -1361,6 +1371,16 @@ fn repl_subcommand<'a>() -> Command<'a> { runtime_args(Command::new("repl"), false, true) .about("Read Eval Print Loop") .arg( + Arg::new("eval-file") + .long("eval-file") + .min_values(1) + .takes_value(true) + .use_value_delimiter(true) + .require_equals(true) + .help("Evaluates the provided file(s) as scripts when the REPL starts. Accepts file paths and URLs.") + .value_hint(ValueHint::AnyPath), + ) + .arg( Arg::new("eval") .long("eval") .help("Evaluates the provided code when the REPL starts.") @@ -2446,9 +2466,15 @@ fn repl_parse(flags: &mut Flags, matches: &clap::ArgMatches) { flags.type_check_mode = TypeCheckMode::None; runtime_args_parse(flags, matches, false, true); unsafely_ignore_certificate_errors_parse(flags, matches); + + let eval_files: Option<Vec<String>> = matches + .values_of("eval-file") + .map(|values| values.map(String::from).collect()); + handle_repl_flags( flags, ReplFlags { + eval_files, eval: matches.value_of("eval").map(ToOwned::to_owned), }, ); @@ -3884,7 +3910,10 @@ mod tests { r.unwrap(), Flags { repl: true, - subcommand: DenoSubcommand::Repl(ReplFlags { eval: None }), + subcommand: DenoSubcommand::Repl(ReplFlags { + eval_files: None, + eval: None + }), allow_net: Some(vec![]), unsafely_ignore_certificate_errors: None, allow_env: Some(vec![]), @@ -3906,7 +3935,10 @@ mod tests { r.unwrap(), Flags { repl: true, - subcommand: DenoSubcommand::Repl(ReplFlags { eval: None }), + subcommand: DenoSubcommand::Repl(ReplFlags { + eval_files: None, + eval: None + }), import_map_path: Some("import_map.json".to_string()), no_remote: true, config_path: Some("tsconfig.json".to_string()), @@ -3942,6 +3974,7 @@ mod tests { Flags { repl: true, subcommand: DenoSubcommand::Repl(ReplFlags { + eval_files: None, eval: Some("console.log('hello');".to_string()), }), allow_net: Some(vec![]), @@ -3958,6 +3991,35 @@ mod tests { } #[test] + fn repl_with_eval_file_flag() { + #[rustfmt::skip] + let r = flags_from_vec(svec!["deno", "repl", "--eval-file=./a.js,./b.ts,https://examples.deno.land/hello-world.ts"]); + assert_eq!( + r.unwrap(), + Flags { + repl: true, + subcommand: DenoSubcommand::Repl(ReplFlags { + eval_files: Some(vec![ + "./a.js".to_string(), + "./b.ts".to_string(), + "https://examples.deno.land/hello-world.ts".to_string() + ]), + eval: None, + }), + allow_net: Some(vec![]), + allow_env: Some(vec![]), + allow_run: Some(vec![]), + allow_read: Some(vec![]), + allow_write: Some(vec![]), + allow_ffi: Some(vec![]), + allow_hrtime: true, + type_check_mode: TypeCheckMode::None, + ..Flags::default() + } + ); + } + + #[test] fn allow_read_allowlist() { use test_util::TempDir; let temp_dir_guard = TempDir::new(); @@ -4590,6 +4652,7 @@ mod tests { Flags { repl: true, subcommand: DenoSubcommand::Repl(ReplFlags { + eval_files: None, eval: Some("console.log('hello');".to_string()), }), unsafely_ignore_certificate_errors: Some(vec![]), @@ -4663,7 +4726,10 @@ mod tests { r.unwrap(), Flags { repl: true, - subcommand: DenoSubcommand::Repl(ReplFlags { eval: None }), + subcommand: DenoSubcommand::Repl(ReplFlags { + eval_files: None, + eval: None + }), unsafely_ignore_certificate_errors: Some(svec![ "deno.land", "localhost", |