summaryrefslogtreecommitdiff
path: root/cli/tools/init/mod.rs
diff options
context:
space:
mode:
authormuddlebee <anweshknayak@gmail.com>2024-07-10 06:43:34 +0530
committerGitHub <noreply@github.com>2024-07-09 21:13:34 -0400
commitff5163af05d95409cbb3d1a13f49a43fefd4849a (patch)
tree1b441f411224a0f45177beb77ba7822505cc8166 /cli/tools/init/mod.rs
parent9776a13e33bc371a67f0d44925e2bf04dca159f1 (diff)
feat(cli): `deno init --lib` (#22499)
Closes #22287 Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'cli/tools/init/mod.rs')
-rw-r--r--cli/tools/init/mod.rs176
1 files changed, 134 insertions, 42 deletions
diff --git a/cli/tools/init/mod.rs b/cli/tools/init/mod.rs
index fb2e6e7af..23b14e17c 100644
--- a/cli/tools/init/mod.rs
+++ b/cli/tools/init/mod.rs
@@ -4,33 +4,11 @@ use crate::args::InitFlags;
use crate::colors;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
+use deno_core::serde_json::json;
use log::info;
use std::io::Write;
use std::path::Path;
-fn create_file(
- dir: &Path,
- filename: &str,
- content: &str,
-) -> Result<(), AnyError> {
- let path = dir.join(filename);
- if path.exists() {
- info!(
- "ℹ️ {}",
- colors::gray(format!("Skipped creating {filename} as it already exists"))
- );
- Ok(())
- } else {
- let mut file = std::fs::OpenOptions::new()
- .write(true)
- .create_new(true)
- .open(path)
- .with_context(|| format!("Failed to create {filename} file"))?;
- file.write_all(content.as_bytes())?;
- Ok(())
- }
-}
-
pub fn init_project(init_flags: InitFlags) -> Result<(), AnyError> {
let cwd =
std::env::current_dir().context("Can't read current working directory.")?;
@@ -42,15 +20,82 @@ pub fn init_project(init_flags: InitFlags) -> Result<(), AnyError> {
cwd
};
- let main_ts = include_str!("./templates/main.ts");
- create_file(&dir, "main.ts", main_ts)?;
+ if init_flags.lib {
+ // Extract the directory name to use as the project name
+ let project_name = dir
+ .file_name()
+ .unwrap_or_else(|| dir.as_os_str())
+ .to_str()
+ .unwrap();
+
+ create_file(
+ &dir,
+ "mod.ts",
+ r#"export function add(a: number, b: number): number {
+ return a + b;
+}
+"#,
+ )?;
+ create_file(
+ &dir,
+ "mod_test.ts",
+ r#"import { assertEquals } from "jsr:@std/assert";
+import { add } from "./mod.ts";
+
+Deno.test(function addTest() {
+ assertEquals(add(2, 3), 5);
+});
+"#,
+ )?;
+
+ create_json_file(
+ &dir,
+ "deno.json",
+ &json!({
+ "name": project_name,
+ "version": "1.0.0",
+ "exports": "./mod.ts",
+ "tasks": {
+ "dev": "deno test --watch mod.ts"
+ }
+ }),
+ )?;
+ } else {
+ create_file(
+ &dir,
+ "main.ts",
+ r#"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));
+}
+"#,
+ )?;
+ create_file(
+ &dir,
+ "main_test.ts",
+ r#"import { assertEquals } from "jsr:@std/assert";
+import { add } from "./main.ts";
- create_file(
- &dir,
- "main_test.ts",
- include_str!("./templates/main_test.ts"),
- )?;
- create_file(&dir, "deno.json", include_str!("./templates/deno.json"))?;
+Deno.test(function addTest() {
+ assertEquals(add(2, 3), 5);
+});
+"#,
+ )?;
+
+ create_json_file(
+ &dir,
+ "deno.json",
+ &json!({
+ "tasks": {
+ "dev": "deno run --watch main.ts"
+ }
+ }),
+ )?;
+ }
info!("✅ {}", colors::green("Project initialized"));
info!("");
@@ -60,16 +105,63 @@ pub fn init_project(init_flags: InitFlags) -> Result<(), AnyError> {
info!(" cd {}", dir);
info!("");
}
- info!(" {}", colors::gray("# Run the program"));
- info!(" deno run main.ts");
- info!("");
- info!(
- " {}",
- colors::gray("# Run the program and watch for file changes")
- );
- info!(" deno task dev");
- info!("");
- info!(" {}", colors::gray("# Run the tests"));
- info!(" deno test");
+ if init_flags.lib {
+ info!(" {}", colors::gray("# Run the tests"));
+ info!(" deno test");
+ info!("");
+ info!(
+ " {}",
+ colors::gray("# Run the tests and watch for file changes")
+ );
+ info!(" deno task dev");
+ info!("");
+ info!(" {}", colors::gray("# Publish to JSR (dry run)"));
+ info!(" deno publish --dry-run");
+ } else {
+ info!(" {}", colors::gray("# Run the program"));
+ info!(" deno run main.ts");
+ info!("");
+ info!(
+ " {}",
+ colors::gray("# Run the program and watch for file changes")
+ );
+ info!(" deno task dev");
+ info!("");
+ info!(" {}", colors::gray("# Run the tests"));
+ info!(" deno test");
+ }
Ok(())
}
+
+fn create_json_file(
+ dir: &Path,
+ filename: &str,
+ value: &deno_core::serde_json::Value,
+) -> Result<(), AnyError> {
+ let mut text = deno_core::serde_json::to_string_pretty(value)?;
+ text.push('\n');
+ create_file(dir, filename, &text)
+}
+
+fn create_file(
+ dir: &Path,
+ filename: &str,
+ content: &str,
+) -> Result<(), AnyError> {
+ let path = dir.join(filename);
+ if path.exists() {
+ info!(
+ "ℹ️ {}",
+ colors::gray(format!("Skipped creating {filename} as it already exists"))
+ );
+ Ok(())
+ } else {
+ let mut file = std::fs::OpenOptions::new()
+ .write(true)
+ .create_new(true)
+ .open(path)
+ .with_context(|| format!("Failed to create {filename} file"))?;
+ file.write_all(content.as_bytes())?;
+ Ok(())
+ }
+}