2021-06-07 18:26:00 +00:00
|
|
|
import * as crypto from 'crypto'
|
|
|
|
import * as core from '@actions/core'
|
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as stream from 'stream'
|
|
|
|
import * as util from 'util'
|
|
|
|
import * as path from 'path'
|
|
|
|
import {Globber} from './glob'
|
|
|
|
|
2022-04-18 18:29:24 +00:00
|
|
|
export async function hashFiles(
|
|
|
|
globber: Globber,
|
|
|
|
verbose: Boolean = false
|
|
|
|
): Promise<string> {
|
|
|
|
const writeDelegate = verbose ? core.info : core.debug
|
2021-06-07 18:26:00 +00:00
|
|
|
let hasMatch = false
|
|
|
|
const githubWorkspace = process.env['GITHUB_WORKSPACE'] ?? process.cwd()
|
|
|
|
const result = crypto.createHash('sha256')
|
|
|
|
let count = 0
|
|
|
|
for await (const file of globber.globGenerator()) {
|
2022-04-18 18:29:24 +00:00
|
|
|
writeDelegate(file)
|
2021-06-07 18:26:00 +00:00
|
|
|
if (!file.startsWith(`${githubWorkspace}${path.sep}`)) {
|
2022-04-18 18:29:24 +00:00
|
|
|
writeDelegate(`Ignore '${file}' since it is not under GITHUB_WORKSPACE.`)
|
2021-06-07 18:26:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (fs.statSync(file).isDirectory()) {
|
2022-04-18 18:29:24 +00:00
|
|
|
writeDelegate(`Skip directory '${file}'.`)
|
2021-06-07 18:26:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
const hash = crypto.createHash('sha256')
|
|
|
|
const pipeline = util.promisify(stream.pipeline)
|
|
|
|
await pipeline(fs.createReadStream(file), hash)
|
|
|
|
result.write(hash.digest())
|
|
|
|
count++
|
|
|
|
if (!hasMatch) {
|
|
|
|
hasMatch = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.end()
|
|
|
|
|
|
|
|
if (hasMatch) {
|
2022-04-18 18:29:24 +00:00
|
|
|
writeDelegate(`Found ${count} files to hash.`)
|
2021-06-07 18:26:00 +00:00
|
|
|
return result.digest('hex')
|
|
|
|
} else {
|
2022-04-18 18:29:24 +00:00
|
|
|
writeDelegate(`No matches found for glob`)
|
2021-06-07 18:26:00 +00:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|