1 import React from "react"
2 import { CodeBlock, CodeBlockMetaFields } from "@/components/CodeBlock"
3 import { CodeTabs } from "@/components/CodeTabs"
4 import { CodeTab } from "@/components/CodeTabs/Item"
5 import { npxToYarn } from "@/utils/npx-to-yarn"
6
7 type Npx2YarnCodeProps = {
8 npxCode: string
9 isExecutable?: boolean
10 } & Omit<CodeBlockMetaFields, "npx2yarn">
11
12 export const Npx2YarnCode = ({
13 npxCode,
14 isExecutable = false,
15 ...codeOptions
16 }: Npx2YarnCodeProps) => {
17 // convert npx code
18 const yarnCode = npxToYarn(npxCode, "yarn", isExecutable)
19 const pnpmCode = npxToYarn(npxCode, "pnpm", isExecutable)
20 const lang = "bash"
21
22 codeOptions.hasTabs = true
23
24 const tabs = [
25 {
26 label: "yarn",
27 value: "yarn",
28 code: {
29 source: yarnCode,
30 lang,
31 ...codeOptions,
32 },
33 },
34 {
35 label: "pnpm",
36 value: "pnpm",
37 code: {
38 source: pnpmCode,
39 lang,
40 ...codeOptions,
41 },
42 },
43 {
44 label: "npx",
45 // keep it npm so it matches the tab name in Npm2YarnCode
46 value: "npm",
47 code: {
48 source: npxCode,
49 lang,
50 ...codeOptions,
51 },
52 },
53 ]
54
55 return (
56 // Keep the group name same as Npm2YarnCode, value selection will be synced across both components
57 <CodeTabs group="npm2yarn">
58 {tabs.map((tab, index) => (
59 <CodeTab label={tab.label} value={tab.value} key={index}>
60 <CodeBlock {...tab.code} />
61 </CodeTab>
62 ))}
63 </CodeTabs>
64 )
65 }