-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathindex.ts
More file actions
54 lines (49 loc) · 1.73 KB
/
Copy pathindex.ts
File metadata and controls
54 lines (49 loc) · 1.73 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
import fs from 'node:fs'
import path from 'node:path'
import { AxiosError } from 'axios'
import yargs, { type Argv, type CommandModule } from 'yargs'
import { hideBin } from 'yargs/helpers'
const pkg = JSON.parse(fs.readFileSync(path.join(import.meta.dirname, '..', 'package.json'), 'utf8'))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const buildInstance = (commands: CommandModule<object, any>[]): Argv => {
const instance: Argv = yargs(hideBin(process.argv))
instance
.scriptName('smartthings')
.version(pkg.version)
.command(commands)
.strict()
.demandCommand()
.wrap(Math.min(160, instance.terminalWidth()))
.recommendCommands()
/* eslint-disable @typescript-eslint/naming-convention */
.updateStrings({
'Positionals:': 'Arguments:',
'Options:': 'Flags:',
})
.parserConfiguration({
'greedy-arrays': false,
'strip-aliased': true,
'strip-dashed': true,
})
/* eslint-enable @typescript-eslint/naming-convention */
.completion('generate-completions-script', 'output completion script setup')
.fail((message, error, yargs) => {
if (error && 'isAxiosError' in error && error.isAxiosError) {
// We don't print axiosError.message here because it just duplicates the things
// we're displaying but unformatted.
const axiosError = error as AxiosError
console.error(`Request to API failed with status ${axiosError.response?.status}`)
if (axiosError.response) {
console.error('Response: ', JSON.stringify(axiosError.response.data, null, 4))
}
} else if (error) {
console.error(error.message)
} else {
console.error(message)
console.error(yargs.help())
}
// eslint-disable-next-line no-process-exit
process.exit(1)
})
return instance
}