-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.ts
215 lines (215 loc) · 6.49 KB
/
log.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { isStringSingleLine } from "https://raw.githubusercontent.com/hugoalh/is-string-singleline-es/v1.0.4/mod.ts";
import {
disableProcessStdOutCommand,
enableProcessStdOutCommand,
GitHubActionsStdOutCommand
} from "./command/stdout.ts";
/**
* Make secret get masked from the log.
* @param {...string} values Secret that need to get masked from the log.
* @returns {void}
*/
export function addSecretMask(...values: string[]): void {
values.forEach((value: string): void => {
if (value.length > 0) {
new GitHubActionsStdOutCommand("add-mask", value).dispatch();
}
});
}
export {
addSecretMask as addMask,
addSecretMask as addSecret
};
/**
* Create a foldable group in the log; Anything write to the log are inside this foldable group in the log.
* @param {string} [title=""] Title of the foldable group.
* @returns {void}
*/
export function enterLogGroup(title: string = ""): void {
if (!isStringSingleLine(title)) {
throw new SyntaxError(`\`${title}\` (parameter \`title\`) is not a string which is single line!`);
}
new GitHubActionsStdOutCommand("group", title).dispatch();
}
export {
enterLogGroup as startLogGroup
};
const commandLogGroupExit = new GitHubActionsStdOutCommand("endgroup");
/**
* End an foldable group in the log.
* @returns {void}
*/
export function exitLogGroup(): void {
commandLogGroupExit.dispatch();
}
export {
exitLogGroup as endLogGroup
};
/**
* GitHub Actions annotation type.
*/
export type GitHubActionsAnnotationType =
| "error"
| "notice"
| "warning";
const annotationTypes: Readonly<Record<string, GitHubActionsAnnotationType>> = {
error: "error",
Error: "error",
note: "notice",
Note: "notice",
notice: "notice",
Notice: "notice",
warn: "warning",
Warn: "warning",
warning: "warning",
Warning: "warning"
};
/**
* GitHub Actions annotation properties.
*/
export interface GitHubActionsAnnotationProperties {
/**
* Path of the issue file of the annotation.
*/
file?: string;
/**
* Line start of the issue file of the annotation.
*/
line?: number;
/**
* Column start of the issue file of the annotation.
*/
column?: number;
/**
* Line end of the issue file of the annotation.
*/
lineEnd?: number;
/**
* Column end of the issue file of the annotation.
*/
columnEnd?: number;
/**
* Title of the annotation.
*/
title?: string;
/**
* Summary of the annotation when the message is too large to display.
*/
summary?: string;
}
/**
* Print an annotation to the log.
* @param {GitHubActionsAnnotationType} type Type of the annotation.
* @param {string} data Data of the annotation.
* @param {GitHubActionsAnnotationProperties} [properties={}] Properties of the annotation.
* @returns {void}
*/
export function writeAnnotation(type: GitHubActionsAnnotationType, data: string, properties: GitHubActionsAnnotationProperties = {}): void {
const typeFmt: GitHubActionsAnnotationType | undefined = annotationTypes[type];
if (typeof typeFmt === "undefined") {
throw new RangeError(`\`${type}\` is not a valid GitHub Actions annotation type! Only accept these values: ${Object.keys(annotationTypes).sort().join(", ")}`);
}
const {
column,
columnEnd,
file,
line,
lineEnd,
summary,
title
}: GitHubActionsAnnotationProperties = properties;
const propertiesFmt: Map<string, string> = new Map<string, string>();
if (typeof file === "string" && file.length > 0) {
propertiesFmt.set("file", file);
}
if (typeof line === "number") {
if (!(Number.isSafeInteger(line) && line >= 0)) {
throw new RangeError(`\`${line}\` (parameter \`properties.line\`) is not a number which is integer, positive, and safe!`);
}
if (line > 0) {
propertiesFmt.set("line", line.toString());
}
}
if (typeof column === "number") {
if (!(Number.isSafeInteger(column) && column >= 0)) {
throw new RangeError(`\`${column}\` (parameter \`properties.column\`) is not a number which is integer, positive, and safe!`);
}
if (column > 0) {
propertiesFmt.set("col", column.toString());
}
}
if (typeof lineEnd === "number") {
if (!(Number.isSafeInteger(lineEnd) && lineEnd >= 0)) {
throw new RangeError(`\`${lineEnd}\` (parameter \`properties.lineEnd\`) is not a number which is integer, positive, and safe!`);
}
if (lineEnd > 0) {
propertiesFmt.set("endLine", lineEnd.toString());
}
}
if (typeof columnEnd === "number") {
if (!(Number.isSafeInteger(columnEnd) && columnEnd >= 0)) {
throw new RangeError(`\`${columnEnd}\` (parameter \`properties.columnEnd\`) is not a number which is integer, positive, and safe!`);
}
if (columnEnd > 0) {
propertiesFmt.set("endColumn", columnEnd.toString());
}
}
if (typeof title === "string" && title.length > 0) {
propertiesFmt.set("title", title);
}
if (data.length > 4096 && typeof summary === "string" && summary.length > 0) {
if (data.trim().startsWith("::")) {
const endToken: string = disableProcessStdOutCommand();
console.log(data);
enableProcessStdOutCommand(endToken);
} else {
console.log(data);
}
new GitHubActionsStdOutCommand(typeFmt, propertiesFmt, summary).dispatch();
} else {
new GitHubActionsStdOutCommand(typeFmt, propertiesFmt, data).dispatch();
}
}
/**
* Print a debug message to the log.
* @param {...string} data Data that need to log at debug level.
* @returns {void}
*/
export function writeDebug(...data: string[]): void {
for (const item of data) {
new GitHubActionsStdOutCommand("debug", item).dispatch();
}
}
/**
* Print an error annotation to the log.
* @param {string} data Data of the annotation.
* @param {GitHubActionsAnnotationProperties} [properties={}] Properties of the annotation.
* @returns {void}
*/
export function writeError(data: string, properties: GitHubActionsAnnotationProperties = {}): void {
return writeAnnotation("error", data, properties);
}
/**
* Print a notice annotation to the log.
* @param {string} data Data of the annotation.
* @param {GitHubActionsAnnotationProperties} [properties={}] Properties of the annotation.
* @returns {void}
*/
export function writeNotice(data: string, properties: GitHubActionsAnnotationProperties = {}): void {
return writeAnnotation("notice", data, properties);
}
export {
writeNotice as writeNote
};
/**
* Print a warning annotation to the log.
* @param {string} data Data of the annotation.
* @param {GitHubActionsAnnotationProperties} [properties={}] Properties of the annotation.
* @returns {void}
*/
export function writeWarning(data: string, properties: GitHubActionsAnnotationProperties = {}): void {
return writeAnnotation("warning", data, properties);
}
export {
writeWarning as writeWarn
};