Skip to content

Commit

Permalink
feature: Split npm package by os and cpu (#281)
Browse files Browse the repository at this point in the history
Splitting allows us to give up with post install scripts which is considered as a best practice

* feature: Split npm package by OS and CPU

Signed-off-by: Valentin Kiselev <mrexox@evilmartians.com>

* docs: Update the docs

Signed-off-by: Valentin Kiselev <mrexox@evilmartians.com>

* chore: Automate publishing

Signed-off-by: Valentin Kiselev <mrexox@evilmartians.com>

* fix: Make lefthook NPM package work

Signed-off-by: Valentin Kiselev <mrexox@evilmartians.com>

* feature: Add new NPM package binary to hook template

Signed-off-by: Valentin Kiselev <mrexox@evilmartians.com>
  • Loading branch information
mrexox authored Jun 23, 2022
1 parent a386b0a commit c8521e5
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ packaging/rubygems/pkg/
packaging/rubygems/libexec/
packaging/npm-bundled/bin/
packaging/npm-*/README.md
packaging/npm/*/bin/
packaging/npm/*/README.md
package.json
!packaging/npm-*/package.json
!packaging/npm/*/package.json
node_modules/
yarn.lock
package-lock.json
12 changes: 10 additions & 2 deletions docs/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ This is guide of using Lefthook git hook manager in Node.js projects. You can fi

Lefthook is available on NPM in two flavors:

1. [lefthook](https://www.npmjs.com/package/lefthook) that will install the proper binary:

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

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

```bash
Expand All @@ -14,7 +22,7 @@ Lefthook is available on NPM in two flavors:
yarn add -D @evilmartians/lefthook
```

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

```bash
npm install @evilmartians/lefthook-installer --save-dev
Expand Down Expand Up @@ -42,7 +50,7 @@ pre-commit:

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

### More info
Expand Down
3 changes: 3 additions & 0 deletions internal/templates/hook.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ call_lefthook()
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 test -f "$dir/node_modules/lefthook/bin/index.js"
test
eval "$dir/node_modules/lefthook/bin/index.js $@"
elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec lefthook $@
Expand Down
36 changes: 36 additions & 0 deletions packaging/npm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# NPM package version to release
VERSION := 1.0.0

DIST_DIR := ../../dist

LINUX_AMD64_BIN=$(DIST_DIR)/lefthook_linux_amd64_v1/lefthook
LINUX_ARM64_BIN=$(DIST_DIR)/lefthook_linux_arm64/lefthook
WINDOWS_AMD64_BIN=$(DIST_DIR)/lefthook_windows_amd64_v1/lefthook.exe
WINDOWS_ARM64_BIN=$(DIST_DIR)/lefthook_windows_arm64/lefthook.exe
DARWIN_AMD64_BIN=$(DIST_DIR)/lefthook_darwin_amd64_v1/lefthook
DARWIN_ARM64_BIN=$(DIST_DIR)/lefthook_darwin_arm64/lefthook

prepare: clean set-version put-readme put-binaries

publish:
find . -type d -name 'lefthook*' -exec npm publish --access public \{} \;

# Update versions of all packages
set-version:
find . -name 'package.json' -type f -print0 | xargs -0 sed -E -i "s/\"version\": \".+\"/\"version\": \"$(VERSION)\"/"
sed -E -i "s/\"(lefthook-.+)\": \".+\"/\"\1\": \"$(VERSION)\"/g" lefthook/package.json

put-binaries:
install -D $(LINUX_AMD64_BIN) lefthook-linux-x64/bin/lefthook
install -D $(LINUX_ARM64_BIN) lefthook-linux-arm64/bin/lefthook
install -D $(WINDOWS_AMD64_BIN) lefthook-windows-x64/bin/lefthook.exe
install -D $(WINDOWS_ARM64_BIN) lefthook-windows-arm64/bin/lefthook.exe
install -D $(DARWIN_AMD64_BIN) lefthook-darwin-x64/bin/lefthook
install -D $(DARWIN_ARM64_BIN) lefthook-darwin-arm64/bin/lefthook

put-readme:
find . -type d -name 'lefthook*' -exec cp -f ../../README.md \{} \;

clean:
find . -name 'README.md' -exec rm \{} \;
find . -type f -executable -exec rm \{} \;
18 changes: 18 additions & 0 deletions packaging/npm/lefthook-darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "lefthook-darwin-arm64",
"version": "1.0.0",
"description": "The macOS ARM 64-bit binary for lefthook, git hooks manager.",
"preferUnplugged": false,
"repository": "https://github.com/evilmartians/lefthook",
"license": "MIT",
"bugs": {
"url": "https://github.com/evilmartians/lefthook/issues"
},
"homepage": "https://github.com/evilmartians/lefthook#readme",
"os": [
"darwin"
],
"cpu": [
"arm64"
]
}
18 changes: 18 additions & 0 deletions packaging/npm/lefthook-darwin-x64/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "lefthook-darwin-x64",
"version": "1.0.0",
"description": "The macOS 64-bit binary for lefthook, git hooks manager.",
"preferUnplugged": false,
"repository": "https://github.com/evilmartians/lefthook",
"license": "MIT",
"bugs": {
"url": "https://github.com/evilmartians/lefthook/issues"
},
"homepage": "https://github.com/evilmartians/lefthook#readme",
"os": [
"darwin"
],
"cpu": [
"x64"
]
}
18 changes: 18 additions & 0 deletions packaging/npm/lefthook-linux-arm64/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "lefthook-linux-arm64",
"version": "1.0.0",
"description": "The Linux ARM 64-bit binary for lefthook, git hooks manager.",
"preferUnplugged": false,
"repository": "https://github.com/evilmartians/lefthook",
"license": "MIT",
"bugs": {
"url": "https://github.com/evilmartians/lefthook/issues"
},
"homepage": "https://github.com/evilmartians/lefthook#readme",
"os": [
"linux"
],
"cpu": [
"arm64"
]
}
18 changes: 18 additions & 0 deletions packaging/npm/lefthook-linux-x64/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "lefthook-linux-x64",
"version": "1.0.0",
"description": "The Linux 64-bit binary for lefthook, git hooks manager.",
"preferUnplugged": false,
"repository": "https://github.com/evilmartians/lefthook",
"license": "MIT",
"bugs": {
"url": "https://github.com/evilmartians/lefthook/issues"
},
"homepage": "https://github.com/evilmartians/lefthook#readme",
"os": [
"linux"
],
"cpu": [
"x64"
]
}
18 changes: 18 additions & 0 deletions packaging/npm/lefthook-windows-arm64/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "lefthook-windows-arm64",
"version": "1.0.0",
"description": "The Windows ARM 64-bit binary for lefthook, git hooks manager.",
"preferUnplugged": false,
"repository": "https://github.com/evilmartians/lefthook",
"license": "MIT",
"bugs": {
"url": "https://github.com/evilmartians/lefthook/issues"
},
"homepage": "https://github.com/evilmartians/lefthook#readme",
"os": [
"win32"
],
"cpu": [
"arm64"
]
}
18 changes: 18 additions & 0 deletions packaging/npm/lefthook-windows-x64/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "lefthook-windows-x64",
"version": "1.0.0",
"description": "The Windows 64-bit binary for lefthook, git hooks manager.",
"preferUnplugged": false,
"repository": "https://github.com/evilmartians/lefthook",
"license": "MIT",
"bugs": {
"url": "https://github.com/evilmartians/lefthook/issues"
},
"homepage": "https://github.com/evilmartians/lefthook#readme",
"os": [
"win32"
],
"cpu": [
"x64"
]
}
20 changes: 20 additions & 0 deletions packaging/npm/lefthook/get-exe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require("path")

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

// Detect architecture
// https://nodejs.org/api/process.html#process_process_arch
let arch = process.arch;

return require.resolve(`lefthook-${os}-${arch}/bin/lefthook${extension}`)
}

exports.getExePath = getExePath;
29 changes: 29 additions & 0 deletions packaging/npm/lefthook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "lefthook",
"version": "1.0.0",
"description": "Simple git hooks manager",
"main": "index.js",
"repository": "https://github.com/evilmartians/lefthook",
"bin": {
"lefthook": "bin/index.js"
},
"keywords": [
"git",
"hook",
"manager"
],
"author": "mrexox",
"license": "MIT",
"bugs": {
"url": "https://github.com/evilmartians/lefthook/issues"
},
"homepage": "https://github.com/evilmartians/lefthook#readme",
"optionalDependencies": {
"lefthook-darwin-arm64": "1.0.0",
"lefthook-darwin-x64": "1.0.0",
"lefthook-linux-arm64": "1.0.0",
"lefthook-linux-x64": "1.0.0",
"lefthook-windows-arm64": "1.0.0",
"lefthook-windows-x64": "1.0.0"
}
}

0 comments on commit c8521e5

Please sign in to comment.