mirror of
https://github.com/github/codeql-action.git
synced 2026-11-10 06:54:40 +00:00
Allow logging functions to accept string[] directly
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
import * as core from "@actions/core";
|
||||
|
||||
/** Types that all of our logging functions accept. */
|
||||
export type Loggable = string | string[];
|
||||
|
||||
/** Some logging functions also accept errors directly. */
|
||||
export type LoggableError = Loggable | Error;
|
||||
|
||||
export interface Logger {
|
||||
debug: (message: string) => void;
|
||||
info: (message: string) => void;
|
||||
warning: (message: string | Error) => void;
|
||||
error: (message: string | Error) => void;
|
||||
debug: (message: Loggable) => void;
|
||||
info: (message: Loggable) => void;
|
||||
warning: (message: LoggableError) => void;
|
||||
error: (message: LoggableError) => void;
|
||||
|
||||
isDebug: () => boolean;
|
||||
|
||||
@@ -12,12 +18,33 @@ export interface Logger {
|
||||
endGroup: () => void;
|
||||
}
|
||||
|
||||
/** If `message` is an array of strings, the array is `join`-ed into one string separated by spaces. */
|
||||
export const joinMessageStrings = <T>(
|
||||
message: string[] | string | T,
|
||||
): string | T => {
|
||||
if (Array.isArray(message)) {
|
||||
return message.join(" ");
|
||||
} else {
|
||||
return message;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Wraps a logging function so that `joinMessageStrings` is applied to the input before
|
||||
* calling the wrapped logging function.
|
||||
*/
|
||||
const autoJoinMessageStrings = <T>(fn: (msg: string | T) => void) => {
|
||||
return (message: string[] | string | T) => {
|
||||
return fn(joinMessageStrings(message));
|
||||
};
|
||||
};
|
||||
|
||||
export function getActionsLogger(): Logger {
|
||||
return {
|
||||
debug: core.debug,
|
||||
info: core.info,
|
||||
warning: core.warning,
|
||||
error: core.error,
|
||||
debug: autoJoinMessageStrings(core.debug),
|
||||
info: autoJoinMessageStrings(core.info),
|
||||
warning: autoJoinMessageStrings(core.warning),
|
||||
error: autoJoinMessageStrings(core.error),
|
||||
isDebug: core.isDebug,
|
||||
startGroup: core.startGroup,
|
||||
endGroup: core.endGroup,
|
||||
|
||||
Reference in New Issue
Block a user