diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2022-08-20 01:37:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-20 01:37:05 +0200 |
commit | 1ffbd561642d05a05e18ada764d50581dea779ef (patch) | |
tree | 93bc8b6c613b8f76c2b5ca713975bca44df4ad61 /cli/args/flags.rs | |
parent | 5beec3f106b0890cc76150d0e3b3661c576d4c3b (diff) |
feat: add "deno init" subcommand (#15469)
This adds an init subcommand to that creates a project starter similar to cargo init.
```
$ deno init my_project
Project initialized
Run these commands to get started:
cd my_project
deno run main.ts
deno run main_test.ts
$ deno run main.ts
Add 2 + 3 5
$ cat main.ts
export function add(a: number, b: number): number {
return a + b;
}
if (import.meta.main) {
console.log("Add 2 + 3", add(2, 3));
}
$ cat main_test.ts
import { assertEquals } from "https://deno.land/std@0.151.0/testing/asserts.ts";
import { add } from "./main.ts";
Deno.test(function addTest() {
assertEquals(add(2, 3), 5);
});
```
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli/args/flags.rs')
-rw-r--r-- | cli/args/flags.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 08b48d1d5..a70b2c675 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -120,6 +120,11 @@ pub struct FmtFlags { } #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +pub struct InitFlags { + pub dir: Option<String>, +} + +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub struct InfoFlags { pub json: bool, pub file: Option<String>, @@ -217,6 +222,7 @@ pub enum DenoSubcommand { Doc(DocFlags), Eval(EvalFlags), Fmt(FmtFlags), + Init(InitFlags), Info(InfoFlags), Install(InstallFlags), Uninstall(UninstallFlags), @@ -554,6 +560,7 @@ pub fn flags_from_vec(args: Vec<String>) -> clap::Result<Flags> { Some(("doc", m)) => doc_parse(&mut flags, m), Some(("eval", m)) => eval_parse(&mut flags, m), Some(("fmt", m)) => fmt_parse(&mut flags, m), + Some(("init", m)) => init_parse(&mut flags, m), Some(("info", m)) => info_parse(&mut flags, m), Some(("install", m)) => install_parse(&mut flags, m), Some(("lint", m)) => lint_parse(&mut flags, m), @@ -629,6 +636,7 @@ fn clap_root(version: &str) -> Command { .subcommand(doc_subcommand()) .subcommand(eval_subcommand()) .subcommand(fmt_subcommand()) + .subcommand(init_subcommand()) .subcommand(info_subcommand()) .subcommand(install_subcommand()) .subcommand(uninstall_subcommand()) @@ -1140,6 +1148,15 @@ Ignore formatting a file by adding an ignore comment at the top of the file: ) } +fn init_subcommand<'a>() -> Command<'a> { + Command::new("init").about("Initialize a new project").arg( + Arg::new("dir") + .takes_value(true) + .required(false) + .value_hint(ValueHint::DirPath), + ) +} + fn info_subcommand<'a>() -> Command<'a> { Command::new("info") .about("Show info about cache or info related to source file") @@ -2436,6 +2453,12 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) { }); } +fn init_parse(flags: &mut Flags, matches: &clap::ArgMatches) { + flags.subcommand = DenoSubcommand::Init(InitFlags { + dir: matches.value_of("dir").map(|f| f.to_string()), + }); +} + fn info_parse(flags: &mut Flags, matches: &clap::ArgMatches) { reload_arg_parse(flags, matches); config_args_parse(flags, matches); @@ -5951,4 +5974,27 @@ mod tests { ]); assert!(r.is_err()); } + + #[test] + fn init() { + let r = flags_from_vec(svec!["deno", "init"]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Init(InitFlags { dir: None }), + ..Flags::default() + } + ); + + let r = flags_from_vec(svec!["deno", "init", "foo"]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Init(InitFlags { + dir: Some(String::from("foo")), + }), + ..Flags::default() + } + ); + } } |