lefthook
lefthook copied to clipboard
Allow setting env vars
Currently as far as I can see the only way to specify an environment variable is to use something like this:
pre-commit:
parallel: true
commands:
cmd:
run: FOO=1 printenv FOO
Though it would be nice if we could use an env
-object to specify the environment like this:
pre-commit:
parallel: true
commands:
cmd:
env:
FOO: "1"
run: printenv FOO
This would be very cool for Windows users, as the documented syntax for using environment variables does in fact not work in powershell or other Windows-based shells.
The following snippet is somewhat challenging to do cross platform.
pre-commit:
lint:
run: FORCE_COLOR=1 yarn eslint {staged_files}
Without FORCE_COLOR it won't show any colors at all.
More specifically: yarn lefthook run pre-commit
will show colors in the sub-commands but git commit
will not).
Husky + lint-staged is suffering from the same problem by the way. Passing an env
would effectively solve 80% of it.
I won't say it's better in pre-commit, but I ended up having less confusion with many of the hooks out of the box and local execution. I want to use Lefthook more because the local shell invocation in pre-commit also suffers from some complications lefthook didn't (fails to inherit certain env configuration values), but until I know of a way to invoke across Linux/Darwin/Windows more easily I can't do that. Env configuration is one key piece of this.
I'll say Taskfile states:
Task uses mvdan.cc/sh, a native Go sh interpreter. So you can write sh/bash commands, and it will work even on Windows, where sh or bash are usually not available. Just remember any executable called must be available by the OS or in PATH.
Script is also pretty cool, and has some nice support for Windows with a less verbose invocation. I'd be fine writing all my lefthook tasks in Go directly if I could figure it out, but worse case, would be good to know the basic commands are executed in a limited shell environment that is platform agnostic.
If there ends up being any documentation or examples on adding my own main.go
file as a "plugin" to just call I might end up revisiting Lefthook even if pre-commit has more out of the box hooks.
Keep up the good work!
I think this is a possible feature to 1.2.0 version. I mean, a settings with ENV vars.
If there ends up being any documentation or examples on adding my own main.go file as a "plugin" to just call I might end up revisiting Lefthook even if pre-commit has more out of the box hooks.
@sheldonhull , what do you mean by "plugin"? You always can use the scripts
, for example, written in go:
// .lefthook/pre-commit/my.go
package main
import "fmt"
func main() {
fmt.Println("Hi!")
}
# lefthook.yml
pre-commit:
scripts:
"my.go":
runner: go run
Is it something that you need?
I'll have to try that out! That's sorta what I'm trying to figure out. I have Mage
a Go command line tool that wraps up most of my tasks. I didn't know how to properly invoke. I'll give this a shot.
I could probably leave the pre-commit ecosystem for a bit simplier and integrated control, but cross platform and invocation of my tasks got problematic with things hanging and such. Thanks for the response. When I can I'll take another swing at it! I have a lot of prebuilt templates I'll see about revamping if so. ci-configuration files for example.