Skip to content

Commit

Permalink
Split NPM package to two: bundled and installer (#273)
Browse files Browse the repository at this point in the history
Bundled NPM package was here before, installer was implemented in #188
  • Loading branch information
Envek authored Jun 7, 2022
1 parent ac987c8 commit 87f84cf
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 230 deletions.
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ cplefthook

tmp/
dist/
.rubygems/pkg/
.rubygems/libexec/
.npm/bin/
!.npm/bin/*.js
packaging/rubygems/pkg/
packaging/rubygems/libexec/
packaging/npm-bundled/bin/
packaging/npm-*/README.md
package.json
!.npm/package.json
!packaging/npm-*/package.json
node_modules/
yarn.lock
package-lock.json
203 changes: 0 additions & 203 deletions .npm/README.md

This file was deleted.

11 changes: 7 additions & 4 deletions cmd/templates/hook.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ call_lefthook()
if lefthook{{.Extension}} -h >/dev/null 2>&1
then
eval lefthook{{.Extension}} $@
elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}"
elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}"
then
eval "$dir/node_modules/@arkweid/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}} $@"
eval "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}} $@"
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}"
then
eval "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}} $@"
elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec lefthook $@
elif yarn lefthook -h >/dev/null 2>&1
then
yarn lefthook $@
elif npx @arkweid/lefthook -h >/dev/null 2>&1
elif npx @evilmartians/lefthook -h >/dev/null 2>&1
then
npx @arkweid/lefthook $@
npx @evilmartians/lefthook $@
else
echo "Can't find lefthook in PATH"
fi
Expand Down
24 changes: 18 additions & 6 deletions docs/full_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@ go get github.com/evilmartians/lefthook

### npm or yarn

```bash
npm i @arkweid/lefthook --save-dev
# or yarn:
yarn add -D @arkweid/lefthook
```
Lefthook is available on NPM in two flavors:

1. [@evilmartians/lefthook](https://www.npmjs.com/package/@evilmartians/lefthook) with pre-bundled binaries for all architectures:

```bash
npm install @evilmartians/lefthook --save-dev
# or yarn:
yarn add -D @evilmartians/lefthook
```

2. [@evilmartians/lefthook-installer](https://www.npmjs.com/package/@evilmartians/lefthook-installer) that wil fetch binary file on installation:

```bash
npm install @evilmartians/lefthook-installer --save-dev
# or yarn:
yarn add -D @evilmartians/lefthook-installer
```

NOTE: if you install it this way you should call it with `npx` or `yarn` for all listed examples below. (for example: `lefthook install` -> `npx lefthook install`)

You can also install lefthook as a global dependency

```bash
npm install -g @arkweid/lefthook
npm install -g @evilmartians/lefthook
```


Expand Down
23 changes: 16 additions & 7 deletions docs/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ This is guide of using Lefthook git hook manager in Node.js projects. You can fi

## Install

Lefthook is [available on npm](https://www.npmjs.com/package/@arkweid/lefthook):
Lefthook is available on NPM in two flavors:

```bash
npm install @arkweid/lefthook --save-dev
# or yarn:
yarn add -D @arkweid/lefthook
```
1. [@evilmartians/lefthook](https://www.npmjs.com/package/@evilmartians/lefthook) with pre-bundled binaries for all architectures:

```bash
npm install @evilmartians/lefthook --save-dev
# or yarn:
yarn add -D @evilmartians/lefthook
```

2. [@evilmartians/lefthook-installer](https://www.npmjs.com/package/@evilmartians/lefthook) that wil fetch binary file on installation:

```bash
npm install @evilmartians/lefthook-installer --save-dev
# or yarn:
yarn add -D @evilmartians/lefthook-installer
```

## Edit

Expand All @@ -33,7 +42,7 @@ pre-commit:

## Test it
```bash
npx @arkweid/lefthook install && npx @arkweid/lefthook run pre-commit
npx @evilmartians/lefthook install && npx @evilmartians/lefthook run pre-commit
```

### More info
Expand Down
17 changes: 17 additions & 0 deletions packaging/npm-bundled/bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node

var spawn = require('child_process').spawn;
const { getExePath } = require('../get-exe');

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

var child = spawn(
getExePath(),
command_args,
{ stdio: "inherit" });

child.on('close', function (code) {
if (code !== 0) {
process.exit(1);
}
});
36 changes: 36 additions & 0 deletions packaging/npm-bundled/get-exe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const path = require("path")

function getExePath() {
// Detect OS
// https://nodejs.org/api/process.html#process_process_platform
let goOS = process.platform;
let extension = '';
if (['win32', 'cygwin'].includes(process.platform)) {
goOS = 'windows';
extension = '.exe';
}

// Detect architecture
// https://nodejs.org/api/process.html#process_process_arch
let goArch = process.arch;
switch (process.arch) {
case 'x64': {
goArch = 'amd64';
break;
}
case 'x32':
case 'ia32': {
goArch = '386';
break;
}
}

const dir = path.join(__dirname, 'bin');
const executable = path.join(
dir,
`lefthook_${goOS}_${goArch}`,
`lefthook${extension}`
);
return executable;
}
exports.getExePath = getExePath;
Loading

0 comments on commit 87f84cf

Please sign in to comment.