Skip to content

Commit

Permalink
fix: always run hooks as CommonJS
Browse files Browse the repository at this point in the history
Add a `package.json` to `…/.git/hooks/` to force hooks to run as CommonJS, not
modules.

Fixes ghooks-org#394.
  • Loading branch information
brady-ds committed Apr 27, 2023
1 parent 569eb3c commit f5d096f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const fs = require('fs')
const resolve = require('path').resolve
const findup = require('findup')
const template = require('./hook.template')
const packageTemplate = require('./package.template')
const hookTemplate = require('./hook.template')

const JSON_INDENTATION = 2

const hooks = [
'applypatch-msg',
Expand All @@ -27,12 +30,17 @@ function installHooks() {
const gitRoot = findGitRoot()
if (gitRoot) {
const hooksDir = resolve(gitRoot, 'hooks')
hooks.forEach(install.bind(null, hooksDir))
installHooksIntoDir(hooksDir)
} else {
warnAboutGit()
}
}

function installHooksIntoDir(dir) {
installPackageJSON(dir)
hooks.forEach(install.bind(null, dir))
}

function findGitRoot() {
try {
return getGitRoot()
Expand Down Expand Up @@ -74,6 +82,13 @@ function warnAboutGit() {
)
}

function installPackageJSON(dir) {
ensureDirExists(dir)
const file = resolve(dir, 'package.json')
needsBackup(file) && backup(file)
fs.writeFileSync(file, JSON.stringify(packageTemplate.content, null, JSON_INDENTATION))
}

function install(dir, hook) {
ensureDirExists(dir)
const file = resolve(dir, hook)
Expand All @@ -90,15 +105,15 @@ function needsBackup(file) {
}

function generatedByGHooks(file) {
return !!fs.readFileSync(file, 'UTF-8').match(template.generatedMessage)
return !!fs.readFileSync(file, 'UTF-8').match(hookTemplate.generatedMessage)
}

function backup(file) {
fs.renameSync(file, `${file}.bkp`)
}

function createExecutableHook(file) {
fs.writeFileSync(file, template.content)
fs.writeFileSync(file, hookTemplate.content)
fs.chmodSync(file, '755')
}

Expand Down
7 changes: 7 additions & 0 deletions lib/package.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.generatedMessage = 'Generated by ghooks. Do not edit this file.'
exports.content = {
type: 'commonjs',
"//": [
exports.generatedMessage
]
}
42 changes: 42 additions & 0 deletions test/install.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ describe('install', function describeInstall() {
expect(fs.existsSync('.git/hooks')).to.be.true
})

it('creates a package.json', () => {
fsStub({'.git/hooks': {}})
install()

const hooks = fs.readdirSync('.git/hooks')
const packageContent = require('../lib/package.template').content
expect(hooks).to.include('package.json')
expect(fs.readFileSync('.git/hooks/package.json', 'UTF-8')).to.equal(packageContent)
})

it('creates hook files', () => {
fsStub({'.git/hooks': {}})
install()
Expand Down Expand Up @@ -49,6 +59,38 @@ describe('install', function describeInstall() {
expectHook('post-rewrite', '.git/hooks/post-rewrite', '755')
})

describe('backs up an existing package.json', () => {

const existingGHookPackage = '// Generated by ghooks. Do not edit this file.'
const existingUserPackage = '# existing content'

it('does not keep a copy of an existing GHook package.json', () => {
fsStub({'.git/hooks': {
'package.json': existingGHookPackage,
}})

install()
const files = fs.readdirSync('.git/hooks')

expect(files).to.not.include('package.json.bkp')
expect(files).to.include('package.json')
})

it('backs up an existing user package.json', () => {
fsStub({'.git/hooks': {
'package.json': existingUserPackage,
}})

install()
const files = fs.readdirSync('.git/hooks')

expect(files).to.include('package.json.bkp')
expect(files).to.include('package.json')
expect(fs.readFileSync('.git/hooks/package.json.bkp', 'UTF-8')).to.equal(existingUserPackage)
})

})

describe('backing up existing hooks', () => {

const existingGHook = '// Generated by ghooks. Do not edit this file.'
Expand Down

0 comments on commit f5d096f

Please sign in to comment.