@syntopica/quality-config 0.12.1 → 0.13.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) BusiRocket
3
+ Copyright (c) Syntopica
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/PUBLIC_API.md CHANGED
@@ -38,10 +38,11 @@ then cruise separately with `scope: 'workspace'`.
38
38
 
39
39
  ## Stable executables (semver)
40
40
 
41
- | Binary | Purpose |
42
- | ------------------- | ---------------------------------------------------------- |
43
- | `baseline-env-init` | Seed a gitignored `.env` from the committed `.env.example` |
44
- | `baseline-dupes` | Run jscpd against this package's canonical `jscpd.json` |
41
+ | Binary | Purpose |
42
+ | ------------------------ | ---------------------------------------------------------- |
43
+ | `baseline-env-init` | Seed a gitignored `.env` from the committed `.env.example` |
44
+ | `baseline-hooks-install` | Run `lefthook install` only from the git repository root |
45
+ | `baseline-dupes` | Run jscpd against this package's canonical `jscpd.json` |
45
46
 
46
47
  `baseline-env-init` takes no arguments and always exits 0. It copies
47
48
  `.env.example` to `.env` in the current working directory, does nothing when
@@ -49,6 +50,11 @@ then cruise separately with `scope: 'workspace'`.
49
50
  into `prepare` so a freshly cloned project boots with the documented example
50
51
  values instead of failing its startup env validation.
51
52
 
53
+ `baseline-hooks-install` forwards its arguments to `lefthook install` and exits
54
+ with its status. It exits 0 without installing when the current directory is not
55
+ the git repository root (a package nested in a workspace, whose root owns the
56
+ hooks) or is not inside a git repository at all.
57
+
52
58
  `baseline-dupes` takes the paths to scan (a bare call scans `.`) and forwards
53
59
  every other argument to jscpd, where it wins over the config file. jscpd 5.x is
54
60
  a Rust binary with no JS config loader, which is why this gate ships as a JSON
package/README.md CHANGED
@@ -45,7 +45,7 @@ renders and reports a blank page rather than a useful error.
45
45
  ```json
46
46
  {
47
47
  "scripts": {
48
- "prepare": "lefthook install && baseline-env-init"
48
+ "prepare": "baseline-hooks-install && baseline-env-init"
49
49
  }
50
50
  }
51
51
  ```
@@ -54,6 +54,15 @@ Idempotent: an existing `.env` is never overwritten, and a project with no
54
54
  `.env.example` is a no-op. It never writes a value into version control - `.env`
55
55
  stays gitignored, `.env.example` stays the only committed copy.
56
56
 
57
+ ### `baseline-hooks-install`
58
+
59
+ Runs `lefthook install`, but only when the package is the root of its git
60
+ repository. A package nested in a workspace skips the install, because lefthook
61
+ would rewrite the enclosing repository's `.git/hooks`, and pnpm runs every
62
+ workspace package's `prepare` in parallel: two packages replacing the same hook
63
+ at once fail the install. Outside a git repository it skips too. Arguments are
64
+ forwarded to `lefthook install`.
65
+
57
66
  ### `baseline-dupes`
58
67
 
