Skip to content

Commit

Permalink
solves the windows issue (with symlinks) (#84)
Browse files Browse the repository at this point in the history
* solved the windows issue with symlinks

Instead of symlinking to the hook in node-modules/pre-commit, this injects a new pre-commit hook (in .git/hooks) that takes care of launching the hook in node-modules/pre-commit. 
In addition it stashes the unstaged changes and applies them again after running the pre-commit hook, so that you only run your pre-commit scripts on the commit actual codebase.

* removed stashing

Stashing automatically is actually a bad idea, it's better to for the user to have control over doing it or not (manually then).
  • Loading branch information
barroudjo authored and 3rd-Eden committed Nov 27, 2016
1 parent 4167676 commit a9c9732
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
var fs = require('fs')
, path = require('path')
, spawn = require('cross-spawn')
, os = require('os')
, hook = path.join(__dirname, 'hook')
, root = path.resolve(__dirname, '..', '..')
, exists = fs.existsSync || path.existsSync;
Expand Down Expand Up @@ -47,15 +47,28 @@ if (exists(precommit) && !fs.lstatSync(precommit).isSymbolicLink()) {
try { fs.unlinkSync(precommit); }
catch (e) {}

// Create generic precommit hook that launches this modules hook (as well
// as stashing - unstashing the unstaged changes)
// TODO: we could keep launching the old pre-commit scripts
var hookRelativeUnixPath = hook.replace(root, '.');
if(os.platform() === 'win32') {
hookRelativeUnixPath = hookRelativeUnixPath.replace(/[\\\/]+/g, '/');
}
var precommitContent = '#!/bin/bash' + os.EOL
+ hookRelativeUnixPath + os.EOL
+ 'RESULT=$?' + os.EOL
+ '[ $RESULT -ne 0 ] && exit 1' + os.EOL
+ 'exit 0' + os.EOL;

//
// It could be that we do not have rights to this folder which could cause the
// installation of this module to completely fail. We should just output the
// error instead destroying the whole npm install process.
//
try { fs.symlinkSync(path.relative(hooks, hook), precommit, 'file'); }
try { fs.writeFileSync(precommit, precommitContent); }
catch (e) {
console.error('pre-commit:');
console.error('pre-commit: Failed to symlink the hook file in your .git/hooks folder because:');
console.error('pre-commit: Failed to create the hook file in your .git/hooks folder because:');
console.error('pre-commit: '+ e.message);
console.error('pre-commit: The hook was not installed.');
console.error('pre-commit:');
Expand Down

0 comments on commit a9c9732

Please sign in to comment.