Files
supabase/apps/design-system/lib/rehype-npm-command.ts
Ivan Vasilov 1cd1ebfc7f chire: Sort imports in all packages, cms, design-system and ui-library apps (#41610)
Sorted all imports in all packages, `cms`, `design-system` and
`ui-library` apps by running `pnpm format` on them.

All changes in this PR are done by the script.
2026-02-05 13:54:10 +01:00

44 lines
1.8 KiB
TypeScript

import { visit } from 'unist-util-visit'
import { UnistNode, UnistTree } from '@/types/unist'
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')
}
})
}
}