From 44ca3ce6aeb99cb968776f5f13420e76acbc8117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 4 Sep 2021 01:33:35 +0200 Subject: [PATCH] refactor: factor out DenoSubcommand enum variant into structs (#11896) This commit refactors "DenoSubcommand" enum in a way that variants no longer contain anonymous structures but instead contain dedicated structures for each subcommand, eg. "DenoSubcommand::Lint" now contains "LintSubcommand". --- cli/flags.rs | 651 +++++++++++++++++++++------------------- cli/main.rs | 383 +++++++++++------------ cli/tools/standalone.rs | 5 +- 3 files changed, 520 insertions(+), 519 deletions(-) diff --git a/cli/flags.rs b/cli/flags.rs index 03167adc6d..8bfcbb3807 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -32,100 +32,145 @@ lazy_static::lazy_static! { ); } +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct BundleFlags { + pub source_file: String, + pub out_file: Option, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct CacheFlags { + pub files: Vec, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct CompileFlags { + pub source_file: String, + pub output: Option, + pub args: Vec, + pub target: Option, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct CompletionsFlags { + pub buf: Box<[u8]>, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct CoverageFlags { + pub files: Vec, + pub ignore: Vec, + pub include: Vec, + pub exclude: Vec, + pub lcov: bool, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct DocFlags { + pub private: bool, + pub json: bool, + pub source_file: Option, + pub filter: Option, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct EvalFlags { + pub print: bool, + pub code: String, + pub ext: String, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct FmtFlags { + pub check: bool, + pub files: Vec, + pub ignore: Vec, + pub ext: String, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct InfoFlags { + pub json: bool, + pub file: Option, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct InstallFlags { + pub module_url: String, + pub args: Vec, + pub name: Option, + pub root: Option, + pub force: bool, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct LintFlags { + pub files: Vec, + pub ignore: Vec, + pub rules: bool, + pub rules_tags: Vec, + pub rules_include: Vec, + pub rules_exclude: Vec, + pub json: bool, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct ReplFlags { + pub eval: Option, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct RunFlags { + pub script: String, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct TestFlags { + pub ignore: Vec, + pub doc: bool, + pub no_run: bool, + pub fail_fast: Option, + pub allow_none: bool, + pub include: Option>, + pub filter: Option, + pub shuffle: Option, + pub concurrent_jobs: NonZeroUsize, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct UpgradeFlags { + pub dry_run: bool, + pub force: bool, + pub canary: bool, + pub version: Option, + pub output: Option, + pub ca_file: Option, +} + #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub enum DenoSubcommand { - Bundle { - source_file: String, - out_file: Option, - }, - Cache { - files: Vec, - }, - Compile { - source_file: String, - output: Option, - args: Vec, - target: Option, - }, - Completions { - buf: Box<[u8]>, - }, - Coverage { - files: Vec, - ignore: Vec, - include: Vec, - exclude: Vec, - lcov: bool, - }, - Doc { - private: bool, - json: bool, - source_file: Option, - filter: Option, - }, - Eval { - print: bool, - code: String, - ext: String, - }, - Fmt { - check: bool, - files: Vec, - ignore: Vec, - ext: String, - }, - Info { - json: bool, - file: Option, - }, - Install { - module_url: String, - args: Vec, - name: Option, - root: Option, - force: bool, - }, + Bundle(BundleFlags), + Cache(CacheFlags), + Compile(CompileFlags), + Completions(CompletionsFlags), + Coverage(CoverageFlags), + Doc(DocFlags), + Eval(EvalFlags), + Fmt(FmtFlags), + Info(InfoFlags), + Install(InstallFlags), Lsp, - Lint { - files: Vec, - ignore: Vec, - rules: bool, - rules_tags: Vec, - rules_include: Vec, - rules_exclude: Vec, - json: bool, - }, - Repl { - eval: Option, - }, - Run { - script: String, - }, - Test { - ignore: Vec, - doc: bool, - no_run: bool, - fail_fast: Option, - allow_none: bool, - include: Option>, - filter: Option, - shuffle: Option, - concurrent_jobs: NonZeroUsize, - }, + Lint(LintFlags), + Repl(ReplFlags), + Run(RunFlags), + Test(TestFlags), Types, - Upgrade { - dry_run: bool, - force: bool, - canary: bool, - version: Option, - output: Option, - ca_file: Option, - }, + Upgrade(UpgradeFlags), } impl Default for DenoSubcommand { fn default() -> DenoSubcommand { - DenoSubcommand::Repl { eval: None } + DenoSubcommand::Repl(ReplFlags { eval: None }) } } @@ -1560,10 +1605,10 @@ fn bundle_parse(flags: &mut Flags, matches: &clap::ArgMatches) { flags.watch = matches.is_present("watch"); - flags.subcommand = DenoSubcommand::Bundle { + flags.subcommand = DenoSubcommand::Bundle(BundleFlags { source_file, out_file, - }; + }); } fn cache_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -1573,7 +1618,7 @@ fn cache_parse(flags: &mut Flags, matches: &clap::ArgMatches) { .unwrap() .map(String::from) .collect(); - flags.subcommand = DenoSubcommand::Cache { files }; + flags.subcommand = DenoSubcommand::Cache(CacheFlags { files }); } fn compile_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -1590,12 +1635,12 @@ fn compile_parse(flags: &mut Flags, matches: &clap::ArgMatches) { let output = matches.value_of("output").map(PathBuf::from); let target = matches.value_of("target").map(String::from); - flags.subcommand = DenoSubcommand::Compile { + flags.subcommand = DenoSubcommand::Compile(CompileFlags { source_file, output, args, target, - }; + }); } fn completions_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -1607,9 +1652,9 @@ fn completions_parse(flags: &mut Flags, matches: &clap::ArgMatches) { &mut buf, ); - flags.subcommand = DenoSubcommand::Completions { + flags.subcommand = DenoSubcommand::Completions(CompletionsFlags { buf: buf.into_boxed_slice(), - }; + }); } fn coverage_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -1630,13 +1675,13 @@ fn coverage_parse(flags: &mut Flags, matches: &clap::ArgMatches) { None => vec![], }; let lcov = matches.is_present("lcov"); - flags.subcommand = DenoSubcommand::Coverage { + flags.subcommand = DenoSubcommand::Coverage(CoverageFlags { files, ignore, include, exclude, lcov, - }; + }); } fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -1647,12 +1692,12 @@ fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) { let private = matches.is_present("private"); let json = matches.is_present("json"); let filter = matches.value_of("filter").map(String::from); - flags.subcommand = DenoSubcommand::Doc { + flags.subcommand = DenoSubcommand::Doc(DocFlags { source_file, json, filter, private, - }; + }); } fn eval_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -1684,7 +1729,7 @@ fn eval_parse(flags: &mut Flags, matches: &clap::ArgMatches) { for v in code_args { flags.argv.push(v); } - flags.subcommand = DenoSubcommand::Eval { print, code, ext }; + flags.subcommand = DenoSubcommand::Eval(EvalFlags { print, code, ext }); } fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -1699,12 +1744,12 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) { }; let ext = matches.value_of("ext").unwrap().to_string(); - flags.subcommand = DenoSubcommand::Fmt { + flags.subcommand = DenoSubcommand::Fmt(FmtFlags { check: matches.is_present("check"), ext, files, ignore, - } + }); } fn info_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -1713,10 +1758,10 @@ fn info_parse(flags: &mut Flags, matches: &clap::ArgMatches) { location_arg_parse(flags, matches); ca_file_arg_parse(flags, matches); let json = matches.is_present("json"); - flags.subcommand = DenoSubcommand::Info { + flags.subcommand = DenoSubcommand::Info(InfoFlags { file: matches.value_of("file").map(|f| f.to_string()), json, - }; + }); } fn install_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -1740,13 +1785,13 @@ fn install_parse(flags: &mut Flags, matches: &clap::ArgMatches) { let module_url = cmd[0].to_string(); let args = cmd[1..].to_vec(); - flags.subcommand = DenoSubcommand::Install { + flags.subcommand = DenoSubcommand::Install(InstallFlags { name, module_url, args, root, force, - }; + }); } fn lsp_parse(flags: &mut Flags, _matches: &clap::ArgMatches) { @@ -1777,7 +1822,7 @@ fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) { None => vec![], }; let json = matches.is_present("json"); - flags.subcommand = DenoSubcommand::Lint { + flags.subcommand = DenoSubcommand::Lint(LintFlags { files, rules, rules_tags, @@ -1785,15 +1830,15 @@ fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) { rules_exclude, ignore, json, - }; + }); } fn repl_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, false, true); flags.repl = true; - flags.subcommand = DenoSubcommand::Repl { + flags.subcommand = DenoSubcommand::Repl(ReplFlags { eval: matches.value_of("eval").map(ToOwned::to_owned), - }; + }); flags.allow_net = Some(vec![]); flags.allow_env = Some(vec![]); flags.allow_run = Some(vec![]); @@ -1819,7 +1864,7 @@ fn run_parse(flags: &mut Flags, matches: &clap::ArgMatches) { } flags.watch = matches.is_present("watch"); - flags.subcommand = DenoSubcommand::Run { script }; + flags.subcommand = DenoSubcommand::Run(RunFlags { script }); } fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -1893,7 +1938,7 @@ fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) { flags.coverage_dir = matches.value_of("coverage").map(String::from); flags.watch = matches.is_present("watch"); - flags.subcommand = DenoSubcommand::Test { + flags.subcommand = DenoSubcommand::Test(TestFlags { no_run, doc, fail_fast, @@ -1903,7 +1948,7 @@ fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) { shuffle, allow_none, concurrent_jobs, - }; + }); } fn types_parse(flags: &mut Flags, _matches: &clap::ArgMatches) { @@ -1924,14 +1969,14 @@ fn upgrade_parse(flags: &mut Flags, matches: &clap::ArgMatches) { None }; let ca_file = matches.value_of("cert").map(|s| s.to_string()); - flags.subcommand = DenoSubcommand::Upgrade { + flags.subcommand = DenoSubcommand::Upgrade(UpgradeFlags { dry_run, force, canary, version, output, ca_file, - }; + }); } fn compile_args_parse(flags: &mut Flags, matches: &clap::ArgMatches) { @@ -2183,9 +2228,9 @@ mod tests { assert_eq!( flags, Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), unstable: true, log_level: Some(Level::Error), ..Flags::default() @@ -2204,14 +2249,14 @@ mod tests { assert_eq!( flags, Flags { - subcommand: DenoSubcommand::Upgrade { + subcommand: DenoSubcommand::Upgrade(UpgradeFlags { force: true, dry_run: true, canary: false, version: None, output: None, ca_file: None, - }, + }), ..Flags::default() } ); @@ -2232,9 +2277,9 @@ mod tests { assert_eq!( flags, Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), reload: true, ..Flags::default() } @@ -2248,9 +2293,9 @@ mod tests { assert_eq!( flags, Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), watch: true, ..Flags::default() } @@ -2265,9 +2310,9 @@ mod tests { r.unwrap(), Flags { reload: true, - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), allow_write: Some(vec![]), ..Flags::default() } @@ -2280,9 +2325,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "_".to_string(), - }, + }), v8_flags: svec!["--help"], ..Flags::default() } @@ -2297,9 +2342,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), v8_flags: svec!["--expose-gc", "--gc-stats=1"], ..Flags::default() } @@ -2319,9 +2364,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "gist.ts".to_string(), - }, + }), argv: svec!["--title", "X"], allow_net: Some(vec![]), ..Flags::default() @@ -2335,9 +2380,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "gist.ts".to_string(), - }, + }), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), @@ -2356,9 +2401,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "gist.ts".to_string(), - }, + }), allow_read: Some(vec![]), ..Flags::default() } @@ -2371,9 +2416,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "gist.ts".to_string(), - }, + }), allow_hrtime: true, ..Flags::default() } @@ -2397,9 +2442,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), argv: svec!["--", "-D", "--allow-net"], allow_write: Some(vec![]), ..Flags::default() @@ -2413,7 +2458,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Fmt { + subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: false, files: vec![ @@ -2421,7 +2466,7 @@ mod tests { PathBuf::from("script_2.ts") ], ext: "ts".to_string() - }, + }), ..Flags::default() } ); @@ -2430,12 +2475,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Fmt { + subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: true, files: vec![], ext: "ts".to_string(), - }, + }), ..Flags::default() } ); @@ -2444,12 +2489,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Fmt { + subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: false, files: vec![], ext: "ts".to_string(), - }, + }), ..Flags::default() } ); @@ -2458,12 +2503,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Fmt { + subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: false, files: vec![], ext: "ts".to_string(), - }, + }), watch: true, ..Flags::default() } @@ -2480,12 +2525,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Fmt { + subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![PathBuf::from("bar.js")], check: true, files: vec![PathBuf::from("foo.ts")], ext: "ts".to_string(), - }, + }), watch: true, ..Flags::default() } @@ -2498,7 +2543,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Lint { + subcommand: DenoSubcommand::Lint(LintFlags { files: vec![ PathBuf::from("script_1.ts"), PathBuf::from("script_2.ts") @@ -2509,7 +2554,7 @@ mod tests { rules_exclude: vec![], json: false, ignore: vec![], - }, + }), ..Flags::default() } ); @@ -2519,7 +2564,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Lint { + subcommand: DenoSubcommand::Lint(LintFlags { files: vec![], rules: false, rules_tags: vec![], @@ -2530,7 +2575,7 @@ mod tests { PathBuf::from("script_1.ts"), PathBuf::from("script_2.ts") ], - }, + }), ..Flags::default() } ); @@ -2539,7 +2584,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Lint { + subcommand: DenoSubcommand::Lint(LintFlags { files: vec![], rules: true, rules_tags: vec![], @@ -2547,7 +2592,7 @@ mod tests { rules_exclude: vec![], json: false, ignore: vec![], - }, + }), ..Flags::default() } ); @@ -2562,7 +2607,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Lint { + subcommand: DenoSubcommand::Lint(LintFlags { files: vec![], rules: false, rules_tags: svec![""], @@ -2570,7 +2615,7 @@ mod tests { rules_exclude: svec!["no-const-assign"], json: false, ignore: vec![], - }, + }), ..Flags::default() } ); @@ -2579,7 +2624,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Lint { + subcommand: DenoSubcommand::Lint(LintFlags { files: vec![PathBuf::from("script_1.ts")], rules: false, rules_tags: vec![], @@ -2587,7 +2632,7 @@ mod tests { rules_exclude: vec![], json: true, ignore: vec![], - }, + }), ..Flags::default() } ); @@ -2603,7 +2648,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Lint { + subcommand: DenoSubcommand::Lint(LintFlags { files: vec![PathBuf::from("script_1.ts")], rules: false, rules_tags: vec![], @@ -2611,7 +2656,7 @@ mod tests { rules_exclude: vec![], json: true, ignore: vec![], - }, + }), config_path: Some("Deno.jsonc".to_string()), ..Flags::default() } @@ -2636,9 +2681,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Cache { + subcommand: DenoSubcommand::Cache(CacheFlags { files: svec!["script.ts"], - }, + }), ..Flags::default() } ); @@ -2650,10 +2695,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Info { + subcommand: DenoSubcommand::Info(InfoFlags { json: false, file: Some("script.ts".to_string()), - }, + }), ..Flags::default() } ); @@ -2662,10 +2707,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Info { + subcommand: DenoSubcommand::Info(InfoFlags { json: false, file: Some("script.ts".to_string()), - }, + }), reload: true, ..Flags::default() } @@ -2675,10 +2720,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Info { + subcommand: DenoSubcommand::Info(InfoFlags { json: true, file: Some("script.ts".to_string()), - }, + }), ..Flags::default() } ); @@ -2687,10 +2732,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Info { + subcommand: DenoSubcommand::Info(InfoFlags { json: false, file: None - }, + }), ..Flags::default() } ); @@ -2699,10 +2744,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Info { + subcommand: DenoSubcommand::Info(InfoFlags { json: true, file: None - }, + }), ..Flags::default() } ); @@ -2715,9 +2760,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), config_path: Some("tsconfig.json".to_owned()), ..Flags::default() } @@ -2730,11 +2775,11 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Eval { + subcommand: DenoSubcommand::Eval(EvalFlags { print: false, code: "'console.log(\"hello\")'".to_string(), ext: "js".to_string(), - }, + }), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), @@ -2753,11 +2798,11 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Eval { + subcommand: DenoSubcommand::Eval(EvalFlags { print: true, code: "1+2".to_string(), ext: "js".to_string(), - }, + }), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), @@ -2777,11 +2822,11 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Eval { + subcommand: DenoSubcommand::Eval(EvalFlags { print: false, code: "'console.log(\"hello\")'".to_string(), ext: "ts".to_string(), - }, + }), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), @@ -2801,11 +2846,11 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Eval { + subcommand: DenoSubcommand::Eval(EvalFlags { print: false, code: "42".to_string(), ext: "js".to_string(), - }, + }), import_map_path: Some("import_map.json".to_string()), no_remote: true, config_path: Some("tsconfig.json".to_string()), @@ -2843,11 +2888,11 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Eval { + subcommand: DenoSubcommand::Eval(EvalFlags { print: false, code: "console.log(Deno.args)".to_string(), ext: "js".to_string(), - }, + }), argv: svec!["arg1", "arg2"], allow_net: Some(vec![]), allow_env: Some(vec![]), @@ -2868,7 +2913,7 @@ mod tests { r.unwrap(), Flags { repl: true, - subcommand: DenoSubcommand::Repl { eval: None }, + subcommand: DenoSubcommand::Repl(ReplFlags { eval: None }), allow_net: Some(vec![]), unsafely_ignore_certificate_errors: None, allow_env: Some(vec![]), @@ -2890,7 +2935,7 @@ mod tests { r.unwrap(), Flags { repl: true, - subcommand: DenoSubcommand::Repl { eval: None }, + subcommand: DenoSubcommand::Repl(ReplFlags { eval: None }), import_map_path: Some("import_map.json".to_string()), no_remote: true, config_path: Some("tsconfig.json".to_string()), @@ -2924,9 +2969,9 @@ mod tests { r.unwrap(), Flags { repl: true, - subcommand: DenoSubcommand::Repl { + subcommand: DenoSubcommand::Repl(ReplFlags { eval: Some("console.log('hello');".to_string()), - }, + }), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), @@ -2954,9 +2999,9 @@ mod tests { r.unwrap(), Flags { allow_read: Some(vec![PathBuf::from("."), temp_dir]), - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), ..Flags::default() } ); @@ -2977,9 +3022,9 @@ mod tests { r.unwrap(), Flags { allow_write: Some(vec![PathBuf::from("."), temp_dir]), - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), ..Flags::default() } ); @@ -2996,9 +3041,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), allow_net: Some(svec!["127.0.0.1"]), ..Flags::default() } @@ -3012,9 +3057,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), allow_env: Some(svec!["HOME"]), ..Flags::default() } @@ -3032,9 +3077,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), allow_env: Some(svec!["HOME", "PATH"]), ..Flags::default() } @@ -3060,10 +3105,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Bundle { + subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: None, - }, + }), ..Flags::default() } ); @@ -3083,10 +3128,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Bundle { + subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: Some(PathBuf::from("bundle.js")), - }, + }), allow_write: Some(vec![]), no_remote: true, config_path: Some("tsconfig.json".to_owned()), @@ -3101,10 +3146,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Bundle { + subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: Some(PathBuf::from("bundle.js")), - }, + }), allow_write: Some(vec![]), ..Flags::default() } @@ -3123,10 +3168,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Bundle { + subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: None, - }, + }), lock_write: true, lock: Some(PathBuf::from("lock.json")), ..Flags::default() @@ -3141,10 +3186,10 @@ mod tests { r.unwrap(), Flags { reload: true, - subcommand: DenoSubcommand::Bundle { + subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: None, - }, + }), ..Flags::default() } ); @@ -3157,10 +3202,10 @@ mod tests { assert_eq!( r, Flags { - subcommand: DenoSubcommand::Bundle { + subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "script.ts".to_string(), out_file: None, - }, + }), no_check: true, ..Flags::default() } @@ -3173,10 +3218,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Bundle { + subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: None, - }, + }), watch: true, ..Flags::default() } @@ -3194,9 +3239,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), import_map_path: Some("import_map.json".to_owned()), ..Flags::default() } @@ -3214,10 +3259,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Info { + subcommand: DenoSubcommand::Info(InfoFlags { file: Some("script.ts".to_string()), json: false, - }, + }), import_map_path: Some("import_map.json".to_owned()), ..Flags::default() } @@ -3235,9 +3280,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Cache { + subcommand: DenoSubcommand::Cache(CacheFlags { files: svec!["script.ts"], - }, + }), import_map_path: Some("import_map.json".to_owned()), ..Flags::default() } @@ -3255,12 +3300,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Doc { + subcommand: DenoSubcommand::Doc(DocFlags { source_file: Some("script.ts".to_owned()), private: false, json: false, filter: None, - }, + }), import_map_path: Some("import_map.json".to_owned()), ..Flags::default() } @@ -3274,9 +3319,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Cache { + subcommand: DenoSubcommand::Cache(CacheFlags { files: svec!["script.ts", "script_two.ts"], - }, + }), ..Flags::default() } ); @@ -3288,9 +3333,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), seed: Some(250_u64), v8_flags: svec!["--random-seed=250"], ..Flags::default() @@ -3311,9 +3356,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), seed: Some(250_u64), v8_flags: svec!["--expose-gc", "--random-seed=250"], ..Flags::default() @@ -3331,13 +3376,13 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Install { + subcommand: DenoSubcommand::Install(InstallFlags { name: None, module_url: "https://deno.land/std/examples/colors.ts".to_string(), args: vec![], root: None, force: false, - }, + }), ..Flags::default() } ); @@ -3350,13 +3395,13 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Install { + subcommand: DenoSubcommand::Install(InstallFlags { name: Some("file_server".to_string()), module_url: "https://deno.land/std/http/file_server.ts".to_string(), args: svec!["foo", "bar"], root: Some(PathBuf::from("/foo")), force: true, - }, + }), import_map_path: Some("import_map.json".to_string()), no_remote: true, config_path: Some("tsconfig.json".to_string()), @@ -3384,9 +3429,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), log_level: Some(Level::Debug), ..Flags::default() } @@ -3399,9 +3444,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), log_level: Some(Level::Error), ..Flags::default() } @@ -3413,7 +3458,9 @@ mod tests { let r = flags_from_vec(svec!["deno", "completions", "zsh"]).unwrap(); match r.subcommand { - DenoSubcommand::Completions { buf } => assert!(!buf.is_empty()), + DenoSubcommand::Completions(CompletionsFlags { buf }) => { + assert!(!buf.is_empty()) + } _ => unreachable!(), } } @@ -3430,9 +3477,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), argv: svec!["--allow-read", "--allow-net"], ..Flags::default() } @@ -3453,9 +3500,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), location: Some(Url::parse("https://foo/").unwrap()), allow_read: Some(vec![]), argv: svec!["--allow-net", "-r", "--help", "--foo", "bar"], @@ -3467,9 +3514,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), argv: svec!["foo", "bar"], ..Flags::default() } @@ -3478,9 +3525,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), argv: svec!["-"], ..Flags::default() } @@ -3491,9 +3538,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), argv: svec!["-", "foo", "bar"], ..Flags::default() } @@ -3506,9 +3553,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), no_check: true, ..Flags::default() } @@ -3526,9 +3573,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), unsafely_ignore_certificate_errors: Some(vec![]), ..Flags::default() } @@ -3546,9 +3593,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), unsafely_ignore_certificate_errors: Some(svec![ "deno.land", "localhost", @@ -3568,9 +3615,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), no_remote: true, ..Flags::default() } @@ -3583,9 +3630,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), cached_only: true, ..Flags::default() } @@ -3603,9 +3650,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), allow_net: Some(svec![ "deno.land", "0.0.0.0:8000", @@ -3631,9 +3678,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), allow_net: Some(svec![ "deno.land", "deno.land:80", @@ -3663,9 +3710,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), lock_write: true, lock: Some(PathBuf::from("lock.json")), ..Flags::default() @@ -3680,7 +3727,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Test { + subcommand: DenoSubcommand::Test(TestFlags { no_run: true, doc: false, fail_fast: None, @@ -3690,7 +3737,7 @@ mod tests { ignore: vec![], shuffle: None, concurrent_jobs: NonZeroUsize::new(1).unwrap(), - }, + }), unstable: true, coverage_dir: Some("cov".to_string()), location: Some(Url::parse("https://foo/").unwrap()), @@ -3713,9 +3760,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), ca_file: Some("example.crt".to_owned()), ..Flags::default() } @@ -3733,9 +3780,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), - }, + }), enable_testing_features: true, ..Flags::default() } @@ -3748,7 +3795,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Test { + subcommand: DenoSubcommand::Test(TestFlags { no_run: false, doc: false, fail_fast: None, @@ -3758,7 +3805,7 @@ mod tests { include: None, ignore: vec![], concurrent_jobs: NonZeroUsize::new(4).unwrap(), - }, + }), ..Flags::default() } ); @@ -3773,7 +3820,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Test { + subcommand: DenoSubcommand::Test(TestFlags { no_run: false, doc: false, fail_fast: Some(NonZeroUsize::new(3).unwrap()), @@ -3783,7 +3830,7 @@ mod tests { include: None, ignore: vec![], concurrent_jobs: NonZeroUsize::new(1).unwrap(), - }, + }), ..Flags::default() } ); @@ -3802,7 +3849,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Test { + subcommand: DenoSubcommand::Test(TestFlags { no_run: false, doc: false, fail_fast: None, @@ -3812,7 +3859,7 @@ mod tests { include: None, ignore: vec![], concurrent_jobs: NonZeroUsize::new(1).unwrap(), - }, + }), enable_testing_features: true, ..Flags::default() } @@ -3825,7 +3872,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Test { + subcommand: DenoSubcommand::Test(TestFlags { no_run: false, doc: false, fail_fast: None, @@ -3835,7 +3882,7 @@ mod tests { include: None, ignore: vec![], concurrent_jobs: NonZeroUsize::new(1).unwrap(), - }, + }), watch: false, ..Flags::default() } @@ -3848,7 +3895,7 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Test { + subcommand: DenoSubcommand::Test(TestFlags { no_run: false, doc: false, fail_fast: None, @@ -3858,7 +3905,7 @@ mod tests { include: None, ignore: vec![], concurrent_jobs: NonZeroUsize::new(1).unwrap(), - }, + }), watch: true, ..Flags::default() } @@ -3877,10 +3924,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Bundle { + subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: None, - }, + }), ca_file: Some("example.crt".to_owned()), ..Flags::default() } @@ -3893,14 +3940,14 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Upgrade { + subcommand: DenoSubcommand::Upgrade(UpgradeFlags { force: false, dry_run: false, canary: false, version: None, output: None, ca_file: Some("example.crt".to_owned()), - }, + }), ca_file: Some("example.crt".to_owned()), ..Flags::default() } @@ -3920,9 +3967,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Cache { + subcommand: DenoSubcommand::Cache(CacheFlags { files: svec!["script.ts", "script_two.ts"], - }, + }), ca_file: Some("example.crt".to_owned()), ..Flags::default() } @@ -3941,10 +3988,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Info { + subcommand: DenoSubcommand::Info(InfoFlags { json: false, file: Some("https://example.com".to_string()), - }, + }), ca_file: Some("example.crt".to_owned()), ..Flags::default() } @@ -3957,12 +4004,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Doc { + subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: true, source_file: Some("path/to/module.ts".to_string()), filter: None, - }, + }), ..Flags::default() } ); @@ -3976,12 +4023,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Doc { + subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, source_file: Some("path/to/module.ts".to_string()), filter: Some("SomeClass.someField".to_string()), - }, + }), ..Flags::default() } ); @@ -3990,12 +4037,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Doc { + subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, source_file: None, filter: None, - }, + }), ..Flags::default() } ); @@ -4004,12 +4051,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Doc { + subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, source_file: Some("--builtin".to_string()), filter: Some("Deno.Listener".to_string()), - }, + }), ..Flags::default() } ); @@ -4019,12 +4066,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Doc { + subcommand: DenoSubcommand::Doc(DocFlags { private: true, json: false, source_file: Some("path/to/module.js".to_string()), filter: None, - }, + }), ..Flags::default() } ); @@ -4036,9 +4083,9 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "foo.js".to_string(), - }, + }), inspect: Some("127.0.0.1:9229".parse().unwrap()), ..Flags::default() } @@ -4055,12 +4102,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Compile { + subcommand: DenoSubcommand::Compile(CompileFlags { source_file: "https://deno.land/std/examples/colors.ts".to_string(), output: None, args: vec![], target: None, - }, + }), ..Flags::default() } ); @@ -4073,12 +4120,12 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Compile { + subcommand: DenoSubcommand::Compile(CompileFlags { source_file: "https://deno.land/std/examples/colors.ts".to_string(), output: Some(PathBuf::from("colors")), args: svec!["foo", "bar"], target: None, - }, + }), import_map_path: Some("import_map.json".to_string()), no_remote: true, config_path: Some("tsconfig.json".to_string()), @@ -4105,13 +4152,13 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Coverage { + subcommand: DenoSubcommand::Coverage(CoverageFlags { files: vec![PathBuf::from("foo.json")], ignore: vec![], include: vec![r"^file:".to_string()], exclude: vec![r"test\.(js|mjs|ts|jsx|tsx)$".to_string()], lcov: false, - }, + }), ..Flags::default() } ); diff --git a/cli/main.rs b/cli/main.rs index 8c1d219ecd..4687761353 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -40,8 +40,23 @@ mod version; use crate::file_fetcher::File; use crate::file_watcher::ResolutionResult; +use crate::flags::BundleFlags; +use crate::flags::CacheFlags; +use crate::flags::CompileFlags; +use crate::flags::CompletionsFlags; +use crate::flags::CoverageFlags; use crate::flags::DenoSubcommand; +use crate::flags::DocFlags; +use crate::flags::EvalFlags; use crate::flags::Flags; +use crate::flags::FmtFlags; +use crate::flags::InfoFlags; +use crate::flags::InstallFlags; +use crate::flags::LintFlags; +use crate::flags::ReplFlags; +use crate::flags::RunFlags; +use crate::flags::TestFlags; +use crate::flags::UpgradeFlags; use crate::fmt_errors::PrettyJsError; use crate::media_type::MediaType; use crate::module_loader::CliModuleLoader; @@ -73,7 +88,6 @@ use std::env; use std::io::Read; use std::io::Write; use std::iter::once; -use std::num::NonZeroUsize; use std::path::PathBuf; use std::pin::Pin; use std::rc::Rc; @@ -365,21 +379,20 @@ pub fn get_types(unstable: bool) -> String { async fn compile_command( flags: Flags, - source_file: String, - output: Option, - args: Vec, - target: Option, + compile_flags: CompileFlags, ) -> Result<(), AnyError> { let debug = flags.log_level == Some(log::Level::Debug); - let run_flags = - tools::standalone::compile_to_runtime_flags(flags.clone(), args)?; + let run_flags = tools::standalone::compile_to_runtime_flags( + flags.clone(), + compile_flags.args, + )?; - let module_specifier = resolve_url_or_path(&source_file)?; + let module_specifier = resolve_url_or_path(&compile_flags.source_file)?; let program_state = ProgramState::build(flags.clone()).await?; let deno_dir = &program_state.dir; - let output = output.or_else(|| { + let output = compile_flags.output.or_else(|| { infer_name_from_url(&module_specifier).map(PathBuf::from) }).ok_or_else(|| generic_error( "An executable name was not provided. One could not be inferred from the URL. Aborting.", @@ -408,7 +421,8 @@ async fn compile_command( // Select base binary based on target let original_binary = - tools::standalone::get_base_binary(deno_dir, target.clone()).await?; + tools::standalone::get_base_binary(deno_dir, compile_flags.target.clone()) + .await?; let final_bin = tools::standalone::create_standalone_binary( original_binary, @@ -418,20 +432,23 @@ async fn compile_command( info!("{} {}", colors::green("Emit"), output.display()); - tools::standalone::write_standalone_binary(output.clone(), target, final_bin) - .await?; + tools::standalone::write_standalone_binary( + output.clone(), + compile_flags.target, + final_bin, + ) + .await?; Ok(()) } async fn info_command( flags: Flags, - maybe_specifier: Option, - json: bool, + info_flags: InfoFlags, ) -> Result<(), AnyError> { let location = flags.location.clone(); let program_state = ProgramState::build(flags).await?; - if let Some(specifier) = maybe_specifier { + if let Some(specifier) = info_flags.file { let specifier = resolve_url_or_path(&specifier)?; let handler = Arc::new(Mutex::new(specifier_handler::FetchHandler::new( &program_state, @@ -452,7 +469,7 @@ async fn info_command( let graph = builder.get_graph(); let info = graph.info()?; - if json { + if info_flags.json { write_json_to_stdout(&json!(info)) } else { write_to_stdout_ignore_sigpipe(info.to_string().as_bytes()) @@ -460,29 +477,32 @@ async fn info_command( } } else { // If it was just "deno info" print location of caches and exit - print_cache_info(&program_state, json, location) + print_cache_info(&program_state, info_flags.json, location) } } async fn install_command( flags: Flags, - module_url: String, - args: Vec, - name: Option, - root: Option, - force: bool, + install_flags: InstallFlags, ) -> Result<(), AnyError> { let mut preload_flags = flags.clone(); preload_flags.inspect = None; preload_flags.inspect_brk = None; let permissions = Permissions::from_options(&preload_flags.clone().into()); let program_state = ProgramState::build(preload_flags).await?; - let main_module = resolve_url_or_path(&module_url)?; + let main_module = resolve_url_or_path(&install_flags.module_url)?; let mut worker = create_main_worker(&program_state, main_module.clone(), permissions, None); // First, fetch and compile the module; this step ensures that the module exists. worker.preload_module(&main_module).await?; - tools::installer::install(flags, &module_url, args, name, root, force) + tools::installer::install( + flags, + &install_flags.module_url, + install_flags.args, + install_flags.name, + install_flags.root, + install_flags.force, + ) } async fn lsp_command() -> Result<(), AnyError> { @@ -492,16 +512,10 @@ async fn lsp_command() -> Result<(), AnyError> { #[allow(clippy::too_many_arguments)] async fn lint_command( flags: Flags, - files: Vec, - list_rules: bool, - rules_tags: Vec, - rules_include: Vec, - rules_exclude: Vec, - ignore: Vec, - json: bool, + lint_flags: LintFlags, ) -> Result<(), AnyError> { - if list_rules { - tools::lint::print_rules_list(json); + if lint_flags.rules { + tools::lint::print_rules_list(lint_flags.json); return Ok(()); } @@ -515,19 +529,19 @@ async fn lint_command( tools::lint::lint_files( maybe_lint_config, - rules_tags, - rules_include, - rules_exclude, - files, - ignore, - json, + lint_flags.rules_tags, + lint_flags.rules_include, + lint_flags.rules_exclude, + lint_flags.files, + lint_flags.ignore, + lint_flags.json, ) .await } async fn cache_command( flags: Flags, - files: Vec, + cache_flags: CacheFlags, ) -> Result<(), AnyError> { let lib = if flags.unstable { module_graph::TypeLib::UnstableDenoWindow @@ -536,7 +550,7 @@ async fn cache_command( }; let program_state = ProgramState::build(flags).await?; - for file in files { + for file in cache_flags.files { let specifier = resolve_url_or_path(&file)?; program_state .prepare_module_load( @@ -555,9 +569,7 @@ async fn cache_command( async fn eval_command( flags: Flags, - code: String, - ext: String, - print: bool, + eval_flags: EvalFlags, ) -> Result<(), AnyError> { // Force TypeScript compile. let main_module = resolve_url_or_path("./$deno$eval.ts").unwrap(); @@ -566,21 +578,21 @@ async fn eval_command( let mut worker = create_main_worker(&program_state, main_module.clone(), permissions, None); // Create a dummy source file. - let source_code = if print { - format!("console.log({})", code) + let source_code = if eval_flags.print { + format!("console.log({})", eval_flags.code) } else { - code + eval_flags.code } .into_bytes(); let file = File { local: main_module.clone().to_file_path().unwrap(), maybe_types: None, - media_type: if ext.as_str() == "ts" { + media_type: if eval_flags.ext.as_str() == "ts" { MediaType::TypeScript - } else if ext.as_str() == "tsx" { + } else if eval_flags.ext.as_str() == "tsx" { MediaType::Tsx - } else if ext.as_str() == "js" { + } else if eval_flags.ext.as_str() == "js" { MediaType::JavaScript } else { MediaType::Jsx @@ -682,15 +694,14 @@ fn bundle_module_graph( async fn bundle_command( flags: Flags, - source_file: String, - out_file: Option, + bundle_flags: BundleFlags, ) -> Result<(), AnyError> { let debug = flags.log_level == Some(log::Level::Debug); let resolver = |_| { let flags = flags.clone(); - let source_file1 = source_file.clone(); - let source_file2 = source_file.clone(); + let source_file1 = bundle_flags.source_file.clone(); + let source_file2 = bundle_flags.source_file.clone(); async move { let module_specifier = resolve_url_or_path(&source_file1)?; @@ -736,7 +747,7 @@ async fn bundle_command( module_graph::Graph, )| { let flags = flags.clone(); - let out_file = out_file.clone(); + let out_file = bundle_flags.out_file.clone(); async move { info!("{} {}", colors::green("Bundle"), module_graph.info()?.root); @@ -780,33 +791,37 @@ async fn bundle_command( async fn doc_command( flags: Flags, - source_file: Option, - json: bool, - maybe_filter: Option, - private: bool, + doc_flags: DocFlags, ) -> Result<(), AnyError> { - tools::doc::print_docs(flags, source_file, json, maybe_filter, private).await + tools::doc::print_docs( + flags, + doc_flags.source_file, + doc_flags.json, + doc_flags.filter, + doc_flags.private, + ) + .await } async fn format_command( flags: Flags, - args: Vec, - ignore: Vec, - check: bool, - ext: String, + fmt_flags: FmtFlags, ) -> Result<(), AnyError> { - if args.len() == 1 && args[0].to_string_lossy() == "-" { - return tools::fmt::format_stdin(check, ext); + if fmt_flags.files.len() == 1 && fmt_flags.files[0].to_string_lossy() == "-" { + return tools::fmt::format_stdin(fmt_flags.check, fmt_flags.ext); } - tools::fmt::format(args, ignore, check, flags.watch).await?; + tools::fmt::format( + fmt_flags.files, + fmt_flags.ignore, + fmt_flags.check, + flags.watch, + ) + .await?; Ok(()) } -async fn run_repl( - flags: Flags, - maybe_eval: Option, -) -> Result<(), AnyError> { +async fn run_repl(flags: Flags, repl_flags: ReplFlags) -> Result<(), AnyError> { let main_module = resolve_url_or_path("./$deno$repl.ts").unwrap(); let permissions = Permissions::from_options(&flags.clone().into()); let program_state = ProgramState::build(flags).await?; @@ -814,7 +829,7 @@ async fn run_repl( create_main_worker(&program_state, main_module.clone(), permissions, None); worker.run_event_loop(false).await?; - tools::repl::run(&program_state, worker, maybe_eval).await + tools::repl::run(&program_state, worker, repl_flags.eval).await } async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> { @@ -991,17 +1006,20 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> { file_watcher::watch_func(resolver, operation, "Process").await } -async fn run_command(flags: Flags, script: String) -> Result<(), AnyError> { +async fn run_command( + flags: Flags, + run_flags: RunFlags, +) -> Result<(), AnyError> { // Read script content from stdin - if script == "-" { + if run_flags.script == "-" { return run_from_stdin(flags).await; } if flags.watch { - return run_with_watch(flags, script).await; + return run_with_watch(flags, run_flags.script).await; } - let main_module = resolve_url_or_path(&script)?; + let main_module = resolve_url_or_path(&run_flags.script)?; let program_state = ProgramState::build(flags.clone()).await?; let permissions = Permissions::from_options(&flags.clone().into()); let mut worker = @@ -1046,39 +1064,26 @@ async fn run_command(flags: Flags, script: String) -> Result<(), AnyError> { async fn coverage_command( flags: Flags, - files: Vec, - ignore: Vec, - include: Vec, - exclude: Vec, - lcov: bool, + coverage_flags: CoverageFlags, ) -> Result<(), AnyError> { - if files.is_empty() { + if coverage_flags.files.is_empty() { return Err(generic_error("No matching coverage profiles found")); } tools::coverage::cover_files( flags.clone(), - files, - ignore, - include, - exclude, - lcov, + coverage_flags.files, + coverage_flags.ignore, + coverage_flags.include, + coverage_flags.exclude, + coverage_flags.lcov, ) .await } -#[allow(clippy::too_many_arguments)] async fn test_command( flags: Flags, - include: Option>, - ignore: Vec, - no_run: bool, - doc: bool, - fail_fast: Option, - allow_none: bool, - filter: Option, - shuffle: Option, - concurrent_jobs: NonZeroUsize, + test_flags: TestFlags, ) -> Result<(), AnyError> { if let Some(ref coverage_dir) = flags.coverage_dir { std::fs::create_dir_all(&coverage_dir)?; @@ -1091,14 +1096,14 @@ async fn test_command( if flags.watch { tools::test::run_tests_with_watch( flags, - include, - ignore, - doc, - no_run, - fail_fast, - filter, - shuffle, - concurrent_jobs, + test_flags.include, + test_flags.ignore, + test_flags.doc, + test_flags.no_run, + test_flags.fail_fast, + test_flags.filter, + test_flags.shuffle, + test_flags.concurrent_jobs, ) .await?; @@ -1107,15 +1112,15 @@ async fn test_command( tools::test::run_tests( flags, - include, - ignore, - doc, - no_run, - fail_fast, - allow_none, - filter, - shuffle, - concurrent_jobs, + test_flags.include, + test_flags.ignore, + test_flags.doc, + test_flags.no_run, + test_flags.fail_fast, + test_flags.allow_none, + test_flags.filter, + test_flags.shuffle, + test_flags.concurrent_jobs, ) .await?; @@ -1150,102 +1155,47 @@ fn get_subcommand( flags: Flags, ) -> Pin>>> { match flags.clone().subcommand { - DenoSubcommand::Bundle { - source_file, - out_file, - } => bundle_command(flags, source_file, out_file).boxed_local(), - DenoSubcommand::Doc { - source_file, - json, - filter, - private, - } => doc_command(flags, source_file, json, filter, private).boxed_local(), - DenoSubcommand::Eval { print, code, ext } => { - eval_command(flags, code, ext, print).boxed_local() + DenoSubcommand::Bundle(bundle_flags) => { + bundle_command(flags, bundle_flags).boxed_local() } - DenoSubcommand::Cache { files } => { - cache_command(flags, files).boxed_local() + DenoSubcommand::Doc(doc_flags) => { + doc_command(flags, doc_flags).boxed_local() } - DenoSubcommand::Compile { - source_file, - output, - args, - target, - } => { - compile_command(flags, source_file, output, args, target).boxed_local() + DenoSubcommand::Eval(eval_flags) => { + eval_command(flags, eval_flags).boxed_local() } - DenoSubcommand::Coverage { - files, - ignore, - include, - exclude, - lcov, - } => coverage_command(flags, files, ignore, include, exclude, lcov) - .boxed_local(), - DenoSubcommand::Fmt { - check, - files, - ignore, - ext, - } => format_command(flags, files, ignore, check, ext).boxed_local(), - DenoSubcommand::Info { file, json } => { - info_command(flags, file, json).boxed_local() + DenoSubcommand::Cache(cache_flags) => { + cache_command(flags, cache_flags).boxed_local() } - DenoSubcommand::Install { - module_url, - args, - name, - root, - force, - } => { - install_command(flags, module_url, args, name, root, force).boxed_local() + DenoSubcommand::Compile(compile_flags) => { + compile_command(flags, compile_flags).boxed_local() + } + DenoSubcommand::Coverage(coverage_flags) => { + coverage_command(flags, coverage_flags).boxed_local() + } + DenoSubcommand::Fmt(fmt_flags) => { + format_command(flags, fmt_flags).boxed_local() + } + DenoSubcommand::Info(info_flags) => { + info_command(flags, info_flags).boxed_local() + } + DenoSubcommand::Install(install_flags) => { + install_command(flags, install_flags).boxed_local() } DenoSubcommand::Lsp => lsp_command().boxed_local(), - DenoSubcommand::Lint { - files, - rules, - rules_tags, - rules_include, - rules_exclude, - ignore, - json, - } => lint_command( - flags, - files, - rules, - rules_tags, - rules_include, - rules_exclude, - ignore, - json, - ) - .boxed_local(), - DenoSubcommand::Repl { eval } => run_repl(flags, eval).boxed_local(), - DenoSubcommand::Run { script } => run_command(flags, script).boxed_local(), - DenoSubcommand::Test { - no_run, - doc, - fail_fast, - ignore, - include, - allow_none, - filter, - shuffle, - concurrent_jobs, - } => test_command( - flags, - include, - ignore, - no_run, - doc, - fail_fast, - allow_none, - filter, - shuffle, - concurrent_jobs, - ) - .boxed_local(), - DenoSubcommand::Completions { buf } => { + DenoSubcommand::Lint(lint_flags) => { + lint_command(flags, lint_flags).boxed_local() + } + DenoSubcommand::Repl(repl_flags) => { + run_repl(flags, repl_flags).boxed_local() + } + DenoSubcommand::Run(run_flags) => { + run_command(flags, run_flags).boxed_local() + } + DenoSubcommand::Test(test_flags) => { + test_command(flags, test_flags).boxed_local() + } + DenoSubcommand::Completions(CompletionsFlags { buf }) => { if let Err(e) = write_to_stdout_ignore_sigpipe(&buf) { eprintln!("{}", e); std::process::exit(1); @@ -1260,17 +1210,20 @@ fn get_subcommand( } std::process::exit(0); } - DenoSubcommand::Upgrade { - force, - dry_run, - canary, - version, - output, - ca_file, - } => tools::upgrade::upgrade_command( - dry_run, force, canary, version, output, ca_file, - ) - .boxed_local(), + DenoSubcommand::Upgrade(upgrade_flags) => { + let UpgradeFlags { + force, + dry_run, + canary, + version, + output, + ca_file, + } = upgrade_flags; + tools::upgrade::upgrade_command( + dry_run, force, canary, version, output, ca_file, + ) + .boxed_local() + } } } diff --git a/cli/tools/standalone.rs b/cli/tools/standalone.rs index 18d8875885..33bc7881ed 100644 --- a/cli/tools/standalone.rs +++ b/cli/tools/standalone.rs @@ -3,6 +3,7 @@ use crate::deno_dir::DenoDir; use crate::flags::DenoSubcommand; use crate::flags::Flags; +use crate::flags::RunFlags; use deno_core::error::bail; use deno_core::error::AnyError; use deno_core::serde_json; @@ -199,9 +200,9 @@ pub fn compile_to_runtime_flags( // change to `Flags` should be reflected here. Ok(Flags { argv: baked_args, - subcommand: DenoSubcommand::Run { + subcommand: DenoSubcommand::Run(RunFlags { script: "placeholder".to_string(), - }, + }), allow_env: flags.allow_env, allow_hrtime: flags.allow_hrtime, allow_net: flags.allow_net,