From 61c8a3f130a0c87f47a222987caec0131cc81f0a Mon Sep 17 00:00:00 2001 From: Sukka Date: Sat, 31 Aug 2024 15:47:31 +0800 Subject: [PATCH] feat(cli): use `package-manager-detector` (#92) --- packages/tools/cli/package.json | 1 + packages/tools/cli/src/cli.ts | 9 +++--- packages/tools/cli/src/index.ts | 2 +- packages/tools/cli/src/package-manager.ts | 35 ++++++----------------- pnpm-lock.yaml | 8 ++++++ 5 files changed, 23 insertions(+), 32 deletions(-) diff --git a/packages/tools/cli/package.json b/packages/tools/cli/package.json index a842f744..87569080 100644 --- a/packages/tools/cli/package.json +++ b/packages/tools/cli/package.json @@ -37,6 +37,7 @@ "commander": "^11.1.0", "detect-indent": "^6.1.0", "fast-npm-meta": "^0.2.2", + "package-manager-detector": "^0.1.1", "picocolors": "^1.0.1", "rollup-plugin-visualizer": "^5.12.0" }, diff --git a/packages/tools/cli/src/cli.ts b/packages/tools/cli/src/cli.ts index 94ff8479..0351d47b 100644 --- a/packages/tools/cli/src/cli.ts +++ b/packages/tools/cli/src/cli.ts @@ -2,7 +2,6 @@ import path from 'path'; import picocolors from 'picocolors'; import { Command, Option } from 'commander'; import handleError from './handle-error'; -import { detectPackageManager, type PackageManager } from './package-manager'; import { renderTree } from './renderTree'; import { overridesPackageJson } from './lib/json'; @@ -11,6 +10,7 @@ import { handleSigTerm } from './lib/handle-sigterm'; import { findPackagesCoveredByNolyfill, findPackagesNotCoveredByNolyfill } from './find-coverable-packages'; import { checkForUpdates } from './check-update'; import { generateIssue } from './generate-issue'; +import { detectPackageManager, type PackageManager } from './package-manager'; interface CliOptions { /** see full error messages, mostly for debugging */ @@ -32,7 +32,7 @@ const pmCommandOption = new Option('--pm [package manager]', 'specify which pack handleSigTerm(); -// eslint-disable-next-line @typescript-eslint/no-var-requires -- version +// eslint-disable-next-line @typescript-eslint/no-require-imports -- TBD const { version } = require('../package.json') as PKG; const checkUnsupportedPM = (packageManager: PackageManager) => { @@ -78,10 +78,10 @@ const program = new Command('nolyfill'); .addOption(pmCommandOption) .addOption(new Option('-f --format [format]', 'output format for console') .choices(['humanreadable', 'json']) - .default('humanreadable') - ) + .default('humanreadable')) .action(async (source: string | undefined, option: CheckCommandOptions) => { const projectPath = path.resolve(source ?? process.cwd()); + // TODO: use `package-manager-detector` agent option const packageManager = option.pm === 'auto' ? await detectPackageManager(projectPath) : option.pm; const format = option.format; @@ -120,6 +120,7 @@ const program = new Command('nolyfill'); .addOption(pmCommandOption) .action(async (source: string | undefined, option: PmCommandOptions) => { const projectPath = path.resolve(source ?? process.cwd()); + // TODO: use `package-manager-detector` agent option const packageManager = option.pm === 'auto' ? await detectPackageManager(projectPath) : option.pm; if (checkUnsupportedPM(packageManager)) { diff --git a/packages/tools/cli/src/index.ts b/packages/tools/cli/src/index.ts index 02b254ed..382bdfe3 100644 --- a/packages/tools/cli/src/index.ts +++ b/packages/tools/cli/src/index.ts @@ -2,5 +2,5 @@ export { allPackages } from './all-packages'; export { findPackagesCoveredByNolyfill, findPackagesNotCoveredByNolyfill } from './find-coverable-packages'; export { overridesPackageJson } from './lib/json'; -export { detectPackageManager, type PackageManager } from './package-manager'; +export { type PackageManager } from './package-manager'; export * from './types'; diff --git a/packages/tools/cli/src/package-manager.ts b/packages/tools/cli/src/package-manager.ts index 49faa6cb..b6b53e43 100644 --- a/packages/tools/cli/src/package-manager.ts +++ b/packages/tools/cli/src/package-manager.ts @@ -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>; -const checkFile = (path: PathLike) => fsp.access(path, fs.constants.F_OK); - -export async function detectPackageManager(projectPath: string): Promise { - 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); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 38c45d6d..4ff2ef67 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -907,6 +907,9 @@ importers: fast-npm-meta: specifier: ^0.2.2 version: 0.2.2 + package-manager-detector: + specifier: ^0.1.1 + version: 0.1.1 picocolors: specifier: ^1.0.1 version: 1.0.1 @@ -3591,6 +3594,9 @@ packages: package-json-from-dist@1.0.0: resolution: {integrity: sha512-dATvCeZN/8wQsGywez1mzHtTlP22H8OEfPrVMLNr4/eGa+ijtLn/6M5f0dY8UKNrC2O9UCU6SSoG3qRKnt7STw==} + package-manager-detector@0.1.1: + resolution: {integrity: sha512-KRfleQVD8jK1lIOo6fJxAtBH0fYo/r9q1+2Zs931cz0GLt1WeGYgIC0J+kETRZk8Ha2HghztDQSEqbeo00bzhw==} + pacote@15.2.0: resolution: {integrity: sha512-rJVZeIwHTUta23sIZgEIM62WYwbmGbThdbnkt81ravBplQv+HjyroqnLRNH2+sLJHcGZmLRmhPwACqhfTcOmnA==} engines: {node: ^14.17.0 || ^16.13.0 || >=18.0.0} @@ -7475,6 +7481,8 @@ snapshots: package-json-from-dist@1.0.0: {} + package-manager-detector@0.1.1: {} + pacote@15.2.0: dependencies: '@npmcli/git': 4.1.0