59
68
  Runs the cross-file duplication gate against this package's canonical
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env node
2
+ // Installs the project's lefthook git hooks, but only from the repository root.
3
+ //
4
+ // `prepare: lefthook install` is right for a standalone project and wrong for a
5
+ // package nested in a workspace: lefthook walks up to the enclosing repository
6
+ // and rewrites its `.git/hooks`, and pnpm runs every workspace package's
7
+ // `prepare` in parallel. In this monorepo eight templates did that at once, and
8
+ // two of them replacing the same hook raced:
9
+ // `could not replace the hook: remove .git/hooks/pre-push: no such file or directory`
10
+ // failed a CI `pnpm install --frozen-lockfile` (green on re-run). Even when it
11
+ // did not race, the last template to finish left the root's hooks pointing at
12
+ // its own lefthook binary.
13
+ //
14
+ // The repository root owns its hooks, so a nested package skips the install
15
+ // and says why. Outside any git repository (an unpacked tarball, a CI cache
16
+ // restore) there is nothing to install into, so it skips too. Any argument is
17
+ // forwarded to `lefthook install`.
18
+ import { spawnSync } from 'node:child_process'
19
+ import { realpathSync } from 'node:fs'
20
+ import { argv, cwd, exit, platform } from 'node:process'
21
+
22
+ const toplevel = spawnSync('git', ['rev-parse', '--show-toplevel'], {
23
+ encoding: 'utf8',
24
+ })
25
+
26
+ if (toplevel.status !== 0) {
27
+ console.log('baseline-hooks-install: not a git repository, skipped.')
28
+ exit(0)
29
+ }
30
+
31
+ // Both paths are the invoking repository's own, from git and the working
32
+ // directory - not untrusted input. Resolved so a symlinked checkout compares
33
+ // equal to itself.
34
+ /* eslint-disable security/detect-non-literal-fs-filename */
35
+ const root = realpathSync(toplevel.stdout.trim())
36
+ const here = realpathSync(cwd())
37
+ /* eslint-enable security/detect-non-literal-fs-filename */
38
+
39
+ if (root !== here) {
40
+ console.log(
41
+ `baseline-hooks-install: ${here} is nested in ${root}, which owns the hooks; skipped.`,
42
+ )
43
+ exit(0)
44
+ }
45
+
46
+ const install = spawnSync('lefthook', ['install', ...argv.slice(2)], {
47
+ stdio: 'inherit',
48
+ shell: platform === 'win32',
49
+ })
50
+
51
+ exit(install.status ?? 1)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syntopica/quality-config",
3
- "version": "0.12.1",
3
+ "version": "0.13.1",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "description": "Shared configuration for cross-file quality gates: knip, dependency-cruiser, type-coverage, jscpd, lefthook",
@@ -25,6 +25,7 @@
25
25
  "baseline-deps-graph": "bin/baseline-deps-graph.mjs",
26
26
  "baseline-dupes": "bin/baseline-dupes.mjs",
27
27
  "baseline-env-init": "bin/baseline-env-init.mjs",
28
+ "baseline-hooks-install": "bin/baseline-hooks-install.mjs",
28
29
  "baseline-licenses": "bin/baseline-licenses.mjs",
29
30
  "baseline-type-coverage": "bin/baseline-type-coverage.mjs"
30
31
  },
@@ -79,14 +80,16 @@
79
80
  "eslint-plugin-regexp": "^3.3.1",
80
81
  "knip": "^6.40.0",
81
82
  "publint": "^0.3.25",
82
- "typescript": "npm:@typescript/typescript6@^6.0.2"
83
+ "typescript": "npm:@typescript/typescript6@^6.0.2",
84
+ "vitest": "^5.0.3"
83
85
  },
84
86
  "scripts": {
85
87
  "fix": "pnpm run lint:fix",
86
- "lint": "eslint src bin --max-warnings 0",
87
- "lint:fix": "eslint src bin --fix",
88
- "lint:suppress": "eslint src bin --suppress-all",
89
- "lint:prune": "eslint src bin --prune-suppressions",
88
+ "lint": "eslint src bin tests --max-warnings 0",
89
+ "lint:fix": "eslint src bin tests --fix",
90
+ "lint:suppress": "eslint src bin tests --suppress-all",
91
+ "lint:prune": "eslint src bin tests --prune-suppressions",
92
+ "test": "vitest run",
90
93
  "type-check": "tsc --noEmit -p tsconfig.json",
91
94
  "publish:check": "baseline-licenses && publint --strict && attw --pack . --profile node16 --ignore-rules cjs-resolves-to-esm internal-resolution-error"
92
95
  }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The binaries each runner dependency provides, keyed by package name. A
