Skip to content

Commit a70bc02

Browse files
committed
Enable banner by default over stderr
1 parent 0d373cb commit a70bc02

File tree

3 files changed

+230
-272
lines changed

3 files changed

+230
-272
lines changed

src/constants.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ type ENV = Remap<
4848
SOCKET_CLI_NO_API_TOKEN: boolean
4949
SOCKET_CLI_PUBLISHED_BUILD: boolean
5050
SOCKET_CLI_SENTRY_BUILD: boolean
51-
SOCKET_CLI_SHOW_BANNER: boolean
5251
SOCKET_CLI_VERSION_HASH: string
5352
}>
5453
>
@@ -109,7 +108,6 @@ type Constants = Remap<
109108
readonly SOCKET_CLI_SENTRY_NPM_BIN_NAME: 'socket-npm-with-sentry'
110109
readonly SOCKET_CLI_SENTRY_NPX_BIN_NAME: 'socket-npx-with-sentry'
111110
readonly SOCKET_CLI_SENTRY_PACKAGE_NAME: '@socketsecurity/cli-with-sentry'
112-
readonly SOCKET_CLI_SHOW_BANNER: 'SOCKET_CLI_SHOW_BANNER'
113111
readonly SOCKET_CLI_VERSION_HASH: 'SOCKET_CLI_VERSION_HASH'
114112
readonly VLT: 'vlt'
115113
readonly WITH_SENTRY: 'with-sentry'
@@ -179,7 +177,6 @@ const SOCKET_CLI_SENTRY_BUILD = 'SOCKET_CLI_SENTRY_BUILD'
179177
const SOCKET_CLI_SENTRY_NPM_BIN_NAME = `${SOCKET_CLI_NPM_BIN_NAME}-${WITH_SENTRY}`
180178
const SOCKET_CLI_SENTRY_NPX_BIN_NAME = `${SOCKET_CLI_NPX_BIN_NAME}-${WITH_SENTRY}`
181179
const SOCKET_CLI_SENTRY_PACKAGE_NAME = `${SOCKET_CLI_LEGACY_PACKAGE_NAME}-${WITH_SENTRY}`
182-
const SOCKET_CLI_SHOW_BANNER = 'SOCKET_CLI_SHOW_BANNER'
183180
const SOCKET_CLI_VERSION_HASH = 'SOCKET_CLI_VERSION_HASH'
184181
const VLT = 'vlt'
185182
const YARN = 'yarn'
@@ -218,8 +215,6 @@ const LAZY_ENV = () => {
218215
// Inlined flag set to determine if this is the Sentry build.
219216
// The '@rollup/plugin-replace' will replace "process.env[SOCKET_CLI_SENTRY_BUILD]".
220217
[SOCKET_CLI_SENTRY_BUILD]: process.env[SOCKET_CLI_SENTRY_BUILD],
221-
// Flag set to toggle the informative ASCII art banner.
222-
[SOCKET_CLI_SHOW_BANNER]: envAsBoolean(env[SOCKET_CLI_SHOW_BANNER]),
223218
// Inlined flag set to determine the version hash of the build.
224219
// The '@rollup/plugin-replace' will replace "process.env[SOCKET_CLI_VERSION_HASH]".
225220
[SOCKET_CLI_VERSION_HASH]: process.env[SOCKET_CLI_VERSION_HASH]
@@ -331,7 +326,6 @@ const constants = <Constants>createConstantsObject(
331326
SOCKET_CLI_SENTRY_NPM_BIN_NAME,
332327
SOCKET_CLI_SENTRY_NPX_BIN_NAME,
333328
SOCKET_CLI_SENTRY_PACKAGE_NAME,
334-
SOCKET_CLI_SHOW_BANNER,
335329
SOCKET_CLI_VERSION_HASH,
336330
VLT,
337331
WITH_SENTRY,

src/utils/meow-with-subcommands.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { MeowFlags, commonFlags } from '../flags'
1212

1313
import type { Options } from 'meow'
1414

15-
const { DRY_RUN_LABEL, REDACTED, SOCKET_CLI_SHOW_BANNER } = constants
15+
const { DRY_RUN_LABEL, REDACTED } = constants
1616

1717
interface CliAlias {
1818
description: string
@@ -93,11 +93,8 @@ export async function meowWithSubcommands(
9393
}
9494
// ...else we provide basic instructions and help.
9595

96-
// Temp disable until we clear the --json and --markdown usage
97-
// Lazily access constants.ENV[SOCKET_CLI_SHOW_BANNER].
98-
if (constants.ENV[SOCKET_CLI_SHOW_BANNER]) {
99-
logger.log(getAsciiHeader(name))
100-
}
96+
emitBanner(name)
97+
10198
const cli = meow(
10299
`
103100
Usage
@@ -167,11 +164,8 @@ export function meowOrExit({
167164
}) {
168165
const command = `${parentName} ${config.commandName}`
169166
lastSeenCommand = command
170-
// Temp disable until we clear the --json and --markdown usage.
171-
// Lazily access constants.ENV[SOCKET_CLI_SHOW_BANNER].
172-
if (constants.ENV[SOCKET_CLI_SHOW_BANNER]) {
173-
logger.log(getAsciiHeader(command))
174-
}
167+
168+
emitBanner(command)
175169

176170
// This exits if .printHelp() is called either by meow itself or by us.
177171
const cli = meow({
@@ -189,6 +183,18 @@ export function meowOrExit({
189183
return cli
190184
}
191185

186+
export function emitBanner(name: string) {
187+
// Print a banner at the top of each command.
188+
// This helps with brand recognition and marketing.
189+
// It also helps with debugging since it contains version and command details.
190+
// Note: print over stderr to preserve stdout for flags like --json and
191+
// --markdown. If we don't do this, you can't use --json in particular
192+
// and pipe the result to other tools. By emiting the banner over stderr
193+
// you can do something like `socket scan view xyz | jq | process`.
194+
// The spinner also emits over stderr for example.
195+
logger.error(getAsciiHeader(name))
196+
}
197+
192198
function getAsciiHeader(command: string) {
193199
// Note: In tests we return <redacted> because otherwise snapshots will fail.
194200
// The '@rollup/plugin-replace' will replace "process.env['SOCKET_CLI_VERSION_HASH']".
@@ -208,7 +214,7 @@ function getAsciiHeader(command: string) {
208214
? REDACTED
209215
: process
210216
.cwd()
211-
.replace(new RegExp(`^${escapeRegExp(constants.homePath)}`, 'i'), '~/')
217+
.replace(new RegExp(`^${escapeRegExp(constants.homePath)}`, 'i'), '~')
212218
const body = `
213219
_____ _ _ /---------------
214220
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver ${cliVersion}

0 commit comments

Comments
 (0)