Skip to content

Commit

Permalink
feat(husky): warned about usage of legacy hook definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
travi committed May 13, 2020
1 parent 4952f7c commit 055045b
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 4 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
extends:
- '@form8ion'
- '@form8ion/mocha'
- '@form8ion/mocha'

overrides:
- files: example.js
rules:
import/no-extraneous-dependencies: off
8 changes: 8 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// #### Import
// remark-usage-ignore-next
import stubbedFs from 'mock-fs';
import {patrol} from './lib/index.cjs';

// remark-usage-ignore-next
stubbedFs();

(async () => {
await patrol();
// remark-usage-ignore-next
stubbedFs.restore();
})();
72 changes: 70 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"husky": "4.2.5",
"lockfile-lint": "4.2.2",
"mocha": "7.1.2",
"mock-fs": "^4.12.0",
"npm-run-all": "4.1.5",
"nyc": "15.0.1",
"remark-cli": "8.0.0",
Expand All @@ -63,5 +64,8 @@
"rollup-plugin-auto-external": "2.0.0",
"sinon": "9.0.2",
"travis-lint": "1.0.0"
},
"dependencies": {
"@travi/cli-messages": "^1.0.4"
}
}
60 changes: 60 additions & 0 deletions src/husky-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {promises as fs} from 'fs';
import * as cliMessages from '@travi/cli-messages';
import sinon from 'sinon';
import {assert} from 'chai';
import any from '@travi/any';
import husky from './husky';

suite('husky', () => {
let sandbox;

setup(() => {
sandbox = sinon.createSandbox();

sandbox.stub(fs, 'readFile');
sandbox.stub(cliMessages, 'warn');
});

teardown(() => sandbox.restore());

test('that no problem is reported when the legacy hooks are not used', async () => {
const projectRoot = any.string();
fs.readFile
.withArgs(`${projectRoot}/package.json`, 'utf8')
.resolves(JSON.stringify({scripts: any.simpleObject()}));

await husky(projectRoot);

assert.notCalled(cliMessages.warn);
});

test('that a warning is provided if a legacy `precommit` hook is defined', async () => {
const projectRoot = any.string();
fs.readFile
.withArgs(`${projectRoot}/package.json`, 'utf8')
.resolves(JSON.stringify({scripts: {precommit: any.sentence()}}));

await husky(projectRoot);

assert.calledWith(cliMessages.warn, '`precommit` npm script found. This approach has been deprecated by `husky`');
assert.neverCalledWith(
cliMessages.warn,
'`commitmsg` npm script found. This approach has been deprecated by `husky`'
);
});

test('that a warning is provided if a legacy `commitmsg` hook is defined', async () => {
const projectRoot = any.string();
fs.readFile
.withArgs(`${projectRoot}/package.json`, 'utf8')
.resolves(JSON.stringify({scripts: {commitmsg: any.sentence()}}));

await husky(projectRoot);

assert.neverCalledWith(
cliMessages.warn,
'`precommit` npm script found. This approach has been deprecated by `husky`'
);
assert.calledWith(cliMessages.warn, '`commitmsg` npm script found. This approach has been deprecated by `husky`');
});
});
8 changes: 7 additions & 1 deletion src/husky.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export default function () {
import {promises as fs} from 'fs';
import {warn} from '@travi/cli-messages';

export default async function (projectRoot) {
const {scripts} = JSON.parse(await fs.readFile(`${projectRoot}/package.json`, 'utf8'));

if (scripts.precommit) warn('`precommit` npm script found. This approach has been deprecated by `husky`');
if (scripts.commitmsg) warn('`commitmsg` npm script found. This approach has been deprecated by `husky`');
}

0 comments on commit 055045b

Please sign in to comment.