Commit cbd22269 authored by David Sveningsson's avatar David Sveningsson
Browse files

fix: custom log callback for `compatibilityCheck`

parent 509d98e0
Pipeline #327841782 passed with stages
in 10 minutes and 28 seconds
......@@ -36,6 +36,14 @@ it("should not warn when using supported html-validate version", () => {
expect(log).not.toHaveBeenCalled();
});
it("should use custom callback", () => {
expect.assertions(2);
const spy = jest.fn();
compatibilityCheck("my-plugin", peerDependency, { version: "1.2.3", logger: spy });
expect(spy).toHaveBeenCalledWith(expect.stringContaining("my-plugin requires"));
expect(log).not.toHaveBeenCalled();
});
it("should return true if version is supported", () => {
expect.assertions(1);
const result = compatibilityCheck("my-plugin", peerDependency, {
......
......@@ -8,11 +8,18 @@ export interface CompatibilityOptions {
/** Use this version number instead of running version. Default: running version */
version: string;
/** Use custom logging callback. Default: `console.error` */
logger(message: string): void;
}
const defaults: CompatibilityOptions = {
silent: false,
version,
logger(text: string): void {
/* eslint-disable-next-line no-console */
console.error(kleur.red(text));
},
};
/**
......@@ -28,7 +35,7 @@ export function compatibilityCheck(
declared: string,
options?: Partial<CompatibilityOptions>
): boolean {
const { silent, version: current } = { ...defaults, ...options };
const { silent, version: current, logger } = { ...defaults, ...options };
const valid = satisfies(current, declared);
if (valid || silent) {
return valid;
......@@ -41,8 +48,7 @@ export function compatibilityCheck(
"-----------------------------------------------------------------------------------------------------",
].join("\n");
/* eslint-disable-next-line no-console */
console.error(kleur.red(text));
logger(text);
return false;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment