Skip to content

Commit a9c9732

Browse files
barroudjo3rd-Eden
authored andcommittedNov 27, 2016
solves the windows issue (with symlinks) (#84)
* 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).
1 parent 4167676 commit a9c9732

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed
 

‎install.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//
66
var fs = require('fs')
77
, path = require('path')
8-
, spawn = require('cross-spawn')
8+
, os = require('os')
99
, hook = path.join(__dirname, 'hook')
1010
, root = path.resolve(__dirname, '..', '..')
1111
, exists = fs.existsSync || path.existsSync;
@@ -47,15 +47,28 @@ if (exists(precommit) && !fs.lstatSync(precommit).isSymbolicLink()) {
4747
try { fs.unlinkSync(precommit); }
4848
catch (e) {}
4949

50+
// Create generic precommit hook that launches this modules hook (as well
51+
// as stashing - unstashing the unstaged changes)
52+
// TODO: we could keep launching the old pre-commit scripts
53+
var hookRelativeUnixPath = hook.replace(root, '.');
54+
if(os.platform() === 'win32') {
55+
hookRelativeUnixPath = hookRelativeUnixPath.replace(/[\\\/]+/g, '/');
56+
}
57+
var precommitContent = '#!/bin/bash' + os.EOL
58+
+ hookRelativeUnixPath + os.EOL
59+
+ 'RESULT=$?' + os.EOL
60+
+ '[ $RESULT -ne 0 ] && exit 1' + os.EOL
61+
+ 'exit 0' + os.EOL;
62+
5063
//
5164
// It could be that we do not have rights to this folder which could cause the
5265
// installation of this module to completely fail. We should just output the
5366
// error instead destroying the whole npm install process.
5467
//
55-
try { fs.symlinkSync(path.relative(hooks, hook), precommit, 'file'); }
68+
try { fs.writeFileSync(precommit, precommitContent); }
5669
catch (e) {
5770
console.error('pre-commit:');
58-
console.error('pre-commit: Failed to symlink the hook file in your .git/hooks folder because:');
71+
console.error('pre-commit: Failed to create the hook file in your .git/hooks folder because:');
5972
console.error('pre-commit: '+ e.message);
6073
console.error('pre-commit: The hook was not installed.');
6174
console.error('pre-commit:');

0 commit comments

Comments
 (0)