-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
127 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/composable-cli/src/lib/detect-package-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { promises as fs } from "fs" | ||
import { resolve } from "path" | ||
import { execa } from "execa" | ||
|
||
export type PM = "npm" | "yarn" | "pnpm" | "bun" | ||
|
||
/** | ||
* Check if a path exists | ||
*/ | ||
async function pathExists(p: string) { | ||
try { | ||
await fs.access(p) | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
const cache = new Map() | ||
|
||
/** | ||
* Check if a global pm is available | ||
*/ | ||
function hasGlobalInstallation(pm: PM): Promise<boolean> { | ||
const key = `has_global_${pm}` | ||
if (cache.has(key)) { | ||
return Promise.resolve(cache.get(key)) | ||
} | ||
|
||
return execa(pm, ["--version"]) | ||
.then((res) => { | ||
return /^\d+.\d+.\d+$/.test(res.stdout) | ||
}) | ||
.then((value) => { | ||
cache.set(key, value) | ||
return value | ||
}) | ||
.catch(() => false) | ||
} | ||
|
||
function getTypeofLockFile(cwd = "."): Promise<PM | null> { | ||
const key = `lockfile_${cwd}` | ||
if (cache.has(key)) { | ||
return Promise.resolve(cache.get(key)) | ||
} | ||
|
||
return Promise.all([ | ||
pathExists(resolve(cwd, "yarn.lock")), | ||
pathExists(resolve(cwd, "package-lock.json")), | ||
pathExists(resolve(cwd, "pnpm-lock.yaml")), | ||
pathExists(resolve(cwd, "bun.lockb")), | ||
]).then(([isYarn, isNpm, isPnpm, isBun]) => { | ||
let value: PM | null = null | ||
|
||
if (isYarn) { | ||
value = "yarn" | ||
} else if (isPnpm) { | ||
value = "pnpm" | ||
} else if (isBun) { | ||
value = "bun" | ||
} else if (isNpm) { | ||
value = "npm" | ||
} | ||
|
||
cache.set(key, value) | ||
return value | ||
}) | ||
} | ||
|
||
const detect = async ({ | ||
cwd, | ||
includeGlobalBun, | ||
}: { cwd?: string; includeGlobalBun?: boolean } = {}) => { | ||
const type = await getTypeofLockFile(cwd) | ||
if (type) { | ||
return type | ||
} | ||
const [hasYarn, hasPnpm, hasBun] = await Promise.all([ | ||
hasGlobalInstallation("yarn"), | ||
hasGlobalInstallation("pnpm"), | ||
includeGlobalBun && hasGlobalInstallation("bun"), | ||
]) | ||
if (hasYarn) { | ||
return "yarn" | ||
} | ||
if (hasPnpm) { | ||
return "pnpm" | ||
} | ||
if (hasBun) { | ||
return "bun" | ||
} | ||
return "npm" | ||
} | ||
|
||
export { detect } | ||
|
||
export function getNpmVersion(pm: PM) { | ||
return execa(pm || "npm", ["--version"]).then((res) => res.stdout) | ||
} | ||
|
||
export function clearCache() { | ||
return cache.clear() | ||
} |