Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance with NPM package #146

Merged
merged 1 commit into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions .npm/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,13 @@ var path = require('path');

var command_args = process.argv.slice(2);

function spawnCommand(binaryExecutable) {
var child = spawn(
path.join(__dirname, binaryExecutable),
command_args,
{ stdio: [process.stdin, process.stdout, process.stderr] });
var child = spawn(
path.join(__dirname, 'lefthook'),
command_args,
{ stdio: [process.stdin, process.stdout, process.stderr] });

child.on('close', function (code) {
if (code !== 0) {
process.exit(1);
}
});
}

if (process.platform === 'darwin') {
spawnCommand('lefthook-mac');
} else if (process.platform === 'linux') {
spawnCommand('lefthook-linux');
} else if (process.platform === 'win32') {
spawnCommand('lefthook-win.exe');
} else {
console.log("Unsupported OS");
process.exit(1);
}
child.on('close', function (code) {
if (code !== 0) {
process.exit(1);
}
});
13 changes: 13 additions & 0 deletions .npm/bin/lefthook
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
$dir/lefthook-linux $@
elif [[ "$OSTYPE" == "darwin"* ]]; then
$dir/lefthook-mac $@
elif [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
$dir/lefthook-win.exe $@
else
Unsupported OS
fi
8 changes: 8 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,17 @@ if [ -t 1 ] ; then
exec < /dev/tty ; # <- enables interactive shell
fi

dir="$(cd "$(dirname $(dirname $(dirname "${BASH_SOURCE[0]}")))" >/dev/null 2>&1 && pwd)"

` + autoInstall(hookName, fs) + "\n" + "cmd=\"lefthook run " + hookName + " $@\"" +
`

if lefthook -h >/dev/null 2>&1
then
eval $cmd
elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook"
then
eval $dir/node_modules/@arkweid/lefthook/bin/$cmd
Comment on lines +80 to +82
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see we're checking if the binaries in the NPM packages exists before the Ruby part. This seems a bit odd at the first glance… but it improves performance for NPM packages because running bundle exec is quite slow. And adding a simple file existence check before the Ruby part is not an issue: the check is fast enough to not penalize Ruby users.

elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec $cmd
Expand Down Expand Up @@ -119,6 +124,9 @@ func autoInstall(hookName string, fs afero.Fs) string {
if lefthook -h >/dev/null 2>&1
then
eval $cmd
elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook"
then
eval $dir/node_modules/@arkweid/lefthook/bin/$cmd
elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec $cmd
Expand Down