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

feat: add `compatibilityCheck` helper for plugins

parent 330552f2
Pipeline #327743708 passed with stages
in 9 minutes and 52 seconds
......@@ -329,3 +329,29 @@ The resulting metadata will now be:
"bar": "original"
}
```
### Version compatibility
- Since: %version%
Plugins can use the `compatibilityCheck` helper to verify the library version is compatible.
```typescript
import { compatibilityCheck } from "html-validate";
const pkg = require("./package.json");
const range = pkg.peerDependencies["html-validate"];
compatibilityCheck(pkg.name, range);
```
The helper will write a friendly notice on console if the version is not supported.
If you want the error to be fatal you it returns `false` if the version is not supported.
Additionally you can pass the `silent` option if you want to disable output`
```typescript
if (!compatibilityCheck(pkg.name, range, { silent: true })) {
/* handle incompatible version */
}
```
This diff is collapsed.
......@@ -139,7 +139,8 @@
"json-merge-patch": "^1.0.0",
"kleur": "^4.1.0",
"minimist": "^1.2.0",
"prompts": "^2.0.0"
"prompts": "^2.0.0",
"semver": "^7.0.0"
},
"devDependencies": {
"@babel/core": "7.14.6",
......@@ -167,6 +168,7 @@
"@types/minimist": "1.2.1",
"@types/node": "11.15.54",
"@types/prompts": "2.0.13",
"@types/semver": "7.3.6",
"autoprefixer": "10.2.6",
"babar": "0.2.0",
"babelify": "10.0.0",
......
......@@ -3,3 +3,4 @@
export * from "./browser";
export { Formatter, getFormatter as formatterFactory } from "./formatters";
export { CLI } from "./cli/cli";
export { compatibilityCheck, CompatibilityOptions } from "./utils";
import kleur from "kleur";
import { compatibilityCheck } from "./compatibility-check";
kleur.enabled = false;
const log = jest.spyOn(console, "error");
const peerDependency = ">= 2.1 || ^3.0";
beforeEach(() => {
log.mockReset();
});
it("should warn when using unsupported html-validate version", () => {
expect.assertions(2);
compatibilityCheck("my-plugin", peerDependency, { version: "1.2.3" });
expect(log).toHaveBeenCalled();
expect(log.mock.calls[0][0]).toMatchInlineSnapshot(`
"-----------------------------------------------------------------------------------------------------
my-plugin requires html-validate version \\">= 2.1 || ^3.0\\" but current installed version is 1.2.3
This is not a supported configuration. Please install a supported version before reporting bugs.
-----------------------------------------------------------------------------------------------------"
`);
});
it("should not warn when using silent option", () => {
expect.assertions(1);
compatibilityCheck("my-plugin", peerDependency, { version: "1.2.3", silent: true });
expect(log).not.toHaveBeenCalled();
});
it("should not warn when using supported html-validate version", () => {
expect.assertions(1);
compatibilityCheck("my-plugin", peerDependency, { version: "2.1.0" });
compatibilityCheck("my-plugin", peerDependency, { version: "2.2.0" });
compatibilityCheck("my-plugin", peerDependency, { version: "3.0.0" });
expect(log).not.toHaveBeenCalled();
});
it("should return true if version is supported", () => {
expect.assertions(1);
const result = compatibilityCheck("my-plugin", peerDependency, {
version: "2.1.0",
silent: true,
});
expect(result).toBeTruthy();
});
it("should return false if version is not supported", () => {
expect.assertions(1);
const result = compatibilityCheck("my-plugin", peerDependency, {
version: "2.0.0",
silent: true,
});
expect(result).toBeFalsy();
});
import satisfies from "semver/functions/satisfies";
import kleur from "kleur";
import { version } from "../package";
export interface CompatibilityOptions {
/** If `true` nothing no output will be generated on console. Default: `false` */
silent: boolean;
/** Use this version number instead of running version. Default: running version */
version: string;
}
const defaults: CompatibilityOptions = {
silent: false,
version,
};
/**
* Tests if plugin is compatible with html-validate library. Unless the `silent`
* option is used a warning is displayed on the console.
*
* @param name - Name of plugin
* @param declared - What library versions the plugin support (e.g. declared peerDependencies)
* @returns - `true` if version is compatible
*/
export function compatibilityCheck(
name: string,
declared: string,
options?: Partial<CompatibilityOptions>
): boolean {
const { silent, version: current } = { ...defaults, ...options };
const valid = satisfies(current, declared);
if (valid || silent) {
return valid;
}
const text = [
"-----------------------------------------------------------------------------------------------------",
`${name} requires html-validate version "${declared}" but current installed version is ${current}`,
"This is not a supported configuration. Please install a supported version before reporting bugs.",
"-----------------------------------------------------------------------------------------------------",
].join("\n");
/* eslint-disable-next-line no-console */
console.error(kleur.red(text));
return false;
}
export { compatibilityCheck, CompatibilityOptions } from "./compatibility-check";
export { requireUncached } from "./require-uncached";
export { ruleExists } from "./rule-exists";
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