From b9fd9a6c9b0bb1d472d58c640adfd82015a666d9 Mon Sep 17 00:00:00 2001 From: jdalton Date: Mon, 7 Apr 2025 20:58:39 -0600 Subject: [PATCH] Create consistent property type and avoid polymorphic values --- src/commands/analytics/cmd-analytics.ts | 6 +++--- src/commands/audit-log/cmd-audit-log.ts | 4 ++-- src/commands/config/cmd-config-set.ts | 4 ++-- src/commands/config/cmd-config-unset.ts | 2 +- src/commands/dependencies/cmd-dependencies.ts | 2 +- src/commands/diff-scan/cmd-diff-scan-get.ts | 2 +- src/commands/info/cmd-info.ts | 2 +- src/commands/manifest/cmd-manifest-gradle.ts | 2 +- src/commands/manifest/cmd-manifest-kotlin.ts | 2 +- src/commands/manifest/cmd-manifest-scala.ts | 2 +- .../organization/cmd-organization-list.ts | 2 +- .../cmd-organization-policy-license.ts | 4 ++-- .../cmd-organization-policy-security.ts | 4 ++-- .../organization/cmd-organization-quota.ts | 2 +- src/commands/package/cmd-package-score.ts | 2 +- src/commands/repos/cmd-repos-create.ts | 6 +++--- src/commands/repos/cmd-repos-del.ts | 6 +++--- src/commands/repos/cmd-repos-list.ts | 4 ++-- src/commands/repos/cmd-repos-update.ts | 6 +++--- src/commands/repos/cmd-repos-view.ts | 6 +++--- src/commands/scan/cmd-scan-create.ts | 6 +++--- src/commands/scan/cmd-scan-del.ts | 6 +++--- src/commands/scan/cmd-scan-list.ts | 4 ++-- src/commands/scan/cmd-scan-metadata.ts | 6 +++--- src/commands/scan/cmd-scan-report.ts | 6 +++--- src/commands/scan/cmd-scan-view.ts | 6 +++--- src/commands/threat-feed/cmd-threat-feed.ts | 4 ++-- src/commands/wrapper/cmd-wrapper.ts | 2 +- src/utils/handle-bad-input.ts | 18 +++++++++--------- src/utils/path-resolve.ts | 7 ++++--- 30 files changed, 68 insertions(+), 67 deletions(-) diff --git a/src/commands/analytics/cmd-analytics.ts b/src/commands/analytics/cmd-analytics.ts index 2c22cd726..8de228d7a 100644 --- a/src/commands/analytics/cmd-analytics.ts +++ b/src/commands/analytics/cmd-analytics.ts @@ -106,14 +106,14 @@ async function run( }, { nook: true, - test: scope === 'org' || repo, + test: scope === 'org' || !!repo, message: 'When scope=repo, repo name should be set through --repo', pass: 'ok', fail: 'missing' }, { nook: true, - test: file === '-' || json || markdown, + test: file === '-' || !!json || !!markdown, message: 'The `--file` flag is only valid when using `--json` or `--markdown`', pass: 'ok', @@ -129,7 +129,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/audit-log/cmd-audit-log.ts b/src/commands/audit-log/cmd-audit-log.ts index 689a130f5..3e68c2d04 100644 --- a/src/commands/audit-log/cmd-audit-log.ts +++ b/src/commands/audit-log/cmd-audit-log.ts @@ -86,14 +86,14 @@ async function run( const wasBadInput = handleBadInput( { - test: orgSlug, + test: !!orgSlug, message: 'Org name should be the first arg', pass: 'ok', fail: 'missing' }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/config/cmd-config-set.ts b/src/commands/config/cmd-config-set.ts index 1bc3924c5..d732341d9 100644 --- a/src/commands/config/cmd-config-set.ts +++ b/src/commands/config/cmd-config-set.ts @@ -68,13 +68,13 @@ async function run( const wasBadInput = handleBadInput( { - test: supportedConfigKeys.has(key as keyof LocalConfig) || key === 'test', + test: key === 'test' || supportedConfigKeys.has(key as keyof LocalConfig), message: 'Config key should be the first arg', pass: 'ok', fail: key ? 'invalid config key' : 'missing' }, { - test: value, // This is a string, empty string is not ok + test: !!value, // This is a string, empty string is not ok message: 'Key value should be the remaining args (use `unset` to unset a value)', pass: 'ok', diff --git a/src/commands/config/cmd-config-unset.ts b/src/commands/config/cmd-config-unset.ts index 689c0c3e0..f5442d496 100644 --- a/src/commands/config/cmd-config-unset.ts +++ b/src/commands/config/cmd-config-unset.ts @@ -62,7 +62,7 @@ async function run( const wasBadInput = handleBadInput( { - test: supportedConfigKeys.has(key as keyof LocalConfig) || key === 'test', + test: key === 'test' || supportedConfigKeys.has(key as keyof LocalConfig), message: 'Config key should be the first arg', pass: 'ok', fail: key ? 'invalid config key' : 'missing' diff --git a/src/commands/dependencies/cmd-dependencies.ts b/src/commands/dependencies/cmd-dependencies.ts index 932d61cb6..f2c070630 100644 --- a/src/commands/dependencies/cmd-dependencies.ts +++ b/src/commands/dependencies/cmd-dependencies.ts @@ -82,7 +82,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/diff-scan/cmd-diff-scan-get.ts b/src/commands/diff-scan/cmd-diff-scan-get.ts index 4d0ed40f8..3700bd3da 100644 --- a/src/commands/diff-scan/cmd-diff-scan-get.ts +++ b/src/commands/diff-scan/cmd-diff-scan-get.ts @@ -127,7 +127,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/info/cmd-info.ts b/src/commands/info/cmd-info.ts index 27d6fe598..6eadc1859 100644 --- a/src/commands/info/cmd-info.ts +++ b/src/commands/info/cmd-info.ts @@ -58,7 +58,7 @@ async function run( const wasBadInput = handleBadInput( { - test: rawPkgName, + test: !!rawPkgName, message: 'Expecting a package name', pass: 'ok', fail: 'missing' diff --git a/src/commands/manifest/cmd-manifest-gradle.ts b/src/commands/manifest/cmd-manifest-gradle.ts index 45a097ce8..367e700d6 100644 --- a/src/commands/manifest/cmd-manifest-gradle.ts +++ b/src/commands/manifest/cmd-manifest-gradle.ts @@ -124,7 +124,7 @@ async function run( const wasBadInput = handleBadInput( { - test: target && target !== '-', + test: !!target && target !== '-', message: 'The DIR arg is required', pass: 'ok', fail: target === '-' ? 'stdin is not supported' : 'missing' diff --git a/src/commands/manifest/cmd-manifest-kotlin.ts b/src/commands/manifest/cmd-manifest-kotlin.ts index 4c431f036..199db7763 100644 --- a/src/commands/manifest/cmd-manifest-kotlin.ts +++ b/src/commands/manifest/cmd-manifest-kotlin.ts @@ -129,7 +129,7 @@ async function run( const wasBadInput = handleBadInput( { - test: target && target !== '-', + test: !!target && target !== '-', message: 'The DIR arg is required', pass: 'ok', fail: target === '-' ? 'stdin is not supported' : 'missing' diff --git a/src/commands/manifest/cmd-manifest-scala.ts b/src/commands/manifest/cmd-manifest-scala.ts index f9d63e75b..cab812681 100644 --- a/src/commands/manifest/cmd-manifest-scala.ts +++ b/src/commands/manifest/cmd-manifest-scala.ts @@ -122,7 +122,7 @@ async function run( const wasBadInput = handleBadInput( { - test: target && target !== '-', + test: !!target && target !== '-', message: 'The DIR arg is required', pass: 'ok', fail: target === '-' ? 'stdin is not supported' : 'missing' diff --git a/src/commands/organization/cmd-organization-list.ts b/src/commands/organization/cmd-organization-list.ts index ad0d3b8bb..020844a24 100644 --- a/src/commands/organization/cmd-organization-list.ts +++ b/src/commands/organization/cmd-organization-list.ts @@ -65,7 +65,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/organization/cmd-organization-policy-license.ts b/src/commands/organization/cmd-organization-policy-license.ts index ddcc21f3c..df8b38237 100644 --- a/src/commands/organization/cmd-organization-policy-license.ts +++ b/src/commands/organization/cmd-organization-policy-license.ts @@ -70,7 +70,7 @@ async function run( const wasBadInput = handleBadInput( { nook: true, - test: orgSlug, + test: !!orgSlug, message: 'Org name as the first argument', pass: 'ok', fail: 'missing' @@ -84,7 +84,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/organization/cmd-organization-policy-security.ts b/src/commands/organization/cmd-organization-policy-security.ts index add73e5ca..4bdf39e7a 100644 --- a/src/commands/organization/cmd-organization-policy-security.ts +++ b/src/commands/organization/cmd-organization-policy-security.ts @@ -70,7 +70,7 @@ async function run( const wasBadInput = handleBadInput( { nook: true, - test: orgSlug, + test: !!orgSlug, message: 'Org name as the first argument', pass: 'ok', fail: 'missing' @@ -84,7 +84,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/organization/cmd-organization-quota.ts b/src/commands/organization/cmd-organization-quota.ts index 0f92e83a4..bbf9df231 100644 --- a/src/commands/organization/cmd-organization-quota.ts +++ b/src/commands/organization/cmd-organization-quota.ts @@ -61,7 +61,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/package/cmd-package-score.ts b/src/commands/package/cmd-package-score.ts index 15e9533fa..64fcb5a87 100644 --- a/src/commands/package/cmd-package-score.ts +++ b/src/commands/package/cmd-package-score.ts @@ -102,7 +102,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/repos/cmd-repos-create.ts b/src/commands/repos/cmd-repos-create.ts index bc6827b0e..636ff8eae 100644 --- a/src/commands/repos/cmd-repos-create.ts +++ b/src/commands/repos/cmd-repos-create.ts @@ -92,20 +92,20 @@ async function run( const wasBadInput = handleBadInput( { nook: true, - test: orgSlug, + test: !!orgSlug, message: 'Org name as the first argument', pass: 'ok', fail: 'missing' }, { - test: repoName, + test: !!repoName, message: 'Repository name using --repoNam', pass: 'ok', fail: 'missing' }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/repos/cmd-repos-del.ts b/src/commands/repos/cmd-repos-del.ts index 2c9f30ff6..b49309db8 100644 --- a/src/commands/repos/cmd-repos-del.ts +++ b/src/commands/repos/cmd-repos-del.ts @@ -62,20 +62,20 @@ async function run( const wasBadInput = handleBadInput( { nook: true, - test: orgSlug, + test: !!orgSlug, message: 'Org name as the first argument', pass: 'ok', fail: 'missing' }, { - test: repoName, + test: !!repoName, message: 'Repository name argument', pass: 'ok', fail: 'missing' }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/repos/cmd-repos-list.ts b/src/commands/repos/cmd-repos-list.ts index 070fccaf9..a27627221 100644 --- a/src/commands/repos/cmd-repos-list.ts +++ b/src/commands/repos/cmd-repos-list.ts @@ -86,7 +86,7 @@ async function run( const wasBadInput = handleBadInput( { nook: true, - test: orgSlug, + test: !!orgSlug, message: 'Org name as the first argument', pass: 'ok', fail: 'missing' @@ -101,7 +101,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/repos/cmd-repos-update.ts b/src/commands/repos/cmd-repos-update.ts index d63927d65..d48c9bcf1 100644 --- a/src/commands/repos/cmd-repos-update.ts +++ b/src/commands/repos/cmd-repos-update.ts @@ -92,20 +92,20 @@ async function run( const wasBadInput = handleBadInput( { nook: true, - test: orgSlug, + test: !!orgSlug, message: 'Org name as the first argument', pass: 'ok', fail: 'missing' }, { - test: repoName, + test: !!repoName, message: 'Repository name using --repoName', pass: 'ok', fail: 'missing' }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/repos/cmd-repos-view.ts b/src/commands/repos/cmd-repos-view.ts index db6ad9792..2f7dd8c1a 100644 --- a/src/commands/repos/cmd-repos-view.ts +++ b/src/commands/repos/cmd-repos-view.ts @@ -69,13 +69,13 @@ async function run( const wasBadInput = handleBadInput( { nook: true, - test: orgSlug, + test: !!orgSlug, message: 'Org name as the first argument', pass: 'ok', fail: 'missing' }, { - test: repoName, + test: !!repoName, message: 'Repository name using --repoName', pass: 'ok', fail: 'missing' @@ -90,7 +90,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/scan/cmd-scan-create.ts b/src/commands/scan/cmd-scan-create.ts index 55d7d8809..f10c16f22 100644 --- a/src/commands/scan/cmd-scan-create.ts +++ b/src/commands/scan/cmd-scan-create.ts @@ -237,7 +237,7 @@ async function run( const wasBadInput = handleBadInput( { nook: !!defaultOrgSlug, - test: orgSlug && orgSlug !== '.', + test: !!orgSlug && orgSlug !== '.', message: 'Org name as the first argument', pass: 'ok', fail: @@ -246,7 +246,7 @@ async function run( : 'missing' }, { - test: targets.length, + test: !!targets.length, message: 'At least one TARGET (e.g. `.` or `./package.json`)', pass: 'ok', fail: 'missing (or perhaps you forgot the org slug?)' @@ -260,7 +260,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'This command requires an API token for access', pass: 'ok', fail: 'missing (try `socket login`)' diff --git a/src/commands/scan/cmd-scan-del.ts b/src/commands/scan/cmd-scan-del.ts index 085e18177..0627af2cf 100644 --- a/src/commands/scan/cmd-scan-del.ts +++ b/src/commands/scan/cmd-scan-del.ts @@ -63,7 +63,7 @@ async function run( const wasBadInput = handleBadInput( { nook: !!defaultOrgSlug, - test: orgSlug && orgSlug !== '.', + test: !!orgSlug && orgSlug !== '.', message: 'Org name as the first argument', pass: 'ok', fail: @@ -72,14 +72,14 @@ async function run( : 'missing' }, { - test: scanId, + test: !!scanId, message: 'Scan ID to delete', pass: 'ok', fail: 'missing' }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/scan/cmd-scan-list.ts b/src/commands/scan/cmd-scan-list.ts index 46196c78d..72fa62ca6 100644 --- a/src/commands/scan/cmd-scan-list.ts +++ b/src/commands/scan/cmd-scan-list.ts @@ -103,7 +103,7 @@ async function run( const wasBadInput = handleBadInput( { nook: !!defaultOrgSlug, - test: orgSlug && orgSlug !== '.', + test: !!orgSlug && orgSlug !== '.', message: 'Org name as the first argument', pass: 'ok', fail: @@ -120,7 +120,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/scan/cmd-scan-metadata.ts b/src/commands/scan/cmd-scan-metadata.ts index 6a9d09759..f77172bee 100644 --- a/src/commands/scan/cmd-scan-metadata.ts +++ b/src/commands/scan/cmd-scan-metadata.ts @@ -67,7 +67,7 @@ async function run( const wasBadInput = handleBadInput( { nook: !!defaultOrgSlug, - test: orgSlug && orgSlug !== '.', + test: !!orgSlug && orgSlug !== '.', message: 'Org name as the first argument', pass: 'ok', fail: @@ -76,7 +76,7 @@ async function run( : 'missing' }, { - test: scanId, + test: !!scanId, message: 'Scan ID to inspect as argument', pass: 'ok', fail: 'missing' @@ -90,7 +90,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/scan/cmd-scan-report.ts b/src/commands/scan/cmd-scan-report.ts index a2f200127..6a2bf0760 100644 --- a/src/commands/scan/cmd-scan-report.ts +++ b/src/commands/scan/cmd-scan-report.ts @@ -106,7 +106,7 @@ async function run( const wasBadInput = handleBadInput( { nook: !!defaultOrgSlug, - test: orgSlug && orgSlug !== '.', + test: !!orgSlug && orgSlug !== '.', message: 'Org name as the first argument', pass: 'ok', fail: @@ -115,7 +115,7 @@ async function run( : 'missing' }, { - test: scanId, + test: !!scanId, message: 'Scan ID to fetch', pass: 'ok', fail: 'missing' @@ -129,7 +129,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/scan/cmd-scan-view.ts b/src/commands/scan/cmd-scan-view.ts index 4123a6225..b543f0874 100644 --- a/src/commands/scan/cmd-scan-view.ts +++ b/src/commands/scan/cmd-scan-view.ts @@ -71,7 +71,7 @@ async function run( const wasBadInput = handleBadInput( { nook: !!defaultOrgSlug, - test: orgSlug && orgSlug !== '.', + test: !!orgSlug && orgSlug !== '.', message: 'Org name as the first argument', pass: 'ok', fail: @@ -80,7 +80,7 @@ async function run( : 'missing' }, { - test: scanId, + test: !!scanId, message: 'Scan ID to delete', pass: 'ok', fail: 'missing' @@ -95,7 +95,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/threat-feed/cmd-threat-feed.ts b/src/commands/threat-feed/cmd-threat-feed.ts index 960825d19..44ffcfc26 100644 --- a/src/commands/threat-feed/cmd-threat-feed.ts +++ b/src/commands/threat-feed/cmd-threat-feed.ts @@ -121,7 +121,7 @@ async function run( const wasBadInput = handleBadInput( { nook: true, - test: orgSlug, + test: !!orgSlug, message: 'Org name as the first argument', pass: 'ok', fail: 'missing' @@ -135,7 +135,7 @@ async function run( }, { nook: true, - test: apiToken, + test: !!apiToken, message: 'You need to be logged in to use this command. See `socket login`.', pass: 'ok', diff --git a/src/commands/wrapper/cmd-wrapper.ts b/src/commands/wrapper/cmd-wrapper.ts index 7483965d8..b158c2ade 100644 --- a/src/commands/wrapper/cmd-wrapper.ts +++ b/src/commands/wrapper/cmd-wrapper.ts @@ -74,7 +74,7 @@ async function run( const wasBadInput = handleBadInput( { - test: enable || disable, + test: !!(enable || disable), message: 'Must use --enabled or --disable', pass: 'ok', fail: 'missing' diff --git a/src/utils/handle-bad-input.ts b/src/utils/handle-bad-input.ts index 19f9d9627..f1fa3a28b 100644 --- a/src/utils/handle-bad-input.ts +++ b/src/utils/handle-bad-input.ts @@ -5,15 +5,15 @@ import { logger } from '@socketsecurity/registry/lib/logger' import { failMsgWithBadge } from './fail-msg-with-badge' export function handleBadInput( - ...arr: Array<{ + ...checks: Array<{ + fail: string message: string - nook?: unknown // Only display error when test is not OK - test: unknown // Truthy checked through !! pass: string - fail: string + test: boolean + nook?: boolean | undefined }> ) { - if (arr.every(data => !!data.test)) { + if (checks.every(d => d.test)) { return false } @@ -24,17 +24,17 @@ export function handleBadInput( ), '' ] - for (const data of arr) { + for (const d of checks) { // If nook, then ignore when test is ok - if (data.nook && data.test) { + if (d.nook && d.test) { continue } - const lines = data.message.split('\n') + const lines = d.message.split('\n') // If the message has newlines then format the first line with the input // expectation and teh rest indented below it msg.push( - ` - ${lines[0]} (${data.test ? colors.green(data.pass) : colors.red(data.fail)})` + ` - ${lines[0]} (${d.test ? colors.green(d.pass) : colors.red(d.fail)})` ) if (lines.length > 1) { msg.push(...lines.slice(1).map(str => ` ${str}`)) diff --git a/src/utils/path-resolve.ts b/src/utils/path-resolve.ts index 1a756515d..40193fbf8 100644 --- a/src/utils/path-resolve.ts +++ b/src/utils/path-resolve.ts @@ -9,6 +9,7 @@ import which from 'which' import { debugLog, isDebug } from '@socketsecurity/registry/lib/debug' import { resolveBinPath } from '@socketsecurity/registry/lib/npm' +import { pluralize } from '@socketsecurity/registry/lib/words' import { directoryPatterns } from './ignore-by-default' import constants from '../constants' @@ -262,11 +263,11 @@ export async function getPackageFilesForScan( // Lazily access constants.spinner. const { spinner } = constants - const pats = pathsToPatterns(inputPaths) + const patterns = pathsToPatterns(inputPaths) spinner.start('Searching for local files to include in scan...') - const entries = await globWithGitIgnore(pats, { + const entries = await globWithGitIgnore(patterns, { cwd, socketConfig: config }) @@ -290,7 +291,7 @@ export async function getPackageFilesForScan( ) spinner.successAndStop( - `Found ${packageFiles.length} local file${packageFiles.length === 1 ? '' : 's'}` + `Found ${packageFiles.length} local ${pluralize('file', packageFiles.length)}` ) debugLog('Absolute paths:\n', packageFiles)