Skip to content

Commit 2021ce3

Browse files
author
Alexander Lisianoi
committed
Remove setup.js and decouple install.js and uninstall.js
Make uninstall.js independent of 3rd party modules. There is an npm v3 bug that (sometimes) uninstalls 3rd party modules before running your uninstall script which may rely on those modules. Refs npm/npm#11894
1 parent 674dea6 commit 2021ce3

3 files changed

Lines changed: 51 additions & 101 deletions

File tree

install.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
1+
var fs = require('fs')
2+
var path = require('path')
13
var chalk = require('chalk')
2-
var setup = require('./setup')
3-
4-
if (setup.selfmadeHook) {
5-
console.log('Found an existing commitplease hook, removing to update it')
6-
setup.destroy()
7-
} else if (setup.hookExists) {
8-
console.log(chalk.red('Detected an existing git commit-msg hook'))
9-
console.log('')
4+
5+
var options = require('./index.js').getOptions()
6+
7+
if (options && options.nohook) {
8+
console.log('commitplease: package.json or .npmrc set to skip hook')
9+
process.exit(0)
10+
}
11+
12+
var git = path.resolve(process.cwd(), '..', '..', '.git')
13+
var hooks = path.join(git, 'hooks')
14+
15+
if (!fs.existsSync(git) || !fs.existsSync(hooks)) {
16+
fs.mkdirSync(git)
17+
fs.mkdirSync(hooks)
18+
}
19+
20+
var dstHook = path.join(hooks, 'commit-msg')
21+
var srcHook = path.relative(hooks, 'commit-msg-hook.js')
22+
23+
if (fs.existsSync(dstHook)) {
24+
console.log(chalk.red('A commit-msg hook already exists'))
1025
console.log(chalk.red('Remove it and install this package again to install commitplease properly'))
1126
process.exit(0)
1227
}
1328

14-
setup.create()
15-
console.log('Installed ' + setup.hook)
29+
try {
30+
fs.writeFileSync(dstHook, fs.readFileSync(srcHook))
31+
fs.chmodSync(dstHook, '755')
32+
} catch (e) {
33+
if (/EPERM/.test(e.message)) {
34+
console.error(
35+
chalk.red(
36+
'Failed to write commit-msg hook. ' +
37+
'Make sure you have the necessary permissions.'
38+
)
39+
)
40+
}
41+
throw e
42+
}

setup.js

Lines changed: 0 additions & 85 deletions
This file was deleted.

uninstall.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
var setup = require('./setup')
1+
var fs = require('fs')
2+
var path = require('path')
23

3-
if (setup.selfmadeHook) {
4-
console.log('Found a hook installed by commitplease, removing')
5-
setup.destroy()
6-
} else {
7-
console.log("Didn't find a commit-msg hook installed by this module, doing nothing")
4+
var hooks = path.join(process.cwd(), '..', '..', '.git', 'hooks')
5+
6+
var dstHook = path.join(hooks, 'commit-msg')
7+
var srcHook = path.relative(hooks, 'commit-msg-hook.js')
8+
9+
if (fs.existsSync(dstHook) && fs.existsSync(srcHook)) {
10+
var githook = fs.readFileSync(dstHook, 'utf-8')
11+
var comhook = fs.readFileSync(srcHook, 'utf-8')
12+
if (githook.toString() === comhook.toString()) {
13+
console.log('Removing the hook installed by commitplease')
14+
fs.unlinkSync(dstHook)
15+
}
816
}

0 commit comments

Comments
 (0)