From c55a5eb7e0b0f21f3044d045adb2a94f1231ec95 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Mon, 24 Feb 2025 15:13:34 +0100 Subject: [PATCH 1/2] Apply meowOrExit logic to remaining commands --- src/commands/manifest/cmd-manifest-auto.ts | 43 ++++++++------------ src/commands/manifest/cmd-manifest-gradle.ts | 15 +++---- src/commands/manifest/cmd-manifest-kotlin.ts | 15 +++---- src/commands/manifest/cmd-manifest-scala.ts | 16 +++----- src/commands/oops/cmd-oops.ts | 10 ++--- src/commands/scan/cmd-scan-delete.ts | 8 ++-- src/commands/scan/cmd-scan-list.ts | 8 ++-- src/commands/scan/cmd-scan-metadata.ts | 8 ++-- src/commands/scan/cmd-scan-stream.ts | 8 ++-- 9 files changed, 57 insertions(+), 74 deletions(-) diff --git a/src/commands/manifest/cmd-manifest-auto.ts b/src/commands/manifest/cmd-manifest-auto.ts index 967ca1260..75451e454 100644 --- a/src/commands/manifest/cmd-manifest-auto.ts +++ b/src/commands/manifest/cmd-manifest-auto.ts @@ -1,11 +1,10 @@ import fs from 'node:fs' import path from 'node:path' -import meow from 'meow' - import { cmdManifestGradle } from './cmd-manifest-gradle.ts' import { cmdManifestScala } from './cmd-manifest-scala.ts' import { commonFlags } from '../../flags.ts' +import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting.ts' import type { CliCommandConfig } from '../../utils/meow-with-subcommands' @@ -51,12 +50,11 @@ async function run( importMeta: ImportMeta, { parentName }: { parentName: string } ): Promise { - const cli = meow(config.help(parentName, config), { + const cli = meowOrExit({ argv, - description: config.description, + config, importMeta, - flags: config.flags, - allowUnknownFlags: false + parentName }) const verbose = cli.flags['verbose'] ?? false @@ -93,24 +91,19 @@ async function run( } // Show new help screen and exit - meow( + console.log( ` - $ ${parentName} ${config.commandName} - - Unfortunately this script did not discover a supported language in the - current folder. - - - Make sure this script would work with your target build - - Make sure to run it from the correct folder - - Make sure the necessary build tools are available (\`PATH\`) - - If that doesn't work, see \`${parentName} --help\` for config details for - your target language. - `, - { - argv: [], - description: config.description, - importMeta - } - ).showHelp() +$ ${parentName} ${config.commandName} + +Unfortunately this script did not discover a supported language in the +current folder. + +- Make sure this script would work with your target build +- Make sure to run it from the correct folder +- Make sure the necessary build tools are available (\`PATH\`) + +If that doesn't work, see \`${parentName} --help\` for config details for +your target language. + `.trim() + ) } diff --git a/src/commands/manifest/cmd-manifest-gradle.ts b/src/commands/manifest/cmd-manifest-gradle.ts index be5e55051..ec2648614 100644 --- a/src/commands/manifest/cmd-manifest-gradle.ts +++ b/src/commands/manifest/cmd-manifest-gradle.ts @@ -1,11 +1,10 @@ import path from 'node:path' -import meow from 'meow' - import { Spinner } from '@socketsecurity/registry/lib/spinner' import { convertGradleToMaven } from './convert_gradle_to_maven.ts' import { commonFlags } from '../../flags.ts' +import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting.ts' import type { CliCommandConfig } from '../../utils/meow-with-subcommands' @@ -96,13 +95,11 @@ async function run( importMeta: ImportMeta, { parentName }: { parentName: string } ): Promise { - // note: meow will exit if it prints the --help screen - const cli = meow(config.help(parentName, config), { - flags: config.flags, - argv: argv.length === 0 ? ['--help'] : argv, - description: config.description, - allowUnknownFlags: false, - importMeta + const cli = meowOrExit({ + argv, + config, + importMeta, + parentName }) const verbose = Boolean(cli.flags['verbose']) diff --git a/src/commands/manifest/cmd-manifest-kotlin.ts b/src/commands/manifest/cmd-manifest-kotlin.ts index a2ce63848..f51e308fc 100644 --- a/src/commands/manifest/cmd-manifest-kotlin.ts +++ b/src/commands/manifest/cmd-manifest-kotlin.ts @@ -1,11 +1,10 @@ import path from 'node:path' -import meow from 'meow' - import { Spinner } from '@socketsecurity/registry/lib/spinner' import { convertGradleToMaven } from './convert_gradle_to_maven.ts' import { commonFlags } from '../../flags.ts' +import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting.ts' import type { CliCommandConfig } from '../../utils/meow-with-subcommands' @@ -101,13 +100,11 @@ async function run( importMeta: ImportMeta, { parentName }: { parentName: string } ): Promise { - // note: meow will exit if it prints the --help screen - const cli = meow(config.help(parentName, config), { - flags: config.flags, - argv: argv.length === 0 ? ['--help'] : argv, - description: config.description, - allowUnknownFlags: false, - importMeta + const cli = meowOrExit({ + argv, + config, + importMeta, + parentName }) const verbose = Boolean(cli.flags['verbose']) diff --git a/src/commands/manifest/cmd-manifest-scala.ts b/src/commands/manifest/cmd-manifest-scala.ts index 3aca30a2f..bb2db5456 100644 --- a/src/commands/manifest/cmd-manifest-scala.ts +++ b/src/commands/manifest/cmd-manifest-scala.ts @@ -1,9 +1,8 @@ -import meow from 'meow' - import { Spinner } from '@socketsecurity/registry/lib/spinner' import { convertSbtToMaven } from './convert_sbt_to_maven.ts' import { commonFlags } from '../../flags.ts' +import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting.ts' import type { CliCommandConfig } from '../../utils/meow-with-subcommands' @@ -91,14 +90,11 @@ async function run( importMeta: ImportMeta, { parentName }: { parentName: string } ): Promise { - // console.log('scala', argv, parentName) - // note: meow will exit if it prints the --help screen - const cli = meow(config.help(parentName, config), { - flags: config.flags, - argv: argv.length === 0 ? ['--help'] : argv, - description: config.description, - allowUnknownFlags: false, - importMeta + const cli = meowOrExit({ + argv, + config, + importMeta, + parentName }) const verbose = Boolean(cli.flags['verbose']) diff --git a/src/commands/oops/cmd-oops.ts b/src/commands/oops/cmd-oops.ts index 51a991f56..5a3c9487a 100644 --- a/src/commands/oops/cmd-oops.ts +++ b/src/commands/oops/cmd-oops.ts @@ -1,6 +1,6 @@ -import meowOrExit from 'meow' +import { meowOrExit } from '../../utils/meow-with-subcommands.ts' -import { CliCommandConfig } from '../../utils/meow-with-subcommands.ts' +import type { CliCommandConfig } from '../../utils/meow-with-subcommands.ts' const config: CliCommandConfig = { commandName: 'oops', @@ -26,11 +26,11 @@ async function run( importMeta: ImportMeta, { parentName }: { parentName: string } ): Promise { - meowOrExit(config.help(parentName, config), { + meowOrExit({ argv, - description: config.description, + config, importMeta, - flags: config.flags + parentName }) throw new Error('This error was intentionally left blank') diff --git a/src/commands/scan/cmd-scan-delete.ts b/src/commands/scan/cmd-scan-delete.ts index e9a9f65b3..4ea2cf385 100644 --- a/src/commands/scan/cmd-scan-delete.ts +++ b/src/commands/scan/cmd-scan-delete.ts @@ -1,9 +1,9 @@ -import meow from 'meow' import colors from 'yoctocolors-cjs' import { deleteOrgFullScan } from './delete-full-scan.ts' import { commonFlags, outputFlags } from '../../flags' import { AuthError } from '../../utils/errors' +import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting' import { getDefaultToken } from '../../utils/sdk' @@ -40,11 +40,11 @@ async function run( importMeta: ImportMeta, { parentName }: { parentName: string } ): Promise { - const cli = meow(config.help(parentName, config), { + const cli = meowOrExit({ argv, - description: config.description, + config, importMeta, - flags: config.flags + parentName }) const [orgSlug = '', fullScanId = ''] = cli.input diff --git a/src/commands/scan/cmd-scan-list.ts b/src/commands/scan/cmd-scan-list.ts index 5bd713e9a..5dbc95c1e 100644 --- a/src/commands/scan/cmd-scan-list.ts +++ b/src/commands/scan/cmd-scan-list.ts @@ -1,9 +1,9 @@ -import meow from 'meow' import colors from 'yoctocolors-cjs' import { listFullScans } from './list-full-scans.ts' import { commonFlags, outputFlags } from '../../flags' import { AuthError } from '../../utils/errors' +import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting' import { getDefaultToken } from '../../utils/sdk' @@ -80,11 +80,11 @@ async function run( importMeta: ImportMeta, { parentName }: { parentName: string } ) { - const cli = meow(config.help(parentName, config), { + const cli = meowOrExit({ argv, - description: config.description, + config, importMeta, - flags: config.flags + parentName }) const orgSlug = cli.input[0] diff --git a/src/commands/scan/cmd-scan-metadata.ts b/src/commands/scan/cmd-scan-metadata.ts index aaacd87f6..c00d52aae 100644 --- a/src/commands/scan/cmd-scan-metadata.ts +++ b/src/commands/scan/cmd-scan-metadata.ts @@ -1,9 +1,9 @@ -import meow from 'meow' import colors from 'yoctocolors-cjs' import { getOrgScanMetadata } from './get-full-scan-metadata.ts' import { commonFlags, outputFlags } from '../../flags' import { AuthError } from '../../utils/errors' +import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting' import { getDefaultToken } from '../../utils/sdk' @@ -43,11 +43,11 @@ async function run( importMeta: ImportMeta, { parentName }: { parentName: string } ): Promise { - const cli = meow(config.help(parentName, config), { + const cli = meowOrExit({ argv, - description: config.description, + config, importMeta, - flags: config.flags + parentName }) const [orgSlug = '', fullScanId = ''] = cli.input diff --git a/src/commands/scan/cmd-scan-stream.ts b/src/commands/scan/cmd-scan-stream.ts index 09cd127b0..29668d3ec 100644 --- a/src/commands/scan/cmd-scan-stream.ts +++ b/src/commands/scan/cmd-scan-stream.ts @@ -1,9 +1,9 @@ -import meow from 'meow' import colors from 'yoctocolors-cjs' import { getFullScan } from './get-full-scan.ts' import { commonFlags, outputFlags } from '../../flags' import { AuthError } from '../../utils/errors' +import { meowOrExit } from '../../utils/meow-with-subcommands' import { getFlagListOutput } from '../../utils/output-formatting' import { getDefaultToken } from '../../utils/sdk' @@ -45,11 +45,11 @@ async function run( importMeta: ImportMeta, { parentName }: { parentName: string } ): Promise { - const cli = meow(config.help(parentName, config), { + const cli = meowOrExit({ argv, - description: config.description, + config, importMeta, - flags: config.flags + parentName }) const [orgSlug = '', fullScanId = '', file = '-'] = cli.input From 071bddb75f95a2948de43ed928bc191519c8ba32 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Mon, 24 Feb 2025 15:24:21 +0100 Subject: [PATCH 2/2] Consistent help callback --- src/commands/action/cmd-action.ts | 6 +++--- src/commands/analytics/cmd-analytics.ts | 12 ++++++------ src/commands/oops/cmd-oops.ts | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/commands/action/cmd-action.ts b/src/commands/action/cmd-action.ts index b8e7db161..fb14514df 100644 --- a/src/commands/action/cmd-action.ts +++ b/src/commands/action/cmd-action.ts @@ -28,12 +28,12 @@ const config: CliCommandConfig = { description: 'After marker' } }, - help: (parentName, { commandName, flags }) => ` + help: (command, config) => ` Usage - $ ${parentName} ${commandName} [options] + $ ${command} [options] Options - ${getFlagListOutput(flags, 6)} + ${getFlagListOutput(config.flags, 6)} ` } diff --git a/src/commands/analytics/cmd-analytics.ts b/src/commands/analytics/cmd-analytics.ts index b993bfb31..3e7dfac0c 100644 --- a/src/commands/analytics/cmd-analytics.ts +++ b/src/commands/analytics/cmd-analytics.ts @@ -42,20 +42,20 @@ const config: CliCommandConfig = { description: 'Path to a local file to save the output' } }, - help: (parentName, { commandName, flags }) => ` + help: (command, config) => ` Usage - $ ${parentName} ${commandName} --scope= --time=