Some scripts to remove husky hooks and convert them to chowchow hooks
./convert-githooks.sh && ./remove-hooks.sh
#! /usr/bin/env node | |
const fs = require('fs') | |
const oldFilename = '.githooks.yml' | |
const newFilename = '.chowchow.yml' | |
const otherReplacements = [ | |
['.githooks.yml', '.chowchow.yml'] | |
] | |
const hooks = [ | |
"applypatch-msg:", | |
"pre-applypatch:", | |
"post-applypatch:", | |
"pre-commit:", | |
"prepare-commit-msg:", | |
"commit-msg:", | |
"post-commit:", | |
"pre-rebase:", | |
"post-checkout:", | |
"post-merge:", | |
"pre-push:", | |
"pre-receive:", | |
"update:", | |
"post-receive:", | |
"post-update:", | |
"push-to-checkout:", | |
"pre-auto-gc:", | |
"post-rewrite:", | |
"sendemail-validate:", | |
"fsmonitor-watchman:" | |
] | |
const replacements = [ | |
...hooks.map( name => [ name.replace(/-/g, ''), name ] ), | |
[ /\.githooks\.yml/g, '.chowchow.yml' ] | |
] | |
try { | |
fs.statSync(oldFilename) | |
} catch (e) { | |
// probably no file to handle | |
process.exit(0) | |
} | |
const oldFileContents = fs.readFileSync(oldFilename, {encoding:'utf8'}) | |
const newFileContents = replacements.reduce( (contents, [old, replacement]) => contents.replace(old, replacement), oldFileContents ) | |
fs.writeFileSync(newFilename, newFileContents, {encoding: 'utf8'}) | |
fs.unlinkSync(oldFilename) |
#! /usr/bin/env node | |
const fs = require('fs') | |
const child_process = require('child_process') | |
const findHooksDir = () => { | |
try { | |
const hooksDir = child_process | |
.execSync('git rev-parse --git-dir', {timeout: 5000, encoding: 'utf8'}) | |
.trim('\n') | |
.concat('/hooks') | |
if( !fs.statSync(hooksDir).isDirectory()) { | |
return undefined | |
} | |
return hooksDir | |
} catch (e) { | |
// error handling one day... | |
return undefined | |
} | |
} | |
const installedHooks = hooksDir => fs.readdirSync(hooksDir, {encoding: 'utf8'}).filter(f => !f.match(/\.sample$/)) | |
const chowchowSig = '# chowchow hook 414715c1f35236b9d2789a653d3d0d3c' | |
const isNotChowChowHook = fpath => !fs.readFileSync(fpath, {encoding: 'utf8'}) | |
.split('\n') | |
.some(l => l.match(`^${chowchowSig}$`)) | |
const deleteHook = fpath => fs.unlinkSync(fpath) | |
const hooksDir = findHooksDir() | |
if(hooksDir) { | |
installedHooks(hooksDir).map( hookname => `${hooksDir}/${hookname}`).filter(isNotChowChowHook).forEach(deleteHook) | |
} |