@w5s/lefthook-config 1.0.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/README.md ADDED
@@ -0,0 +1,40 @@
1
+ <!-- AUTO-GENERATED-CONTENT:START (PKG_JSON:template=# W5S Lefthook configuration _(${name})_) -->
2
+ # W5S Lefthook configuration _(@w5s/lefthook-config)_
3
+ <!-- AUTO-GENERATED-CONTENT:END -->
4
+
5
+ [![NPM Version][package-version-svg]][package-url]
6
+ [![License][license-image]][license-url]
7
+
8
+ <!-- AUTO-GENERATED-CONTENT:START (PKG_JSON:template=> ${description}&unknownTxt= ) -->
9
+ > Lefthook shared configuration
10
+ <!-- AUTO-GENERATED-CONTENT:END -->
11
+
12
+ ## Installation
13
+
14
+ <!-- AUTO-GENERATED-CONTENT:START (PKG_JSON:template=```console\nnpm install --save-dev ${name}\n```) -->
15
+ ```console
16
+ npm install --save-dev @w5s/lefthook-config
17
+ ```
18
+ <!-- AUTO-GENERATED-CONTENT:END -->
19
+
20
+ ## Usage
21
+
22
+
23
+ ## License
24
+
25
+ <!-- AUTO-GENERATED-CONTENT:START (PKG_JSON:template=[${license}][license-url] © ${author}) -->
26
+ [MIT][license-url] © Julien Polo <julien.polo@gmail.com>
27
+ <!-- AUTO-GENERATED-CONTENT:END -->
28
+
29
+ <!-- VARIABLES -->
30
+
31
+ <!-- AUTO-GENERATED-CONTENT:START (PKG_JSON:template=[package-version-svg]: https://img.shields.io/npm/v/${name}.svg?style=flat-square) -->
32
+ [package-version-svg]: https://img.shields.io/npm/v/@w5s/lefthook-config.svg?style=flat-square
33
+ <!-- AUTO-GENERATED-CONTENT:END -->
34
+ <!-- AUTO-GENERATED-CONTENT:START (PKG_JSON:template=[package-url]: https://www.npmjs.com/package/${name}) -->
35
+ [package-url]: https://www.npmjs.com/package/@w5s/lefthook-config
36
+ <!-- AUTO-GENERATED-CONTENT:END -->
37
+ <!-- AUTO-GENERATED-CONTENT:START (PKG_JSON:template=[license-image]: https://img.shields.io/badge/license-${license}-green.svg?style=flat-square) -->
38
+ [license-image]: https://img.shields.io/badge/license-MIT-green.svg?style=flat-square
39
+ <!-- AUTO-GENERATED-CONTENT:END -->
40
+ [license-url]: ../../LICENSE
package/default.yml ADDED
@@ -0,0 +1,8 @@
1
+ # @w5s/lefthook-config — recommended preset
2
+ #
3
+ # Extend this file as-is, or cherry-pick individual fragments under hook/.
4
+ # Do not extend both this preset and a fragment it already includes.
5
+ extends:
6
+ - node_modules/@w5s/lefthook-config/hook/commit-msg/commitlint.yml
7
+ - node_modules/@w5s/lefthook-config/hook/pre-commit/eslint.yml
8
+ - node_modules/@w5s/lefthook-config/hook/pre-commit/prettier.yml
@@ -0,0 +1,4 @@
1
+ commit-msg:
2
+ commands:
3
+ commitlint:
4
+ run: bash node_modules/@w5s/lefthook-config/script/commit-msg/commitlint.sh "{1}"
@@ -0,0 +1,6 @@
1
+ pre-commit:
2
+ parallel: true
3
+ commands:
4
+ eslint:
5
+ glob: '**/*.{ts,tsx,js,jsx,mts,mtsx,cts,ctsx,cjs,mjs,json,md,yml,yaml,html}'
6
+ run: bash node_modules/@w5s/lefthook-config/script/pre-commit/eslint.sh {staged_files}
@@ -0,0 +1,6 @@
1
+ pre-commit:
2
+ parallel: true
3
+ commands:
4
+ prettier:
5
+ glob: '**/*.{ts,tsx,js,jsx,mts,mtsx,cts,ctsx,cjs,mjs,json,json5,md,mdx,yml,yaml,html,css,scss,less,vue,graphql}'
6
+ run: bash node_modules/@w5s/lefthook-config/script/pre-commit/prettier.sh {staged_files}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@w5s/lefthook-config",
3
+ "version": "1.0.0",
4
+ "description": "Lefthook shared configuration",
5
+ "keywords": [
6
+ "lefthook",
7
+ "config",
8
+ "git-hooks"
9
+ ],
10
+ "homepage": "https://github.com/w5s/project-config/blob/main/packages/lefthook-config#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/w5s/project-config/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/w5s/project-config.git",
17
+ "directory": "packages/lefthook-config"
18
+ },
19
+ "license": "MIT",
20
+ "author": "Julien Polo <julien.polo@gmail.com>",
21
+ "sideEffects": false,
22
+ "type": "module",
23
+ "exports": {
24
+ "./package.json": "./package.json",
25
+ ".": "./default.yml"
26
+ },
27
+ "files": [
28
+ "hook/",
29
+ "script/",
30
+ "default.yml",
31
+ "README.md",
32
+ "!**/*.spec.*",
33
+ "!**/*.ts",
34
+ "!**/__snapshots__/**"
35
+ ],
36
+ "scripts": {
37
+ "postpack": "clean-package restore"
38
+ },
39
+ "peerDependencies": {
40
+ "@commitlint/cli": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^20.0.0 || ^21.0.0",
41
+ "eslint": ">=8.0.0",
42
+ "lefthook": "^2.0.0",
43
+ "prettier": "2.x || 3.x"
44
+ },
45
+ "peerDependenciesMeta": {
46
+ "@commitlint/cli": {
47
+ "optional": true
48
+ },
49
+ "eslint": {
50
+ "optional": true
51
+ },
52
+ "lefthook": {
53
+ "optional": true
54
+ },
55
+ "prettier": {
56
+ "optional": true
57
+ }
58
+ },
59
+ "engines": {
60
+ "node": ">=22.0.0"
61
+ },
62
+ "publishConfig": {
63
+ "access": "public"
64
+ }
65
+ }
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ # shellcheck source=../env.sh
5
+ source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../env.sh"
6
+
7
+ commit_msg_file="${1:?commit message file path is required}"
8
+
9
+ if is_falsy "${LEFTHOOK_COMMITLINT_ENABLED:-true}"; then
10
+ log info "commitlint disabled via LEFTHOOK_COMMITLINT_ENABLED"
11
+ exit 0
12
+ fi
13
+
14
+ if ! commitlint_config_present; then
15
+ log warn "no commitlint configuration found; skipping"
16
+ exit 0
17
+ fi
18
+
19
+ command_assert commitlint "Install @commitlint/cli in this project."
20
+
21
+ exec commitlint --edit "$commit_msg_file"
package/script/env.sh ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env bash
2
+ # Shared environment for @w5s/lefthook-config hook scripts.
3
+ # Source from hook scripts via: source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../env.sh"
4
+
5
+ _env_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6
+
7
+ LEFTHOOK_CONFIG_PACKAGE="@w5s/lefthook-config"
8
+ export LEFTHOOK_CONFIG_PACKAGE
9
+
10
+ # Disable corepack download prompt
11
+ COREPACK_ENABLE_DOWNLOAD_PROMPT=${COREPACK_ENABLE_DOWNLOAD_PROMPT:-"0"}
12
+ export COREPACK_ENABLE_DOWNLOAD_PROMPT
13
+
14
+ # Prepend project-local binaries so hook scripts can call tools by name.
15
+ if [[ -d "./node_modules/.bin" ]]; then
16
+ PATH="./node_modules/.bin:${PATH}"
17
+ export PATH
18
+ fi
19
+
20
+ # Load one function per file from lib/.
21
+ if [[ -d "${_env_dir}/lib" ]]; then
22
+ for _lib_file in "${_env_dir}"/lib/*.sh; do
23
+ [[ -f "$_lib_file" ]] || continue
24
+ # shellcheck source=/dev/null
25
+ source "$_lib_file"
26
+ done
27
+ fi
28
+
29
+ unset _env_dir _lib_file
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+
3
+ _log_write() {
4
+ local level="${1:?level is required}"
5
+ local message="${2:?message is required}"
6
+
7
+ echo "${level}: [${LEFTHOOK_CONFIG_PACKAGE}] ${message}"
8
+ }
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # command_assert - Ensure a CLI is available on PATH.
4
+ #
5
+ # Usage:
6
+ # command_assert commitlint
7
+ # command_assert commitlint "Install @commitlint/cli in this project."
8
+ #
9
+ # Arguments:
10
+ # command_name CLI name to look up on PATH (required)
11
+ # fixer_message Optional hint printed on a second line when missing
12
+ command_assert() {
13
+ local command_name="${1:?command name is required}"
14
+ local fixer_message="${2:-}"
15
+ local message
16
+
17
+ command -v "$command_name" >/dev/null 2>&1 && return 0
18
+
19
+ message="${command_name} not found in PATH"
20
+ if [[ -n "$fixer_message" ]]; then
21
+ message="${message}"$'\n'"${fixer_message}"
22
+ fi
23
+
24
+ die "$message"
25
+ }
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # commitlint_config_present - Return whether commitlint configuration exists at cwd.
4
+ #
5
+ # Usage:
6
+ # if commitlint_config_present; then ...
7
+ #
8
+ # Exit status:
9
+ # 0 when a known commitlint config file or package.json commitlint key exists
10
+ # 1 otherwise
11
+ commitlint_config_present() {
12
+ config_present --app commitlint
13
+ }
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # config_present - Return whether tool configuration exists at cwd (cosmiconfig-style).
4
+ #
5
+ # Usage:
6
+ # if config_present --app commitlint; then ...
7
+ # if config_present --app eslint --package-config eslintConfig; then ...
8
+ # if config_present --app eslint --ext "json,js,cjs,mjs,yml,yaml"; then ...
9
+ #
10
+ # Options:
11
+ # --app NAME Tool/module name (required). Looks for .{NAME}rc,
12
+ # .{NAME}rc.{ext}, and {NAME}.config.{ext}.
13
+ # --package-config KEY package.json key to check (optional). Defaults to
14
+ # the same value as --app.
15
+ # --ext LIST Comma-separated file extensions (optional). Default:
16
+ # json,yaml,yml,js,cjs,mjs,ts,cts,mts
17
+ #
18
+ # Exit status:
19
+ # 0 when a matching config file or package.json key exists
20
+ # 1 otherwise
21
+ #
22
+ # Notes:
23
+ # package.json key detection uses jq when available; if jq is missing the
24
+ # package.json check is skipped (file-based configs still work).
25
+ config_present() {
26
+ local app=""
27
+ local package_config=""
28
+ local ext_list="json,yaml,yml,js,cjs,mjs,ts,cts,mts"
29
+ local config_file
30
+ local extension
31
+ local -a extensions
32
+
33
+ while [[ $# -gt 0 ]]; do
34
+ case "$1" in
35
+ --app)
36
+ app="${2:?--app requires a value}"
37
+ shift 2
38
+ ;;
39
+ --package-config)
40
+ package_config="${2:?--package-config requires a value}"
41
+ shift 2
42
+ ;;
43
+ --ext)
44
+ ext_list="${2:?--ext requires a value}"
45
+ shift 2
46
+ ;;
47
+ *)
48
+ die "config_present: unknown argument: $1"
49
+ ;;
50
+ esac
51
+ done
52
+
53
+ [[ -n "$app" ]] || die "config_present: --app is required"
54
+ [[ -n "$package_config" ]] || package_config="$app"
55
+
56
+ if [[ -f ".${app}rc" ]]; then
57
+ return 0
58
+ fi
59
+
60
+ IFS=',' read -r -a extensions <<< "$ext_list"
61
+ for extension in "${extensions[@]}"; do
62
+ # Trim surrounding whitespace from each extension token.
63
+ extension="${extension#"${extension%%[![:space:]]*}"}"
64
+ extension="${extension%"${extension##*[![:space:]]}"}"
65
+ [[ -n "$extension" ]] || continue
66
+
67
+ config_file=".${app}rc.${extension}"
68
+ if [[ -f "$config_file" ]]; then
69
+ return 0
70
+ fi
71
+
72
+ config_file="${app}.config.${extension}"
73
+ if [[ -f "$config_file" ]]; then
74
+ return 0
75
+ fi
76
+ done
77
+
78
+ if [[ -f package.json ]] && command -v jq >/dev/null 2>&1; then
79
+ jq -e --arg key "$package_config" '.[$key]' package.json >/dev/null 2>&1 && return 0
80
+ fi
81
+
82
+ return 1
83
+ }
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # die - Write a message to stderr and exit.
4
+ #
5
+ # Usage:
6
+ # die "something went wrong"
7
+ # die "missing dependency"$'\n'"Install @commitlint/cli in this project."
8
+ # die "invalid configuration" 2
9
+ #
10
+ # Arguments:
11
+ # message Message printed to stderr (required)
12
+ # status Exit status (optional, default: 1)
13
+ die() {
14
+ local message="${1:?message is required}"
15
+ local status="${2:-1}"
16
+
17
+ log error "$message"
18
+ exit "$status"
19
+ }
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # eslint_config_present - Return whether eslint configuration exists at cwd.
4
+ #
5
+ # Usage:
6
+ # if eslint_config_present; then ...
7
+ #
8
+ # Exit status:
9
+ # 0 when a known eslint config file or package.json eslintConfig key exists
10
+ # 1 otherwise
11
+ #
12
+ # Notes:
13
+ # Detects both legacy .eslintrc* / package.json eslintConfig and flat
14
+ # eslint.config.* files via config_present. The package.json key is
15
+ # eslintConfig (not eslint), so --package-config overrides the default.
16
+ eslint_config_present() {
17
+ config_present --app eslint --package-config eslintConfig
18
+ }
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # is_falsy - Return whether an environment-style value is falsy.
4
+ #
5
+ # Usage:
6
+ # if is_falsy "${LEFTHOOK_COMMITLINT_ENABLED:-true}"; then ...
7
+ #
8
+ # Arguments:
9
+ # value Value to test (required)
10
+ #
11
+ # Exit status:
12
+ # 0 when the value is falsy (empty, 0, false, no, off, n, disabled; case-insensitive)
13
+ # 1 otherwise
14
+ is_falsy() {
15
+ local value="${1-}"
16
+ local normalized
17
+
18
+ if [[ -z "$value" ]]; then
19
+ return 0
20
+ fi
21
+
22
+ normalized="$(printf '%s' "$value" | tr '[:upper:]' '[:lower:]')"
23
+
24
+ case "$normalized" in
25
+ 0 | false | no | off | n | disabled)
26
+ return 0
27
+ ;;
28
+ *)
29
+ return 1
30
+ ;;
31
+ esac
32
+ }
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # log - Write a leveled, package-prefixed message to stdout or stderr.
4
+ #
5
+ # Usage:
6
+ # log debug "resolved commitlint config at commitlint.config.js"
7
+ # log info "commitlint disabled via LEFTHOOK_COMMITLINT_ENABLED"
8
+ # log warn "no commitlint configuration found; skipping"
9
+ # log error "something went wrong"
10
+ #
11
+ # Arguments:
12
+ # level Log level: debug, info, warn, error (required)
13
+ # message Message printed to stdout (debug/info) or stderr (warn/error) (required)
14
+ log() {
15
+ local level="${1:?level is required}"
16
+ local message="${2:?message is required}"
17
+ local normalized
18
+ local label
19
+
20
+ log_enabled "$level" || return 0
21
+
22
+ normalized="$(printf '%s' "$level" | tr '[:upper:]' '[:lower:]')"
23
+
24
+ case "$normalized" in
25
+ debug)
26
+ label="Debug"
27
+ ;;
28
+ info)
29
+ label="Info"
30
+ ;;
31
+ warn | warning)
32
+ label="Warning"
33
+ ;;
34
+ error)
35
+ label="Error"
36
+ ;;
37
+ *)
38
+ label="$level"
39
+ ;;
40
+ esac
41
+
42
+ case "$normalized" in
43
+ warn | warning | error)
44
+ _log_write "$label" "$message" >&2
45
+ ;;
46
+ *)
47
+ _log_write "$label" "$message"
48
+ ;;
49
+ esac
50
+ }
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # log_level_rank - Return numeric rank for a log level name.
4
+ #
5
+ # Usage:
6
+ # rank="$(log_level_rank info)"
7
+ #
8
+ # Arguments:
9
+ # level Log level name (debug, info, warn, error; case-insensitive)
10
+ #
11
+ # Output:
12
+ # Rank on stdout: debug=0, info=1, warn=2, error=3, unknown=-1
13
+ log_level_rank() {
14
+ local level="${1-}"
15
+ local normalized
16
+
17
+ normalized="$(printf '%s' "$level" | tr '[:upper:]' '[:lower:]')"
18
+
19
+ case "$normalized" in
20
+ debug)
21
+ printf '%s' 0
22
+ ;;
23
+ info)
24
+ printf '%s' 1
25
+ ;;
26
+ warn | warning)
27
+ printf '%s' 2
28
+ ;;
29
+ error)
30
+ printf '%s' 3
31
+ ;;
32
+ *)
33
+ printf '%s' -1
34
+ ;;
35
+ esac
36
+ }
37
+
38
+ # log_enabled - Return whether a message at the given level should be emitted.
39
+ #
40
+ # Usage:
41
+ # log_enabled info || return 0
42
+ #
43
+ # Arguments:
44
+ # level Message log level (required)
45
+ #
46
+ # Exit status:
47
+ # 0 when message severity is >= LEFTHOOK_LOG_LEVEL (default: info)
48
+ # 1 otherwise
49
+ log_enabled() {
50
+ local message_level="${1:?level is required}"
51
+ local configured_level="${LEFTHOOK_LOG_LEVEL:-info}"
52
+ local message_rank
53
+ local configured_rank
54
+
55
+ message_rank="$(log_level_rank "$message_level")"
56
+ configured_rank="$(log_level_rank "$configured_level")"
57
+
58
+ if [[ "$configured_rank" == "-1" ]]; then
59
+ configured_rank="$(log_level_rank info)"
60
+ fi
61
+
62
+ if [[ "$message_rank" == "-1" ]]; then
63
+ return 1
64
+ fi
65
+
66
+ if (( message_rank >= configured_rank )); then
67
+ return 0
68
+ fi
69
+
70
+ return 1
71
+ }
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # prettier_config_present - Return whether prettier configuration exists at cwd.
4
+ #
5
+ # Usage:
6
+ # if prettier_config_present; then ...
7
+ #
8
+ # Exit status:
9
+ # 0 when a known prettier config file or package.json prettier key exists
10
+ # 1 otherwise
11
+ #
12
+ # Notes:
13
+ # Detects .prettierrc*, prettier.config.*, and a prettier key in package.json
14
+ # via config_present. Extra extensions include json5 and toml for Prettier's
15
+ # supported config file formats.
16
+ prettier_config_present() {
17
+ config_present --app prettier --ext "json,json5,yaml,yml,toml,js,cjs,mjs,ts,cts,mts"
18
+ }
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ # shellcheck source=../env.sh
5
+ source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../env.sh"
6
+
7
+ if is_falsy "${LEFTHOOK_ESLINT_ENABLED:-true}"; then
8
+ log info "eslint disabled via LEFTHOOK_ESLINT_ENABLED"
9
+ exit 0
10
+ fi
11
+
12
+ if ! eslint_config_present; then
13
+ log warn "(skip) no configuration found"
14
+ exit 0
15
+ fi
16
+
17
+ command_assert eslint "Install eslint in this project."
18
+
19
+ if [[ $# -eq 0 ]]; then
20
+ log info "(skip) no staged files to lint"
21
+ exit 0
22
+ fi
23
+
24
+ exec eslint -- "$@"
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ # shellcheck source=../env.sh
5
+ source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../env.sh"
6
+
7
+ if is_falsy "${LEFTHOOK_PRETTIER_ENABLED:-true}"; then
8
+ log info "prettier disabled via LEFTHOOK_PRETTIER_ENABLED"
9
+ exit 0
10
+ fi
11
+
12
+ if ! prettier_config_present; then
13
+ log info "(skip) no configuration found"
14
+ exit 0
15
+ fi
16
+
17
+ command_assert prettier "Install prettier in this project."
18
+
19
+ if [[ $# -eq 0 ]]; then
20
+ log info "(skip) no staged files to check"
21
+ exit 0
22
+ fi
23
+
24
+ exec prettier --check -- "$@"