From 3fbe3314d92b870509ac8cdc8d0c638a8e58d02f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lehoczky=20Zolt=C3=A1n?= Date: Sun, 29 Dec 2024 23:07:05 +0100 Subject: [PATCH] feat(npm-to-yarn): add support for Bun --- .../README.md | 5 ++-- .../__snapshots__/index.test.ts.snap | 24 +++++++++++++++++++ .../src/__tests__/index.test.ts | 6 +++++ .../src/index.ts | 15 ++++++++++-- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-remark-plugin-npm2yarn/README.md b/packages/docusaurus-remark-plugin-npm2yarn/README.md index 055ca12d30cd..e0a08aa30f4d 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/README.md +++ b/packages/docusaurus-remark-plugin-npm2yarn/README.md @@ -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]; @@ -83,6 +83,7 @@ type CustomConverter = [name: string, cb: (npmCode: string) => string]; converters: [ 'yarn', 'pnpm', + 'bun', ['Turbo', (code) => code.replace(/npm/g, 'turbo')], ], }, diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.ts.snap index adbbcadc9e67..797b89fb9780 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/__snapshots__/index.test.ts.snap @@ -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. + + + + \`\`\`bash + npm install --save docusaurus-plugin-name + \`\`\` + + + + \`\`\`bash + bun add docusaurus-plugin-name + \`\`\` + + +" +`; + exports[`npm2yarn plugin work with custom converter 1`] = ` "import Tabs from '@theme/Tabs' import TabItem from '@theme/TabItem' diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.ts b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.ts index 575365c8f26d..9cf69eccf797 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.ts +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/__tests__/index.test.ts @@ -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')]], diff --git a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts index 17bd834fb4bc..9e3a16a7777e 100644 --- a/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts +++ b/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts @@ -20,7 +20,7 @@ import type {Transformer} from 'unified'; // eslint-disable-next-line @typescript-eslint/no-unused-vars type Plugin = any; // TODO fix this asap -type KnownConverter = 'yarn' | 'pnpm'; +type KnownConverter = 'yarn' | 'pnpm' | 'bun'; type CustomConverter = [name: string, cb: (npmCode: string) => string]; @@ -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; @@ -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',