commit-sheriff 1.5.0 → 1.6.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 CHANGED
@@ -56,11 +56,10 @@ Different repos tend to drift into different commit-message and branch-naming co
56
56
  npm install --save-dev commit-sheriff husky
57
57
  ```
58
58
 
59
- `lint-staged`, `eslint`, and `prettier` are optional — install them too if you want the pre-commit hook to lint/format staged files:
60
-
61
- ```bash
62
- npm install --save-dev lint-staged eslint prettier
63
- ```
59
+ `npx commit-sheriff init` installs `lint-staged`, `eslint`, and `prettier` for you automatically
60
+ (via `npm install --save-dev`) if they're missing, so the pre-commit lint/format step is actually
61
+ functional right away — you don't need to install them yourself first. This only happens as part
62
+ of `init` (see below), not from installing `commit-sheriff` itself.
64
63
 
65
64
  ## Usage
66
65
 
@@ -77,10 +76,19 @@ Run this once per repo, from the repo root (where `package.json` lives). This wo
77
76
  3. Creates a default `.commitsheriffrc.json` file at the repo root **only if one doesn't already exist** (and only if there's no legacy `package.json` → `commitGuard` block either — see [Configuration](#configuration-commitsheriffrcjson)) — re-running `init` never overwrites your customized config.
78
77
  4. Adds a default `lint-staged` block to `package.json` **only if one doesn't already exist**.
79
78
  5. Adds a `"prepare": "husky"` npm script if missing, so hooks are (re)installed automatically after `npm install`.
80
- 6. If `react` and `typescript` are both present in your `dependencies`/`devDependencies`, also runs [`add-eslint-react`](#advanced-eslint-module-boundary-template-typescriptreact) automatically, which writes the ESLint flat-config module-boundary template (as `eslint.config.mjs` if you have none yet, or as a separate `eslint.config.commit-sheriff.mjs` if you already have one) and **overwrites** `.prettierrc.js` with the latest template. Otherwise it's skipped and you can add it manually later.
79
+ 6. Installs `lint-staged`, `eslint`, and `prettier` as devDependencies (via `npm install --save-dev`) if any of them are missing, so the pre-commit lint/format step actually runs instead of silently no-oping.
80
+ 7. If `react` and `typescript` are both present in your `dependencies`/`devDependencies`, also runs [`add-eslint-react`](#advanced-eslint-module-boundary-template-typescriptreact) automatically, which writes the ESLint flat-config module-boundary template (as `eslint.config.mjs` if you have none yet, or as a separate `eslint.config.commit-sheriff.mjs` if you already have one), **overwrites** `.prettierrc.js` with the latest template, and installs the extra plugins it needs. Otherwise it's skipped and you can add it manually later.
81
81
 
82
82
  Re-running `npx commit-sheriff init` later (e.g. after updating the package) is safe for your project-specific settings: it refreshes the hook scripts and (on react+typescript projects) the ESLint/Prettier templates to the latest version, but leaves your `.commitsheriffrc.json` / `lint-staged` config alone. If you've hand-edited `.prettierrc.js`, or `eslint.config.mjs` in the no-existing-config case, back them up first — those get overwritten on every re-run (the side-file case, `eslint.config.commit-sheriff.mjs`, is also overwritten each time, but your own `eslint.config.mjs` is never touched there).
83
83
 
84
+ **Why the auto-install matters**: the pre-commit hook only runs `lint-staged` if it can actually
85
+ find the package — if it's missing, older versions of this hook **silently skipped linting
86
+ entirely**, so a broken/incomplete setup looked identical to a working one until someone noticed
87
+ bad code slipping through. The hook itself is now hardened too: if `package.json` has a
88
+ `lint-staged` config block but the package isn't actually installed (e.g. `node_modules` got
89
+ wiped, or someone added the config by hand), the commit now **fails loudly** with instructions,
90
+ instead of passing silently.
91
+
84
92
  ## Commit message format
85
93
 
86
94
  ```
