-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (118 loc) · 3.76 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const path = require('path')
const hasha = require('hasha')
const slash = require('slash')
const walker = require('walk')
const defaults = require('defaults')
const hashaOptions = {algorithm: 'sha512'}
const defaultOptions = {sep: ' '}
function normPath (cwd, file) {
return slash(path.relative(cwd, file))
}
function computeExtendedHash (file, stat, options, callback) {
hasha.fromFile(file, hashaOptions)
.catch(callback)
.then(function (res) {
const normed = options.cwd ? normPath(options.cwd, file) : file
const hash = {
depth: (normed.match(/\/|\\/g) || []).length,
fname: stat.name,
value: res + options.sep + normed
}
callback(null, hash)
})
}
function computeExtendedHashSync (file, stat, options) {
const normed = options.cwd ? normPath(options.cwd, file) : file
return {
depth: (normed.match(/\/|\\/g) || []).length,
fname: stat.name,
value: hasha.fromFileSync(file, hashaOptions) + options.sep + normed
}
}
function sortExtendedHashes (hashes) {
hashes.sort(function (h1, h2) {
return (h1.depth < h2.depth ? -1 : (h1.depth > h2.depth ? 1 : h1.fname.localeCompare(h2.fname)))
})
return hashes
}
/**
* Compute the sha512 hash of a file in the format "<hash> /path/to/file"
* @param file
* @param options
* @param callback
*/
module.exports.fromFile = function (file, options, callback) {
if (typeof callback === 'undefined') callback = options
options = defaults(options, defaultOptions)
hasha.fromFile(file, hashaOptions)
.catch(callback)
.then(function (hash) {
file = options.cwd ? normPath(options.cwd, file) : file
callback(null, hash + options.sep + file)
})
}
/**
* <SYNC> Compute the sha512 hash of a file in the format "<hash> /path/to/file"
* @param file
* @param options
* @returns {*}
*/
module.exports.fromFileSync = function (file, options) {
options = defaults(options, defaultOptions)
const hash = hasha.fromFileSync(file, hashaOptions)
file = options.cwd ? normPath(options.cwd, file) : file
return hash + options.sep + file
}
/**
* Compute the sha512 hash of all files recusrively in a directory in the format "<hash> /path/to/file\n<hash> /path/to/file"
* @param directory
* @param options
* @param callback
*/
module.exports.fromDirectory = function (directory, options, callback) {
if (typeof callback === 'undefined') callback = options
options = defaults(options, defaultOptions)
const hashes = []
walker.walk(directory)
.on('errors', callback)
.on('file', function (root, stat, next) {
const file = path.normalize(path.join(root, stat.name))
computeExtendedHash(file, stat, options, function (err, hash) {
if (err) return callback(err)
hashes.push(hash)
next()
})
})
.on('end', function () {
const res = sortExtendedHashes(hashes).map(function (hash) {
return hash.value
}).join('\n')
return callback(null, res)
})
}
/**
* <SYNC> Compute the sha512 hash of all files recusrively in a directory in the format "<hash> /path/to/file\n<hash> /path/to/file"
* @param directory
* @param options
* @returns {string}
*/
module.exports.fromDirectorySync = function (directory, options) {
options = defaults(options, defaultOptions)
const hashes = []
const walkerOptions = {
listeners: {
file: function (root, stat, next) {
const file = path.normalize(path.join(root, stat.name))
hashes.push(computeExtendedHashSync(file, stat, options))
next()
},
errors: function () {
throw new Error(JSON.stringify([].slice.call(arguments), null, 4))
}
}
}
walker.walkSync(directory, walkerOptions)
return sortExtendedHashes(hashes).map(function (hash) {
return hash.value
}).join('\n')
}