1+ import { logger } from '@socketsecurity/registry/lib/logger'
2+
3+ import { npmFix } from './npm-fix.mts'
14import { outputFixResult } from './output-fix-result.mts'
2- import { runFix } from './run-fix.mts'
5+ import { pnpmFix } from './pnpm-fix.mts'
6+ import { CMD_NAME } from './shared.mts'
7+ import constants from '../../constants.mts'
8+ import { detectAndValidatePackageEnvironment } from '../../utils/package-environment.mts'
39
410import type { OutputKind } from '../../types.mts'
511import type { RangeStyle } from '../../utils/semver.mts'
612
13+ const { NPM , PNPM } = constants
14+
715export async function handleFix ( {
816 autoMerge,
917 cwd,
@@ -23,12 +31,47 @@ export async function handleFix({
2331 test : boolean
2432 testScript : string
2533} ) {
26- const result = await runFix ( {
34+ const pkgEnvResult = await detectAndValidatePackageEnvironment ( cwd , {
35+ cmdName : CMD_NAME ,
36+ logger,
37+ } )
38+ if ( ! pkgEnvResult . ok ) {
39+ return pkgEnvResult
40+ }
41+
42+ const pkgEnvDetails = pkgEnvResult . data
43+ if ( ! pkgEnvDetails ) {
44+ return {
45+ ok : false ,
46+ message : 'No package found' ,
47+ cause : `No valid package environment was found in given cwd (${ cwd } )` ,
48+ }
49+ }
50+
51+ logger . info (
52+ `Fixing packages for ${ pkgEnvDetails . agent } v${ pkgEnvDetails . agentVersion } .\n` ,
53+ )
54+
55+ const { agent } = pkgEnvDetails
56+ if ( agent !== NPM && agent !== PNPM ) {
57+ return {
58+ ok : false ,
59+ message : 'Not supported' ,
60+ cause : `${ agent } is not supported by this command at the moment.` ,
61+ }
62+ }
63+
64+ // Lazily access spinner.
65+ const { spinner } = constants
66+ const fixer = agent === NPM ? npmFix : pnpmFix
67+
68+ const result = await fixer ( pkgEnvDetails , {
2769 autoMerge,
2870 cwd,
2971 limit,
3072 purls,
3173 rangeStyle,
74+ spinner,
3275 test,
3376 testScript,
3477 } )
0 commit comments