From 0b412d085b0ee8980c5d70de26507334f8465149 Mon Sep 17 00:00:00 2001 From: AVVS Date: Wed, 26 Jan 2022 13:07:33 -0800 Subject: [PATCH] fix: commitlint hook --- .husky/commit-msg | 2 +- .husky/commit-msg.sample | 4 ++++ __tests__/install.spec.js | 17 ++++++++++++++-- bin/cmds/bin.js | 33 +++++++++++++++++++++++++++++++ bin/cmds/get-config.js | 4 ++-- scripts/setup-semantic-release.js | 13 +++++++++--- 6 files changed, 65 insertions(+), 8 deletions(-) create mode 100755 .husky/commit-msg.sample create mode 100644 bin/cmds/bin.js diff --git a/.husky/commit-msg b/.husky/commit-msg index ddef51f..8eedee4 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,4 +1,4 @@ #!/bin/sh . "$(dirname "$0")/_/husky.sh" -"$(dirname "$0")/../node_modules/.bin/commitlint" --edit $1 +npm x commitlint -- --edit $1 diff --git a/.husky/commit-msg.sample b/.husky/commit-msg.sample new file mode 100755 index 0000000..5e3dc95 --- /dev/null +++ b/.husky/commit-msg.sample @@ -0,0 +1,4 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +"$(npm x mdep bin commitlint)" --edit $1 diff --git a/__tests__/install.spec.js b/__tests__/install.spec.js index 1dbd7b2..7929364 100644 --- a/__tests__/install.spec.js +++ b/__tests__/install.spec.js @@ -7,6 +7,12 @@ const fs = require('fs/promises'); const debug = require('debug')('test'); const execa = require('execa'); +// setup git so that we can try commits +beforeAll(async () => { + await execa.command('git config --global user.name deploy-tests'); + await execa.command('git config --global user.email ci@github.com'); +}); + describe('(yarn) test installing the package', () => { const kFilename = 'deploy-yarn.tgz'; const cwd = process.cwd(); @@ -120,7 +126,14 @@ describe('(yarn) test installing the package', () => { test('.husky files content sane', async () => { const contents = await fs.readFile('.husky/commit-msg', { encoding: 'utf8' }); - expect(contents).toContain('npx --no-install commitlint -e $1'); + expect(contents).toContain('"$(npm x mdep bin commitlint)" --edit $1'); + }); + + test('able to commit with hooks installed', async () => { + await fs.writeFile('.gitignore', 'node_modules\n'); + await execa('git', ['add', '.']); + await expect(execa('git', ['commit', '-m', 'wrong message'])).rejects.toThrow('subject may not be empty'); + await execa('git', ['commit', '-m', 'chore(test): correct message']); }); }); @@ -267,7 +280,7 @@ describe('(pnpm) test installing the package', () => { test('.husky files content sane', async () => { const contents = await fs.readFile('.husky/commit-msg', { encoding: 'utf8' }); - expect(contents).toContain('npx --no-install commitlint -e $1'); + expect(contents).toContain('"$(npm x mdep bin commitlint)" --edit $1'); }); }); diff --git a/bin/cmds/bin.js b/bin/cmds/bin.js new file mode 100644 index 0000000..37b43d5 --- /dev/null +++ b/bin/cmds/bin.js @@ -0,0 +1,33 @@ +const execa = require('execa'); +const fs = require('fs/promises'); +const { resolve } = require('path'); + +exports.command = 'bin [binary]'; +exports.desc = 'return absolute path to bundled bin'; +exports.builder = (yargs) => { + return yargs + .positional('binary', { + describe: 'path to binary dep', + type: 'string', + }) + .strict(false) + .help(); +}; +exports.handler = async (argv) => { + const bin = (await execa('npm', ['bin'], { cwd: resolve(__dirname, '../..') })).stdout; + const path = resolve(bin, argv.binary); + + try { + if ((await fs.stat(path)).isFile()) { + process.stdout.write(`${path}\n`); + } else { + // eslint-disable-next-line no-console + console.error('path %s isnt a file', path); + process.exit(128); + } + } catch (e) { + // eslint-disable-next-line no-console + console.error('can\'t locate path %s - %o', path, e); + process.exit(128); + } +}; diff --git a/bin/cmds/get-config.js b/bin/cmds/get-config.js index ae25580..c43b745 100644 --- a/bin/cmds/get-config.js +++ b/bin/cmds/get-config.js @@ -2,8 +2,8 @@ const get = require('lodash.get'); exports.command = 'get-config [path]'; exports.desc = 'return mdeprc properties'; -exports.builder = async (yargs) => { - yargs +exports.builder = (yargs) => { + return yargs .positional('path', { describe: 'path in config', type: 'string', diff --git a/scripts/setup-semantic-release.js b/scripts/setup-semantic-release.js index cba48de..fa8aaae 100644 --- a/scripts/setup-semantic-release.js +++ b/scripts/setup-semantic-release.js @@ -69,10 +69,10 @@ async function isInstallingGlobally() { return globalDirs.includes(rootDir()); } -async function copyConfiguration(filename, _fallback = []) { +async function copyConfiguration(filename, _fallback = [], renameTo = filename) { const names = Array.isArray(_fallback) ? [filename].concat(_fallback) : [filename]; const prefix = rootDir(); - const rcpath = path.join(prefix, filename); + const rcpath = path.join(prefix, renameTo); for (const name of names) { try { @@ -181,6 +181,13 @@ async function main() { continue; } + if (key === 'commit-msg' + && script === 'commitlint -e $HUSKY_GIT_PARAMS') { + delete pkg.husky.hooks[key]; + // eslint-disable-next-line no-continue + continue; + } + if (!/^(yarn|np[mx]) /.test(script)) { script = `npx --no-install ${script}`; } @@ -197,7 +204,7 @@ async function main() { console.log(stdout); } - await copyConfiguration('.husky/commit-msg'); + await copyConfiguration('.husky/commit-msg.sample', [], '.husky/commit-msg'); await copyConfiguration('.husky/prepare-commit-msg'); console.log('⚠️ Use "semantic-release-cli setup" to complete setting up semantic-release');