|
| 1 | +import { cacheDir as ghactionsToolCacheCacheDirectory, cacheFile as ghactionsToolCacheCacheFile, downloadTool as ghactionToolCacheDownloadTool, extract7z as ghactionToolCacheExtract7z, extractTar as ghactionToolCacheExtractTar, extractXar as ghactionToolCacheExtractXar, extractZip as ghactionToolCacheExtractZip, find as ghactionsToolCacheFind, findAllVersions as ghactionsToolCacheFindAllVersions } from "@actions/tool-cache"; |
| 2 | +import { create as ghactionsArtifact } from "@actions/artifact"; |
| 3 | +import { debug as ghactionsDebug, getIDToken as ghactionsGetOpenIDConnectToken } from "@actions/core"; |
| 4 | +import { restoreCache as ghactionsCacheRestoreCache, saveCache as ghactionsCacheSaveCache } from "@actions/cache"; |
| 5 | +function encodeConvert(item, from, to) { |
| 6 | + return Buffer.from(item, from).toString(to); |
| 7 | +} |
| 8 | +function errorHandle(reason) { |
| 9 | + let message; |
| 10 | + if (typeof reason.message === "undefined") { |
| 11 | + message = reason; |
| 12 | + } else { |
| 13 | + message = reason.message; |
| 14 | + if (typeof reason.stack === "undefined") { |
| 15 | + message = `\n${reason.stack}` |
| 16 | + } |
| 17 | + } |
| 18 | + console.error(message); |
| 19 | + return process.exit(1); |
| 20 | +} |
| 21 | +function resultHandle(result) { |
| 22 | + return encodeConvert(JSON.stringify(result), "utf8", "base64"); |
| 23 | +} |
| 24 | +let [wrapperName, inputsRaw, delimiter] = process.argv.slice(2); |
| 25 | +[wrapperName, inputsRaw, delimiter] = [wrapperName, inputsRaw, delimiter].map((value) => { |
| 26 | + return encodeConvert(value, "base64", "utf8"); |
| 27 | +}); |
| 28 | +const inputs = JSON.parse(inputsRaw); |
| 29 | +switch (wrapperName) { |
| 30 | + case "__debug": |
| 31 | + { |
| 32 | + ghactionsDebug(inputs.Message); |
| 33 | + console.log(delimiter); |
| 34 | + console.log(resultHandle({ |
| 35 | + Message: "Hello, world!", |
| 36 | + Message2: "Good day, world!" |
| 37 | + })); |
| 38 | + } |
| 39 | + break; |
| 40 | + case "artifact/download": |
| 41 | + { |
| 42 | + const result = await ghactionsArtifact().downloadArtifact(inputs.Name, inputs.Destination, { createArtifactFolder: inputs.CreateSubfolder }).catch(errorHandle); |
| 43 | + console.log(delimiter); |
| 44 | + console.log(resultHandle({ |
| 45 | + Name: result.artifactName, |
| 46 | + Path: result.downloadPath |
| 47 | + })); |
| 48 | + } |
| 49 | + break; |
| 50 | + case "artifact/download-all": |
| 51 | + { |
| 52 | + const result = await ghactionsArtifact().downloadAllArtifacts(inputs.Destination).catch(errorHandle); |
| 53 | + console.log(delimiter); |
| 54 | + console.log(resultHandle(result.map((value) => { |
| 55 | + return { |
| 56 | + Name: value.artifactName, |
| 57 | + Path: value.downloadPath |
| 58 | + }; |
| 59 | + }))); |
| 60 | + } |
| 61 | + break; |
| 62 | + case "artifact/upload": |
| 63 | + { |
| 64 | + const result = await ghactionsArtifact().uploadArtifact(inputs.Name, inputs.Path, inputs.BaseRoot, { |
| 65 | + continueOnError: inputs.ContinueOnIssue, |
| 66 | + retentionDays: inputs.RetentionTime |
| 67 | + }).catch(errorHandle); |
| 68 | + console.log(delimiter); |
| 69 | + console.log(resultHandle({ |
| 70 | + FailedItem: result.failedItems, |
| 71 | + FailedItems: result.failedItems, |
| 72 | + Item: result.artifactItems, |
| 73 | + Items: result.artifactItems, |
| 74 | + Name: result.artifactName, |
| 75 | + Size: result.size, |
| 76 | + Sizes: result.size |
| 77 | + })); |
| 78 | + } |
| 79 | + break; |
| 80 | + case "cache/restore": |
| 81 | + { |
| 82 | + const result = await ghactionsCacheRestoreCache(inputs.Path, inputs.PrimaryKey, inputs.RestoreKey, { |
| 83 | + downloadConcurrency: inputs.DownloadConcurrency, |
| 84 | + lookupOnly: inputs.LookUp, |
| 85 | + segmentTimeoutInMs: inputs.SegmentTimeout, |
| 86 | + timeoutInMs: inputs.Timeout, |
| 87 | + useAzureSdk: inputs.UseAzureSdk |
| 88 | + }).catch(errorHandle); |
| 89 | + console.log(delimiter); |
| 90 | + console.log(resultHandle({ CacheKey: result ?? null })); |
| 91 | + } |
| 92 | + break; |
| 93 | + case "cache/save": |
| 94 | + { |
| 95 | + const result = await ghactionsCacheSaveCache(inputs.Path, inputs.Key, { |
| 96 | + uploadChunkSize: inputs.UploadChunkSizes, |
| 97 | + uploadConcurrency: inputs.UploadConcurrency |
| 98 | + }).catch(errorHandle); |
| 99 | + console.log(delimiter); |
| 100 | + console.log(resultHandle({ CacheId: result })); |
| 101 | + } |
| 102 | + break; |
| 103 | + case "open-id-connect/get-token": |
| 104 | + { |
| 105 | + const result = await ghactionsGetOpenIDConnectToken(inputs.Audience).catch(errorHandle); |
| 106 | + console.log(delimiter); |
| 107 | + console.log(resultHandle({ Token: result })); |
| 108 | + } |
| 109 | + break; |
| 110 | + case "tool-cache/cache-directory": |
| 111 | + { |
| 112 | + const result = await ghactionsToolCacheCacheDirectory(inputs.Source, inputs.Name, inputs.Version, inputs.Architecture).catch(errorHandle); |
| 113 | + console.log(delimiter); |
| 114 | + console.log(resultHandle({ Path: result })); |
| 115 | + } |
| 116 | + break; |
| 117 | + case "tool-cache/cache-file": |
| 118 | + { |
| 119 | + const result = await ghactionsToolCacheCacheFile(inputs.Source, inputs.Target, inputs.Name, inputs.Version, inputs.Architecture).catch(errorHandle); |
| 120 | + console.log(delimiter); |
| 121 | + console.log(resultHandle({ Path: result })); |
| 122 | + } |
| 123 | + break; |
| 124 | + case "tool-cache/download-tool": |
| 125 | + { |
| 126 | + const result = await ghactionToolCacheDownloadTool(inputs.Uri, inputs.Destination, inputs.Authorization, inputs.Header).catch(errorHandle); |
| 127 | + console.log(delimiter); |
| 128 | + console.log(resultHandle({ Path: result })); |
| 129 | + } |
| 130 | + break; |
| 131 | + case "tool-cache/extract-7z": |
| 132 | + { |
| 133 | + const result = await ghactionToolCacheExtract7z(inputs.File, inputs.Destination, inputs["7zrPath"]).catch(errorHandle); |
| 134 | + console.log(delimiter); |
| 135 | + console.log(resultHandle({ Path: result })); |
| 136 | + } |
| 137 | + break; |
| 138 | + case "tool-cache/extract-tar": |
| 139 | + { |
| 140 | + const result = await ghactionToolCacheExtractTar(inputs.File, inputs.Destination, inputs.Flag).catch(errorHandle); |
| 141 | + console.log(delimiter); |
| 142 | + console.log(resultHandle({ Path: result })); |
| 143 | + } |
| 144 | + break; |
| 145 | + case "tool-cache/extract-xar": |
| 146 | + { |
| 147 | + const result = await ghactionToolCacheExtractXar(inputs.File, inputs.Destination, inputs.Flag).catch(errorHandle); |
| 148 | + console.log(delimiter); |
| 149 | + console.log(resultHandle({ Path: result })); |
| 150 | + } |
| 151 | + break; |
| 152 | + case "tool-cache/extract-zip": |
| 153 | + { |
| 154 | + const result = await ghactionToolCacheExtractZip(inputs.File, inputs.Destination).catch(errorHandle); |
| 155 | + console.log(delimiter); |
| 156 | + console.log(resultHandle({ Path: result })); |
| 157 | + } |
| 158 | + break; |
| 159 | + case "tool-cache/find": |
| 160 | + { |
| 161 | + const result = ghactionsToolCacheFind(inputs.Name, inputs.Version, inputs.Architecture); |
| 162 | + console.log(delimiter); |
| 163 | + console.log(resultHandle({ Path: result })); |
| 164 | + } |
| 165 | + break; |
| 166 | + case "tool-cache/find-all-versions": |
| 167 | + { |
| 168 | + const result = ghactionsToolCacheFindAllVersions(inputs.Name, inputs.Architecture); |
| 169 | + console.log(delimiter); |
| 170 | + console.log(resultHandle({ Paths: result })); |
| 171 | + } |
| 172 | + break; |
| 173 | + default: |
| 174 | + errorHandle(`\`${wrapperName}\` is not a valid NodeJS wrapper name! Most likely a mistake made by the contributors, please report this issue.`); |
| 175 | + break; |
| 176 | +} |
0 commit comments