Skip to content

Commit 50c3b35

Browse files
authored
fix: detect package manager for missing collection install suggestion (#474)
1 parent b45023d commit 50c3b35

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

src/runtime/server/api.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ const warnOnceSet = /* @__PURE__ */ new Set<string>()
1212

1313
const DEFAULT_ENDPOINT = 'https://api.iconify.design'
1414

15+
function getInstallCommand(pkg: string): string {
16+
const ua = process.env.npm_config_user_agent || ''
17+
if (ua.startsWith('pnpm')) return `pnpm add -D ${pkg}`
18+
if (ua.startsWith('yarn')) return `yarn add -D ${pkg}`
19+
if (ua.startsWith('bun')) return `bun add -D ${pkg}`
20+
return `npm i -D ${pkg}`
21+
}
22+
1523
export default defineCachedEventHandler(async (event: H3Event) => {
1624
const url = getRequestURL(event) as URL
1725
if (!url)
@@ -41,7 +49,7 @@ export default defineCachedEventHandler(async (event: H3Event) => {
4149
if (collectionName && !warnOnceSet.has(collectionName) && apiEndPoint === DEFAULT_ENDPOINT) {
4250
consola.warn([
4351
`[Icon] Collection \`${collectionName}\` is not found locally`,
44-
`We suggest to install it via \`npm i -D @iconify-json/${collectionName}\` to provide the best end-user experience.`,
52+
`We suggest to install it via \`${getInstallCommand(`@iconify-json/${collectionName}`)}\` to provide the best end-user experience.`,
4553
].join('\n'))
4654
warnOnceSet.add(collectionName)
4755
}

0 commit comments

Comments
 (0)