Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
630 changes: 630 additions & 0 deletions src/commands/fix/agent-fix.mts

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions src/commands/fix/get-actual-tree.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
Arborist,
SAFE_ARBORIST_REIFY_OPTIONS_OVERRIDES,
} from '../../shadow/npm/arborist/index.mts'

import type { NodeClass } from '../../shadow/npm/arborist/types.mts'

export async function getActualTree(
cwd: string = process.cwd(),
): Promise<NodeClass> {
// @npmcli/arborist DOES have partial support for pnpm structured node_modules
// folders. However, support is iffy resulting in unhappy path errors and hangs.
// So, to avoid the unhappy path, we restrict our usage to --dry-run loading
// of the node_modules folder.
const arb = new Arborist({
path: cwd,
...SAFE_ARBORIST_REIFY_OPTIONS_OVERRIDES,
})
return await arb.loadActual()
}
47 changes: 45 additions & 2 deletions src/commands/fix/handle-fix.mts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { logger } from '@socketsecurity/registry/lib/logger'

import { npmFix } from './npm-fix.mts'
import { outputFixResult } from './output-fix-result.mts'
import { runFix } from './run-fix.mts'
import { pnpmFix } from './pnpm-fix.mts'
import { CMD_NAME } from './shared.mts'
import constants from '../../constants.mts'
import { detectAndValidatePackageEnvironment } from '../../utils/package-environment.mts'

import type { OutputKind } from '../../types.mts'
import type { RangeStyle } from '../../utils/semver.mts'

const { NPM, PNPM } = constants

export async function handleFix({
autoMerge,
cwd,
Expand All @@ -23,12 +31,47 @@ export async function handleFix({
test: boolean
testScript: string
}) {
const result = await runFix({
const pkgEnvResult = await detectAndValidatePackageEnvironment(cwd, {
cmdName: CMD_NAME,
logger,
})
if (!pkgEnvResult.ok) {
return pkgEnvResult
}

const pkgEnvDetails = pkgEnvResult.data
if (!pkgEnvDetails) {
return {
ok: false,
message: 'No package found',
cause: `No valid package environment was found in given cwd (${cwd})`,
}
}

logger.info(
`Fixing packages for ${pkgEnvDetails.agent} v${pkgEnvDetails.agentVersion}.\n`,
)

const { agent } = pkgEnvDetails
if (agent !== NPM && agent !== PNPM) {
return {
ok: false,
message: 'Not supported',
cause: `${agent} is not supported by this command at the moment.`,
}
}

// Lazily access spinner.
const { spinner } = constants
const fixer = agent === NPM ? npmFix : pnpmFix

const result = await fixer(pkgEnvDetails, {
autoMerge,
cwd,
limit,
purls,
rangeStyle,
spinner,
test,
testScript,
})
Expand Down
Loading