forked from keithamus/hashmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashmark
executable file
·71 lines (69 loc) · 1.93 KB
/
hashmark
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
'use strict';
var cli = require('cli');
var fs = require('fs');
var hash = require('..');
cli
.enable('version', 'autocomplete', 'help', 'glob')
.setApp(__dirname + '/../package.json')
.setUsage(cli.app + ' [OPTIONS] [file] filename-pattern')
.parse({
digest: [
'd',
'Digest Type (e.g. md5, sha1, sha256, sha512)',
'string',
'sha256'
],
length: [
'l',
'Limit the digest to this many characters (0 for unlimited)',
'number',
0
],
silent: [
's',
'Do not log any output',
'boolean',
false
],
'asset-map': [
'm',
'Name of a file to write an asset map to',
'string',
null
],
'name': [
'n',
'If passing stdin, use this flag to force a name for the file',
'string',
null
]
});
if (!cli.args.length) {
cli.getUsage(1);
} else {
cli.options.pattern = cli.args.pop();
var files = process.stdin;
if (cli.options.name) {
files.fileName = cli.options.name;
}
if (cli.args.length) {
files = cli.glob.sync(cli.args[0]);
}
hash(files, cli.options).on('end', function (map) {
// Output hash
if (!cli.options.silent) {
process.stdout.write(JSON.stringify(map));
}
if (cli.options['asset-map']) {
if (fs.existsSync(cli.options['asset-map'])) {
var assetMap = JSON.parse(fs.readFileSync(cli.options['asset-map']));
Object.keys(assetMap).reduce(function (map, key) {
map[key] = map[key] || assetMap[key];
return map;
}, map);
}
fs.writeFileSync(cli.options['asset-map'], JSON.stringify(map, null, 2));
}
});
}