Skip to content

Commit 5e9e01d

Browse files
mtorpjdalton
andauthored
fix: make --version exit with code 0 instead of 2 (#1125)
* fix: make --version exit with code 0 instead of 2 The --version flag was defined and parsed but never explicitly handled at the root command level, causing it to fall through to showHelp with exit code 2. This broke automation (e.g. Ansible) that checks the exit code of `socket --version`. * fix: add patch command to public commands set The patch command was registered and unhidden but missing from the hardcoded public commands validation set, causing a spurious "Received an unknown command: patch" error on every CLI invocation. Also removes the workaround in test utils that stripped this error. --------- Co-authored-by: John-David Dalton <jdalton@users.noreply.github.com>
1 parent 004c293 commit 5e9e01d

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "socket",
3-
"version": "1.1.74",
3+
"version": "1.1.75",
44
"description": "CLI for Socket.dev",
55
"homepage": "https://github.com/SocketDev/socket-cli",
66
"license": "MIT AND OFL-1.1",

src/commands/cli.test.mts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import constants, {
66
FLAG_CONFIG,
77
FLAG_DRY_RUN,
88
FLAG_HELP,
9+
FLAG_VERSION,
910
} from '../constants.mts'
1011

1112
describe('socket root command', async () => {
@@ -48,6 +49,7 @@ describe('socket root command', async () => {
4849
manifest Generate a dependency manifest for certain ecosystems
4950
npm Wraps npm with Socket security scanning
5051
npx Wraps npx with Socket security scanning
52+
patch Apply, manage, and rollback Socket security patches for vulnerable dependencies
5153
raw-npm Run npm without the Socket wrapper
5254
raw-npx Run npx without the Socket wrapper
5355
@@ -87,6 +89,17 @@ describe('socket root command', async () => {
8789
},
8890
)
8991

92+
cmdit(
93+
[FLAG_VERSION, FLAG_CONFIG, '{}'],
94+
`should support ${FLAG_VERSION}`,
95+
async cmd => {
96+
const { code, stdout } = await spawnSocketCli(binCliPath, cmd)
97+
// Version output should be a semver string.
98+
expect(stdout).toMatch(/^\d+\.\d+\.\d+/)
99+
expect(code, 'version should exit with code 0').toBe(0)
100+
},
101+
)
102+
90103
cmdit(
91104
['mootools', FLAG_DRY_RUN, FLAG_CONFIG, '{"apiToken":"fakeToken"}'],
92105
'should require args with just dry-run',

src/utils/meow-with-subcommands.mts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ export async function meowWithSubcommands(
553553
'optimize',
554554
'organization',
555555
'package',
556-
//'patch',
556+
'patch',
557557
// PNPM,
558558
'raw-npm',
559559
'raw-npx',
@@ -612,6 +612,7 @@ export async function meowWithSubcommands(
612612
` manifest ${description(subcommands['manifest'])}`,
613613
` npm ${description(subcommands[NPM])}`,
614614
` npx ${description(subcommands[NPX])}`,
615+
` patch ${description(subcommands['patch'])}`,
615616
` raw-npm ${description(subcommands['raw-npm'])}`,
616617
` raw-npx ${description(subcommands['raw-npx'])}`,
617618
'',
@@ -743,9 +744,21 @@ export async function meowWithSubcommands(
743744
help: lines.map(l => indentString(l, HELP_INDENT)).join('\n'),
744745
})
745746

746-
const { dryRun, help: helpFlag } = cli2.flags as {
747+
const {
748+
dryRun,
749+
help: helpFlag,
750+
version: versionFlag,
751+
} = cli2.flags as {
747752
dryRun: boolean
748753
help: boolean
754+
version: boolean
755+
}
756+
757+
// Handle --version: print version and exit successfully.
758+
if (versionFlag) {
759+
logger.log(constants.ENV.INLINED_SOCKET_CLI_VERSION)
760+
process.exitCode = 0
761+
return
749762
}
750763

751764
// ...else we provide basic instructions and help.

test/utils.mts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ function normalizeBanner(str: string): string {
7878
)
7979
// Replace cwd path with "<redacted>".
8080
.replace(/cwd: [^\n"]+/g, 'cwd: <redacted>')
81-
// Strip "Received an unknown command: patch" error line that appears
82-
// when socket-patch binary is not available in the test build.
83-
// Also consume any leading whitespace on the next line so indentation
84-
// stays consistent when the error line is absent.
85-
.replace(/[^\n]*Received an unknown command: patch[^\n]*\n\s*/g, '')
8681
)
8782
}
8883

0 commit comments

Comments
 (0)