Skip to content

Commit

Permalink
fix: commitlint hook
Browse files Browse the repository at this point in the history
  • Loading branch information
AVVS committed Jan 26, 2022
1 parent 182835f commit 0b412d0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

"$(dirname "$0")/../node_modules/.bin/commitlint" --edit $1
npm x commitlint -- --edit $1
4 changes: 4 additions & 0 deletions .husky/commit-msg.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

"$(npm x mdep bin commitlint)" --edit $1
17 changes: 15 additions & 2 deletions __tests__/install.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]');
});

describe('(yarn) test installing the package', () => {
const kFilename = 'deploy-yarn.tgz';
const cwd = process.cwd();
Expand Down Expand Up @@ -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']);
});
});

Expand Down Expand Up @@ -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');
});
});

Expand Down
33 changes: 33 additions & 0 deletions bin/cmds/bin.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
4 changes: 2 additions & 2 deletions bin/cmds/get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 10 additions & 3 deletions scripts/setup-semantic-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}`;
}
Expand All @@ -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');
Expand Down

0 comments on commit 0b412d0

Please sign in to comment.