Skip to content

Commit

Permalink
feat: support bun package manager in create-docusaurus (#9241)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks authored Aug 24, 2023
1 parent 101e133 commit 13a8ba1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/create-docusaurus/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ program
.arguments('[siteName] [template] [rootDir]')
.option(
'-p, --package-manager <manager>',
'The package manager used to install dependencies. One of yarn, npm, and pnpm.',
'The package manager used to install dependencies. One of yarn, npm, pnpm, and bun.',
)
.option(
'-s, --skip-install',
Expand Down
20 changes: 14 additions & 6 deletions packages/create-docusaurus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const lockfileNames = {
npm: 'package-lock.json',
yarn: 'yarn.lock',
pnpm: 'pnpm-lock.yaml',
bun: 'bun.lockb',
};

type PackageManager = keyof typeof lockfileNames;
Expand Down Expand Up @@ -57,11 +58,12 @@ function findPackageManagerFromUserAgent(): PackageManager | undefined {
async function askForPackageManagerChoice(): Promise<PackageManager> {
const hasYarn = shell.exec('yarn --version', {silent: true}).code === 0;
const hasPnpm = shell.exec('pnpm --version', {silent: true}).code === 0;
const hasBun = shell.exec('bun --version', {silent: true}).code === 0;

if (!hasYarn && !hasPnpm) {
if (!hasYarn && !hasPnpm && !hasBun) {
return 'npm';
}
const choices = ['npm', hasYarn && 'yarn', hasPnpm && 'pnpm']
const choices = ['npm', hasYarn && 'yarn', hasPnpm && 'pnpm', hasBun && 'bun']
.filter((p): p is string => Boolean(p))
.map((p) => ({title: p, value: p}));

Expand Down Expand Up @@ -524,7 +526,11 @@ export default async function init(
logger.info`Installing dependencies with name=${pkgManager}...`;
if (
shell.exec(
pkgManager === 'yarn' ? 'yarn' : `${pkgManager} install --color always`,
pkgManager === 'yarn'
? 'yarn'
: pkgManager === 'bun'
? 'bun install'
: `${pkgManager} install --color always`,
{
env: {
...process.env,
Expand All @@ -545,19 +551,21 @@ export default async function init(
}

const useNpm = pkgManager === 'npm';
const useBun = pkgManager === 'bun';
const useRunCommand = useNpm || useBun;
logger.success`Created name=${cdpath}.`;
logger.info`Inside that directory, you can run several commands:
code=${`${pkgManager} start`}
Starts the development server.
code=${`${pkgManager} ${useNpm ? 'run ' : ''}build`}
code=${`${pkgManager} ${useRunCommand ? 'run ' : ''}build`}
Bundles your website into static files for production.
code=${`${pkgManager} ${useNpm ? 'run ' : ''}serve`}
code=${`${pkgManager} ${useRunCommand ? 'run ' : ''}serve`}
Serves the built website locally.
code=${`${pkgManager} deploy`}
code=${`${pkgManager} ${useRunCommand ? 'run ' : ''}deploy`}
Publishes the website to GitHub pages.
We recommend that you begin by typing:
Expand Down
2 changes: 2 additions & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ browserslist
browserstack
buble
builtins
bunx
caabernathy
cacheable
callouts
Expand Down Expand Up @@ -167,6 +168,7 @@ lifecycles
lighthouserc
linkify
localizable
lockb
longpaths
lorber
lowercased
Expand Down
4 changes: 2 additions & 2 deletions website/docs/api/misc/create-docusaurus.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ Used when the template argument is a git repo. It needs to be one of:

### `-p, --package-manager` {#package-manager}

Value should be one of `npm`, `yarn`, or `pnpm`. If it's not explicitly provided, Docusaurus will infer one based on:
Value should be one of `npm`, `yarn`, `pnpm`, or `bun`. If it's not explicitly provided, Docusaurus will infer one based on:

- The lockfile already present in the CWD (e.g. if you are setting up website in an existing project)
- The command used to invoke `create-docusaurus` (e.g. `npm init`, `npx`, `yarn create`, etc.)
- The command used to invoke `create-docusaurus` (e.g. `npm init`, `npx`, `yarn create`, `bunx`, etc.)
- Interactive prompting, in case all heuristics are not present

### `-s, --skip-install` {#skip-install}
Expand Down

0 comments on commit 13a8ba1

Please sign in to comment.