Skip to content

Commit d1a23ab

Browse files
committed
bin: adhere to standard
1 parent 0041dbb commit d1a23ab

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

bin.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
#! /usr/bin/env node
22

33
var createHash = require('./browserify')
4-
54
var argv = process.argv.slice(2)
65

7-
if(/--help|-h/.test(argv[0])) return usage()
8-
9-
function stream (alg, s) {
6+
function pipe (algorithm, s) {
107
var start = Date.now()
11-
var hash = createHash(alg || 'sha1')
12-
s
13-
.on('data', function (data) {
8+
var hash = createHash(algorithm || 'sha1')
9+
10+
s.on('data', function (data) {
1411
hash.update(data)
1512
})
16-
.on('end', function (data) {
17-
if(process.env.DEBUG)
13+
14+
s.on('end', function () {
15+
if (process.env.DEBUG) {
1816
return console.log(hash.digest('hex'), Date.now() - start)
17+
}
18+
1919
console.log(hash.digest('hex'))
2020
})
2121
}
22-
23-
if(!process.stdin.isTTY) {
24-
stream(argv[0], process.stdin)
25-
} else if (argv.length) {
26-
var filename = argv.pop()
27-
var alg = argv.pop()
28-
stream(alg, require('fs').createReadStream(filename))
29-
} else {
30-
usage()
31-
}
3222

3323
function usage () {
3424
console.error('sha.js [algorithm=sha1] [filename] # hash filename with algorithm')
3525
console.error('input | sha.js [algorithm=sha1] # hash stdin with algorithm')
3626
console.error('sha.js --help # display this message')
3727
}
28+
29+
if (!process.stdin.isTTY) {
30+
pipe(argv[0], process.stdin)
31+
32+
} else if (argv.length) {
33+
if (/--help|-h/.test(argv[0])) {
34+
usage()
35+
36+
} else {
37+
var filename = argv.pop()
38+
var algorithm = argv.pop()
39+
pipe(algorithm, require('fs').createReadStream(filename))
40+
}
41+
} else {
42+
usage()
43+
}

0 commit comments

Comments
 (0)