-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathhash-artifacts
executable file
·60 lines (46 loc) · 1.3 KB
/
hash-artifacts
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
#!/usr/bin/env node
const glob = require('glob')
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
const { EOL } = require('os')
function sha256(filePath) {
const hash = crypto.createHash('sha256')
const stream = fs.createReadStream(filePath)
return new Promise((resolve, reject) => {
stream.on('data', (d) => hash.update(d))
stream.on('end', () => {
resolve(hash.digest('hex'))
})
stream.on('error', reject)
})
}
async function main() {
const sha256TxtLines = []
const root = '/build/dist'
const fileManifestPromises =
glob.sync(`${root}/**/*`)
.filter((path) => fs.lstatSync(path).isFile())
.map(async (filePath) => {
const sum = await sha256(filePath)
const rel = path.relative(root, filePath)
sha256TxtLines.push([sum, rel])
return {
sha256: sum,
path: rel
}
})
const fileManifests = await Promise.all(fileManifestPromises)
const asJSON = JSON.stringify({
files: fileManifests
}, null, 2)
fs.writeFileSync(path.join(root, 'SHA256.json'), asJSON)
let asText = sha256TxtLines.map((line) => line.join(' ')).join(EOL)
asText = sha256TxtLines.length === 1 ? asText + EOL : asText
fs.writeFileSync(
path.join(root, 'SHA256.txt'),
asText,
{encoding: 'utf8'}
)
}
main()