From 21d272cdafd105f2a3e45b9d54a560132c76c118 Mon Sep 17 00:00:00 2001 From: Matt Travi Date: Wed, 3 Aug 2022 23:47:35 -0500 Subject: [PATCH] fix(legacy): prevented error when attempting to create hook when directory does not exist when the project still uses a legacy version of husky --- src/hook-creator-test.js | 19 ++++++++++++++++--- src/hook-creator.js | 13 ++++++++----- test/integration/features/lift.feature | 1 + 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/hook-creator-test.js b/src/hook-creator-test.js index 3238af10..f85eb659 100644 --- a/src/hook-creator-test.js +++ b/src/hook-creator-test.js @@ -1,24 +1,29 @@ import {promises as fs} from 'fs'; +import * as core from '@form8ion/core'; + import sinon from 'sinon'; import {assert} from 'chai'; import any from '@travi/any'; + import createHook from './hook-creator'; suite('hook creator', () => { let sandbox; + const configDirectory = any.string(); + const hookName = any.word(); + const script = any.string(); setup(() => { sandbox = sinon.createSandbox(); sandbox.stub(fs, 'writeFile'); + sandbox.stub(core, 'directoryExists'); }); teardown(() => sandbox.restore()); test('that a hook file is created', async () => { - const configDirectory = any.string(); - const hookName = any.word(); - const script = any.string(); + core.directoryExists.withArgs(configDirectory).resolves(true); await createHook({configDirectory, hookName, script}); @@ -32,4 +37,12 @@ ${script}`, {mode: 0o755} ); }); + + test('that the hook file is not created if the config directory does not exist', async () => { + core.directoryExists.resolves(false); + + await createHook({configDirectory, hookName, script}); + + assert.notCalled(fs.writeFile); + }); }); diff --git a/src/hook-creator.js b/src/hook-creator.js index b3c978e1..a7adeec0 100644 --- a/src/hook-creator.js +++ b/src/hook-creator.js @@ -1,12 +1,15 @@ import {promises as fs} from 'fs'; +import {directoryExists} from '@form8ion/core'; export default async function ({configDirectory, hookName, script}) { - await fs.writeFile( - `${configDirectory}/${hookName}`, - `#!/bin/sh + if (await directoryExists(configDirectory)) { + await fs.writeFile( + `${configDirectory}/${hookName}`, + `#!/bin/sh . "$(dirname "$0")/_/husky.sh" ${script}`, - {mode: 0o755} - ); + {mode: 0o755} + ); + } } diff --git a/test/integration/features/lift.feature b/test/integration/features/lift.feature index 2106f765..5b046f0d 100644 --- a/test/integration/features/lift.feature +++ b/test/integration/features/lift.feature @@ -26,6 +26,7 @@ Feature: Husky Scenario: Husky v4 installed, v4 config Given husky v4 is installed + And commitlint is configured for the project And husky config is in v4 format When the husky details are lifted Then the next-steps do not include a warning about the husky config