-
Notifications
You must be signed in to change notification settings - Fork 20
/
hashmark
executable file
·88 lines (86 loc) · 2.46 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env node
'use strict';
var cli = require('cli');
var fs = require('fs');
var hash = require('..');
var path = require('path');
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'
],
cwd: [
'c',
'Resolve path names from this directory',
'string',
'.'
],
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
],
'rename': [
'r',
'Rename input files with hash rather than creating hashed copies',
'boolean',
false
],
});
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.args.reduce(function(files, pattern) {
return files.concat(cli.glob.sync(path.join(cli.options.cwd, pattern)));
}, []);
}
hash(files, cli.options).on('end', function (map) {
// Output hash
var assetMapPath = cli.options['asset-map'];
if (!cli.options.silent) {
process.stdout.write(JSON.stringify(map));
}
if (assetMapPath) {
assetMapPath = path.join(cli.options.cwd, assetMapPath);
if (fs.existsSync(assetMapPath)) {
var assetMap = JSON.parse(fs.readFileSync(assetMapPath));
Object.keys(assetMap).reduce(function (map, key) {
map[key] = map[key] || assetMap[key];
return map;
}, map);
}
fs.writeFileSync(assetMapPath, JSON.stringify(map, null, 2).replace(/\\\\/g,'/'));
}
});
}