Browse files

Using the --enable option will write a hook in the `hooks` directory …

…that will call the --update options

Using the --disable options will delete the created hook in the `hooks` directory
  • Loading branch information...
1 parent eb0e2a3 commit 14c3bfb882e50430d9bbbed7bb460bf3a1d93c77 @larzconwell committed Jun 26, 2012
Showing with 91 additions and 11 deletions.
  1. +23 −7 bin/auto_npm.js
  2. +68 −0 lib/auto_npm.js
  3. +0 −4 package.json
View
30 bin/auto_npm.js
@@ -1,11 +1,17 @@
#!/usr/bin/env node
-var fs = require('fs')
- , path = require('path')
+//
+// Dependencies
+var path = require('path')
, package = require('../package')
- , cd = process.cwd()
- , args = process.argv.slice(2)
- , enabled = false
+ , runner = require('../lib/auto_npm');
+
+//
+// Vars
+var cd = process.cwd()
+ , args = process.argv.slice(2)
+ , enabled = false
+ , updating = false
, gitDir
, help
, arg;
@@ -50,6 +56,10 @@ while(args.length) {
case '--disable':
enabled = false;
break;
+ case '-u':
+ case '--updated':
+ updating = true;
+ break;
}
}
@@ -59,9 +69,15 @@ 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) {
+ runner.update();
+}
+
+//
// Enable Auto NPM or disable it
if(enabled) {
- // Start watching for commits
+ runner.enable();
} else {
- // Disable watching for commits
+ runner.disable();
}
View
68 lib/auto_npm.js
@@ -0,0 +1,68 @@
+//
+// Dependencies
+var fs = require('fs')
+ , path = require('path');
+
+//
+// Vars
+var cd = process.cwd()
+ , hookDir = path.join(cd, '.git', 'hooks')
+ , hookFile = path.join(hookDir, 'post-commit');
+
+exports.enable = function() {
+ var gitDir, hookContent;
+
+ // 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);
+
+ // If post-commit hook already exists, throw error
+ 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`');
+ });
+ }
+};
+
+exports.disable = function() {
+ if(path.existsSync(hookFile)) {
+ fs.open(hookFile, 'w', function(err) {
+ if(err) throw err;
+
+ 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.");
+ }
+};
+
+exports.update = function() {
+ console.log('updating NPM package');
+};
View
4 package.json
@@ -11,10 +11,6 @@
]
, "version": "0.0.1"
, "author": "Larz Conwell <larzconwell@gmail.com>"
- , "devDependencies": {}
- , "dependencies": {
- "git": "*"
- }
, "bin": {
"auto-npm": "./bin/auto_npm.js"
}

0 comments on commit 14c3bfb

Please sign in to comment.