-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): use
package-manager-detector
(#92)
- Loading branch information
Showing
5 changed files
with
23 additions
and
32 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
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 |
---|---|---|
@@ -1,32 +1,13 @@ | ||
import path from 'path'; | ||
import fs, { type PathLike } from 'fs'; | ||
import fsp from 'fs/promises'; | ||
|
||
import { fileExists } from '@nolyfill/internal'; | ||
|
||
import PromiseAny from '@nolyfill/promise.any'; | ||
import { detect } from 'package-manager-detector'; | ||
|
||
export type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun'; | ||
type PackageManagerDetectorReturn = Awaited<ReturnType<typeof detect>>; | ||
|
||
const checkFile = (path: PathLike) => fsp.access(path, fs.constants.F_OK); | ||
|
||
export async function detectPackageManager(projectPath: string): Promise<PackageManager> { | ||
const packageJsonPath = path.join(projectPath, 'package.json'); | ||
|
||
if (!await fileExists(packageJsonPath)) { | ||
throw new Error(`Failed to locate package.json at ${projectPath}. Are you sure the path is correct?`); | ||
} | ||
|
||
try { | ||
return await PromiseAny([ | ||
checkFile(path.join(projectPath, 'yarn.lock')).then<'yarn'>(() => 'yarn'), | ||
checkFile(path.join(projectPath, 'pnpm-lock.yaml')).then<'pnpm'>(() => 'pnpm'), | ||
checkFile(path.join(projectPath, 'package-lock.json')).then<'npm'>(() => 'npm'), | ||
checkFile(path.join(projectPath, 'npm-shrinkwrap.json')).then<'npm'>(() => 'npm'), | ||
checkFile(path.join(projectPath, 'bun.lockb')).then<'bun'>(() => 'bun') | ||
]); | ||
} catch (e) { | ||
console.log(e); | ||
throw new Error('Can not determine the preferred package manager.'); | ||
export function tramsformPackageManager(input: PackageManagerDetectorReturn): PackageManager { | ||
if (input.agent == null) { | ||
throw new Error('Can not determine the preferred package manager'); | ||
} | ||
return input.agent.split('@')[0] as PackageManager; | ||
} | ||
|
||
export const detectPackageManager = (cwd: string) => detect({ cwd }).then(tramsformPackageManager); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.