From b2b48b9af1da11fc8f30dc2cd1a5ed21e1d4e448 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 4 Jul 2023 09:36:18 +0800 Subject: [PATCH] feat(crb): support bun.js --- src/index.ts | 31 ++++++++++++++++--------------- src/utils.ts | 18 ------------------ 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/index.ts b/src/index.ts index b68fb1a..ac5ef01 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,7 +16,6 @@ import { isEmpty, isValidPackageName, pkgFromUserAgent, - setupReactSwc, toValidPackageName, write, } from "./utils"; @@ -113,14 +112,7 @@ async function init() { } // determine template - let template: string = boilerplate || argvTemplate; - - let isReactSwc = false; - - if (template.includes("-swc")) { - isReactSwc = true; - template = template.replace("-swc", ""); - } + const template: string = boilerplate || argvTemplate; const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent); @@ -132,18 +124,31 @@ async function init() { if (customCommand) { const fullCustomCommand = customCommand - .replace(/^npm create/, `${pkgManager} create`) + .replace(/^npm create/, () => { + // `bun create` uses it's own set of templates, + // the closest alternative is using `bun x` directly on the package + if (pkgManager === "bun") { + return "bun x create-"; + } + + return `${pkgManager} create `; + }) // Only Yarn 1.x doesn't support `@version` in the `create` command .replace("@latest", () => (isYarn1 ? "" : "@latest")) .replace(/^npm exec/, () => { - // Prefer `pnpm dlx` or `yarn dlx` + // Prefer `pnpm dlx`, `yarn dlx`, or `bun x` if (pkgManager === "pnpm") { return "pnpm dlx"; } + if (pkgManager === "yarn" && !isYarn1) { return "yarn dlx"; } + if (pkgManager === "bun") { + return "bun x"; + } + // Use `npm exec` in all other cases, // including Yarn 1.x and other custom npm clients. return "npm exec"; @@ -185,10 +190,6 @@ async function init() { write(root, templateDir, "package.json", JSON.stringify(pkg, null, 2) + "\n"); - if (isReactSwc) { - setupReactSwc(root, template.endsWith("-ts")); - } - const cdProjectName = path.relative(cwd, root); console.log(`\nDone. Now run:\n`); diff --git a/src/utils.ts b/src/utils.ts index 3079daf..208f09d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -84,24 +84,6 @@ export const pkgFromUserAgent = (userAgent: string | undefined) => { }; }; -export const setupReactSwc = (root: string, isTs: boolean) => { - editFile(path.resolve(root, "package.json"), (content) => { - return content.replace( - /"@vitejs\/plugin-react": ".+?"/, - `"@vitejs/plugin-react-swc": "^3.0.0"` - ); - }); - editFile( - path.resolve(root, `vite.config.${isTs ? "ts" : "js"}`), - (content) => { - return content.replace( - "@vitejs/plugin-react", - "@vitejs/plugin-react-swc" - ); - } - ); -}; - export const editFile = ( file: string, callback: (content: string) => string