Created
September 10, 2023 11:53
-
-
Save NagayamaRyoga/1632acfae102d28b7205026b9d52d099 to your computer and use it in GitHub Desktop.
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
// ~/.config/zeno/scripts/package-manager.ts | |
import { dirname } from "https://deno.land/std/path/mod.ts"; | |
const detectPackageManager = async (path: string) => { | |
const packageManagers = [ | |
{ name: "npm", lockFile: "package-lock.json" }, | |
{ name: "yarn", lockFile: "yarn.lock" }, | |
{ name: "pnpm", lockFile: "pnpm-lock.yaml" }, | |
]; | |
for (let dir = path; ; ) { | |
for (const { name, lockFile } of packageManagers) { | |
try { | |
const info = await Deno.lstat(`${dir}/${lockFile}`); | |
if (info.isFile) { | |
return name; | |
} | |
} catch (err) { | |
// ignore | |
} | |
} | |
const parent = dirname(dir); | |
if (parent === dir) { | |
break; | |
} | |
dir = parent; | |
} | |
return "npm"; | |
}; | |
console.log(await detectPackageManager(Deno.cwd())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment