Skip to content

Commit

Permalink
feat(cli): use package-manager-detector
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Aug 31, 2024
1 parent 2f2a655 commit 3ce10e6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 41 deletions.
1 change: 1 addition & 0 deletions packages/tools/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
9 changes: 5 additions & 4 deletions packages/tools/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 */
Expand All @@ -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) => {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/tools/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
35 changes: 8 additions & 27 deletions packages/tools/cli/src/package-manager.ts
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);
20 changes: 11 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3ce10e6

Please sign in to comment.