From 1337bb69231d439bcfe458fd7454a5197590ffd6 Mon Sep 17 00:00:00 2001 From: dave caruso Date: Thu, 7 Dec 2023 00:57:31 -0800 Subject: [PATCH] fix(create): make `bun create @ org` work (#7498) --- src/cli/bunx_command.zig | 20 ++++++++++++++++-- test/cli/install/bun-create.test.ts | 32 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/cli/bunx_command.zig b/src/cli/bunx_command.zig index 9e7c1567d5..83832bf513 100644 --- a/src/cli/bunx_command.zig +++ b/src/cli/bunx_command.zig @@ -23,13 +23,29 @@ pub const BunxCommand = struct { var new_str = try allocator.allocSentinel(u8, input.len + prefixLength, 0); if (input[0] == '@') { - if (strings.indexAnyComptime(input, "/")) |slashIndex| { - const index = slashIndex + 1; + // @org/some -> @org/create-some + // @org/some@v -> @org/create-some@v + if (strings.indexOfChar(input, '/')) |slash_i| { + const index = slash_i + 1; @memcpy(new_str[0..index], input[0..index]); @memcpy(new_str[index .. index + prefixLength], "create-"); @memcpy(new_str[index + prefixLength ..], input[index..]); return new_str; } + // @org@v -> @org/create@v + else if (strings.indexOfChar(input[1..], '@')) |at_i| { + const index = at_i + 1; + @memcpy(new_str[0..index], input[0..index]); + @memcpy(new_str[index .. index + prefixLength], "/create"); + @memcpy(new_str[index + prefixLength ..], input[index..]); + return new_str; + } + // @org -> @org/create + else { + @memcpy(new_str[0..input.len], input); + @memcpy(new_str[input.len..], "/create"); + return new_str; + } } @memcpy(new_str[0..prefixLength], "create-"); diff --git a/test/cli/install/bun-create.test.ts b/test/cli/install/bun-create.test.ts index 95540717da..660077db52 100644 --- a/test/cli/install/bun-create.test.ts +++ b/test/cli/install/bun-create.test.ts @@ -30,6 +30,38 @@ it("should create selected template with @ prefix", async () => { ); }); +it("should create selected template with @ prefix implicit `/create`", async () => { + const { stderr } = spawn({ + cmd: [bunExe(), "create", "@second-quick-start"], + cwd: x_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + + const err = await new Response(stderr).text(); + expect(err.split(/\r?\n/)).toContain( + `error: package "@second-quick-start/create" not found registry.npmjs.org/@second-quick-start%2fcreate 404`, + ); +}); + +it("should create selected template with @ prefix implicit `/create` with version", async () => { + const { stderr } = spawn({ + cmd: [bunExe(), "create", "@second-quick-start"], + cwd: x_dir, + stdout: null, + stdin: "pipe", + stderr: "pipe", + env, + }); + + const err = await new Response(stderr).text(); + expect(err.split(/\r?\n/)).toContain( + `error: package "@second-quick-start/create" not found registry.npmjs.org/@second-quick-start%2fcreate 404`, + ); +}); + it("should create template from local folder", async () => { const bunCreateDir = join(x_dir, "bun-create"); const testTemplate = "test-template";