Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

package-bin.ts

14 lines548 Bytes

1import { createRequire } from 'node:module'
2​
3const require = createRequire(import.meta.url)
4​
5/** Absolute path of a dependency's bin, to run with `process.execPath`. */
6export function packageBin(name: string): string {
7 const manifest = require(`${name}/package.json`) as {
8 bin?: string | Record<string, string>
9 }
10 const bin = manifest.bin
11 const relative = typeof bin === 'string' ? bin : bin?.[name]
12 if (!relative) throw new Error(`${name} has no "${name}" bin`)
13 return require.resolve(`${name}/${relative.replace(/^\.\//, '')}`)