Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(npm-to-yarn): add missing npm-to-yarn converter for Bun #10803

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading