generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.ts
87 lines (80 loc) · 2.46 KB
/
main.ts
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
import * as core from '@actions/core'
import {unzip, get} from './src/downloader'
import {restoreCache, saveCache} from '@actions/cache'
import {readdirSync, unlinkSync} from 'fs'
async function run(): Promise<void> {
try {
const {
artifactName,
stripPrefix,
download,
bytesToExtract,
cacheId
} = await get(
core.getInput('repository'),
core.getInput('definitionId'),
core.getInput('artifact'),
core.getInput('stripPrefix'),
core.getInput('reasonFilter')
)
const outputDirectory = core.getInput('path') || artifactName
let useCache = core.getInput('cache') === 'true'
const verbose: number | boolean = ((input?: string) =>
input && input.match(/^\d+$/) ? parseInt(input) : input === 'true')(
core.getInput('verbose')
)
const isDirectoryEmpty = (path: string): boolean => {
try {
return readdirSync(path).length === 0
} catch (e) {
return e instanceof Object && (e as any).code === 'ENOENT'
}
}
let needToDownload = true
let storeZipAs: string | undefined
if (useCache) {
try {
if (!isDirectoryEmpty(outputDirectory)) {
storeZipAs = `${outputDirectory}/.${cacheId}.zip`
if (await restoreCache([storeZipAs], cacheId)) {
await unzip(
`file:${storeZipAs}`,
bytesToExtract,
stripPrefix,
outputDirectory,
verbose
)
core.info(`Cached ${cacheId} was successfully restored`)
needToDownload = false
}
} else if (await restoreCache([outputDirectory], cacheId)) {
core.info(`Cached ${cacheId} was successfully restored`)
needToDownload = false
}
} catch (e) {
core.warning(`Cannot use @actions/cache (${e})`)
useCache = false
}
}
if (needToDownload) {
core.info(`Downloading ${artifactName}`)
await download(outputDirectory, verbose, storeZipAs)
try {
if (
useCache &&
!(await saveCache([storeZipAs || outputDirectory], cacheId))
) {
core.warning(`Failed to cache ${cacheId}`)
}
} catch (e) {
core.warning(`Failed to cache ${cacheId}: ${e instanceof Error && e.message}`)
}
if (storeZipAs) {
unlinkSync(storeZipAs)
}
}
} catch (error) {
core.setFailed(error instanceof Error ? error.message : `${error}`)
}
}
run()