1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

feat: deno eval -p (#5682)

This commit is contained in:
Ryan Dahl 2020-05-21 10:35:36 -04:00
parent 2093ee55d4
commit 8a4533eb75
3 changed files with 68 additions and 2 deletions

View file

@ -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,
},
@ -1708,6 +1720,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![
@ -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,
},

View file

@ -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()
}

View file

@ -53,6 +53,23 @@ fn x_deno_warning() {
drop(g);
}
#[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()