Skip to content

Commit ccdeec1

Browse files
committed
Add test-script flag to fix command
1 parent 3ae2a11 commit ccdeec1

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

src/commands/fix/cmd-fix.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ const config: CliCommandConfig = {
1515
description: 'Fix "fixable" Socket alerts',
1616
hidden: true,
1717
flags: {
18-
...commonFlags
18+
...commonFlags,
19+
testScript: {
20+
type: 'string',
21+
default: 'test',
22+
description: 'The test script to run for each fix attempt'
23+
}
1924
},
2025
help: (command, config) => `
2126
Usage
@@ -49,5 +54,7 @@ async function run(
4954
return
5055
}
5156

52-
await runFix()
57+
await runFix({
58+
testScript: cli.flags['testScript'] as string | undefined
59+
})
5360
}

src/commands/fix/npm-fix.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,20 @@ function isTopLevel(tree: SafeNode, node: SafeNode): boolean {
2929
}
3030

3131
type NpmFixOptions = {
32+
cwd?: string | undefined
3233
spinner?: Spinner | undefined
34+
testScript?: string | undefined
3335
}
3436

3537
export async function npmFix(
3638
_pkgEnvDetails: EnvDetails,
37-
cwd: string,
3839
options?: NpmFixOptions | undefined
3940
) {
40-
const { spinner } = { __proto__: null, ...options } as NpmFixOptions
41+
const {
42+
cwd = process.cwd(),
43+
spinner,
44+
testScript = 'test'
45+
} = { __proto__: null, ...options } as NpmFixOptions
4146

4247
spinner?.start()
4348

@@ -113,7 +118,7 @@ export async function npmFix(
113118
) {
114119
try {
115120
// eslint-disable-next-line no-await-in-loop
116-
await runScript('test', [], { spinner, stdio: 'ignore' })
121+
await runScript(testScript, [], { spinner, stdio: 'ignore' })
117122

118123
spinner?.info(`Patched ${name} ${oldVersion} -> ${node.version}`)
119124

src/commands/fix/pnpm-fix.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { readWantedLockfile } from '@pnpm/lockfile.fs'
22

33
import { getManifestData } from '@socketsecurity/registry'
4+
import { runScript } from '@socketsecurity/registry/lib/npm'
45
import {
56
fetchPackagePackument,
67
readPackageJson
@@ -25,15 +26,20 @@ import type { Spinner } from '@socketsecurity/registry/lib/spinner'
2526
const { NPM, OVERRIDES, PNPM } = constants
2627

2728
type PnpmFixOptions = {
29+
cwd?: string | undefined
2830
spinner?: Spinner | undefined
31+
testScript?: string | undefined
2932
}
3033

3134
export async function pnpmFix(
3235
pkgEnvDetails: EnvDetails,
33-
cwd: string,
3436
options?: PnpmFixOptions | undefined
3537
) {
36-
const { spinner } = { __proto__: null, ...options } as PnpmFixOptions
38+
const {
39+
cwd = process.cwd(),
40+
spinner,
41+
testScript = 'test'
42+
} = { __proto__: null, ...options } as PnpmFixOptions
3743

3844
spinner?.start()
3945

@@ -125,6 +131,8 @@ export async function pnpmFix(
125131
}
126132
}
127133
})
134+
// eslint-disable-next-line no-await-in-loop
135+
await runScript(testScript, [], { spinner, stdio: 'ignore' })
128136

129137
spinner?.info(`Patched ${name} ${oldVersion} -> ${node.version}`)
130138

src/commands/fix/run-fix.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ const { NPM, PNPM } = constants
99

1010
const CMD_NAME = 'socket fix'
1111

12-
export async function runFix() {
12+
export async function runFix({ cwd = process.cwd(), testScript = 'test' }) {
1313
// Lazily access constants.spinner.
1414
const { spinner } = constants
1515

1616
spinner.start()
1717

18-
const cwd = process.cwd()
19-
2018
const pkgEnvDetails = await detectAndValidatePackageEnvironment(cwd, {
2119
cmdName: CMD_NAME,
2220
logger
@@ -28,11 +26,15 @@ export async function runFix() {
2826

2927
switch (pkgEnvDetails.agent) {
3028
case NPM: {
31-
await npmFix(pkgEnvDetails, cwd)
29+
await npmFix(pkgEnvDetails, {
30+
testScript
31+
})
3232
break
3333
}
3434
case PNPM: {
35-
await pnpmFix(pkgEnvDetails, cwd)
35+
await pnpmFix(pkgEnvDetails, {
36+
testScript
37+
})
3638
break
3739
}
3840
}

0 commit comments

Comments
 (0)