3
+ * script that names one of them directly is something knip resolves on its
4
+ * own, so the package needs no `ignoreDependencies` entry; a script that only
5
+ * names the `baseline-*` runner hides it.
6
+ */
7
+ export const RUNNER_BINARIES: Record<string, string[]> = {
8
+ jscpd: ['jscpd'],
9
+ 'type-coverage': ['type-coverage'],
10
+ 'dependency-cruiser': ['depcruise', 'dependency-cruise'],
11
+ lefthook: ['lefthook'],
12
+ }
@@ -0,0 +1,19 @@
1
+ import { existsSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+
4
+ const GLOB_CHARACTERS = /[*?{}[\]!]/u
5
+
6
+ /**
7
+ * Drops the literal entry paths that do not exist in `cwd`; globs are kept,
8
+ * since whether they match is knip's call. Used for a preset's default entry
9
+ * when the caller names its own: a CLI package whose entry is
10
+ * `src/cli/main.ts` has no `src/index.ts`, and listing it is a "Refine entry
11
+ * pattern (no matches)" hint the caller cannot silence.
12
+ */
13
+ export const existingEntries = (entries: string[], cwd: string): string[] =>
14
+ entries.filter(
15
+ (entry) =>
16
+ GLOB_CHARACTERS.test(entry) ||
17
+ // eslint-disable-next-line security/detect-non-literal-fs-filename -- the entry is the preset's own literal, resolved against the project root
18
+ existsSync(join(cwd, entry)),
19
+ )
package/src/knip.ts CHANGED
@@ -5,7 +5,9 @@
5
5
  // config without casting it first.
6
6
  import type { KnipConfiguration } from 'knip'
7
7
 
8
+ import { existingEntries } from './existingEntries'
8
9
  import { FRAMEWORK_ENTRIES, type KnipFramework } from './knip-framework'
10
+ import { runnerDependenciesToIgnore } from './runnerDependenciesToIgnore'
9
11
 
10
12
  // Every template installs these as peer dependencies of
11
13
  // @syntopica/eslint-config: the config factories (base.ts and friends)
@@ -30,24 +32,6 @@ import { FRAMEWORK_ENTRIES, type KnipFramework } from './knip-framework'
30
32
  // not a gate failure. Do not filter the list consumer-side to silence it: the
31
33
  // filter would drift the moment this list changes, and the cost of being wrong
32
34
  // is a dependency that silently stops being checked.
33
- // The tools this package's own runners spawn through `pnpm exec`:
34
- // `baseline-dupes` spawns `jscpd`, `baseline-type-coverage` spawns
35
- // `type-coverage`, `baseline-deps-graph` spawns `depcruise`. A project that
36
- // wires the runner into its scripts never names the underlying tool anywhere
37
- // knip can see, so knip reports a real dependency as unused. Found in
38
- // a consumer the moment its `type-coverage` script became
39
- // `baseline-type-coverage`.
40
- //
41
- // `dependency-cruiser` is on the list even though a project calling
42
- // `depcruise` directly resolves it fine: there knip emits a `Remove from
43
- // ignoreDependencies` hint, which is a hint and not a gate failure, and the
44
- // cost of leaving it off is a real dependency silently reported as dead.
45
- const BASELINE_RUNNER_DEPENDENCIES = [
46
- 'jscpd',
47
- 'type-coverage',
48
- 'dependency-cruiser',
49
- ]
50
-
51
35
  const ESLINT_PEER_DEPENDENCIES = [
52
36
  '@eslint/js',
53
37
  'eslint-config-prettier',
@@ -102,7 +86,9 @@ export const createKnipConfig = (options: {
102
86
  /**
103
87
  * Extra entry globs, merged with the framework preset's `entry`. Same
104
88
  * reasoning as `project`, for a root nobody's plugin registers - a worker,
105
- * a CLI, a script the app spawns.
89
+ * a CLI, a script the app spawns. A preset entry that is a literal path
90
+ * missing from the project (`ts-package`'s `src/index.ts` in a CLI package)
91
+ * is dropped once this is set, so it cannot become a "no matches" hint.
106
92
  */
107
93
  entry?: string[]
108
94
  /**
@@ -120,12 +106,17 @@ export const createKnipConfig = (options: {
120
106
  * only this does.
121
107
  */
122
108
  drizzle?: false
109
+ /** The project root, for the checks that read it. Defaults to the cwd. */
110
+ cwd?: string
123
111
  }): KnipConfiguration => {
112
+ const cwd = options.cwd ?? process.cwd()
124
113
  const { entry, project, includeEntryExports } =
125
114
  FRAMEWORK_ENTRIES[options.framework]
126
115
 
127
116
  return {
128
- entry: [...entry, ...(options.entry ?? [])],
117
+ entry: options.entry
118
+ ? [...existingEntries(entry, cwd), ...options.entry]
119
+ : entry,
129
120
  project: [...project, ...(options.project ?? [])],
130
121
  ...(options.ignore ? { ignore: options.ignore } : {}),
131
122
  ...(options.drizzle === false ? { drizzle: false } : {}),
@@ -149,7 +140,7 @@ export const createKnipConfig = (options: {
149
140
  ignoreExportsUsedInFile: true,
150
141
  ignoreBinaries: ['gitleaks', ...(options.ignoreBinaries ?? [])],
151
142
  ignoreDependencies: [
152
- ...BASELINE_RUNNER_DEPENDENCIES,
143
+ ...runnerDependenciesToIgnore(cwd),
153
144
  ...ESLINT_PEER_DEPENDENCIES,
154
145
  ...(options.ignoreDependencies ?? []),
155
146
  ],
@@ -0,0 +1,23 @@
1
+ import { readFileSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+
4
+ /**
5
+ * Every word of every `package.json` script in `cwd`, so a caller can ask
6
+ * whether a binary is named anywhere. Unreadable or malformed manifests give
7
+ * an empty set: the caller then ignores the dependency, which is the safe side.
8
+ */
9
+ export const readScriptWords = (cwd: string): Set<string> => {
10
+ try {
11
+ const manifest: unknown = JSON.parse(
12
+ // eslint-disable-next-line security/detect-non-literal-fs-filename -- fixed file name under the project root
13
+ readFileSync(join(cwd, 'package.json'), 'utf8'),
14
+ )
15
+ const scripts = (manifest as { scripts?: Record<string, unknown> }).scripts
16
+ const text = Object.values(scripts ?? {})
17
+ .filter((script): script is string => typeof script === 'string')
18
+ .join('\n')
19
+ return new Set(text.split(/[\s;&|()]+/u).filter(Boolean))
20
+ } catch {
21
+ return new Set()
22
+ }
23
+ }
@@ -0,0 +1,20 @@
1
+ import { RUNNER_BINARIES } from './RUNNER_BINARIES'
2
+ import { readScriptWords } from './readScriptWords'
3
+
4
+ /**
5
+ * The runner dependencies knip cannot see used, and so must be ignored.
6
+ *
7
+ * `baseline-dupes`, `baseline-type-coverage`, `baseline-deps-graph` and
8
+ * `baseline-hooks-install` spawn `jscpd`, `type-coverage`, `depcruise` and
9
+ * `lefthook` themselves, which hides a real dependency from knip. A project
10
+ * that also names the binary in a script (`"prepare": "lefthook install"`,
11
+ * `"deps:graph": "depcruise src"`) lets knip resolve it, and ignoring it then
12
+ * becomes a permanent `Remove from ignoreDependencies` hint no consumer can
13
+ * silence. So only the packages no script names directly are ignored.
14
+ */
15
+ export const runnerDependenciesToIgnore = (cwd: string): string[] => {
16
+ const words = readScriptWords(cwd)
17
+ return Object.entries(RUNNER_BINARIES)
18
+ .filter(([, binaries]) => !binaries.some((binary) => words.has(binary)))
19
+ .map(([dependency]) => dependency)
20
+ }
package/tsconfig.json CHANGED
@@ -13,5 +13,10 @@
13
13
  "types": ["node"],
14
14
  "allowImportingTsExtensions": true
15
15
  },
16
- "include": ["src/**/*.ts", "eslint.config.ts"]
16
+ "include": [
17
+ "src/**/*.ts",
18
+ "tests/**/*.ts",
19
+ "eslint.config.ts",
20
+ "vitest.config.ts"
21
+ ]
17
22
  }