Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
executable file
·76 lines (68 loc) · 2.18 KB
/
Copy pathcli.ts
File metadata and controls
executable file
·76 lines (68 loc) · 2.18 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
#!/usr/bin/env node
import process from 'node:process'
import { pathToFileURL } from 'node:url'
import { messageWithCauses, stackWithCauses } from 'pony-cause'
import updateNotifier from 'tiny-updater'
import colors from 'yoctocolors-cjs'
import * as cliCommands from './commands'
import constants from './constants'
import { AuthError, InputError } from './utils/errors'
import { logSymbols } from './utils/logging'
import { meowWithSubcommands } from './utils/meow-with-subcommands'
const { rootPkgJsonPath } = constants
const formattedCliCommands = Object.fromEntries(
Object.entries(cliCommands).map(entry => {
const key = entry[0]
entry[0] = camelToHyphen(key)
return entry
})
)
function camelToHyphen(str: string): string {
return str.replace(/[A-Z]+/g, '-$&').toLowerCase()
}
// TODO: Add autocompletion using https://socket.dev/npm/package/omelette
void (async () => {
await updateNotifier({
name: 'socket',
version: require(rootPkgJsonPath).version,
ttl: 86_400_000 /* 24 hours in milliseconds */
})
try {
await meowWithSubcommands(formattedCliCommands, {
aliases: {
ci: {
description: 'Alias for "report create --view --strict"',
argv: ['report', 'create', '--view', '--strict']
}
},
argv: process.argv.slice(2),
name: 'socket',
importMeta: { url: `${pathToFileURL(__filename)}` } as ImportMeta
})
} catch (err) {
let errorBody: string | undefined
let errorTitle: string
let errorMessage = ''
if (err instanceof AuthError) {
errorTitle = 'Authentication error'
errorMessage = err.message
} else if (err instanceof InputError) {
errorTitle = 'Invalid input'
errorMessage = err.message
errorBody = err.body
} else if (err instanceof Error) {
errorTitle = 'Unexpected error'
errorMessage = messageWithCauses(err)
errorBody = stackWithCauses(err)
} else {
errorTitle = 'Unexpected error with no details'
}
console.error(
`${logSymbols.error} ${colors.bgRed(colors.white(errorTitle + ':'))} ${errorMessage}`
)
if (errorBody) {
console.error(`\n${errorBody}`)
}
process.exit(1)
}
})()