Permalink
Browse files

Added stricter checking, now checks for the version line, and checks …

…if package.json was edited. Also added tagging and cleaned up all the code a bit.
  • Loading branch information...
1 parent 58898a7 commit 7a1441a43636e33a2cb603eb93e79e6d9aba2d08 @larzconwell committed Sep 7, 2012
Showing with 97 additions and 70 deletions.
  1. +3 −16 Makefile
  2. +2 −2 README.md
  3. +20 −18 bin/auto_npm.js
  4. +70 −32 lib/auto_npm.js
  5. +1 −1 lib/compat/index.js
  6. +1 −1 package.json
View
@@ -1,22 +1,9 @@
-.PHONY: install clean uninstall reinstall
-
-PREFIX=/usr/local
-DESTDIR=
+.PHONY: install uninstall reinstall
install:
- @mkdir -p $(DESTDIR)$(PREFIX)/bin && \
- mkdir -p $(DESTDIR)$(PREFIX)/lib/node_modules/auto_npm && \
- cp -R ./* $(DESTDIR)$(PREFIX)/lib/node_modules/auto_npm/ && \
- ln -snf ../lib/node_modules/auto_npm/bin/auto_npm.js $(DESTDIR)$(PREFIX)/bin/auto_npm && \
- chmod 755 $(DESTDIR)$(PREFIX)/lib/node_modules/auto_npm/bin/auto_npm.js && \
- echo 'Auto NPM installed.'
-
-clean:
- @true
+ @npm -g install .
uninstall:
- @rm -f $(DESTDIR)$(PREFIX)/bin/auto_npm && \
- rm -fr $(DESTDIR)$(PREFIX)/lib/node_modules/auto_npm/ && \
- echo 'Auto NPM uninstalled.'
+ @npm -g uninstall auto_npm
reinstall: uninstall install
View
@@ -1,13 +1,13 @@
Auto NPM
--------
-Automatically update a NPM package when you make a commit that changes the version number in `package.json`
+Automatically update a NPM package when you make a commit that changes the version number in `package.json`, also creates a git tag for you.
####Arguments
- `--enable` or `-e` Enable Auto NPM in a git repo
- `--disable` or `-d` Disable Auto NPM in a git repo
- `--force` or `-f` Force Auto NPM to rewrite any exists Git hooks that we need
-- `--update` or `-u` Updated the NPM package for the current repo
+- `--update` or `-u` Update the NPM package for the current repo
- `--help` or `-h` Display the help dialog
View
@@ -21,8 +21,7 @@ var cd = process.cwd()
//
// Help dialog
help = [
- ''
- , package.name + ' ' + package.version
+ package.name + ' ' + package.version
, ''
, 'Description'
, ' ' + package.description
@@ -31,22 +30,23 @@ help = [
, ' --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'
+ , ' --update, -u # Update the NPM package for the current repo'
, ' --help, -h # Dipslay this help dialog'
+ , ''
].join('\n');
// Show help dialog if no args are given
-if(args.length === 0) {
+if (args.length <= 0) {
console.log(help);
process.exit(0);
}
//
// Get arguments
-while(args.length) {
+while (args.length) {
arg = args.shift();
- switch(arg) {
+ switch (arg) {
case '-h':
case '--help':
console.log(help);
@@ -73,25 +73,27 @@ while(args.length) {
//
// Check if directory is a Git repo
-gitDir = compat.existsSync(path.join(cd, '.git'));
-if(!gitDir) throw new Error('The Directory "' + cd + '" is not a git repo.');
+if (!compat.existsSync(path.join(cd, '.git'))) {
+ 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) {
+if (updating) {
runner.update();
} else {
- //
// Enable Auto NPM or disable it
- if(enabled) {
- // If force is true send it as an options
+ if (enabled) {
if(force) {
- runner.enable({ force: true });
- } else runner.enable();
+ runner.enable({force: true});
+ } else {
+ runner.enable();
+ }
} else {
- // If force is true send it as an options
- if(force) {
- runner.disable({ force: true });
- } else runner.disable();
+ if (force) {
+ runner.disable({force: true});
+ } else {
+ runner.disable();
+ }
}
}
View
@@ -21,9 +21,11 @@ hookContent = [
//
// Write the 'post-commit' hook the hook content
-exports.writeHook = function() {
+exports.writeHook = function () {
fs.writeFile(hookFile, hookContent, 'utf8', function(err) {
- if(err) throw err;
+ if (err) {
+ throw err;
+ }
fs.chmodSync(hookFile, 0755);
console.log('Auto NPM is enabled in "' + cd + '". To disable it run `auto_npm -d`');
@@ -32,50 +34,65 @@ exports.writeHook = function() {
//
// Deletes a `post-commit` hook
-exports.deleteHook = function() {
+exports.deleteHook = function () {
fs.open(hookFile, 'w', function(err) {
- if(err) throw err;
+ if (err) {
+ throw err;
+ }
fs.unlinkSync(hookFile);
});
};
//
// Returns the commit information from the last commit
-exports.getCommit = function(exec, callback) {
+exports.getCommit = function (exec, callback) {
var cmd = 'git --git-dir=' + gitDir + ' log -p -1';
exec(cmd, function(err, stdout, stderr) {
- if(err) callback && callback(err);
+ if (err) {
+ callback && callback(undefined, err);
+ }
- callback && callback(undefined, stdout);
+ callback && callback(stdout, undefined);
});
};
//
+// Get package.json in the current directory
+exports.getPackage = function () {
+ return require(path.join(process.cwd(), "package.json"));
+};
+
+//
// Install the hook file
-exports.enable = function(options) {
+exports.enable = function (options) {
options = options || {};
// Throw error if no .git directory is present
- var gitDirExists = compat.existsSync(gitDir);
- if(!gitDirExists) throw new Error('The Directory "' + cd + '" is not a git repo.');
+ if (!compat.existsSync(gitDir)) {
+ throw new Error('The Directory "' + cd + '" is not a git repo.');
+ }
// If hooks directory can't be found, create it
- if(!compat.existsSync(hookDir)) fs.mkdirSync(hookDir);
+ if (!compat.existsSync(hookDir)) {
+ fs.mkdirSync(hookDir);
+ }
// If hookFile exists check if it's ours and if theres a force option, rewrite it
- if(compat.existsSync(hookFile)) {
+ if (compat.existsSync(hookFile)) {
fs.readFile(hookFile, 'utf8', function(err, content) {
- if(err) throw err;
+ if (err) {
+ throw err;
+ }
// If force option is given write the hook
- if(options.force) {
+ if (options.force) {
exports.writeHook();
}
// Check if it's ours then exit
else {
- if(content === hookContent) {
+ if (content === hookContent) {
console.log('Auto NPM is already installed for "' + cd + '"');
process.exit(0);
} else {
@@ -86,23 +103,25 @@ exports.enable = function(options) {
}
}
});
- } else exports.writeHook();
+ } else {
+ exports.writeHook();
+ }
};
//
// Remove the hook file
-exports.disable = function(options) {
+exports.disable = function (options) {
options = options || {};
- if(compat.existsSync(hookFile)) {
+ if (compat.existsSync(hookFile)) {
// If force option is given then delete it no matter what
- if(options.force) {
+ if (options.force) {
exports.deleteHook();
}
// Delete file if it's our hook file
else {
fs.readFile(hookFile, 'utf8', function(err, content) {
- if(content === hookContent) {
+ if (content === hookContent) {
fs.open(hookFile, 'w', function(err) {
if(err) throw err;
@@ -113,25 +132,44 @@ exports.disable = function(options) {
}
});
}
- } 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() {
- var pat = /"version*":\s("([0-9].|[0-9])*"|([0-9].|[0-9])*)/
+exports.update = function () {
+ var pat = /"version*":\s.*/
, exec = require('child_process').exec
- , cmd;
+ , pkgVersion = exports.getPackage().version
+ , cmds = []
+ , cmd = '';
- exports.getCommit(exec, function(err, data) {
- if(err) throw err;
-
- // If version has updated then update NPM package
- if(data.match(pat)) {
- exec('sudo npm publish', function(err, stdout, stderr) {
- if(err) throw err;
+ exports.getCommit(exec, function (data, err) {
+ if (err) {
+ throw err;
+ }
- console.log(stdout);
+ // If version has updated in package.json then update NPM package
+ if (data.match(pat) && data.match("package.json")) {
+ cmds = [
+ // Create annotated tag
+ 'git tag -a v' + pkgVersion + ' -m "Version ' + pkgVersion + '"'
+ // Push new tag to server
+ , 'git push origin v' + pkgVersion
+ // Publish package to NPM
+ , 'sudo npm publish'
+ ];
+
+ cmds.forEach(function(v) {
+ exec(v, function(err, stdout, stderr) {
+ if (err) {
+ throw err;
+ }
+
+ console.log(stdout);
+ });
});
}
});
View
@@ -1,4 +1,4 @@
var fs = require('fs')
, path = require('path');
-exports.existsSync = typeof fs.existsSync === 'function' ? fs.existsSync : path.existsSync;
+exports.existsSync = (typeof fs.existsSync === 'function') ? fs.existsSync : path.existsSync;
View
@@ -9,7 +9,7 @@
, "version"
, "control"
]
- , "version": "0.0.6"
+ , "version": "0.0.7"
, "author": "Larz Conwell <larzconwell@gmail.com>"
, "bin": {
"auto_npm": "./bin/auto_npm.js"

0 comments on commit 7a1441a

Please sign in to comment.