-
Notifications
You must be signed in to change notification settings - Fork 19
/
core.ts
85 lines (75 loc) · 2.3 KB
/
core.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
import os from 'node:os';
/**
* This file is a lightweight re-implementation of the functions
* in `@actions/core`.
*
* @see {@link https://github.com/actions/toolkit/tree/main/packages/core}
* @see {@link https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions}
*/
// ignoring this file since it's mainly all `@actions/core`
/* v8 ignore start */
/**
* Sanitizes an input into a string so it can be passed into issueCommand safely
* @param input input to sanitize into a string
*/
function toCommandValue(input: unknown): string {
if (input === null || input === undefined) {
return '';
} else if (typeof input === 'string' || input instanceof String) {
return input as string;
}
return JSON.stringify(input);
}
function escapeData(s: unknown): string {
return toCommandValue(s).replace(/%/g, '%25').replace(/\r/g, '%0D').replace(/\n/g, '%0A');
}
/**
* Commands
*
* Command Format:
* ::name key=value,key=value::message
*
* Examples:
* ::debug::This is the debug message
* ::error::This is the error message
*/
function issueCommand(command: 'debug' | 'error', message: unknown): void {
const cmd = `::${command}::${escapeData(message)}`;
process.stdout.write(cmd.toString() + os.EOL);
}
/**
* Writes debug message to user log
* @param message debug message
*/
export function debug(message: string): void {
issueCommand('debug', message);
}
/**
* Gets the value of an input.
* Returns an empty string if the value is not defined.
*
* @param name name of the input to get
*/
export function getInput(name: string, options: { required?: boolean } = {}): string {
const val: string = process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || '';
if (options.required && !val) {
throw new Error(`Input required and not supplied: ${name}`);
}
return val.trim();
}
/**
* Writes info to log with console.log.
* @param message info message
*/
export function info(message: string): void {
process.stdout.write(message + os.EOL);
}
/**
* Sets the action status to failed.
* When the action exits it will be with an exit code of 1
* @param message add error issue message
*/
export function setFailed(message: Error | string): void {
process.exitCode = 1;
issueCommand('error', message instanceof Error ? message.toString() : message);
}