Skip to content

Commit 8974996

Browse files
committed
Add range-style flag to fix command
1 parent d93d761 commit 8974996

File tree

7 files changed

+65
-13
lines changed

7 files changed

+65
-13
lines changed

.dep-stats.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"@socketregistry/is-interactive": "1.0.5",
1111
"@socketregistry/packageurl-js": "1.0.5",
1212
"@socketsecurity/config": "2.1.3",
13-
"@socketsecurity/registry": "1.0.139",
13+
"@socketsecurity/registry": "1.0.140",
1414
"@socketsecurity/sdk": "1.4.26",
1515
"browserslist": "4.24.4",
1616
"chalk-table": "1.0.2",

src/commands/fix/cmd-fix.test.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,22 @@ describe('socket fix', async () => {
2020
`
2121
"Fix "fixable" Socket alerts
2222
23-
Usage
24-
$ socket fix
25-
26-
Options
27-
--dryRun Do input validation for a command and exit 0 when input is ok
28-
--help Print this help
29-
--test Very the fix by running unit tests
30-
--testScript The test script to run for each fix attempt"
23+
Usage
24+
$ socket fix
25+
26+
Options
27+
--dryRun Do input validation for a command and exit 0 when input is ok
28+
--help Print this help
29+
--rangeStyle Define how updated dependency versions should be written in package.json.
30+
Available styles:
31+
*\\x09caret - Use ^ range for compatible updates (e.g. ^1.2.3)
32+
*\\x09gt - Use >= to allow any newer version (e.g. >=1.2.3)
33+
*\\x09lt - Use < to allow only lower versions (e.g. <1.2.3)
34+
*\\x09pin - Use the exact version (e.g. 1.2.3)
35+
*\\x09preserve - Retain the existing version range as-is
36+
*\\x09tilde - Use ~ range for patch/minor updates (e.g. ~1.2.3)
37+
--test Very the fix by running unit tests
38+
--testScript The test script to run for each fix attempt"
3139
`
3240
)
3341
expect(`\n ${stderr}`).toMatchInlineSnapshot(`

src/commands/fix/cmd-fix.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import { stripIndent } from 'common-tags'
2+
3+
import { joinOr } from '@socketsecurity/registry/lib/arrays'
14
import { logger } from '@socketsecurity/registry/lib/logger'
25

36
import { runFix } from './run-fix'
7+
import { RangeStyles } from './types'
48
import constants from '../../constants'
59
import { commonFlags } from '../../flags'
10+
import { handleBadInput } from '../../utils/handle-bad-input'
611
import { meowOrExit } from '../../utils/meow-with-subcommands'
712
import { getFlagListOutput } from '../../utils/output-formatting'
813

14+
import type { RangeStyle } from './types'
915
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1016

1117
const { DRY_RUN_BAIL_TEXT } = constants
@@ -16,6 +22,20 @@ const config: CliCommandConfig = {
1622
hidden: true,
1723
flags: {
1824
...commonFlags,
25+
rangeStyle: {
26+
type: 'string',
27+
default: 'preserve',
28+
description: stripIndent`
29+
Define how updated dependency versions should be written in package.json.
30+
Available styles:
31+
* caret - Use ^ range for compatible updates (e.g. ^1.2.3)
32+
* gt - Use >= to allow any newer version (e.g. >=1.2.3)
33+
* lt - Use < to allow only lower versions (e.g. <1.2.3)
34+
* pin - Use the exact version (e.g. 1.2.3)
35+
* preserve - Retain the existing version range as-is
36+
* tilde - Use ~ range for patch/minor updates (e.g. ~1.2.3)
37+
`
38+
},
1939
test: {
2040
type: 'boolean',
2141
default: true,
@@ -54,6 +74,16 @@ async function run(
5474
parentName
5575
})
5676

77+
const wasBadInput = handleBadInput({
78+
test: RangeStyles.includes(cli.flags['rangeStyle'] as string),
79+
message: `Expecting range style of ${joinOr(RangeStyles)}`,
80+
pass: 'ok',
81+
fail: 'missing'
82+
})
83+
if (wasBadInput) {
84+
return
85+
}
86+
5787
if (cli.flags['dryRun']) {
5888
logger.log(DRY_RUN_BAIL_TEXT)
5989
return
@@ -64,6 +94,9 @@ async function run(
6494

6595
await runFix({
6696
spinner,
97+
rangeStyle: (cli.flags['rangeStyle'] ?? undefined) as
98+
| RangeStyle
99+
| undefined,
67100
test: Boolean(cli.flags['test']),
68101
testScript: cli.flags['testScript'] as string | undefined
69102
})

src/commands/fix/npm-fix.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from '../../utils/arborist-helpers'
2424
import { getCveInfoByAlertsMap } from '../../utils/socket-package-alert'
2525

26+
import type { RangeStyle } from './types'
2627
import type { SafeNode } from '../../shadow/npm/arborist/lib/node'
2728
import type { EnvDetails } from '../../utils/package-environment'
2829
import type { PackageJson } from '@socketsecurity/registry/lib/packages'
@@ -49,6 +50,7 @@ async function install(
4950

5051
type NpmFixOptions = {
5152
cwd?: string | undefined
53+
rangeStyle?: RangeStyle | undefined
5254
spinner?: Spinner | undefined
5355
test?: boolean | undefined
5456
testScript?: string | undefined

src/commands/fix/pnpm-fix.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ import { getAlertsMapFromPnpmLockfile } from '../../utils/pnpm-lock-yaml'
2525
import { getCveInfoByAlertsMap } from '../../utils/socket-package-alert'
2626
import { runAgentInstall } from '../optimize/run-agent'
2727

28+
import type { RangeStyle } from './types'
29+
import type { StringKeyValueObject } from '../../types'
2830
import type { EnvDetails } from '../../utils/package-environment'
2931
import type { PackageJson } from '@socketsecurity/registry/lib/packages'
3032
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
3133

3234
const { CI, NPM, OVERRIDES, PNPM } = constants
3335

34-
type StringKeyedObject = { [key: string]: string }
35-
3636
type InstallOptions = {
3737
spinner?: Spinner | undefined
3838
}
@@ -51,6 +51,7 @@ async function install(
5151

5252
type PnpmFixOptions = {
5353
cwd?: string | undefined
54+
rangeStyle?: RangeStyle | undefined
5455
spinner?: Spinner | undefined
5556
test?: boolean | undefined
5657
testScript?: string | undefined
@@ -136,9 +137,9 @@ export async function pnpmFix(
136137
? packument.versions[targetVersion]
137138
: undefined
138139
if (targetVersion && targetPackument) {
139-
const oldPnpm = pkgJson[PNPM] as StringKeyedObject | undefined
140+
const oldPnpm = pkgJson[PNPM] as StringKeyValueObject | undefined
140141
const pnpmKeyCount = oldPnpm ? Object.keys(oldPnpm).length : 0
141-
const oldOverrides = (oldPnpm as StringKeyedObject)?.[OVERRIDES] as
142+
const oldOverrides = (oldPnpm as StringKeyValueObject)?.[OVERRIDES] as
142143
| Record<string, string>
143144
| undefined
144145
const overridesCount = oldOverrides

src/commands/fix/run-fix.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { pnpmFix } from './pnpm-fix'
55
import constants from '../../constants'
66
import { detectAndValidatePackageEnvironment } from '../../utils/package-environment'
77

8+
import type { RangeStyle } from './types'
89
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
910

1011
const { NPM, PNPM } = constants
@@ -13,13 +14,15 @@ const CMD_NAME = 'socket fix'
1314

1415
type RunFixOptions = {
1516
cwd?: string | undefined
17+
rangeStyle?: RangeStyle | undefined
1618
spinner?: Spinner | undefined
1719
test?: boolean | undefined
1820
testScript?: string | undefined
1921
}
2022

2123
export async function runFix({
2224
cwd = process.cwd(),
25+
rangeStyle,
2326
spinner,
2427
test = false,
2528
testScript = 'test'
@@ -36,6 +39,7 @@ export async function runFix({
3639
switch (pkgEnvDetails.agent) {
3740
case NPM: {
3841
await npmFix(pkgEnvDetails, {
42+
rangeStyle,
3943
spinner,
4044
test,
4145
testScript
@@ -44,6 +48,7 @@ export async function runFix({
4448
}
4549
case PNPM: {
4650
await pnpmFix(pkgEnvDetails, {
51+
rangeStyle,
4752
spinner,
4853
test,
4954
testScript

src/commands/fix/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type RangeStyle = 'caret' | 'gt' | 'lt' | 'pin' | 'preserve' | 'tilde'
2+
3+
export const RangeStyles = ['caret', 'gt', 'lt', 'pin', 'preserve', 'tilde']

0 commit comments

Comments
 (0)