-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbin.js
executable file
·65 lines (54 loc) · 1.43 KB
/
bin.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
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const encode = require('.')
const pkg = require('./package.json')
const argv = mri(process.argv.slice(2), {
boolean: [
'help', 'h',
'version', 'v',
'url-encoded', 'u'
]
})
if (argv.help || argv.h) {
process.stdout.write(`
Usage:
as-data-uri [--url-encoded] [--mime mime-type] [--charset utf8]
Options:
--url-encoded -u Url-encode instead of base64.
--mime -m Specify a mime type. Default: text/plain.
--charset -c Specify an optional charset (for text only).
Examples:
echo -n 'hello world' | as-data-uri --mime text/plain --url-encoded
cat picture.png | as-data-uri --mime image/png | pbcopy
\n`)
process.exit()
}
if (argv.version || argv.v) {
process.stdout.write(pkg.name + ' ' + pkg.version + '\n')
process.exit(0)
}
const showError = (err) => {
if (process.env.NODE_DEBUG === 'listen-to-youtube-cli') {
console.error(err)
} else process.stderr.write(err + '\n')
process.exit(err.code || 1)
}
const base64 = !(argv['url-encoded'] || argv.u)
const mime = argv.mime || argv.m || null
const charset = argv.charset || argv.c || null
// collect data from stdin
let data = Buffer.alloc(0)
process.stdin
.on('data', (chunk) => {
data = Buffer.concat([data, chunk])
})
.once('error', showError)
.once('end', () => {
try {
data = encode(data, mime, charset, base64)
} catch (err) {
return showError(err)
}
process.stdout.write(data)
})