From ad649f146e5206e586890f3ad49aa4522d79edf1 Mon Sep 17 00:00:00 2001 From: muddlebee Date: Tue, 20 Feb 2024 23:48:28 +0530 Subject: [PATCH 1/7] feat(cli): deno init should add name, version and exports fields to deno.json --- cli/tools/init/mod.rs | 12 +++++++++++- cli/tools/init/templates/deno.json | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cli/tools/init/mod.rs b/cli/tools/init/mod.rs index d0e1990854de23..a29a47265ad0b6 100644 --- a/cli/tools/init/mod.rs +++ b/cli/tools/init/mod.rs @@ -43,13 +43,23 @@ pub async fn init_project(init_flags: InitFlags) -> Result<(), AnyError> { cwd }; + // 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(); + 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}", deno_std::CURRENT_STD_URL_STR); create_file(&dir, "main_test.ts", &main_test_ts)?; - create_file(&dir, "deno.json", include_str!("./templates/deno.json"))?; + + let template_content = include_str!("./templates/deno.json"); + let new_content = template_content.replace("{{name}}", project_name); + create_file(&dir, "deno.json", &new_content)?; info!("✅ {}", colors::green("Project initialized")); info!(""); diff --git a/cli/tools/init/templates/deno.json b/cli/tools/init/templates/deno.json index 3c5130f1d39938..6845112410aa34 100644 --- a/cli/tools/init/templates/deno.json +++ b/cli/tools/init/templates/deno.json @@ -1,4 +1,7 @@ { + "name" : "{{name}}", + "version": "1.0.0", + "imports": {}, "tasks": { "dev": "deno run --watch main.ts" } From 42f3c621f30e24d9a435dbabb2f2d941cbd35630 Mon Sep 17 00:00:00 2001 From: muddlebee Date: Thu, 22 Feb 2024 18:46:51 +0530 Subject: [PATCH 2/7] feat(cli): deno init should add name, version and exports fields to deno.json --- cli/args/flags.rs | 55 ++++++++++++++++++++++---- cli/tools/init/mod.rs | 10 +++-- cli/tools/init/templates/deno.json | 3 -- cli/tools/init/templates/deno_lib.json | 8 ++++ 4 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 cli/tools/init/templates/deno_lib.json diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 0ce52129658252..2b349a8c5f51ff 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -160,6 +160,7 @@ impl FmtFlags { #[derive(Clone, Debug, Eq, PartialEq)] pub struct InitFlags { pub dir: Option, + pub lib: bool, } #[derive(Clone, Debug, Eq, PartialEq)] @@ -1740,11 +1741,18 @@ fn init_subcommand() -> Command { Command::new("init") .about("Initialize a new project") .defer(|cmd| { - cmd.arg( - Arg::new("dir") - .required(false) - .value_hint(ValueHint::DirPath), - ) + cmd + .arg( + Arg::new("dir") + .required(false) + .value_hint(ValueHint::DirPath), + ) + .arg( + Arg::new("lib") + .long("lib") + .required(false) + .action(ArgAction::SetTrue), + ) }) } @@ -3497,6 +3505,7 @@ fn fmt_parse(flags: &mut Flags, matches: &mut ArgMatches) { fn init_parse(flags: &mut Flags, matches: &mut ArgMatches) { flags.subcommand = DenoSubcommand::Init(InitFlags { dir: matches.remove_one::("dir"), + lib: matches.get_flag("lib"), }); } @@ -8455,7 +8464,10 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Init(InitFlags { dir: None }), + subcommand: DenoSubcommand::Init(InitFlags { + dir: None, + lib: false + }), ..Flags::default() } ); @@ -8466,6 +8478,7 @@ mod tests { Flags { subcommand: DenoSubcommand::Init(InitFlags { dir: Some(String::from("foo")), + lib: false }), ..Flags::default() } @@ -8475,11 +8488,39 @@ mod tests { assert_eq!( r.unwrap(), Flags { - subcommand: DenoSubcommand::Init(InitFlags { dir: None }), + subcommand: DenoSubcommand::Init(InitFlags { + dir: None, + lib: false + }), log_level: Some(Level::Error), ..Flags::default() } ); + + let r = flags_from_vec(svec!["deno", "init", "--lib"]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Init(InitFlags { + dir: None, + lib: true + }), + ..Flags::default() + } + ); + + let r = flags_from_vec(svec!["deno", "init", "foo", "--lib"]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Init(InitFlags { + dir: Some(String::from("foo")), + lib: true + }), + ..Flags::default() + } + ); + } #[test] diff --git a/cli/tools/init/mod.rs b/cli/tools/init/mod.rs index a29a47265ad0b6..5dd2695ea487e0 100644 --- a/cli/tools/init/mod.rs +++ b/cli/tools/init/mod.rs @@ -57,9 +57,13 @@ pub async fn init_project(init_flags: InitFlags) -> Result<(), AnyError> { .replace("{CURRENT_STD_URL}", deno_std::CURRENT_STD_URL_STR); create_file(&dir, "main_test.ts", &main_test_ts)?; - let template_content = include_str!("./templates/deno.json"); - let new_content = template_content.replace("{{name}}", project_name); - create_file(&dir, "deno.json", &new_content)?; + if init_flags.lib { + let template_content = include_str!("./templates/deno_lib.json"); + let new_content = template_content.replace("{{name}}", project_name); + create_file(&dir, "deno.json", &new_content)?; + } else { + create_file(&dir, "deno.json", include_str!("./templates/deno.json"))?; + } info!("✅ {}", colors::green("Project initialized")); info!(""); diff --git a/cli/tools/init/templates/deno.json b/cli/tools/init/templates/deno.json index 6845112410aa34..3c5130f1d39938 100644 --- a/cli/tools/init/templates/deno.json +++ b/cli/tools/init/templates/deno.json @@ -1,7 +1,4 @@ { - "name" : "{{name}}", - "version": "1.0.0", - "imports": {}, "tasks": { "dev": "deno run --watch main.ts" } diff --git a/cli/tools/init/templates/deno_lib.json b/cli/tools/init/templates/deno_lib.json new file mode 100644 index 00000000000000..6845112410aa34 --- /dev/null +++ b/cli/tools/init/templates/deno_lib.json @@ -0,0 +1,8 @@ +{ + "name" : "{{name}}", + "version": "1.0.0", + "imports": {}, + "tasks": { + "dev": "deno run --watch main.ts" + } +} From 1b6e76577b5f84f4d0d8768192f27b0db7920730 Mon Sep 17 00:00:00 2001 From: muddlebee Date: Thu, 22 Feb 2024 22:42:21 +0530 Subject: [PATCH 3/7] feat(cli): deno init should add name, version and exports fields to deno.json --- cli/tools/init/mod.rs | 14 +++++++------- cli/tools/init/templates/deno_lib.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cli/tools/init/mod.rs b/cli/tools/init/mod.rs index 5dd2695ea487e0..440decc12108fd 100644 --- a/cli/tools/init/mod.rs +++ b/cli/tools/init/mod.rs @@ -43,13 +43,6 @@ pub async fn init_project(init_flags: InitFlags) -> Result<(), AnyError> { cwd }; - // 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(); - let main_ts = include_str!("./templates/main.ts"); create_file(&dir, "main.ts", main_ts)?; @@ -58,6 +51,13 @@ pub async fn init_project(init_flags: InitFlags) -> Result<(), AnyError> { create_file(&dir, "main_test.ts", &main_test_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(); + let template_content = include_str!("./templates/deno_lib.json"); let new_content = template_content.replace("{{name}}", project_name); create_file(&dir, "deno.json", &new_content)?; diff --git a/cli/tools/init/templates/deno_lib.json b/cli/tools/init/templates/deno_lib.json index 6845112410aa34..dd03aab2b51e24 100644 --- a/cli/tools/init/templates/deno_lib.json +++ b/cli/tools/init/templates/deno_lib.json @@ -1,7 +1,7 @@ { "name" : "{{name}}", "version": "1.0.0", - "imports": {}, + "exports": {}, "tasks": { "dev": "deno run --watch main.ts" } From 5a300d7a5c3d2f227e9299fe2352f79175a40631 Mon Sep 17 00:00:00 2001 From: muddlebee Date: Thu, 22 Feb 2024 22:43:22 +0530 Subject: [PATCH 4/7] feat(cli): fix format errors --- cli/tools/init/templates/deno_lib.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/tools/init/templates/deno_lib.json b/cli/tools/init/templates/deno_lib.json index dd03aab2b51e24..5c310eb4f9de98 100644 --- a/cli/tools/init/templates/deno_lib.json +++ b/cli/tools/init/templates/deno_lib.json @@ -1,5 +1,5 @@ { - "name" : "{{name}}", + "name" : "{{name}}", "version": "1.0.0", "exports": {}, "tasks": { From ccacaaf94219f495a09146d022c8df0f3e18180d Mon Sep 17 00:00:00 2001 From: muddlebee Date: Fri, 23 Feb 2024 02:02:04 +0530 Subject: [PATCH 5/7] feat(cli): fix format errors --- cli/args/flags.rs | 1 - cli/tools/init/templates/deno_lib.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 2b349a8c5f51ff..96f8f1986823c0 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -8520,7 +8520,6 @@ mod tests { ..Flags::default() } ); - } #[test] diff --git a/cli/tools/init/templates/deno_lib.json b/cli/tools/init/templates/deno_lib.json index 5c310eb4f9de98..e713b010c4dc88 100644 --- a/cli/tools/init/templates/deno_lib.json +++ b/cli/tools/init/templates/deno_lib.json @@ -1,5 +1,5 @@ { - "name" : "{{name}}", + "name": "{{name}}", "version": "1.0.0", "exports": {}, "tasks": { From d160e13625b9c99a2925aadf62bf3cabaf34bd10 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Tue, 18 Jun 2024 08:02:45 +1000 Subject: [PATCH 6/7] mod.ts --- cli/tools/init/templates/deno_lib.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/tools/init/templates/deno_lib.json b/cli/tools/init/templates/deno_lib.json index e713b010c4dc88..efcb77ca1c87c9 100644 --- a/cli/tools/init/templates/deno_lib.json +++ b/cli/tools/init/templates/deno_lib.json @@ -3,6 +3,6 @@ "version": "1.0.0", "exports": {}, "tasks": { - "dev": "deno run --watch main.ts" + "dev": "deno test --watch mod.ts" } } From 8159542e3d94fd839dcd3f3a188aa2982e32df8d Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 9 Jul 2024 17:35:16 -0400 Subject: [PATCH 7/7] better --- cli/tools/init/mod.rs | 172 ++++++++++++++++++------- cli/tools/init/templates/deno.json | 5 - cli/tools/init/templates/deno_lib.json | 8 -- cli/tools/init/templates/main.ts | 8 -- cli/tools/init/templates/main_test.ts | 6 - tests/specs/init/lib/__test__.jsonc | 16 +++ tests/specs/init/lib/dry_publish.out | 7 + tests/specs/init/lib/init.out | 14 ++ tests/specs/init/lib/test.out | 14 ++ 9 files changed, 176 insertions(+), 74 deletions(-) delete mode 100644 cli/tools/init/templates/deno.json delete mode 100644 cli/tools/init/templates/deno_lib.json delete mode 100644 cli/tools/init/templates/main.ts delete mode 100644 cli/tools/init/templates/main_test.ts create mode 100644 tests/specs/init/lib/__test__.jsonc create mode 100644 tests/specs/init/lib/dry_publish.out create mode 100644 tests/specs/init/lib/init.out create mode 100644 tests/specs/init/lib/test.out diff --git a/cli/tools/init/mod.rs b/cli/tools/init/mod.rs index 1a5cd51ee931e1..23b14e17cbde9b 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,6 @@ 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)?; - - create_file( - &dir, - "main_test.ts", - include_str!("./templates/main_test.ts"), - )?; - if init_flags.lib { // Extract the directory name to use as the project name let project_name = dir @@ -59,11 +28,73 @@ pub fn init_project(init_flags: InitFlags) -> Result<(), AnyError> { .to_str() .unwrap(); - let template_content = include_str!("./templates/deno_lib.json"); - let new_content = template_content.replace("{{name}}", project_name); - create_file(&dir, "deno.json", &new_content)?; + 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, "deno.json", include_str!("./templates/deno.json"))?; + 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"; + +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")); @@ -74,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(()) + } +} diff --git a/cli/tools/init/templates/deno.json b/cli/tools/init/templates/deno.json deleted file mode 100644 index 3c5130f1d39938..00000000000000 --- a/cli/tools/init/templates/deno.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "tasks": { - "dev": "deno run --watch main.ts" - } -} diff --git a/cli/tools/init/templates/deno_lib.json b/cli/tools/init/templates/deno_lib.json deleted file mode 100644 index efcb77ca1c87c9..00000000000000 --- a/cli/tools/init/templates/deno_lib.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "{{name}}", - "version": "1.0.0", - "exports": {}, - "tasks": { - "dev": "deno test --watch mod.ts" - } -} diff --git a/cli/tools/init/templates/main.ts b/cli/tools/init/templates/main.ts deleted file mode 100644 index be043e97c87dc9..00000000000000 --- a/cli/tools/init/templates/main.ts +++ /dev/null @@ -1,8 +0,0 @@ -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 deleted file mode 100644 index 26af2582bc2370..00000000000000 --- a/cli/tools/init/templates/main_test.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { assertEquals } from "jsr:@std/assert"; -import { add } from "./main.ts"; - -Deno.test(function addTest() { - assertEquals(add(2, 3), 5); -}); diff --git a/tests/specs/init/lib/__test__.jsonc b/tests/specs/init/lib/__test__.jsonc new file mode 100644 index 00000000000000..076930ae626921 --- /dev/null +++ b/tests/specs/init/lib/__test__.jsonc @@ -0,0 +1,16 @@ +{ + "tempDir": true, + "steps": [{ + "args": "init --lib project", + "output": "init.out" + }, { + "cwd": "project", + "args": "test", + "output": "test.out" + }, { + "cwd": "project", + "args": "publish --dry-run", + "output": "dry_publish.out", + "exitCode": 1 + }] +} diff --git a/tests/specs/init/lib/dry_publish.out b/tests/specs/init/lib/dry_publish.out new file mode 100644 index 00000000000000..31f91c38dcf48f --- /dev/null +++ b/tests/specs/init/lib/dry_publish.out @@ -0,0 +1,7 @@ +Check file:///[WILDLINE]/mod.ts +Checking for slow types in the public API... +Check file:///[WILDLINE]/mod.ts +error: Failed preparing 'project'. + +Caused by: + Invalid package name, use '@/ format diff --git a/tests/specs/init/lib/init.out b/tests/specs/init/lib/init.out new file mode 100644 index 00000000000000..0f1a83f300eee2 --- /dev/null +++ b/tests/specs/init/lib/init.out @@ -0,0 +1,14 @@ +✅ Project initialized + +Run these commands to get started + + cd project + + # Run the tests + deno test + + # Run the tests and watch for file changes + deno task dev + + # Publish to JSR (dry run) + deno publish --dry-run diff --git a/tests/specs/init/lib/test.out b/tests/specs/init/lib/test.out new file mode 100644 index 00000000000000..0b225a52bb302d --- /dev/null +++ b/tests/specs/init/lib/test.out @@ -0,0 +1,14 @@ +Download http://127.0.0.1:4250/@std/assert/meta.json +Download http://127.0.0.1:4250/@std/assert/0.220.1_meta.json +[UNORDERED_START] +Download http://127.0.0.1:4250/@std/assert/0.220.1/mod.ts +Download http://127.0.0.1:4250/@std/assert/0.220.1/assert_equals.ts +Download http://127.0.0.1:4250/@std/assert/0.220.1/assert.ts +Download http://127.0.0.1:4250/@std/assert/0.220.1/fail.ts +[UNORDERED_END] +Check file:///[WILDLINE]/mod_test.ts +running 1 test from ./mod_test.ts +addTest ... ok ([WILDLINE]) + +ok | 1 passed | 0 failed ([WILDLINE]) +