-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·71 lines (57 loc) · 1.23 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
import process from 'node:process'
import {Buffer} from 'node:buffer'
import meow from 'meow'
import stdin from 'get-stdin'
const cli = meow(`$ b64 --help
Usage
$ b64 [--encode|--decode] <string>
$ echo <string> | b64
Options
-e, --encode Encode
-d, --decode Decode
Examples
$ b64 --encode 'test'
dGVzdA==
$ b64 -e 'test'
dGVzdA==
$ b64 'dGVzdA=='
test
$ b64 --decode 'dGVzdA=='
test
$ echo 'dGVzdA==' | b64
test
`, {
importMeta: import.meta,
flags: {
encode: {
type: 'boolean',
alias: 'e',
default: false,
},
decode: {
type: 'boolean',
alias: 'd',
default: true,
},
},
})
const {input: [input], flags} = cli
if (!input && process.stdin.isTTY) {
console.error('Please specify text to decode/encode')
process.exit(1)
}
const {encode = !flags.decode || false} = flags
const init = ({data, encode = false}) => {
const from = encode === true ? 'ascii' : 'base64'
const to = encode === true ? 'base64' : 'ascii'
console.log(Buffer.from(data, from).toString(to))
}
(async () => {
if (input) {
init({data: input, encode})
} else {
const data = await stdin()
init({data, encode})
}
})()