2024-11-18 08:35:34 -05:00
|
|
|
import * as crypto from 'node:crypto';
|
2024-11-18 08:34:43 -05:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import * as os from 'node:os';
|
2024-01-23 16:28:28 -05:00
|
|
|
|
|
|
|
/** Based on https://github.com/actions/toolkit/blob/4e3b068ce116d28cb840033c02f912100b4592b0/packages/core/src/file-command.ts */
|
|
|
|
export function setOutput(key, value) {
|
2024-11-18 08:34:43 -05:00
|
|
|
const filePath = process.env['GITHUB_OUTPUT'] || '';
|
|
|
|
if (filePath) {
|
|
|
|
return issueFileCommand('OUTPUT', prepareKeyValueMessage(key, value));
|
|
|
|
}
|
|
|
|
process.stdout.write(os.EOL);
|
2024-01-23 16:28:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function issueFileCommand(command, message) {
|
2024-11-18 08:34:43 -05:00
|
|
|
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',
|
|
|
|
});
|
2024-01-23 16:28:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function prepareKeyValueMessage(key, value) {
|
2024-11-18 08:34:43 -05:00
|
|
|
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}`;
|
2024-01-23 16:28:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function toCommandValue(input) {
|
2024-11-18 08:34:43 -05:00
|
|
|
if (input === null || input === undefined) {
|
|
|
|
return '';
|
|
|
|
} else if (typeof input === 'string' || input instanceof String) {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
return JSON.stringify(input);
|
2024-01-23 16:28:28 -05:00
|
|
|
}
|