chore(release): v1.0.0

This commit is contained in:
seepine
2023-08-02 00:39:45 +08:00
commit 8e2d3a87e1
29 changed files with 10636 additions and 0 deletions

12
src/constants.ts Normal file
View File

@@ -0,0 +1,12 @@
/* eslint-disable no-shadow */
export enum Inputs {
Workdir = 'workdir', // Input for cache, restore, save action
Patterns = 'patterns', // Input for cache, restore, save action
Gitignore = 'gitignore', // Input for cache, restore action
IgnoreFiles = 'ignoreFiles' // Input for cache, save action
}
export enum Outputs {
Hash = 'hash', // Output from cache, restore action
MatchedFiles = 'matched-files' // Output from restore action
}

22
src/files.ts Normal file
View File

@@ -0,0 +1,22 @@
import {Options, globby} from '@cjs-exporter/globby'
import * as fs from 'fs'
export async function getFiles(
workdir: string,
patterns: string[],
options?: Options
): Promise<string[]> {
return new Promise(async RES => {
const paths = await globby(
patterns.map(item => {
return workdir + item
}),
options
)
RES(paths)
})
}
export async function readFile(path: string): Promise<string> {
return fs.promises.readFile(path, 'utf-8')
}

57
src/main.ts Normal file
View File

@@ -0,0 +1,57 @@
import * as core from '@actions/core'
import {Inputs, Outputs} from './constants'
import * as utils from './utils'
import {getFiles, readFile} from './files'
import {hashHexAsync} from './utils'
async function run(): Promise<void> {
try {
let workdir: string = utils.getInput(Inputs.Workdir, {required: true})
const patterns = utils.getInputAsArray(Inputs.Patterns, {
required: true
})
const gitignore = utils.getInputAsBool(Inputs.Gitignore) || true
const ignoreFiles = utils.getInputAsArray(Inputs.IgnoreFiles)
if (!workdir.endsWith('/')) {
workdir += '/'
}
core.debug(`workdir: ${workdir}`)
core.debug(`patterns: ${patterns}`)
core.debug(`gitignore: ${gitignore}`)
core.debug(`ignoreFiles: ${ignoreFiles}`)
const files = await getFiles(workdir, patterns, {gitignore, ignoreFiles})
let hash = ''
const reads = files.map(async file => readFile(file))
const fileContents = await Promise.all(reads)
const contents = await Promise.all(
fileContents.map(async fileContent => hashHexAsync(fileContent))
)
if (contents.length === 1) {
hash = contents[0]
} else if (contents.length > 1) {
let hashStr = ''
for (const content of contents) {
hashStr += content
}
hash = utils.hashHex(hashStr)
}
core.info('')
core.info('MatchedFiles:')
core.info(` ${files.toString()}`)
core.info(`Hash: ${hash}`)
core.setOutput(Outputs.Hash, hash)
core.setOutput(Outputs.MatchedFiles, files)
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
}
}
run()

49
src/utils.ts Normal file
View File

@@ -0,0 +1,49 @@
import * as core from '@actions/core'
import crypto, {BinaryLike} from 'crypto'
export function getInput(name: string, options?: core.InputOptions): string {
return core.getInput(name, options)
}
export function getInputAsArray(
name: string,
options?: core.InputOptions
): string[] {
return core
.getInput(name, options)
.split('\n')
.map(s => s.replace(/^!\s+/, '!').trim())
.filter(x => x !== '')
}
export function getInputAsInt(
name: string,
options?: core.InputOptions
): number | undefined {
const value = parseInt(core.getInput(name, options))
if (isNaN(value) || value < 0) {
return undefined
}
return value
}
export function getInputAsBool(
name: string,
options?: core.InputOptions
): boolean {
const result = core.getInput(name, options)
return result.toLowerCase() === 'true'
}
export function hashHex(content: BinaryLike, shaAlgorithm = 'sha256'): string {
return crypto.createHash(shaAlgorithm).update(content).digest('hex')
}
export async function hashHexAsync(
content: BinaryLike,
shaAlgorithm = 'sha256'
): Promise<string> {
return new Promise(RES => {
RES(hashHex(content, shaAlgorithm))
})
}