diff --git a/.eslintrc.yml b/.eslintrc.yml index c8f8211..29f3dc6 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -1,3 +1,8 @@ extends: - '@form8ion' - - '@form8ion/mocha' \ No newline at end of file + - '@form8ion/mocha' + +overrides: + - files: example.js + rules: + import/no-extraneous-dependencies: off diff --git a/example.js b/example.js index f36a8c4..8860902 100644 --- a/example.js +++ b/example.js @@ -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(); })(); diff --git a/package-lock.json b/package-lock.json index b08f9da..8be1e78 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1496,11 +1496,73 @@ "mersenne-twister": "1.1.0" } }, + "@travi/cli-messages": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@travi/cli-messages/-/cli-messages-1.0.4.tgz", + "integrity": "sha512-TV6jXZ3oK5bIPONw/3+lGju1AJCFwWLRY2o4qj1oq0QDYT6mnowEqWdC7psy5saP2t9hK2LNVyeaSESMEqbO3A==", + "requires": { + "chalk": "^4.0.0", + "log-symbols": "4.0.0" + }, + "dependencies": { + "ansi-styles": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.2.1.tgz", + "integrity": "sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA==", + "requires": { + "@types/color-name": "^1.1.1", + "color-convert": "^2.0.1" + } + }, + "chalk": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.0.0.tgz", + "integrity": "sha512-N9oWFcegS0sFr9oh1oz2d7Npos6vNoWW9HvtCg5N1KRFpUhaAhvTv5Y58g880fZaEYSNm3qDz8SU1UrGvp+n7A==", + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + }, + "log-symbols": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.0.0.tgz", + "integrity": "sha512-FN8JBzLx6CzeMrB0tg6pqlGU1wCrXW+ZXGH481kfsBqer0hToTIiHdjH4Mq8xJUbvATujKCvaREGWpGUionraA==", + "requires": { + "chalk": "^4.0.0" + } + }, + "supports-color": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz", + "integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==", + "requires": { + "has-flag": "^4.0.0" + } + } + } + }, "@types/color-name": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz", - "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==", - "dev": true + "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==" }, "@types/mdast": { "version": "3.0.3", @@ -6863,6 +6925,12 @@ } } }, + "mock-fs": { + "version": "4.12.0", + "resolved": "https://registry.npmjs.org/mock-fs/-/mock-fs-4.12.0.tgz", + "integrity": "sha512-/P/HtrlvBxY4o/PzXY9cCNBrdylDNxg7gnrv2sMNxj+UJ2m8jSpl0/A6fuJeNAWr99ZvGWH8XCbE0vmnM5KupQ==", + "dev": true + }, "moment": { "version": "2.23.0", "resolved": "https://registry.npmjs.org/moment/-/moment-2.23.0.tgz", diff --git a/package.json b/package.json index abb1f78..0d3ed9a 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } diff --git a/src/husky-test.js b/src/husky-test.js new file mode 100644 index 0000000..57e984f --- /dev/null +++ b/src/husky-test.js @@ -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`'); + }); +}); diff --git a/src/husky.js b/src/husky.js index da2589a..81944bb 100644 --- a/src/husky.js +++ b/src/husky.js @@ -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`'); }