From d5ed9d42ece342a124648ffead446850e26ed925 Mon Sep 17 00:00:00 2001 From: Trevor Brindle Date: Fri, 30 Mar 2018 14:40:40 -0400 Subject: [PATCH] feat: inject versions into friendly errors (#195) --- __tests__/command_helpers/checkCLI.ts | 13 ++++++++++ __tests__/command_helpers/checkRequirement.ts | 10 +++++++ docs/options.md | 26 +++++++++++++++---- src/extensions/functions/checkCLI.ts | 8 +++++- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/__tests__/command_helpers/checkCLI.ts b/__tests__/command_helpers/checkCLI.ts index d8d7c40..0189105 100644 --- a/__tests__/command_helpers/checkCLI.ts +++ b/__tests__/command_helpers/checkCLI.ts @@ -19,6 +19,12 @@ const outOfDateCLI = { semver: '10.99', } +const injectVersion = { + binary: 'node', + semver: '~7.6', + error: "Wanted: '{{wantedVersion}}', Installed: '{{installedVersion}}'. Update with `nvm install {{wantedVersion}}`", +} + const context = require('gluegun/toolbox') test('error on missing binary', async () => { @@ -40,3 +46,10 @@ test('returns message on improper version', async () => { expect(await checkCLI(outOfDateCLI, context)).toBe(message) }) + +test('returns message with injected versions', async () => { + const message = `Wanted: '7.6', Installed: '7.5'. Update with \`nvm install 7.6\`` + solidarityExtension(context) + context.solidarity.getVersion = () => '7.5' + expect(await checkCLI(injectVersion, context)).toBe(message) +}) diff --git a/__tests__/command_helpers/checkRequirement.ts b/__tests__/command_helpers/checkRequirement.ts index 1e885fc..8d4b55e 100644 --- a/__tests__/command_helpers/checkRequirement.ts +++ b/__tests__/command_helpers/checkRequirement.ts @@ -90,6 +90,16 @@ describe('checkRequirement', () => { const result = await checkRequirement(rule, context) expect(result).toEqual([[]]) }) + + test('inject versions', async () => { + checkCLI.mockImplementation(async () => "Wanted: '~1.5.1', Installed '1.3.2'") + + const rule = toPairs({ + YARN: [{ rule: 'cli', binary: 'yarn' }], + })[0] + const result = await checkRequirement(rule, context) + expect(result).toEqual(["Wanted: '~1.5.1', Installed '1.3.2'"]) + }) }) describe('when rule: dir', () => { diff --git a/docs/options.md b/docs/options.md index 82b4c21..1bb0fc7 100644 --- a/docs/options.md +++ b/docs/options.md @@ -99,9 +99,11 @@ Lastly, if output has multiple versions, you can identify the index of the versi ``` ### Friendly Errors -So what do we do if a rule fails? The return code will be non-zero, but that's not the most friendly option. You can set the `error` for any rule to give the user legible instruction on why the failure happened, and how they should solve it. -*e.g.* Prompt them to install the missing CLI +So what do we do if a rule fails? The return code will be non-zero, but that's not the most friendly option. You can set the `error` for any rule to give the user legible instruction on why the failure happened, and how they should solve it. + +_e.g._ Prompt them to install the missing CLI + ```json "Watchman": [ { @@ -112,12 +114,26 @@ So what do we do if a rule fails? The return code will be non-zero, but that's ] ``` +In your error message, you can include `{{wantedVersion}}` and `{{installedVersion}}` to give the user version information, or even to customize scripts to help them install or update. + +```json + "Yarn": [ + { + "rule": "cli", + "binary": "yarn", + "error": "You have yarn@{{installedVersion}}, and need yarn@{{wantedVersion}}. Fix with `npm install -g yarn@{{wantedVersion}}`" + } + ] +``` + ### Platform Specific Rules -Some rules are only essential for a given node platform. You can identify these rules with passing the `"platform"` property on any rule. -A platform property takes a string or and array of strings that identify the platforms that rule pertains to. Platforms can be any of the following: `["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"]` +Some rules are only essential for a given node platform. You can identify these rules with passing the `"platform"` property on any rule. + +A platform property takes a string or and array of strings that identify the platforms that rule pertains to. Platforms can be any of the following: `["darwin", "macos", "freebsd", "linux", "sunos", "win32", "windows"]` + +_e.g._ Rule only performs a check on Mac and Linux -*e.g.* Rule only performs a check on Mac and Linux ```json "Watchman": [ { diff --git a/src/extensions/functions/checkCLI.ts b/src/extensions/functions/checkCLI.ts index 672a067..87def3d 100644 --- a/src/extensions/functions/checkCLI.ts +++ b/src/extensions/functions/checkCLI.ts @@ -26,9 +26,15 @@ module.exports = async (rule: CLIRule, context: SolidarityRunContext): Promise