-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
56 lines (50 loc) · 996 Bytes
/
cli.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
49
50
51
52
53
54
55
56
const meow = require('meow')
const fourByte = require('.')
const cli = meow(`
Usage
$ 4byte <hash-of-function-sig>
Examples
$ 4byte 0x51c6590a
addLiquidity(uint256)
`, {
flags: {
format: {
type: 'string',
alias: 'f'
}
}
})
let hash = cli.input[0]
const format = cli.flags.format || cli.flags.f
if (process.stdin) {
process.stdin.setEncoding('utf8')
process.stdin.resume()
let content = ''
setTimeout(() => {
content = content.trim()
if (content) {
hash = content
}
run()
}, 10)
} else {
run()
}
async function run () {
try {
const result = await fourByte(hash)
if (format) {
if (format === 'json') {
console.log(JSON.stringify(result, null, 2))
} else {
throw new Error('format is invalid. Options are "json"')
}
} else {
console.log(result.join('\n'))
}
process.exit(0)
} catch (err) {
console.log(err.message)
process.exit(1)
}
}