diff options
author | 2024-11-21 12:02:21 +0000 | |
---|---|---|
committer | 2024-11-21 12:02:21 +0000 | |
commit | 285c6e3598eac81632057a37ad74459cc65c357d (patch) | |
tree | 392acb06a54080f20422ac89c290b245ead7875f /.github/scripts/utils.mjs | |
parent | 8309c61f0dfa5991d3f6c5c5fca4403794d6fda2 (diff) | |
parent | b9c05f45b2c9780d7b5518adb4a545cf69f075a6 (diff) | |
download | astro-285c6e3598eac81632057a37ad74459cc65c357d.tar.gz astro-285c6e3598eac81632057a37ad74459cc65c357d.tar.zst astro-285c6e3598eac81632057a37ad74459cc65c357d.zip |
chore: merge fixes
Diffstat (limited to '.github/scripts/utils.mjs')
-rw-r--r-- | .github/scripts/utils.mjs | 86 |
1 files changed, 40 insertions, 46 deletions
diff --git a/.github/scripts/utils.mjs b/.github/scripts/utils.mjs index da5befc2c..768302230 100644 --- a/.github/scripts/utils.mjs +++ b/.github/scripts/utils.mjs @@ -1,59 +1,53 @@ -import * as fs from 'node:fs' -import * as os from 'node:os' -import * as crypto from 'node:crypto' +import * as crypto from 'node:crypto'; +import * as fs from 'node:fs'; +import * as os from 'node:os'; /** Based on https://github.com/actions/toolkit/blob/4e3b068ce116d28cb840033c02f912100b4592b0/packages/core/src/file-command.ts */ export function setOutput(key, value) { - const filePath = process.env['GITHUB_OUTPUT'] || '' - if (filePath) { - return issueFileCommand('OUTPUT', prepareKeyValueMessage(key, value)) - } - process.stdout.write(os.EOL) + const filePath = process.env['GITHUB_OUTPUT'] || ''; + if (filePath) { + return issueFileCommand('OUTPUT', prepareKeyValueMessage(key, value)); + } + process.stdout.write(os.EOL); } function issueFileCommand(command, message) { - const filePath = process.env[`GITHUB_${command}`] - if (!filePath) { - throw new Error( - `Unable to find environment variable for file command ${command}` - ) - } - if (!fs.existsSync(filePath)) { - throw new Error(`Missing file at path: ${filePath}`) - } - - fs.appendFileSync(filePath, `${toCommandValue(message)}${os.EOL}`, { - encoding: 'utf8' - }) + const filePath = process.env[`GITHUB_${command}`]; + if (!filePath) { + throw new Error(`Unable to find environment variable for file command ${command}`); + } + if (!fs.existsSync(filePath)) { + throw new Error(`Missing file at path: ${filePath}`); + } + + fs.appendFileSync(filePath, `${toCommandValue(message)}${os.EOL}`, { + encoding: 'utf8', + }); } function prepareKeyValueMessage(key, value) { - const delimiter = `gh-delimiter-${crypto.randomUUID()}` - const convertedValue = toCommandValue(value) - - // These should realistically never happen, but just in case someone finds a - // way to exploit uuid generation let's not allow keys or values that contain - // the delimiter. - if (key.includes(delimiter)) { - throw new Error( - `Unexpected input: name should not contain the delimiter "${delimiter}"` - ) - } - - if (convertedValue.includes(delimiter)) { - throw new Error( - `Unexpected input: value should not contain the delimiter "${delimiter}"` - ) - } - - return `${key}<<${delimiter}${os.EOL}${convertedValue}${os.EOL}${delimiter}` + const delimiter = `gh-delimiter-${crypto.randomUUID()}`; + const convertedValue = toCommandValue(value); + + // These should realistically never happen, but just in case someone finds a + // way to exploit uuid generation let's not allow keys or values that contain + // the delimiter. + if (key.includes(delimiter)) { + throw new Error(`Unexpected input: name should not contain the delimiter "${delimiter}"`); + } + + if (convertedValue.includes(delimiter)) { + throw new Error(`Unexpected input: value should not contain the delimiter "${delimiter}"`); + } + + return `${key}<<${delimiter}${os.EOL}${convertedValue}${os.EOL}${delimiter}`; } function toCommandValue(input) { - if (input === null || input === undefined) { - return '' - } else if (typeof input === 'string' || input instanceof String) { - return input - } - return JSON.stringify(input) + if (input === null || input === undefined) { + return ''; + } else if (typeof input === 'string' || input instanceof String) { + return input; + } + return JSON.stringify(input); } |