mirror of
https://git.mirrors.martin98.com/https://github.com/actions/toolkit
synced 2026-04-01 00:03:17 +08:00
@@ -1,5 +1,7 @@
|
||||
import {Globber, DefaultGlobber} from './internal-globber'
|
||||
import {GlobOptions} from './internal-glob-options'
|
||||
import {HashFileOptions} from './internal-hash-file-options'
|
||||
import {hashFiles as _hashFiles} from './internal-hash-files'
|
||||
|
||||
export {Globber, GlobOptions}
|
||||
|
||||
@@ -15,3 +17,21 @@ export async function create(
|
||||
): Promise<Globber> {
|
||||
return await DefaultGlobber.create(patterns, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the sha256 hash of a glob
|
||||
*
|
||||
* @param patterns Patterns separated by newlines
|
||||
* @param options Glob options
|
||||
*/
|
||||
export async function hashFiles(
|
||||
patterns: string,
|
||||
options?: HashFileOptions
|
||||
): Promise<string> {
|
||||
let followSymbolicLinks = true
|
||||
if (options && typeof options.followSymbolicLinks === 'boolean') {
|
||||
followSymbolicLinks = options.followSymbolicLinks
|
||||
}
|
||||
const globber = await create(patterns, {followSymbolicLinks})
|
||||
return _hashFiles(globber)
|
||||
}
|
||||
|
||||
12
packages/glob/src/internal-hash-file-options.ts
Normal file
12
packages/glob/src/internal-hash-file-options.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Options to control globbing behavior
|
||||
*/
|
||||
export interface HashFileOptions {
|
||||
/**
|
||||
* Indicates whether to follow symbolic links. Generally should set to false
|
||||
* when deleting files.
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
followSymbolicLinks?: boolean
|
||||
}
|
||||
42
packages/glob/src/internal-hash-files.ts
Normal file
42
packages/glob/src/internal-hash-files.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
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'
|
||||
|
||||
export async function hashFiles(globber: Globber): Promise<string> {
|
||||
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()) {
|
||||
core.debug(file)
|
||||
if (!file.startsWith(`${githubWorkspace}${path.sep}`)) {
|
||||
core.debug(`Ignore '${file}' since it is not under GITHUB_WORKSPACE.`)
|
||||
continue
|
||||
}
|
||||
if (fs.statSync(file).isDirectory()) {
|
||||
core.debug(`Skip directory '${file}'.`)
|
||||
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) {
|
||||
core.debug(`Found ${count} files to hash.`)
|
||||
return result.digest('hex')
|
||||
} else {
|
||||
core.warning(`No matches found for glob`)
|
||||
return ''
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user