Files
shadcn-ui/apps/www/lib/rehype-npm-command.ts
/raj 5877dcd21a docs(www): bunx scripts run using bun instead of node (#1590)
* fix: using bun to initialize project works now

* style(www): format write

---------

Co-authored-by: shadcn <m@shadcn.com>
2023-10-21 16:47:21 +04:00

67 lines
2.0 KiB
TypeScript

import { UnistNode, UnistTree } from "types/unist"
import { visit } from "unist-util-visit"
export function rehypeNpmCommand() {
return (tree: UnistTree) => {
visit(tree, (node: UnistNode) => {
if (node.type !== "element" || node?.tagName !== "pre") {
return
}
// npm install.
if (node.properties?.["__rawString__"]?.startsWith("npm install")) {
const npmCommand = node.properties?.["__rawString__"]
node.properties["__npmCommand__"] = npmCommand
node.properties["__yarnCommand__"] = npmCommand.replace(
"npm install",
"yarn add"
)
node.properties["__pnpmCommand__"] = npmCommand.replace(
"npm install",
"pnpm add"
)
node.properties["__bunCommand__"] = npmCommand.replace(
"npm install",
"bun add"
)
}
// npx create.
if (node.properties?.["__rawString__"]?.startsWith("npx create-")) {
const npmCommand = node.properties?.["__rawString__"]
node.properties["__npmCommand__"] = npmCommand
node.properties["__yarnCommand__"] = npmCommand.replace(
"npx create-",
"yarn create "
)
node.properties["__pnpmCommand__"] = npmCommand.replace(
"npx create-",
"pnpm create "
)
node.properties["__bunCommand__"] = npmCommand.replace(
"npx",
"bunx --bun"
)
}
// npx.
if (
node.properties?.["__rawString__"]?.startsWith("npx") &&
!node.properties?.["__rawString__"]?.startsWith("npx create-")
) {
const npmCommand = node.properties?.["__rawString__"]
node.properties["__npmCommand__"] = npmCommand
node.properties["__yarnCommand__"] = npmCommand
node.properties["__pnpmCommand__"] = npmCommand.replace(
"npx",
"pnpm dlx"
)
node.properties["__bunCommand__"] = npmCommand.replace(
"npx",
"bunx --bun"
)
}
})
}
}