From 1ffbd561642d05a05e18ada764d50581dea779ef Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Sat, 20 Aug 2022 01:37:05 +0200 Subject: feat: add "deno init" subcommand (#15469) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- cli/tools/init/mod.rs | 49 +++++++++++++++++++++++++++++++++++ cli/tools/init/templates/main.ts | 8 ++++++ cli/tools/init/templates/main_test.ts | 6 +++++ cli/tools/mod.rs | 1 + 4 files changed, 64 insertions(+) create mode 100644 cli/tools/init/mod.rs create mode 100644 cli/tools/init/templates/main.ts create mode 100644 cli/tools/init/templates/main_test.ts (limited to 'cli/tools') 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; -- cgit v1.2.3