Permalink
Browse files

Added force option so that it makes changes no matter what

  • Loading branch information...
1 parent 04552f3 commit d27cd02af7cf6626c881bf3733fbd748a6c0ba13 @larzconwell committed Jun 26, 2012
Showing with 91 additions and 42 deletions.
  1. +14 −3 bin/auto_npm.js
  2. +77 −39 lib/auto_npm.js
View
@@ -12,6 +12,7 @@ var cd = process.cwd()
, args = process.argv.slice(2)
, enabled = false
, updating = false
+ , force = false
, gitDir
, help
, arg;
@@ -27,6 +28,7 @@ help = [
, ''
, 'Usage'
, ' --enable, -e # Enable Auto NPM in a git repo'
+ , ' --force, -f # Force Auto NPM to rewrite any existing Git hooks'
, ' --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'
@@ -49,6 +51,10 @@ while(args.length) {
console.log(help);
process.exit(0);
break;
+ case '-f':
+ case '--force':
+ force = true;
+ break;
case '-e':
case '--enable':
enabled = true;
@@ -69,7 +75,6 @@ while(args.length) {
gitDir = path.existsSync(path.join(cd, '.git'));
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) {
@@ -78,8 +83,14 @@ if(updating) {
//
// Enable Auto NPM or disable it
if(enabled) {
- runner.enable();
+ // If force is true send it as an options
+ if(force) {
+ runner.enable({ force: true });
+ } else runner.enable();
} else {
- runner.disable();
+ // If force is true send it as an options
+ if(force) {
+ runner.disable({ force: true });
+ } else runner.disable();
}
}
View
@@ -8,64 +8,102 @@ var fs = require('fs')
var cd = process.cwd()
, hookDir = path.join(cd, '.git', 'hooks')
, hookFile = path.join(hookDir, 'post-commit')
- , hookContent = [
- '#!/bin/sh'
- , 'exec auto_npm --update'
- , ''
- ].join('\n');
+ , hookContent;
-exports.enable = function() {
- var gitDir;
+// Content to be written to hookFile
+hookContent = [
+ '#!/bin/sh'
+ , 'exec auto_npm --update'
+ , ''
+].join('\n');
+
+//
+// Write the 'post-commit' hook the hook content
+exports.writeHook = function() {
+ fs.writeFile(hookFile, hookContent, 'utf8', function(err) {
+ if(err) throw err;
+
+ fs.chmodSync(hookFile, 0755);
+ console.log('Auto NPM is enabled in "' + cd + '". To disable it run `auto_npm -d`');
+ });
+};
+
+//
+// Deletes a `post-commit` hook
+exports.deleteHook = function() {
+ fs.open(hookFile, 'w', function(err) {
+ if(err) throw err;
+
+ fs.unlinkSync(hookFile);
+ });
+};
+
+//
+// Install the hook file
+exports.enable = function(options) {
+ options = options || {};
// Throw error if no .git directory is present
- gitDir = path.existsSync(path.join(cd, '.git'));
+ var gitDir = path.existsSync(path.join(cd, '.git'));
if(!gitDir) throw new Error('The Directory "' + cd + '" is not a git repo.');
// If hooks directory can't be found, create it
if(!path.existsSync(hookDir)) fs.mkdirSync(hookDir);
- // If post-commit hook already exists, throw error
+ // If hookFile exists check if it's ours and if theres a force option, rewrite it
if(path.existsSync(hookFile)) {
fs.readFile(hookFile, 'utf8', function(err, content) {
- if(content === hookContent) {
- console.log('Auto NPM is already installed for "' + cd + '"');
- process.exit(0);
- } else {
- console.log('There is a Git hook that conflicts with Auto NPM, please delete "' +
- hookFile +
- '" if you\'d like to use Auto NPM.');
- process.exit(1);
- }
- });
- } else {
- fs.writeFile(hookFile, hookContent, 'utf8', function(err) {
if(err) throw err;
- fs.chmodSync(hookFile, 0755);
- console.log('Auto NPM is enabled in "' + cd + '". To disable just do `auto_npm -d`');
+ // If force option is given write the hook
+ if(options.force) {
+ exports.writeHook();
+ }
+ // Check if it's ours then exit
+ else {
+ if(content === hookContent) {
+ console.log('Auto NPM is already installed for "' + cd + '"');
+ process.exit(0);
+ } else {
+ console.log('There is a Git hook that conflicts with Auto NPM, please delete "' +
+ hookFile +
+ '" if you\'d like to use Auto NPM.');
+ process.exit(1);
+ }
+ }
});
- }
+ } else exports.writeHook();
};
-exports.disable = function() {
+//
+// Remove the hook file
+exports.disable = function(options) {
+ options = options || {};
+
if(path.existsSync(hookFile)) {
- // 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;
+ // If force option is given then delete it no matter what
+ if(options.force) {
+ exports.deleteHook();
+ }
+ // Delete file if it's our hook file
+ else {
+ 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 + '"');
- }
- });
- } else {
- console.log("Auto NPM is not enabled for this repo, so there's nothing to do.");
- }
+ 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.");
};
+//
+// Updated NPM packages
exports.update = function() {
- console.log('updating NPM package');
+ console.log('Test');
};

0 comments on commit d27cd02

Please sign in to comment.