diff --git a/cli/tools/init/mod.rs b/cli/tools/init/mod.rs index 25b86cb957..36bdbac2bc 100644 --- a/cli/tools/init/mod.rs +++ b/cli/tools/init/mod.rs @@ -252,8 +252,46 @@ Deno.test(function addTest() { Ok(0) } +fn npm_name_to_create_package(name: &str) -> String { + let mut s = "npm:".to_string(); + + let mut scoped = false; + let mut create = false; + + for (i, ch) in name.char_indices() { + if i == 0 { + if ch == '@' { + scoped = true; + } else { + create = true; + s.push_str("create-"); + } + } else if scoped { + if ch == '/' { + scoped = false; + create = true; + s.push_str("/create-"); + continue; + } else if ch == '@' && !create { + scoped = false; + create = true; + s.push_str("/create@"); + continue; + } + } + + s.push(ch); + } + + if !create { + s.push_str("/create"); + } + + s +} + async fn init_npm(name: &str, args: Vec) -> Result { - let script_name = format!("npm:create-{}", name); + let script_name = npm_name_to_create_package(name); fn print_manual_usage(script_name: &str, args: &[String]) -> i32 { log::info!("{}", cformat!("You can initialize project manually by running deno run {} {} and applying desired permissions.", script_name, args.join(" "))); @@ -336,3 +374,37 @@ fn create_file( Ok(()) } } + +#[cfg(test)] +mod test { + use crate::tools::init::npm_name_to_create_package; + + #[test] + fn npm_name_to_create_package_test() { + // See https://docs.npmjs.com/cli/v8/commands/npm-init#description + assert_eq!( + npm_name_to_create_package("foo"), + "npm:create-foo".to_string() + ); + assert_eq!( + npm_name_to_create_package("foo@1.0.0"), + "npm:create-foo@1.0.0".to_string() + ); + assert_eq!( + npm_name_to_create_package("@foo"), + "npm:@foo/create".to_string() + ); + assert_eq!( + npm_name_to_create_package("@foo@1.0.0"), + "npm:@foo/create@1.0.0".to_string() + ); + assert_eq!( + npm_name_to_create_package("@foo/bar"), + "npm:@foo/create-bar".to_string() + ); + assert_eq!( + npm_name_to_create_package("@foo/bar@1.0.0"), + "npm:@foo/create-bar@1.0.0".to_string() + ); + } +}