@@ -227,7 +235,7 @@ Allowed `type` words in **commit messages**, following [Conventional Commits](ht
227
235
  In addition to the branch name check, `pre-commit` conditionally runs (skipped when the project doesn't have the relevant tool):
228
236
 
229
237
  - `npm run type-check` — if a `type-check` script exists in `package.json`
230
- - `npx lint-staged` — if `lint-staged` is listed as a dependency
238
+ - `npx lint-staged` — if `package.json` has a `lint-staged` config block. If the block exists but the `lint-staged` package itself isn't actually installed, the commit **fails with instructions** rather than silently skipping the check (this used to silently no-op — see [What `init` does](#what-init-does)).
231
239
  - `npx vitest related --run --passWithNoTests <staged .ts/.tsx files>` — if `vitest` is listed as a dependency and staged `.ts`/`.tsx` files exist (bounded to 60s via `timeout`/`gtimeout` when available)
232
240
 
233
241
  Any failure here aborts the commit.
@@ -292,15 +300,24 @@ just fill in `modules` in `.commitsheriffrc.json` and it picks it up automatical
292
300
  This template assumes a `src/app/<module>/...` folder layout with `<module>.public.ts` barrel
293
301
  files; adjust the paths inside the generated config if your structure differs.
294
302
 
295
- It doesn't install any dependencies for you install what your project needs, e.g.:
303
+ It installs whatever it needs automatically (via `npm install --save-dev`, same as `init` does for
304
+ `lint-staged`/`eslint`/`prettier`), skipping anything already present:
296
305
 
297
306
  ```bash
298
- npm install --save-dev typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin \
299
- @eslint/js @eslint/eslintrc eslint-plugin-sonarjs eslint-plugin-import eslint-plugin-prettier \
300
- eslint-config-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y \
301
- eslint-plugin-testing-library @vitest/eslint-plugin eslint-plugin-jest prettier
307
+ npm install --save-dev eslint@^9 typescript @typescript-eslint/parser \
308
+ @typescript-eslint/eslint-plugin @eslint/js @eslint/eslintrc eslint-plugin-sonarjs \
309
+ eslint-plugin-import eslint-plugin-prettier eslint-config-prettier eslint-plugin-react \
310
+ eslint-plugin-react-hooks eslint-plugin-jsx-a11y eslint-plugin-testing-library \
311
+ @vitest/eslint-plugin eslint-plugin-jest prettier
302
312
  ```
303
313
 
314
+ `eslint` is pinned to `^9` on purpose rather than left to resolve to whatever `latest` is — plugins
315
+ like `eslint-plugin-react` have historically taken a while to support each new ESLint major, so an
316
+ unpinned install can grab a version the rest of the toolchain doesn't work with yet.
317
+
318
+ If the install fails (offline, registry issue, etc.), it prints this exact command so you can run
319
+ it yourself.
320
+
304
321
  #### Why flat config?
305
322
 
306
323
  ESLint 9+ looks for `eslint.config.*` first and, if one is found, **ignores `.eslintrc.js`
@@ -91,6 +91,46 @@ function ensureConfigFile() {
91
91
  console.log(`✔ ${CONFIG_FILE_NAME} yazıldı`);
92
92
  }
93
93
 
94
+ // Installs whatever in `packages` isn't already a listed dependency/devDependency, so the hooks
95
+ // this tool wires up are actually functional right after `init`/`add-eslint-react` — not just
96
+ // documented as a copy-paste command the user has to remember to run separately. Falls back to
97
+ // printing the manual command if the install itself fails (offline, registry auth issue, etc.),
98
+ // so the user isn't left silently thinking it worked.
99
+ // Strips a trailing "@version" from an install spec to get the plain package name for looking it
100
+ // up in package.json — careful with scoped packages ("@eslint/js" has no version, but
101
+ // "@vitest/eslint-plugin@^1" does; only the *last* "@" is ever the version separator).
102
+ function packageNameOf(spec) {
103
+ const atIndex = spec.lastIndexOf("@");
104
+ return atIndex > 0 ? spec.slice(0, atIndex) : spec;
105
+ }
106
+
107
+ function ensureDevDeps(packages) {
108
+ const { pkg } = readPackageJson();
109
+ const deps = Object.assign({}, pkg.dependencies, pkg.devDependencies);
110
+ const missing = packages.filter((spec) => !deps[packageNameOf(spec)]);
111
+ if (!missing.length) {
112
+ console.log("• Lazımi devDependencies artıq quraşdırılıb, toxunulmadı");
113
+ return;
114
+ }
115
+ console.log(`→ Quraşdırılır: ${missing.join(" ")}`);
116
+ try {
117
+ // --legacy-peer-deps: some ESLint plugins lag behind the newest ESLint major in their
118
+ // declared peerDependencies (e.g. eslint-plugin-import still capping at ^9 while npm already
119
+ // resolves plain "eslint" to 10.x), which makes npm's default strict peer resolution abort
120
+ // the whole install with ERESOLVE even though the versions work fine together in practice.
121
+ execSync(`npm install --save-dev --legacy-peer-deps ${missing.join(" ")}`, {
122
+ cwd,
123
+ stdio: "inherit",
124
+ });
125
+ console.log("✔ devDependencies quraşdırıldı");
126
+ } catch {
127
+ console.log(
128
+ "\n Avtomatik quraşdırma alınmadı (şəbəkə/npm xətası ola bilər) — özünüz işlədin:\n" +
129
+ ` npm install --save-dev ${missing.join(" ")}\n`,
130
+ );
131
+ }
132
+ }
133
+
94
134
  function mergeConfig() {
95
135
  const { pkgPath, pkg } = readPackageJson();
96
136
  let changed = false;
@@ -114,6 +154,18 @@ function mergeConfig() {
114
154
  if (changed) {
115
155
  fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
116
156
  }
157
+
158
+ // Without this, the pre-commit hook's `lint-staged` step silently no-ops when the package
159
+ // itself isn't installed (it only checks whether *some* config/listing exists), so lint/format
160
+ // checks would never actually run on commit even though everything *looks* set up. `eslint`
161
+ // and `prettier` are included too since the default lint-staged config above already assumes
162
+ // both are runnable — otherwise the very first commit would fail on "command not found" instead
163
+ // of an actual lint/format problem. `eslint` is pinned to ^9 (not left to resolve to whatever
164
+ // "latest" is) because the plugin ecosystem this tool depends on (eslint-plugin-react, etc.)
165
+ // regularly lags behind ESLint's newest major for months — installing an unpinned "latest" has
166
+ // broken real installs (e.g. eslint-plugin-react crashing under ESLint 10's changed rule-context
167
+ // API) within days of a new major landing on npm.
168
+ ensureDevDeps(["lint-staged", "eslint@^9", "prettier"]);
117
169
  }
118
170
 
119
171
  function isReactTypescriptProject() {
@@ -145,6 +197,9 @@ function init() {
145
197
  }
146
198
 
147
199
  const ESLINT_REACT_DEV_DEPS = [
200
+ // Pinned (not left to resolve to "latest") — see the comment on the eslint@^9 install in
201
+ // mergeConfig() for why: the plugin ecosystem here regularly lags behind ESLint's newest major.
202
+ "eslint@^9",
148
203
  "typescript",
149
204
  "@typescript-eslint/parser",
150
205
  "@typescript-eslint/eslint-plugin",
@@ -218,10 +273,18 @@ function addEslintReact() {
218
273
  "Diqqət: eslint.config.* (və ya yuxarıdakı side-file) və .prettierrc.js hər dəfə\n" +
219
274
  "şablonun ən son versiyası ilə üzərinə yazılır (commit-msg/pre-commit hook-ları kimi)\n" +
220
275
  "— özünüzə uyğun etdiyiniz dəyişiklikləri qorumaq istəsəniz, əvvəlcə fərqli adla\n" +
221
- "backup götürün.\n\n" +
222
- "Lazımi devDependencies (özünüz quraşdırın, layihənizin real versiyalarına uyğun):\n" +
223
- ` npm install --save-dev ${ESLINT_REACT_DEV_DEPS.join(" ")}\n`,
276
+ "backup götürün.\n",
224
277
  );
278
+ ensureDevDeps(ESLINT_REACT_DEV_DEPS);
279
+ if (flatConfigAlreadyExists) {
280
+ console.log(
281
+ `\nUnutmayın: ${FLAT_CONFIG_SIDE_FILE} avtomatik import olunmur — onu öz\n` +
282
+ "eslint.config.* faylınıza əlavə etməsəniz, bu qaydalar heç vaxt işə düşməyəcək\n" +
283
+ "(yuxarıdakı nümunəyə baxın). Həmçinin, əgər layihənizdə hələ də köhnə .eslintrc.js\n" +
284
+ "varsa, onu silin — flat config varkən ESLint onu oxumur, üstəlik özü də indi\n" +
285
+ "adi .js fayl kimi lint xətaları törədə bilər.\n",
286
+ );
287
+ }
225
288
  }
226
289
 
227
290
  if (command === "init") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commit-sheriff",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "Shared husky commit-msg / pre-commit hooks enforcing ticket-based commit messages and branch naming",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -129,6 +129,12 @@ const moduleBoundaryConfig = [
129
129
  "**/*.d.ts",
130
130
  "**/*.config.js",
131
131
  "**/*.config.mjs",
132
+ // Root-level CommonJS config dotfiles (this template's own .prettierrc.js among them) aren't
133
+ // part of the TS project's `include`, so `parserOptions.project: true` below fails to parse
134
+ // them with a "TSConfig does not include this file" error unless they're excluded here.
135
+ ".eslintrc.js",
136
+ ".prettierrc.js",
137
+ ".prettierrc.cjs",
132
138
  ],
133
139
  },
134
140
  // FlatCompat translates the same legacy-style `extends`/`env`/`parserOptions`/`plugins`/`rules`
@@ -66,13 +66,28 @@ if [ "$HAS_TYPE_CHECK" = "1" ]; then
66
66
  npm run type-check || exit 1
67
67
  fi
68
68
 
69
- HAS_LINT_STAGED=$(node -e "
70
- const p = require('./package.json');
71
- const deps = Object.assign({}, p.dependencies, p.devDependencies);
72
- process.stdout.write(deps['lint-staged'] ? '1' : '0');
69
+ # Two separate signals on purpose: whether the project *wants* lint-staged (has a config block
70
+ # in package.json — commit-sheriff's own `init` writes one by default) vs. whether the package is
71
+ # *actually installed* right now. If a config exists but the package isn't resolvable, that's not
72
+ # "this project doesn't use lint-staged" it's a broken setup (e.g. someone ran `npm prune`,
73
+ # skipped `npm install`, or deleted node_modules) and used to fail silently, letting broken commits
74
+ # through with zero lint/format enforcement. Now it fails loudly instead.
75
+ HAS_LINT_CONFIG=$(node -e "
76
+ process.stdout.write(require('./package.json')['lint-staged'] ? '1' : '0');
73
77
  " 2>/dev/null)
74
- if [ "$HAS_LINT_STAGED" = "1" ]; then
75
- npx lint-staged || exit 1
78
+ if [ "$HAS_LINT_CONFIG" = "1" ]; then
79
+ if node -e "require.resolve('lint-staged')" 2>/dev/null; then
80
+ npx lint-staged || exit 1
81
+ else
82
+ echo ""
83
+ echo " package.json-da 'lint-staged' konfiqurasiyası var, amma paketin özü"
84
+ echo " node_modules-də tapılmadı — lint/format yoxlanışı keçirilə bilmədi."
85
+ echo ""
86
+ echo " Düzəltmək üçün:"
87
+ echo " npm install --save-dev lint-staged"
88
+ echo ""
89
+ exit 1
90
+ fi
76
91
  fi
77
92
 
78
93
  # ── Related tests (only if vitest is installed) ─────────────────────────────