Permalink
Browse files

[publish] Made all code synch, added support for finding option based…

… git hook, and added content about how to tag and publish
  • Loading branch information...
1 parent bce2938 commit 986e4ea482e0661e1876c7354b8a3d06c0976883 @larzconwell committed Sep 8, 2012
Showing with 56 additions and 55 deletions.
  1. +5 −1 README.md
  2. +6 −0 bin/auto_npm.js
  3. +45 −54 lib/auto_npm.js
View
@@ -19,4 +19,8 @@ auto_npm <command> [options]
- `--help`, `-h` Display the help dialog
-_Note_: There hasn't been much success using `git commit -a`, but `git commit -m` works fine.
+###Notes
+- There hasn't been much success using `git commit -a` but `but commit -m` works most of the time.
+- When a commit includes `[publish]` it will increment your patch number, create Git tags and publish to NPM.
+- When you manually change your version number, it will create a Git tag and publish it to NPM.
+- Sometimes when it asks for your password when publishing it can be a bit iffy on your shell. Not sure why it's weird so it'll be fixed as soon as I found out how to.
View
@@ -28,6 +28,12 @@ help = [
, 'Usage'
, ' auto_npm <command> [options]'
, ''
+ , ' When you make a commit that manually changes the version'
+ , ' it will create a Git tag and publish it to NPM.'
+ , ' If you make a commit that has [publish] in the subject'
+ , ' it will increment your patch number, create'
+ , ' a Git tag and publish it to NPM.'
+ , ''
, 'Commands'
, ' enable # Enable Auto NPM in the current repo'
, ' disable # Disable Auto NPM in the current repo'
View
@@ -18,7 +18,7 @@ var cwd = process.cwd()
// Content to be written to hookFile
hookContent = [
'#!/bin/sh'
- , 'exec auto_npm --update'
+ , 'exec auto_npm update'
, ''
].join('\n');
@@ -33,14 +33,10 @@ exports.errors = errors;
//
// Write the 'post-commit' hook the hook content
exports.writeHook = function () {
- fs.writeFile(hookFile, hookContent, 'utf8', function (err) {
- if (err) {
- utils.kill(err, 1);
- }
+ fs.writeFileSync(hookFile, hookContent, 'utf8');
+ fs.chmodSync(hookFile, 0755);
- fs.chmodSync(hookFile, 0755);
- utils.kill('Auto NPM is now enabled in this repo.', 0);
- });
+ utils.kill('Auto NPM is now enabled in this repo.', 0);
};
//
@@ -56,8 +52,8 @@ exports.deleteHook = function () {
exports.getCommit = function (callback) {
var gitCMD = 'git --git-dir=' + gitDir + ' log -1'
, subjectCMD = gitCMD + ' --pretty=format:"%s"'
- , subject = ''
, diffCMD = gitCMD + ' -p'
+ , subject = ''
, diff = '';
exec(subjectCMD, function (err, stdout) {
@@ -95,11 +91,7 @@ exports.enable = function (options) {
// Create hooks directory is it doesn't exist
if (!utils.existsSync(hookDir)) {
- fs.mkdir(hookDir, function (err) {
- if (err) {
- utils.kill(err, 1);
- }
- });
+ fs.mkdirSync(hookDir);
}
// If write hook if needed
@@ -109,22 +101,24 @@ exports.enable = function (options) {
exports.writeHook();
}
- fs.readFile(hookFile, 'utf8', function (err, content) {
- if (err) {
- utils.kill(err, 1);
- }
+ var content = fs.readFileSync(hookFile, 'utf8');
- // Check if it's ours then exit with a message
- if (content === hookContent) {
- utils.kill('Auto NPM is already installed in this repo.', 0);
- }
+ // If the hook is ours then it's already enabled
+ if (content === hookContent) {
+ utils.kill('Auto NPM is already installed in this repo.', 0);
+ }
- var msg = [
- 'There is a conflicting Git hook, to use Auto NPM please delete'
- , 'the post-commit hook or use the --force option.'
- ].join(' ');
- utils.kill(msg, 1);
- });
+ // Check if the hook is the old option based version
+ if (content.match(/--update/)) {
+ exports.writeHook();
+ }
+
+ // Not ours so kill with message
+ var msg = [
+ 'There is a conflicting Git hook, to use Auto NPM please delete'
+ , 'the post-commit hook or use the --force option.'
+ ].join(' ');
+ utils.kill(msg, 1);
} else {
exports.writeHook();
}
@@ -140,21 +134,23 @@ exports.disable = function (options) {
exports.deleteHook();
}
- fs.readFile(hookFile, 'utf8', function (err, content) {
- if (err) {
- utils.kill(err, 1);
- }
+ var content = fs.readFileSync(hookFile, 'utf8');
- if (content === hookContent) {
- exports.deleteHook();
- }
+ // If the hook is ours then it's safe to remove
+ if (content === hookContent) {
+ exports.deleteHook();
+ }
- var msg = [
- 'Couldn\' remove Git hook as it\'s not for Auto NPM, use the'
- , '--force option if you\'d like to remove it anyway.'
- ].join(' ');
- utils.kill(msg, 1);
- });
+ // Check if the hook is the old option based version
+ if (content.match(/--update/)) {
+ exports.deleteHook();
+ }
+
+ var msg = [
+ 'Couldn\'t remove Git hook as it\'s not for Auto NPM, use the'
+ , '--force option if you\'d like to remove it anyway.'
+ ].join(' ');
+ utils.kill(msg, 1);
} else {
utils.kill('Auto NPM is not enabled for this repo.', 0);
}
@@ -178,7 +174,8 @@ exports.update = function () {
// Increment version write it to package.json then push to server
incrementAndPush = function () {
- var nums = pkgVersion.split('.');
+ var nums = pkgVersion.split('.')
+ , cmd = '';
// Increment patch number
nums[2] = Number(nums[2]) + 1
@@ -188,19 +185,13 @@ exports.update = function () {
fs.writeFileSync(packagePath, JSON.stringify(pkg, true, 2));
// Push to the server
- cmds = [
- 'git commit package.json -m "Version ' + pkgVersion + '"'
- , 'git push origin master'
- ];
-
- cmds.forEach(function(v) {
- exec(v, function(err, stdout, stderr) {
- if (err) {
- throw err;
- }
+ cmd = 'git commit package.json -m "Version ' + pkgVersion + '"';
+ exec(cmd, function(err, stdout, stderr) {
+ if (err) {
+ throw err;
+ }
- console.log(stdout);
- });
+ console.log(stdout);
});
};

0 comments on commit 986e4ea

Please sign in to comment.