Skip to content

Commit

Permalink
feat(npm-to-yarn): add support for Bun
Browse files Browse the repository at this point in the history
  • Loading branch information
Lehoczky committed Dec 29, 2024
1 parent 43795fc commit 3fbe331
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/docusaurus-remark-plugin-npm2yarn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ module.exports = {
| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `sync` | `boolean` | `false` | Syncing tab choices (Yarn and npm). See https://docusaurus.io/docs/markdown-features/#syncing-tab-choices for details. |
| `converters` | `array` | `'yarn'`, `'pnpm'` | The list of converters to use. The order of the converters is important, as the first converter will be used as the default choice. |
| `converters` | `array` | `'yarn'`, `'pnpm'`, `'bun'` | The list of converters to use. The order of the converters is important, as the first converter will be used as the default choice. |

## Custom converters

In case you want to convert npm commands to something else than `yarn` or `pnpm`, you can use custom converters:
In case you want to convert npm commands to something else than `yarn`, `pnpm` or `bun`, you can use custom converters:

```ts
type CustomConverter = [name: string, cb: (npmCode: string) => string];
Expand All @@ -83,6 +83,7 @@ type CustomConverter = [name: string, cb: (npmCode: string) => string];
converters: [
'yarn',
'pnpm',
'bun',
['Turbo', (code) => code.replace(/npm/g, 'turbo')],
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,30 @@ npm install --save docusaurus-plugin-name
"
`;

exports[`npm2yarn plugin work with bun converter 1`] = `
"import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
## Installing a plugin
A plugin is usually a npm package, so you install them like other npm packages using npm.
<Tabs>
<TabItem value="npm">
\`\`\`bash
npm install --save docusaurus-plugin-name
\`\`\`
</TabItem>
<TabItem value="bun" label="Bun">
\`\`\`bash
bun add docusaurus-plugin-name
\`\`\`
</TabItem>
</Tabs>
"
`;

exports[`npm2yarn plugin work with custom converter 1`] = `
"import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ describe('npm2yarn plugin', () => {
expect(result).toMatchSnapshot();
});

it('work with bun converter', async () => {
const result = await processFixture('plugin', {converters: ['bun']});

expect(result).toMatchSnapshot();
});

it('work with custom converter', async () => {
const result = await processFixture('plugin', {
converters: [['Turbo', (code) => code.replace(/npm/g, 'turbo')]],
Expand Down
15 changes: 13 additions & 2 deletions packages/docusaurus-remark-plugin-npm2yarn/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {Transformer} from 'unified';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Plugin<T> = any; // TODO fix this asap

type KnownConverter = 'yarn' | 'pnpm';
type KnownConverter = 'yarn' | 'pnpm' | 'bun';

type CustomConverter = [name: string, cb: (npmCode: string) => string];

Expand Down Expand Up @@ -90,7 +90,7 @@ const transformNode = (
code: npmToYarn(npmCode, converter),
node,
value: converter,
label: converter === 'yarn' ? 'Yarn' : converter,
label: getLabelForConverter(converter),
});
}
const [converterName, converterFn] = converter;
Expand All @@ -101,6 +101,17 @@ const transformNode = (
});
}

function getLabelForConverter(converter: KnownConverter) {
switch (converter) {
case 'yarn':
return 'Yarn';
case 'bun':
return 'Bun';
default:
return converter;
}
}

return [
{
type: 'mdxJsxFlowElement',
Expand Down

0 comments on commit 3fbe331

Please sign in to comment.