-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcli.mts
More file actions
executable file
·241 lines (229 loc) · 7.98 KB
/
cli.mts
File metadata and controls
executable file
·241 lines (229 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env node
import { fileURLToPath, pathToFileURL } from 'node:url'
import meow from 'meow'
import { messageWithCauses, stackWithCauses } from 'pony-cause'
import lookupRegistryAuthToken from 'registry-auth-token'
import lookupRegistryUrl from 'registry-url'
import updateNotifier from 'tiny-updater'
import { debugFn, debugLog } from '@socketsecurity/registry/lib/debug'
import { logger } from '@socketsecurity/registry/lib/logger'
import { cmdAnalytics } from './commands/analytics/cmd-analytics.mts'
import { cmdAuditLog } from './commands/audit-log/cmd-audit-log.mts'
import { cmdCI } from './commands/ci/cmd-ci.mts'
import { cmdConfig } from './commands/config/cmd-config.mts'
import { cmdFix } from './commands/fix/cmd-fix.mts'
import { cmdInstall } from './commands/install/cmd-install.mts'
import { cmdJson } from './commands/json/cmd-json.mts'
import { cmdLogin } from './commands/login/cmd-login.mts'
import { cmdLogout } from './commands/logout/cmd-logout.mts'
import { cmdManifestCdxgen } from './commands/manifest/cmd-manifest-cdxgen.mts'
import { cmdManifest } from './commands/manifest/cmd-manifest.mts'
import { cmdNpm } from './commands/npm/cmd-npm.mts'
import { cmdNpx } from './commands/npx/cmd-npx.mts'
import { cmdOops } from './commands/oops/cmd-oops.mts'
import { cmdOptimize } from './commands/optimize/cmd-optimize.mts'
import { cmdOrganizationDependencies } from './commands/organization/cmd-organization-dependencies.mts'
import { cmdOrganizationPolicyLicense } from './commands/organization/cmd-organization-policy-license.mts'
import { cmdOrganizationPolicySecurity } from './commands/organization/cmd-organization-policy-security.mts'
import { cmdOrganization } from './commands/organization/cmd-organization.mts'
import { cmdPackage } from './commands/package/cmd-package.mts'
import { cmdRawNpm } from './commands/raw-npm/cmd-raw-npm.mts'
import { cmdRawNpx } from './commands/raw-npx/cmd-raw-npx.mts'
import { cmdRepository } from './commands/repository/cmd-repository.mts'
import { cmdScan } from './commands/scan/cmd-scan.mts'
import { cmdThreatFeed } from './commands/threat-feed/cmd-threat-feed.mts'
import { cmdUninstall } from './commands/uninstall/cmd-uninstall.mts'
import { cmdWrapper } from './commands/wrapper/cmd-wrapper.mts'
import constants from './constants.mts'
import { AuthError, InputError, captureException } from './utils/errors.mts'
import { failMsgWithBadge } from './utils/fail-msg-with-badge.mts'
import { meowWithSubcommands } from './utils/meow-with-subcommands.mts'
import { serializeResultJson } from './utils/serialize-result-json.mts'
const __filename = fileURLToPath(import.meta.url)
const { SOCKET_CLI_BIN_NAME } = constants
void (async () => {
const registryUrl = lookupRegistryUrl()
await updateNotifier({
authInfo: lookupRegistryAuthToken(registryUrl, { recursive: true }),
name: SOCKET_CLI_BIN_NAME,
registryUrl,
ttl: 86_400_000 /* 24 hours in milliseconds */,
// Lazily access constants.ENV.INLINED_SOCKET_CLI_VERSION.
version: constants.ENV.INLINED_SOCKET_CLI_VERSION,
})
try {
await meowWithSubcommands(
{
ci: cmdCI,
config: cmdConfig,
fix: cmdFix,
install: cmdInstall,
json: cmdJson,
login: cmdLogin,
logout: cmdLogout,
npm: cmdNpm,
npx: cmdNpx,
oops: cmdOops,
optimize: cmdOptimize,
organization: cmdOrganization,
package: cmdPackage,
'raw-npm': cmdRawNpm,
'raw-npx': cmdRawNpx,
wrapper: cmdWrapper,
scan: cmdScan,
'audit-log': cmdAuditLog,
repos: cmdRepository,
analytics: cmdAnalytics,
'threat-feed': cmdThreatFeed,
manifest: cmdManifest,
uninstall: cmdUninstall,
},
{
aliases: {
audit: {
description: cmdAuditLog.description,
hidden: true,
argv: ['audit-log'],
},
auditLog: {
description: cmdAuditLog.description,
hidden: true,
argv: ['audit-log'],
},
auditLogs: {
description: cmdAuditLog.description,
hidden: true,
argv: ['audit-log'],
},
['audit-logs']: {
description: cmdAuditLog.description,
hidden: true,
argv: ['audit-log'],
},
cdxgen: {
description: cmdManifestCdxgen.description,
hidden: true,
argv: ['manifest', 'cdxgen'],
},
deps: {
description: cmdOrganizationDependencies.description,
hidden: true,
argv: ['dependencies'],
},
feed: {
description: cmdThreatFeed.description,
hidden: true,
argv: ['threat-feed'],
},
license: {
description: cmdOrganizationPolicyLicense.description,
hidden: true,
argv: ['organization', 'policy', 'license'],
},
org: {
description: cmdOrganization.description,
hidden: true,
argv: ['organization'],
},
orgs: {
description: cmdOrganization.description,
hidden: true,
argv: ['organization'],
},
organizations: {
description: cmdOrganization.description,
hidden: true,
argv: ['organization'],
},
organisation: {
description: cmdOrganization.description,
hidden: true,
argv: ['organization'],
},
organisations: {
description: cmdOrganization.description,
hidden: true,
argv: ['organization'],
},
pkg: {
description: cmdPackage.description,
hidden: true,
argv: ['package'],
},
repo: {
description: cmdRepository.description,
hidden: true,
argv: ['repos'],
},
repository: {
description: cmdRepository.description,
hidden: true,
argv: ['repos'],
},
repositories: {
description: cmdRepository.description,
hidden: true,
argv: ['repos'],
},
security: {
description: cmdOrganizationPolicySecurity.description,
hidden: true,
argv: ['organization', 'policy', 'security'],
},
},
argv: process.argv.slice(2),
name: SOCKET_CLI_BIN_NAME,
importMeta: { url: `${pathToFileURL(__filename)}` } as ImportMeta,
},
)
} catch (e) {
process.exitCode = 1
debugFn('Uncaught error (BAD!):')
debugFn(e)
// Try to parse the flags, find out if --json or --markdown is set.
let isJson = false
try {
const cli = meow(``, {
argv: process.argv.slice(2),
autoHelp: false,
flags: {},
importMeta: { url: `${pathToFileURL(__filename)}` } as ImportMeta,
})
isJson = !!cli.flags['json']
} catch {}
let errorBody: string | undefined
let errorTitle: string
let errorMessage = ''
if (e instanceof AuthError) {
errorTitle = 'Authentication error'
errorMessage = e.message
} else if (e instanceof InputError) {
errorTitle = 'Invalid input'
errorMessage = e.message
errorBody = e.body
} else if (e instanceof Error) {
errorTitle = 'Unexpected error'
errorMessage = messageWithCauses(e)
errorBody = stackWithCauses(e)
} else {
errorTitle = 'Unexpected error with no details'
}
if (isJson) {
logger.log(
serializeResultJson({
ok: false,
message: errorTitle,
cause: errorMessage,
}),
)
} else {
logger.error('\n') // Any-spinner-newline
logger.fail(failMsgWithBadge(errorTitle, errorMessage))
if (errorBody) {
// Explicitly use debugLog here.
debugLog(errorBody)
}
}
await captureException(e)
}
})()