lint-staged.sh 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +34 -0
- package/lint-staged.sh +39 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Iiro Jäppinen
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# `lint-staged.sh`
|
|
2
|
+
|
|
3
|
+
> Run linters against Git staged files
|
|
4
|
+
|
|
5
|
+
`lint-staged.sh` is a shell script to evaluate a command with the list of [Git](https://git-scm.com) staged files as its arguments, filtered by globs.
|
|
6
|
+
|
|
7
|
+
There are no additional features. If you are familiar with [lint-staged](https://github.com/lint-staged/lint-staged) but only use it to check if staged files are valid — _but not automatically fix them_ — you might be interested in this simpler shell script.
|
|
8
|
+
|
|
9
|
+
## Example
|
|
10
|
+
|
|
11
|
+
Run `prettier --check` with all staged JS, JSON and MD files as its arguments:
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
lint-staged.sh "prettier --check" "*.js" "*.json" "*.md"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Given staged files `index.js` and `README.md`, this will essentially run:
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
prettier --check index.js README.md
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
Copy the `lint-staged.sh` file from this repo somewhere and run it. It should work in the [Bourne shell (`sh`)](https://en.wikipedia.org/wiki/Bourne_shell).
|
|
26
|
+
|
|
27
|
+
You probably want to run `lint-staged.sh` in a Git `pre-commit` hook so that you can abort the commit if one of your commands fails.
|
|
28
|
+
|
|
29
|
+
For example, create the file `.git/hooks/pre-commit` (and make it executable):
|
|
30
|
+
|
|
31
|
+
```shell
|
|
32
|
+
#!/bin/sh
|
|
33
|
+
lint-staged.sh "prettier --check" "*.js" "*.json" "*.md"
|
|
34
|
+
```
|
package/lint-staged.sh
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env sh
|
|
2
|
+
|
|
3
|
+
DIM='\033[1;30m'
|
|
4
|
+
NC='\033[0m'
|
|
5
|
+
|
|
6
|
+
get_staged_files() {
|
|
7
|
+
git diff --staged --name-only --diff-filter ACMR -z -- "$@" | xargs --null
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
usage() {
|
|
11
|
+
echo "lint-staged.sh" - run linters against Git staged files
|
|
12
|
+
echo ""
|
|
13
|
+
echo "${DIM}Usage:${NC} lint-staged.sh \"<command>\" \"<glob>\" [\"<glob>\"]..."
|
|
14
|
+
echo "${DIM}Example:${NC} lint-staged.sh \"echo staged files:\" \"*\""
|
|
15
|
+
echo ""
|
|
16
|
+
echo "${DIM}At least one glob is required. They are passed"
|
|
17
|
+
echo "${DIM}directly to the \"git diff\" command."
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
lint_staged() {
|
|
21
|
+
if [ $# -lt 2 ]; then
|
|
22
|
+
usage
|
|
23
|
+
exit 2
|
|
24
|
+
fi
|
|
25
|
+
|
|
26
|
+
local command="$1"
|
|
27
|
+
shift # remove first argument
|
|
28
|
+
|
|
29
|
+
local files=$(get_staged_files "$@")
|
|
30
|
+
if [ -n "$files" ]; then
|
|
31
|
+
echo "${DIM}${command} $files${NC}"
|
|
32
|
+
eval "$command" "$files"
|
|
33
|
+
echo ""
|
|
34
|
+
fi
|
|
35
|
+
|
|
36
|
+
exit 0
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
lint_staged "$@"
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lint-staged.sh",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Run linters against Git staged files",
|
|
5
|
+
"author": "Iiro Jäppinen <iiro@jappinen.fi> (https://iiro.fi)",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/lint-staged/lint-staged.sh.git"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"lint-staged.sh": "lint-staged.sh"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lint-staged.sh"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"version": "commit-and-tag-version"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@commitlint/cli": "20.4.1",
|
|
22
|
+
"@commitlint/config-conventional": "20.4.1",
|
|
23
|
+
"commit-and-tag-version": "12.6.1",
|
|
24
|
+
"husky": "9.1.7",
|
|
25
|
+
"prettier": "3.8.1"
|
|
26
|
+
}
|
|
27
|
+
}
|