Skip to content

Commit 8d607ca

Browse files
committed
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`.
1 parent 51d1eb7 commit 8d607ca

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
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: 12 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 () => {
@@ -87,6 +88,17 @@ describe('socket root command', async () => {
8788
},
8889
)
8990

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

src/utils/meow-with-subcommands.mts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,21 @@ export async function meowWithSubcommands(
743743
help: lines.map(l => indentString(l, HELP_INDENT)).join('\n'),
744744
})
745745

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

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

0 commit comments

Comments
 (0)