summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2020-05-21 10:35:36 -0400
committerRyan Dahl <ry@tinyclouds.org>2020-06-06 09:07:59 -0400
commit8a4533eb75ff505f8aa16c206af1ca13f0e6c166 (patch)
tree2c0b7dec8df5dba684b9f35f2bfbbe9fd51ff7d4
parent2093ee55d4c728448a3db373dd416b6eea845c14 (diff)
feat: deno eval -p (#5682)
-rw-r--r--cli/flags.rs40
-rw-r--r--cli/main.rs13
-rw-r--r--cli/tests/integration_tests.rs17
3 files changed, 68 insertions, 2 deletions
diff --git a/cli/flags.rs b/cli/flags.rs
index 2c201fcb6..9bf838bfa 100644
--- a/cli/flags.rs
+++ b/cli/flags.rs
@@ -28,6 +28,7 @@ pub enum DenoSubcommand {
filter: Option<String>,
},
Eval {
+ print: bool,
code: String,
as_typescript: bool,
},
@@ -430,7 +431,9 @@ fn eval_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
flags.allow_hrtime = true;
let code = matches.value_of("code").unwrap().to_string();
let as_typescript = matches.is_present("ts");
+ let print = matches.is_present("print");
flags.subcommand = DenoSubcommand::Eval {
+ print,
code,
as_typescript,
}
@@ -744,6 +747,14 @@ This command has implicit access to all permissions (--allow-all).",
.takes_value(false)
.multiple(false),
)
+ .arg(
+ Arg::with_name("print")
+ .long("print")
+ .short("p")
+ .help("print result to stdout")
+ .takes_value(false)
+ .multiple(false),
+ )
.arg(Arg::with_name("code").takes_value(true).required(true))
.arg(v8_flags_arg())
}
@@ -1693,6 +1704,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Eval {
+ print: false,
code: "'console.log(\"hello\")'".to_string(),
as_typescript: false,
},
@@ -1709,6 +1721,29 @@ mod tests {
}
#[test]
+ fn eval_p() {
+ let r = flags_from_vec_safe(svec!["deno", "eval", "-p", "1+2"]);
+ assert_eq!(
+ r.unwrap(),
+ Flags {
+ subcommand: DenoSubcommand::Eval {
+ print: true,
+ code: "1+2".to_string(),
+ as_typescript: false,
+ },
+ allow_net: true,
+ allow_env: true,
+ allow_run: true,
+ allow_read: true,
+ allow_write: true,
+ allow_plugin: true,
+ allow_hrtime: true,
+ ..Flags::default()
+ }
+ );
+ }
+
+ #[test]
fn eval_unstable() {
let r = flags_from_vec_safe(svec![
"deno",
@@ -1721,6 +1756,7 @@ mod tests {
Flags {
unstable: true,
subcommand: DenoSubcommand::Eval {
+ print: false,
code: "'console.log(\"hello\")'".to_string(),
as_typescript: false,
},
@@ -1748,6 +1784,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Eval {
+ print: false,
code: "'console.log(\"hello\")'".to_string(),
as_typescript: true,
},
@@ -1771,6 +1808,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Eval {
+ print: false,
code: "42".to_string(),
as_typescript: false,
},
@@ -2456,6 +2494,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Eval {
+ print: false,
code: "console.log('hello world')".to_string(),
as_typescript: false,
},
@@ -2484,6 +2523,7 @@ mod tests {
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Eval {
+ print: false,
code: "const foo = 'bar'".to_string(),
as_typescript: false,
},
diff --git a/cli/main.rs b/cli/main.rs
index 6d9aacf37..7da0e2df3 100644
--- a/cli/main.rs
+++ b/cli/main.rs
@@ -333,6 +333,7 @@ async fn eval_command(
flags: Flags,
code: String,
as_typescript: bool,
+ print: bool,
) -> Result<(), ErrBox> {
// Force TypeScript compile.
let main_module =
@@ -341,6 +342,13 @@ async fn eval_command(
let mut worker = MainWorker::create(global_state, main_module.clone())?;
let main_module_url = main_module.as_url().to_owned();
// Create a dummy source file.
+ let source_code = if print {
+ "console.log(".to_string() + &code + ")"
+ } else {
+ code.clone()
+ }
+ .into_bytes();
+
let source_file = SourceFile {
filename: main_module_url.to_file_path().unwrap(),
url: main_module_url,
@@ -351,7 +359,7 @@ async fn eval_command(
} else {
MediaType::JavaScript
},
- source_code: code.clone().into_bytes(),
+ source_code,
};
// Save our fake file into file fetcher cache
// to allow module access by TS compiler (e.g. op_fetch_source_files)
@@ -627,9 +635,10 @@ pub fn main() {
filter,
} => doc_command(flags, source_file, json, filter).boxed_local(),
DenoSubcommand::Eval {
+ print,
code,
as_typescript,
- } => eval_command(flags, code, as_typescript).boxed_local(),
+ } => eval_command(flags, code, as_typescript, print).boxed_local(),
DenoSubcommand::Cache { files } => {
cache_command(flags, files).boxed_local()
}
diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs
index 54d88b874..450c26538 100644
--- a/cli/tests/integration_tests.rs
+++ b/cli/tests/integration_tests.rs
@@ -54,6 +54,23 @@ fn x_deno_warning() {
}
#[test]
+fn eval_p() {
+ let output = util::deno_cmd()
+ .arg("eval")
+ .arg("-p")
+ .arg("1+2")
+ .stdout(std::process::Stdio::piped())
+ .spawn()
+ .unwrap()
+ .wait_with_output()
+ .unwrap();
+ assert!(output.status.success());
+ let stdout_str =
+ util::strip_ansi_codes(std::str::from_utf8(&output.stdout).unwrap().trim());
+ assert_eq!("3", stdout_str);
+}
+
+#[test]
fn no_color() {
let output = util::deno_cmd()
.current_dir(util::root_path())