/
index.js
48 lines (46 loc) 路 1.42 KB
/
index.js
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
import clipboard from 'clipboardy';
import { program } from 'commander';
import chalk from 'chalk';
import Debug from 'debug';
import JWT from './src/index.js';
import { Render } from './src/table.js';
import pkg from './package.json' assert { type: 'json' }
global.debug = Debug('hwt')
try {
program
.version(pkg.version, '-v, --version', 'output the current version')
.option('--raw')
.option('-c, --claims <claims>')
.hook('preAction', (command, actionCommand) => {
const options = command.opts()
let format = 'human'
if (options.raw) {
format = 'raw'
}
if (options.claims) {
command.opts().claims = options.claims.split(',').map(_ => _.trim())
}
command.opts().format = format
})
.action(({ format, claims }) => {
debug('received token:')
const input = clipboard.readSync()
debug(input)
const result = JWT(input, { format, claims })
process.stdout.write(Render(result, { format }) + '\n')
})
.parse()
} catch(e) {
debug(e)
let { message } = e
if (e instanceof SyntaxError) {
const input = clipboard.readSync()
message = `The clipboard ${chalk.red('does not contain a JWT')}, here what I got 馃憞` + '\n'
message += '---' + '\n'
message += input + '\n'
message += '---' + '\n'
} else {
message = '馃槱 something went wrong: ' + chalk.red(message) + '\n'
}
process.stdout.write(message)
}