Skip to content

Commit

Permalink
feat(crb): support bun.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Simoon-F committed Jul 4, 2023
1 parent 6043101 commit b2b48b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
31 changes: 16 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
isEmpty,
isValidPackageName,
pkgFromUserAgent,
setupReactSwc,
toValidPackageName,
write,
} from "./utils";
Expand Down Expand Up @@ -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);

Expand All @@ -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";
Expand Down Expand Up @@ -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`);
Expand Down
18 changes: 0 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b2b48b9

Please sign in to comment.