Skip to content

Commit

Permalink
Added force option so that it makes changes no matter what
Browse files Browse the repository at this point in the history
  • Loading branch information
larzconwell committed Jun 26, 2012
1 parent 04552f3 commit d27cd02
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 42 deletions.
17 changes: 14 additions & 3 deletions bin/auto_npm.js
Expand Up @@ -12,6 +12,7 @@ var cd = process.cwd()
, args = process.argv.slice(2) , args = process.argv.slice(2)
, enabled = false , enabled = false
, updating = false , updating = false
, force = false
, gitDir , gitDir
, help , help
, arg; , arg;
Expand All @@ -27,6 +28,7 @@ help = [
, '' , ''
, 'Usage' , 'Usage'
, ' --enable, -e # Enable Auto NPM in a git repo' , ' --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' , ' --disable, -d # Disable Auto NPM in a git repo'
, ' --update, -u # Updated the NPM package for the current repo' , ' --update, -u # Updated the NPM package for the current repo'
, ' --help, -h # Dipslay this help dialog' , ' --help, -h # Dipslay this help dialog'
Expand All @@ -49,6 +51,10 @@ while(args.length) {
console.log(help); console.log(help);
process.exit(0); process.exit(0);
break; break;
case '-f':
case '--force':
force = true;
break;
case '-e': case '-e':
case '--enable': case '--enable':
enabled = true; enabled = true;
Expand All @@ -69,7 +75,6 @@ while(args.length) {
gitDir = path.existsSync(path.join(cd, '.git')); gitDir = path.existsSync(path.join(cd, '.git'));
if(!gitDir) throw new Error('The Directory "' + cd + '" is not a git repo.'); 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 arg is set then we need to update NPM package
if(updating) { if(updating) {
Expand All @@ -78,8 +83,14 @@ if(updating) {
// //
// Enable Auto NPM or disable it // Enable Auto NPM or disable it
if(enabled) { if(enabled) {
runner.enable(); // If force is true send it as an options
if(force) {
runner.enable({ force: true });
} else runner.enable();
} else { } else {
runner.disable(); // If force is true send it as an options
if(force) {
runner.disable({ force: true });
} else runner.disable();
} }
} }
116 changes: 77 additions & 39 deletions lib/auto_npm.js
Expand Up @@ -8,64 +8,102 @@ var fs = require('fs')
var cd = process.cwd() var cd = process.cwd()
, hookDir = path.join(cd, '.git', 'hooks') , hookDir = path.join(cd, '.git', 'hooks')
, hookFile = path.join(hookDir, 'post-commit') , hookFile = path.join(hookDir, 'post-commit')
, hookContent = [ , hookContent;
'#!/bin/sh'
, 'exec auto_npm --update'
, ''
].join('\n');


exports.enable = function() { // Content to be written to hookFile
var gitDir; 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 // 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(!gitDir) throw new Error('The Directory "' + cd + '" is not a git repo.');


// If hooks directory can't be found, create it // If hooks directory can't be found, create it
if(!path.existsSync(hookDir)) fs.mkdirSync(hookDir); 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)) { if(path.existsSync(hookFile)) {
fs.readFile(hookFile, 'utf8', function(err, content) { 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; if(err) throw err;


fs.chmodSync(hookFile, 0755); // If force option is given write the hook
console.log('Auto NPM is enabled in "' + cd + '". To disable just do `auto_npm -d`'); 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)) { if(path.existsSync(hookFile)) {
// Only delete the Git hook if it's our hook // If force option is given then delete it no matter what
fs.readFile(hookFile, 'utf8', function(err, content) { if(options.force) {
if(content === hookContent) { exports.deleteHook();
fs.open(hookFile, 'w', function(err) { }
if(err) throw err; // 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 + '"'); 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."); } else console.log("Auto NPM is not enabled for this repo, so there's nothing to do.");
}
}; };


//
// Updated NPM packages
exports.update = function() { exports.update = function() {
console.log('updating NPM package'); console.log('Test');
}; };

0 comments on commit d27cd02

Please sign in to comment.