Skip to content

Commit e2bf576

Browse files
committed
Add more constants
1 parent 439dfab commit e2bf576

File tree

10 files changed

+170
-36
lines changed

10 files changed

+170
-36
lines changed

src/commands/fix/cmd-fix.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { arrayUnique, joinOr } from '@socketsecurity/registry/lib/arrays'
66
import { logger } from '@socketsecurity/registry/lib/logger'
77

88
import { handleFix } from './handle-fix.mts'
9-
import constants from '../../constants.mts'
9+
import constants, { ERROR_UNABLE_RESOLVE_ORG } from '../../constants.mts'
1010
import { commonFlags, outputFlags } from '../../flags.mts'
1111
import { checkCommandInput } from '../../utils/check-input.mts'
1212
import { cmdFlagValueToArray } from '../../utils/cmd.mts'
@@ -257,7 +257,7 @@ async function run(
257257
if (!orgSlugCResult.ok) {
258258
process.exitCode = orgSlugCResult.code ?? 1
259259
logger.fail(
260-
'Unable to resolve a Socket account organization.\nEnsure a Socket API token is specified for the organization using the SOCKET_CLI_API_TOKEN environment variable.',
260+
`${ERROR_UNABLE_RESOLVE_ORG}.\nEnsure a Socket API token is specified for the organization using the SOCKET_CLI_API_TOKEN environment variable.`,
261261
)
262262
return
263263
}

src/commands/login/apply-login.mts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import {
2+
CONFIG_KEY_API_BASE_URL,
3+
CONFIG_KEY_API_PROXY,
4+
CONFIG_KEY_API_TOKEN,
5+
CONFIG_KEY_ENFORCED_ORGS,
6+
} from '../../constants.mts'
17
import { updateConfigValue } from '../../utils/config.mts'
28

39
export function applyLogin(
@@ -6,8 +12,8 @@ export function applyLogin(
612
apiBaseUrl: string | undefined,
713
apiProxy: string | undefined,
814
) {
9-
updateConfigValue('enforcedOrgs', enforcedOrgs)
10-
updateConfigValue('apiToken', apiToken)
11-
updateConfigValue('apiBaseUrl', apiBaseUrl)
12-
updateConfigValue('apiProxy', apiProxy)
15+
updateConfigValue(CONFIG_KEY_ENFORCED_ORGS, enforcedOrgs)
16+
updateConfigValue(CONFIG_KEY_API_TOKEN, apiToken)
17+
updateConfigValue(CONFIG_KEY_API_BASE_URL, apiBaseUrl)
18+
updateConfigValue(CONFIG_KEY_API_PROXY, apiProxy)
1319
}

src/commands/login/attempt-login.mts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { logger } from '@socketsecurity/registry/lib/logger'
55
import { confirm, password, select } from '@socketsecurity/registry/lib/prompts'
66

77
import { applyLogin } from './apply-login.mts'
8-
import constants from '../../constants.mts'
8+
import constants, {
9+
CONFIG_KEY_API_BASE_URL,
10+
CONFIG_KEY_API_PROXY,
11+
CONFIG_KEY_API_TOKEN,
12+
CONFIG_KEY_DEFAULT_ORG,
13+
} from '../../constants.mts'
914
import {
1015
getConfigValueOrUndef,
1116
isReadOnlyConfig,
@@ -26,8 +31,8 @@ export async function attemptLogin(
2631
apiBaseUrl: string | undefined,
2732
apiProxy: string | undefined,
2833
) {
29-
apiBaseUrl ??= getConfigValueOrUndef('apiBaseUrl') ?? undefined
30-
apiProxy ??= getConfigValueOrUndef('apiProxy') ?? undefined
34+
apiBaseUrl ??= getConfigValueOrUndef(CONFIG_KEY_API_BASE_URL) ?? undefined
35+
apiProxy ??= getConfigValueOrUndef(CONFIG_KEY_API_PROXY) ?? undefined
3136
const apiTokenInput = await password({
3237
message: `Enter your ${terminalLink(
3338
'Socket.dev API token',
@@ -148,9 +153,9 @@ export async function attemptLogin(
148153
}
149154
}
150155

151-
updateConfigValue('defaultOrg', orgSlugs[0])
156+
updateConfigValue(CONFIG_KEY_DEFAULT_ORG, orgSlugs[0])
152157

153-
const previousPersistedToken = getConfigValueOrUndef('apiToken')
158+
const previousPersistedToken = getConfigValueOrUndef(CONFIG_KEY_API_TOKEN)
154159
try {
155160
applyLogin(apiToken, enforcedOrgs, apiBaseUrl, apiProxy)
156161
logger.success(
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import {
2+
CONFIG_KEY_API_BASE_URL,
3+
CONFIG_KEY_API_PROXY,
4+
CONFIG_KEY_API_TOKEN,
5+
CONFIG_KEY_ENFORCED_ORGS,
6+
} from '../../constants.mts'
17
import { updateConfigValue } from '../../utils/config.mts'
28

39
export function applyLogout() {
4-
updateConfigValue('apiToken', null)
5-
updateConfigValue('apiBaseUrl', null)
6-
updateConfigValue('apiProxy', null)
7-
updateConfigValue('enforcedOrgs', null)
10+
updateConfigValue(CONFIG_KEY_API_TOKEN, null)
11+
updateConfigValue(CONFIG_KEY_API_BASE_URL, null)
12+
updateConfigValue(CONFIG_KEY_API_PROXY, null)
13+
updateConfigValue(CONFIG_KEY_ENFORCED_ORGS, null)
814
}

src/commands/npm/cmd-npm.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createRequire } from 'node:module'
22

33
import { logger } from '@socketsecurity/registry/lib/logger'
44

5-
import constants, { NPM } from '../../constants.mts'
5+
import constants, { FLAG_JSON, NPM } from '../../constants.mts'
66
import { commonFlags, outputFlags } from '../../flags.mts'
77
import { filterFlags } from '../../utils/cmd.mts'
88
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
@@ -79,7 +79,7 @@ async function run(
7979

8080
// Filter Socket flags from argv but keep --json for npm.
8181
const argsToForward = filterFlags(argv, { ...commonFlags, ...outputFlags }, [
82-
'--json',
82+
FLAG_JSON,
8383
])
8484
const { spawnPromise } = await shadowBin(NPM, argsToForward, {
8585
stdio: 'inherit',

src/constants.mts

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ const ALERT_TYPE_MEDIUM_CVE = 'mediumCVE'
176176
const ALERT_TYPE_MILD_CVE = 'mildCVE'
177177
const API_V0_URL = 'https://api.socket.dev/v0/'
178178
const BUN = 'bun'
179+
const CONFIG_KEY_API_BASE_URL = 'apiBaseUrl'
180+
const CONFIG_KEY_API_PROXY = 'apiProxy'
181+
const CONFIG_KEY_API_TOKEN = 'apiToken'
182+
const CONFIG_KEY_DEFAULT_ORG = 'defaultOrg'
183+
const CONFIG_KEY_ENFORCED_ORGS = 'enforcedOrgs'
184+
const CONFIG_KEY_ORG = 'org'
185+
const DOT_GIT = '.git'
179186
const DOT_SOCKET = '.socket'
180187
const DOT_SOCKET_DOT_FACTS_JSON = `${DOT_SOCKET}.facts.json`
181188
const DRY_RUN_LABEL = '[DryRun]'
@@ -184,6 +191,25 @@ const DRY_RUN_NOT_SAVING = `${DRY_RUN_LABEL}: Not saving`
184191
const EMPTY_VALUE = '<empty>'
185192
const ENVIRONMENT_YAML = 'environment.yaml'
186193
const ENVIRONMENT_YML = 'environment.yml'
194+
const ERROR_NO_MANIFEST_FILES = 'No manifest files found'
195+
const ERROR_NO_PACKAGE_JSON = 'No package.json found'
196+
const ERROR_NO_REPO_FOUND = 'No repo found'
197+
const ERROR_NO_SOCKET_DIR = 'No .socket directory found'
198+
const ERROR_UNABLE_RESOLVE_ORG = 'Unable to resolve a Socket account organization'
199+
const EXT_YAML = '.yaml'
200+
const EXT_YML = '.yml'
201+
const FLAG_CONFIG = '--config'
202+
const FLAG_DRY_RUN = '--dry-run'
203+
const FLAG_HELP = '--help'
204+
const FLAG_JSON = '--json'
205+
const FLAG_MARKDOWN = '--markdown'
206+
const FLAG_ORG = '--org'
207+
const FLAG_PIN = '--pin'
208+
const FLAG_PROD = '--prod'
209+
const FLAG_QUIET = '--quiet'
210+
const FLAG_SILENT = '--silent'
211+
const FLAG_TEXT = '--text'
212+
const FLAG_VERBOSE = '--verbose'
187213
const FOLD_SETTING_FILE = 'file'
188214
const FOLD_SETTING_NONE = 'none'
189215
const FOLD_SETTING_PKG = 'pkg'
@@ -239,15 +265,41 @@ export type Constants = Remap<
239265
readonly ALERT_TYPE_MILD_CVE: typeof ALERT_TYPE_MILD_CVE
240266
readonly API_V0_URL: typeof API_V0_URL
241267
readonly BUN: typeof BUN
242-
readonly EMPTY_VALUE: typeof EMPTY_VALUE
243-
readonly ENV: ENV
268+
readonly CONFIG_KEY_API_BASE_URL: typeof CONFIG_KEY_API_BASE_URL
269+
readonly CONFIG_KEY_API_PROXY: typeof CONFIG_KEY_API_PROXY
270+
readonly CONFIG_KEY_API_TOKEN: typeof CONFIG_KEY_API_TOKEN
271+
readonly CONFIG_KEY_DEFAULT_ORG: typeof CONFIG_KEY_DEFAULT_ORG
272+
readonly CONFIG_KEY_ENFORCED_ORGS: typeof CONFIG_KEY_ENFORCED_ORGS
273+
readonly CONFIG_KEY_ORG: typeof CONFIG_KEY_ORG
274+
readonly DOT_GIT: typeof DOT_GIT
244275
readonly DOT_SOCKET: typeof DOT_SOCKET
245276
readonly DOT_SOCKET_DOT_FACTS_JSON: typeof DOT_SOCKET_DOT_FACTS_JSON
246277
readonly DRY_RUN_LABEL: typeof DRY_RUN_LABEL
247278
readonly DRY_RUN_BAILING_NOW: typeof DRY_RUN_BAILING_NOW
248279
readonly DRY_RUN_NOT_SAVING: typeof DRY_RUN_NOT_SAVING
280+
readonly EMPTY_VALUE: typeof EMPTY_VALUE
281+
readonly ENV: ENV
249282
readonly ENVIRONMENT_YAML: typeof ENVIRONMENT_YAML
250283
readonly ENVIRONMENT_YML: typeof ENVIRONMENT_YML
284+
readonly ERROR_NO_MANIFEST_FILES: typeof ERROR_NO_MANIFEST_FILES
285+
readonly ERROR_NO_PACKAGE_JSON: typeof ERROR_NO_PACKAGE_JSON
286+
readonly ERROR_NO_REPO_FOUND: typeof ERROR_NO_REPO_FOUND
287+
readonly ERROR_NO_SOCKET_DIR: typeof ERROR_NO_SOCKET_DIR
288+
readonly ERROR_UNABLE_RESOLVE_ORG: typeof ERROR_UNABLE_RESOLVE_ORG
289+
readonly EXT_YAML: typeof EXT_YAML
290+
readonly EXT_YML: typeof EXT_YML
291+
readonly FLAG_CONFIG: typeof FLAG_CONFIG
292+
readonly FLAG_DRY_RUN: typeof FLAG_DRY_RUN
293+
readonly FLAG_HELP: typeof FLAG_HELP
294+
readonly FLAG_JSON: typeof FLAG_JSON
295+
readonly FLAG_MARKDOWN: typeof FLAG_MARKDOWN
296+
readonly FLAG_ORG: typeof FLAG_ORG
297+
readonly FLAG_PIN: typeof FLAG_PIN
298+
readonly FLAG_PROD: typeof FLAG_PROD
299+
readonly FLAG_QUIET: typeof FLAG_QUIET
300+
readonly FLAG_SILENT: typeof FLAG_SILENT
301+
readonly FLAG_TEXT: typeof FLAG_TEXT
302+
readonly FLAG_VERBOSE: typeof FLAG_VERBOSE
251303
readonly FOLD_SETTING_FILE: typeof FOLD_SETTING_FILE
252304
readonly FOLD_SETTING_NONE: typeof FOLD_SETTING_NONE
253305
readonly FOLD_SETTING_PKG: typeof FOLD_SETTING_PKG
@@ -766,15 +818,41 @@ const constants: Constants = createConstantsObject(
766818
ALERT_TYPE_MILD_CVE,
767819
API_V0_URL,
768820
BUN,
821+
CONFIG_KEY_API_BASE_URL,
822+
CONFIG_KEY_API_PROXY,
823+
CONFIG_KEY_API_TOKEN,
824+
CONFIG_KEY_DEFAULT_ORG,
825+
CONFIG_KEY_ENFORCED_ORGS,
826+
CONFIG_KEY_ORG,
827+
DOT_GIT,
769828
DOT_SOCKET,
770829
DOT_SOCKET_DOT_FACTS_JSON,
771830
DRY_RUN_LABEL,
772831
DRY_RUN_BAILING_NOW,
773832
DRY_RUN_NOT_SAVING,
774833
EMPTY_VALUE,
834+
ENV: undefined,
775835
ENVIRONMENT_YAML,
776836
ENVIRONMENT_YML,
777-
ENV: undefined,
837+
ERROR_NO_MANIFEST_FILES,
838+
ERROR_NO_PACKAGE_JSON,
839+
ERROR_NO_REPO_FOUND,
840+
ERROR_NO_SOCKET_DIR,
841+
ERROR_UNABLE_RESOLVE_ORG,
842+
EXT_YAML,
843+
EXT_YML,
844+
FLAG_CONFIG,
845+
FLAG_DRY_RUN,
846+
FLAG_HELP,
847+
FLAG_JSON,
848+
FLAG_MARKDOWN,
849+
FLAG_ORG,
850+
FLAG_PIN,
851+
FLAG_PROD,
852+
FLAG_QUIET,
853+
FLAG_SILENT,
854+
FLAG_TEXT,
855+
FLAG_VERBOSE,
778856
FOLD_SETTING_FILE,
779857
FOLD_SETTING_NONE,
780858
FOLD_SETTING_PKG,
@@ -917,6 +995,13 @@ export {
917995
BUN,
918996
CI,
919997
COLUMN_LIMIT,
998+
CONFIG_KEY_API_BASE_URL,
999+
CONFIG_KEY_API_PROXY,
1000+
CONFIG_KEY_API_TOKEN,
1001+
CONFIG_KEY_DEFAULT_ORG,
1002+
CONFIG_KEY_ENFORCED_ORGS,
1003+
CONFIG_KEY_ORG,
1004+
DOT_GIT,
9201005
DOT_SOCKET,
9211006
DOT_SOCKET_DOT_FACTS_JSON,
9221007
DRY_RUN_BAILING_NOW,
@@ -926,6 +1011,11 @@ export {
9261011
EMPTY_VALUE,
9271012
ENVIRONMENT_YAML,
9281013
ENVIRONMENT_YML,
1014+
ERROR_NO_MANIFEST_FILES,
1015+
ERROR_NO_PACKAGE_JSON,
1016+
ERROR_NO_REPO_FOUND,
1017+
ERROR_NO_SOCKET_DIR,
1018+
ERROR_UNABLE_RESOLVE_ORG,
9291019
ESLINT_CONFIG_JS,
9301020
ESNEXT,
9311021
EXTENSIONS,
@@ -942,6 +1032,20 @@ export {
9421032
EXT_MJS,
9431033
EXT_MTS,
9441034
EXT_PS1,
1035+
EXT_YAML,
1036+
EXT_YML,
1037+
FLAG_CONFIG,
1038+
FLAG_DRY_RUN,
1039+
FLAG_HELP,
1040+
FLAG_JSON,
1041+
FLAG_MARKDOWN,
1042+
FLAG_ORG,
1043+
FLAG_PIN,
1044+
FLAG_PROD,
1045+
FLAG_QUIET,
1046+
FLAG_SILENT,
1047+
FLAG_TEXT,
1048+
FLAG_VERBOSE,
9451049
FOLD_SETTING_FILE,
9461050
FOLD_SETTING_NONE,
9471051
FOLD_SETTING_PKG,

src/utils/config.mts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ import { safeReadFileSync } from '@socketsecurity/registry/lib/fs'
77
import { logger } from '@socketsecurity/registry/lib/logger'
88
import { naturalCompare } from '@socketsecurity/registry/lib/sorts'
99

10-
import constants from '../constants.mts'
10+
import constants, {
11+
CONFIG_KEY_API_BASE_URL,
12+
CONFIG_KEY_API_PROXY,
13+
CONFIG_KEY_API_TOKEN,
14+
CONFIG_KEY_DEFAULT_ORG,
15+
CONFIG_KEY_ENFORCED_ORGS,
16+
CONFIG_KEY_ORG,
17+
} from '../constants.mts'
1118

1219
import type { CResult } from '../types.mts'
1320
import type { SocketYml } from '@socketsecurity/config'
@@ -27,28 +34,28 @@ export interface LocalConfig {
2734
org?: string | undefined
2835
}
2936

30-
const sensitiveConfigKeyLookup: Set<keyof LocalConfig> = new Set(['apiToken'])
37+
const sensitiveConfigKeyLookup: Set<keyof LocalConfig> = new Set([CONFIG_KEY_API_TOKEN])
3138

3239
const supportedConfig: Map<keyof LocalConfig, string> = new Map([
33-
['apiBaseUrl', 'Base URL of the Socket API endpoint'],
34-
['apiProxy', 'A proxy through which to access the Socket API'],
40+
[CONFIG_KEY_API_BASE_URL, 'Base URL of the Socket API endpoint'],
41+
[CONFIG_KEY_API_PROXY, 'A proxy through which to access the Socket API'],
3542
[
36-
'apiToken',
43+
CONFIG_KEY_API_TOKEN,
3744
'The Socket API token required to access most Socket API endpoints',
3845
],
3946
[
40-
'defaultOrg',
47+
CONFIG_KEY_DEFAULT_ORG,
4148
'The default org slug to use; usually the org your Socket API token has access to. When set, all orgSlug arguments are implied to be this value.',
4249
],
4350
[
44-
'enforcedOrgs',
51+
CONFIG_KEY_ENFORCED_ORGS,
4552
'Orgs in this list have their security policies enforced on this machine',
4653
],
4754
[
4855
'skipAskToPersistDefaultOrg',
4956
'This flag prevents the Socket CLI from asking you to persist the org slug when you selected one interactively',
5057
],
51-
['org', 'Alias for defaultOrg'],
58+
[CONFIG_KEY_ORG, 'Alias for defaultOrg'],
5259
])
5360

5461
const supportedConfigEntries = [...supportedConfig.entries()].sort((a, b) =>
@@ -77,7 +84,7 @@ function getConfigValues(): LocalConfig {
7784
if (_cachedConfig['apiKey']) {
7885
const token = _cachedConfig['apiKey']
7986
delete _cachedConfig['apiKey']
80-
updateConfigValue('apiToken', token)
87+
updateConfigValue(CONFIG_KEY_API_TOKEN, token)
8188
}
8289
} else {
8390
mkdirSync(path.dirname(socketAppDataPath), { recursive: true })
@@ -94,7 +101,7 @@ function normalizeConfigKey(
94101
// property apiKey, we'll copy that to apiToken and delete the old property.
95102
// We added `org` as a convenience alias for `defaultOrg`
96103
const normalizedKey =
97-
key === 'apiKey' ? 'apiToken' : key === 'org' ? 'defaultOrg' : key
104+
key === 'apiKey' ? CONFIG_KEY_API_TOKEN : key === CONFIG_KEY_ORG ? CONFIG_KEY_DEFAULT_ORG : key
98105
if (!isSupportedConfigKey(normalizedKey)) {
99106
return {
100107
ok: false,

src/utils/determine-org-slug.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { logger } from '@socketsecurity/registry/lib/logger'
22

3+
import { CONFIG_KEY_DEFAULT_ORG } from '../constants.mts'
34
import { getConfigValueOrUndef } from './config.mts'
45
import { suggestOrgSlug } from '../commands/scan/suggest-org-slug.mts'
56
import { suggestToPersistOrgSlug } from '../commands/scan/suggest-to-persist-orgslug.mts'
@@ -9,7 +10,7 @@ export async function determineOrgSlug(
910
interactive: boolean,
1011
dryRun: boolean,
1112
): Promise<[string, string | undefined]> {
12-
const defaultOrgSlug = getConfigValueOrUndef('defaultOrg')
13+
const defaultOrgSlug = getConfigValueOrUndef(CONFIG_KEY_DEFAULT_ORG)
1314
let orgSlug = String(orgFlag || defaultOrgSlug || '')
1415
if (!orgSlug) {
1516
if (!interactive) {

src/utils/meow-with-subcommands.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import { getFlagListOutput, getHelpListOutput } from './output-formatting.mts'
2727
import constants, {
2828
API_V0_URL,
29+
CONFIG_KEY_DEFAULT_ORG,
2930
NPM,
3031
NPX,
3132
// PNPM,
@@ -134,7 +135,7 @@ function getAsciiHeader(command: string, orgFlag: string | undefined) {
134135
? REDACTED
135136
: constants.ENV.INLINED_SOCKET_CLI_VERSION_HASH
136137
const nodeVersion = redacting ? REDACTED : process.version
137-
const defaultOrg = getConfigValueOrUndef('defaultOrg')
138+
const defaultOrg = getConfigValueOrUndef(CONFIG_KEY_DEFAULT_ORG)
138139
const readOnlyConfig = isReadOnlyConfig() ? '*' : '.'
139140
const shownToken = redacting
140141
? REDACTED

0 commit comments

Comments
 (0)