Skip to content

Commit 94f42b7

Browse files
authored
Make fix reuse implementations (#671)
1 parent 05c63be commit 94f42b7

File tree

9 files changed

+899
-1339
lines changed

9 files changed

+899
-1339
lines changed

src/commands/fix/agent-fix.mts

Lines changed: 630 additions & 0 deletions
Large diffs are not rendered by default.

src/commands/fix/get-actual-tree.mts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
Arborist,
3+
SAFE_ARBORIST_REIFY_OPTIONS_OVERRIDES,
4+
} from '../../shadow/npm/arborist/index.mts'
5+
6+
import type { NodeClass } from '../../shadow/npm/arborist/types.mts'
7+
8+
export async function getActualTree(
9+
cwd: string = process.cwd(),
10+
): Promise<NodeClass> {
11+
// @npmcli/arborist DOES have partial support for pnpm structured node_modules
12+
// folders. However, support is iffy resulting in unhappy path errors and hangs.
13+
// So, to avoid the unhappy path, we restrict our usage to --dry-run loading
14+
// of the node_modules folder.
15+
const arb = new Arborist({
16+
path: cwd,
17+
...SAFE_ARBORIST_REIFY_OPTIONS_OVERRIDES,
18+
})
19+
return await arb.loadActual()
20+
}

src/commands/fix/handle-fix.mts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
import { logger } from '@socketsecurity/registry/lib/logger'
2+
3+
import { npmFix } from './npm-fix.mts'
14
import { 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

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

13+
const { NPM, PNPM } = constants
14+
715
export 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

Comments
 (0)