Permalink
Browse files

Only delete the hook if it's our hook.

Added help option for update
  • Loading branch information...
1 parent 14c3bfb commit 4772226199bb78d56abe8eb2de3b2dab0208b525 @larzconwell committed Jun 26, 2012
Showing with 25 additions and 21 deletions.
  1. +8 −7 bin/auto_npm.js
  2. +17 −14 lib/auto_npm.js
View
@@ -28,6 +28,7 @@ help = [
, 'Usage'
, ' --enable, -e # Enable Auto NPM in a git repo'
, ' --disable, -d # Disable Auto NPM in a git repo'
+ , ' --update, -u # Updated the NPM package for the current repo'
, ' --help, -h # Dipslay this help dialog'
].join('\n');
@@ -72,12 +73,12 @@ if(!gitDir) throw new Error('The Directory "' + cd + '" is not a git repo.');
// If updating arg is set then we need to update NPM package
if(updating) {
runner.update();
-}
-
-//
-// Enable Auto NPM or disable it
-if(enabled) {
- runner.enable();
} else {
- runner.disable();
+ //
+ // Enable Auto NPM or disable it
+ if(enabled) {
+ runner.enable();
+ } else {
+ runner.disable();
+ }
}
View
@@ -7,22 +7,20 @@ var fs = require('fs')
// Vars
var cd = process.cwd()
, hookDir = path.join(cd, '.git', 'hooks')
- , hookFile = path.join(hookDir, 'post-commit');
+ , hookFile = path.join(hookDir, 'post-commit')
+ , hookContent = [
+ '#!/bin/sh'
+ , 'exec auto_npm --update'
+ , ''
+ ].join('\n');
exports.enable = function() {
- var gitDir, hookContent;
+ var gitDir;
// Throw error if no .git directory is present
gitDir = path.existsSync(path.join(cd, '.git'));
if(!gitDir) throw new Error('The Directory "' + cd + '" is not a git repo.');
- // Content for hook
- hookContent = [
- '#!/bin/sh'
- , 'exec auto_npm --update'
- , ''
- ].join('\n');
-
// If hooks directory can't be found, create it
if(!path.existsSync(hookDir)) fs.mkdirSync(hookDir);
@@ -51,13 +49,18 @@ exports.enable = function() {
exports.disable = function() {
if(path.existsSync(hookFile)) {
- fs.open(hookFile, 'w', function(err) {
- if(err) throw err;
+ // Only delete the Git hook if it's our hook
+ fs.readFile(hookFile, 'utf8', function(err, content) {
+ if(content === hookContent) {
+ fs.open(hookFile, 'w', function(err) {
+ if(err) throw err;
- fs.unlinkSync(hookFile);
- });
+ fs.unlinkSync(hookFile);
+ });
- console.log('Auto NPM has been disabled for "' + cd + '"');
+ console.log('Auto NPM has been disabled for "' + cd + '"');
+ }
+ });
} else {
console.log("Auto NPM is not enabled for this repo, so there's nothing to do.");
}

0 comments on commit 4772226

Please sign in to comment.