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/tools | |
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/tools')
-rw-r--r-- | cli/tools/init/mod.rs | 49 | ||||
-rw-r--r-- | cli/tools/init/templates/main.ts | 8 | ||||
-rw-r--r-- | cli/tools/init/templates/main_test.ts | 6 | ||||
-rw-r--r-- | cli/tools/mod.rs | 1 |
4 files changed, 64 insertions, 0 deletions
diff --git a/cli/tools/init/mod.rs b/cli/tools/init/mod.rs new file mode 100644 index 000000000..e47b88702 --- /dev/null +++ b/cli/tools/init/mod.rs @@ -0,0 +1,49 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + +use crate::args::InitFlags; +use crate::compat; +use deno_core::{anyhow::Context, error::AnyError}; +use std::io::Write; +use std::path::Path; + +fn create_file( + dir: &Path, + filename: &str, + content: &str, +) -> Result<(), AnyError> { + let mut file = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(dir.join(filename)) + .with_context(|| format!("Failed to create {} file", filename))?; + file.write_all(content.as_bytes())?; + Ok(()) +} + +pub async fn init_project(init_flags: InitFlags) -> Result<(), AnyError> { + let cwd = + std::env::current_dir().context("Can't read current working directory.")?; + let dir = if let Some(dir) = &init_flags.dir { + let dir = cwd.join(dir); + std::fs::create_dir_all(&dir)?; + dir + } else { + cwd + }; + + let main_ts = include_str!("./templates/main.ts"); + create_file(&dir, "main.ts", main_ts)?; + + let main_test_ts = include_str!("./templates/main_test.ts") + .replace("{CURRENT_STD_URL}", compat::STD_URL_STR); + create_file(&dir, "main_test.ts", &main_test_ts)?; + + println!("✅ Project initialized"); + println!("Run these commands to get started"); + if let Some(dir) = init_flags.dir { + println!(" cd {}", dir); + } + println!(" deno run main.ts"); + println!(" deno test"); + Ok(()) +} diff --git a/cli/tools/init/templates/main.ts b/cli/tools/init/templates/main.ts new file mode 100644 index 000000000..be043e97c --- /dev/null +++ b/cli/tools/init/templates/main.ts @@ -0,0 +1,8 @@ +export function add(a: number, b: number): number { + return a + b; +} + +// Learn more at https://deno.land/manual/examples/module_metadata#concepts +if (import.meta.main) { + console.log("Add 2 + 3 =", add(2, 3)); +} diff --git a/cli/tools/init/templates/main_test.ts b/cli/tools/init/templates/main_test.ts new file mode 100644 index 000000000..5f60b571c --- /dev/null +++ b/cli/tools/init/templates/main_test.ts @@ -0,0 +1,6 @@ +import { assertEquals } from "{CURRENT_STD_URL}testing/asserts.ts"; +import { add } from "./main.ts"; + +Deno.test(function addTest() { + assertEquals(add(2, 3), 5); +}); diff --git a/cli/tools/mod.rs b/cli/tools/mod.rs index 7c5d79744..11c904776 100644 --- a/cli/tools/mod.rs +++ b/cli/tools/mod.rs @@ -4,6 +4,7 @@ pub mod bench; pub mod coverage; pub mod doc; pub mod fmt; +pub mod init; pub mod installer; pub mod lint; pub mod repl; |