@geonosis/ratchet 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 +202 -0
- package/README.md +91 -0
- package/bin/geonosis-ratchet.mjs +4 -0
- package/dist/chunk-VX2PL7QG.js +395 -0
- package/dist/cli.js +23 -0
- package/dist/index.js +22 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# @geonosis/ratchet
|
|
2
|
+
|
|
3
|
+
Debt as a number that may only shrink. It measures a repo's counters, compares each against a
|
|
4
|
+
committed baseline, and fails **only** on regression — so a repo can adopt a gate the day it is
|
|
5
|
+
written, with the existing debt named rather than forgiven, and nothing new can land behind it.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add -D @geonosis/ratchet
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
geonosis-ratchet # every counter
|
|
15
|
+
geonosis-ratchet --tier fast # only the counters in that tier
|
|
16
|
+
geonosis-ratchet --cwd apps/web
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Exit 1 when a number grew, 2 when a counter could not measure at all, 0 otherwise.
|
|
20
|
+
|
|
21
|
+
## Config
|
|
22
|
+
|
|
23
|
+
`geonosis.ratchet.json` at the repo root names the counters and how to run them:
|
|
24
|
+
|
|
25
|
+
```jsonc
|
|
26
|
+
{
|
|
27
|
+
"baseline": "gate-baseline.json",
|
|
28
|
+
"counters": [
|
|
29
|
+
{ "counter": "oxlintErrors", "command": "npx oxlint --format=unix ." },
|
|
30
|
+
{ "counter": "typecheckErrors", "command": "npx tsc --noEmit" },
|
|
31
|
+
{ "counter": "cloneCount", "command": "npx jscpd .", "tiers": ["full"] },
|
|
32
|
+
{ "counter": "lawLineCount", "path": "CLAUDE.md" }
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
- `key` — the baseline key this entry writes. Defaults to `counter`; required when one counter is
|
|
38
|
+
used twice. Two entries writing one key is a config error, not a race.
|
|
39
|
+
- `tiers` — the tiers this entry belongs to. Absent means every tier, so a repo that never passes
|
|
40
|
+
`--tier` is unaffected. Names are free-form; `fast` and `full` are convention.
|
|
41
|
+
|
|
42
|
+
`gate-baseline.json` holds one number per key (plus any `_comment`, which is preserved):
|
|
43
|
+
|
|
44
|
+
```json
|
|
45
|
+
{ "oxlintErrors": 21, "typecheckErrors": 0, "cloneCount": 40, "lawLineCount": 137 }
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
A number that **grew** fails. A number that **shrank** rewrites the baseline down, in place, so the
|
|
49
|
+
win is locked in by the same commit that earned it. Equal passes.
|
|
50
|
+
|
|
51
|
+
A counter outside the requested `--tier` is not measured and says so —
|
|
52
|
+
`SKIP cloneCount: not measured by --tier fast`. It never counts as held, never reaches the exit
|
|
53
|
+
code, and its baseline survives a rewrite another counter earned: printing OK for a census nobody
|
|
54
|
+
took is how a red main goes unnoticed for three commits.
|
|
55
|
+
|
|
56
|
+
A counter whose command cannot run is a hard error, never a silent zero. A gate that cannot measure
|
|
57
|
+
has not passed.
|
|
58
|
+
|
|
59
|
+
## Counters
|
|
60
|
+
|
|
61
|
+
Every counter takes its `command` from the config, so the toolchain stays the repo's own.
|
|
62
|
+
|
|
63
|
+
| id | counts | params |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| `oxlintErrors` / `oxlintWarnings` | `: error ` / `: warning ` lines | `command` |
|
|
66
|
+
| `oxlintRule` | one named rule's findings; with `config`, after forcing it to `error` in a temp copy, so debt cannot grow behind a downgrade | `rule`, `config`, `command` |
|
|
67
|
+
| `typecheckErrors` | `error TS` occurrences | `command` |
|
|
68
|
+
| `testFailures` | the runner's own failure summary; throws when neither a pass nor a fail count is readable | `command` |
|
|
69
|
+
| `unformattedFiles` | paths `--list-different` names that exist on disk | `command` |
|
|
70
|
+
| `cloneCount` | jscpd's `Found N clones` | `command` |
|
|
71
|
+
| `knipIssues` | the totals under knip's unused-* headings | `command`, `headings` |
|
|
72
|
+
| `boundaryIssues` | `N issues found` from a boundary scan | `command` |
|
|
73
|
+
| `archViolations` | lines matching a marker your own architecture scan prints | `command`, `match` |
|
|
74
|
+
| `sumOfCounts` | the total of one capture group across a per-file census (`grep -rc`) | `command`, `match` |
|
|
75
|
+
| `lawLineCount` | the lines of the law file — a ceiling that can only come down | `path` |
|
|
76
|
+
| `runtimeCodeShipped` | 0 when a change shipped runtime code, 1 when it shipped none | `command`, `patterns` |
|
|
77
|
+
|
|
78
|
+
Two example configs from real repos live in `examples/` in the repository.
|
|
79
|
+
|
|
80
|
+
## Programmatic use
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { COUNTERS, formatReport, runRatchet } from '@geonosis/ratchet'
|
|
84
|
+
|
|
85
|
+
const result = await runRatchet({ counters: COUNTERS, cwd: process.cwd(), tier: 'fast' })
|
|
86
|
+
process.stdout.write(formatReport(result))
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`counters` is a plain array, so a repo can add its own `{ id, run }` beside the built-ins.
|
|
90
|
+
|
|
91
|
+
Apache-2.0.
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var CounterError = class extends Error {
|
|
3
|
+
constructor(counter, message) {
|
|
4
|
+
super(`${counter}: ${message}`);
|
|
5
|
+
this.counter = counter;
|
|
6
|
+
this.name = "CounterError";
|
|
7
|
+
}
|
|
8
|
+
counter;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/counters/params.ts
|
|
12
|
+
var stringParam = (counter, params, name, fallback) => {
|
|
13
|
+
const value = params[name];
|
|
14
|
+
if (typeof value === "string" && value !== "") return value;
|
|
15
|
+
if (fallback !== void 0) return fallback;
|
|
16
|
+
throw new CounterError(counter, `needs a "${name}" in geonosis.ratchet.json`);
|
|
17
|
+
};
|
|
18
|
+
var stringsParam = (params, name, fallback) => {
|
|
19
|
+
const value = params[name];
|
|
20
|
+
if (!Array.isArray(value)) return fallback;
|
|
21
|
+
return value.filter((one) => typeof one === "string");
|
|
22
|
+
};
|
|
23
|
+
var countMatches = (text, pattern) => text.match(new RegExp(pattern.source, `${pattern.flags.replace("g", "")}g`))?.length ?? 0;
|
|
24
|
+
var escapeForRegex = (value) => value.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
25
|
+
|
|
26
|
+
// src/counters/arch.ts
|
|
27
|
+
var archViolations = {
|
|
28
|
+
id: "archViolations",
|
|
29
|
+
run: async ({ params, run }) => {
|
|
30
|
+
const command = stringParam("archViolations", params, "command");
|
|
31
|
+
const match = new RegExp(stringParam("archViolations", params, "match", "^\u2717"));
|
|
32
|
+
return run(command).output.split("\n").filter((line) => match.test(line)).length;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// src/counters/duplication.ts
|
|
37
|
+
var CLONES = /Found (\d+) clones?/;
|
|
38
|
+
var cloneCount = {
|
|
39
|
+
id: "cloneCount",
|
|
40
|
+
run: async ({ params, run }) => {
|
|
41
|
+
const command = stringParam("cloneCount", params, "command", "npx jscpd .");
|
|
42
|
+
const output = run(command).output;
|
|
43
|
+
const found = output.match(CLONES)?.[1];
|
|
44
|
+
if (found === void 0) {
|
|
45
|
+
throw new CounterError(
|
|
46
|
+
"cloneCount",
|
|
47
|
+
`no "Found N clones" line:
|
|
48
|
+
${output.trim().slice(-500)}`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return Number(found);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
var DEFAULT_HEADINGS = [
|
|
55
|
+
"Unused files",
|
|
56
|
+
"Unused dependencies",
|
|
57
|
+
"Unused devDependencies",
|
|
58
|
+
"Unused exports",
|
|
59
|
+
"Unused exported types"
|
|
60
|
+
];
|
|
61
|
+
var knipIssues = {
|
|
62
|
+
id: "knipIssues",
|
|
63
|
+
run: async ({ params, run }) => {
|
|
64
|
+
const command = stringParam("knipIssues", params, "command", "npx knip --reporter compact");
|
|
65
|
+
const output = run(command).output;
|
|
66
|
+
return stringsParam(params, "headings", DEFAULT_HEADINGS).map((heading) => Number(output.match(new RegExp(`${heading} \\((\\d+)\\)`))?.[1] ?? 0)).reduce((sum, count) => sum + count, 0);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var ISSUES = /(\d+) issues? found/;
|
|
70
|
+
var boundaryIssues = {
|
|
71
|
+
id: "boundaryIssues",
|
|
72
|
+
run: async ({ params, run }) => {
|
|
73
|
+
const command = stringParam("boundaryIssues", params, "command", "npx turbo boundaries");
|
|
74
|
+
return Number(run(command).output.match(ISSUES)?.[1] ?? 0);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// src/counters/format.ts
|
|
79
|
+
import { existsSync } from "fs";
|
|
80
|
+
import { resolve } from "path";
|
|
81
|
+
var unformattedFiles = {
|
|
82
|
+
id: "unformattedFiles",
|
|
83
|
+
run: async ({ cwd, params, run }) => {
|
|
84
|
+
const command = stringParam(
|
|
85
|
+
"unformattedFiles",
|
|
86
|
+
params,
|
|
87
|
+
"command",
|
|
88
|
+
"npx oxfmt --config .oxfmtrc.json --list-different ."
|
|
89
|
+
);
|
|
90
|
+
return run(command).output.split("\n").map((line) => line.trim()).filter((line) => line.length > 0 && existsSync(resolve(cwd, line))).length;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// src/counters/law.ts
|
|
95
|
+
import { existsSync as existsSync2, readFileSync } from "fs";
|
|
96
|
+
import { resolve as resolve2 } from "path";
|
|
97
|
+
var lawLineCount = {
|
|
98
|
+
id: "lawLineCount",
|
|
99
|
+
run: async ({ cwd, params }) => {
|
|
100
|
+
const relative = stringParam("lawLineCount", params, "path", "CLAUDE.md");
|
|
101
|
+
const path = resolve2(cwd, relative);
|
|
102
|
+
if (!existsSync2(path)) throw new CounterError("lawLineCount", `no law file at ${relative}`);
|
|
103
|
+
return readFileSync(path, "utf8").replace(/\n$/, "").split("\n").length;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// src/counters/oxlint.ts
|
|
108
|
+
import { existsSync as existsSync3, readFileSync as readFileSync2, rmSync, writeFileSync } from "fs";
|
|
109
|
+
import { resolve as resolve3 } from "path";
|
|
110
|
+
var DEFAULT_COMMAND = "npx oxlint --format=unix --config .oxlintrc.json .";
|
|
111
|
+
var oxlintErrors = {
|
|
112
|
+
id: "oxlintErrors",
|
|
113
|
+
run: async ({ params, run }) => {
|
|
114
|
+
const command = stringParam("oxlintErrors", params, "command", DEFAULT_COMMAND);
|
|
115
|
+
return countMatches(run(command).output, /: error /);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
var oxlintWarnings = {
|
|
119
|
+
id: "oxlintWarnings",
|
|
120
|
+
run: async ({ params, run }) => {
|
|
121
|
+
const command = stringParam("oxlintWarnings", params, "command", DEFAULT_COMMAND);
|
|
122
|
+
return countMatches(run(command).output, /: warning /);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
var oxlintRule = {
|
|
126
|
+
id: "oxlintRule",
|
|
127
|
+
run: async ({ cwd, key, params, run }) => {
|
|
128
|
+
const rule = stringParam("oxlintRule", params, "rule");
|
|
129
|
+
const command = stringParam("oxlintRule", params, "command", DEFAULT_COMMAND);
|
|
130
|
+
const config = typeof params.config === "string" ? params.config : "";
|
|
131
|
+
if (config === "")
|
|
132
|
+
return countMatches(run(command).output, new RegExp(`\\(${escapeForRegex(rule)}\\)`));
|
|
133
|
+
const source = resolve3(cwd, config);
|
|
134
|
+
if (!existsSync3(source)) throw new CounterError("oxlintRule", `no config at ${config}`);
|
|
135
|
+
const strictName = `.oxlintrc.ratchet-${key}.json`;
|
|
136
|
+
const strict = readFileSync2(source, "utf8").replace(
|
|
137
|
+
new RegExp(`("[^"]*${escapeForRegex(rule)}"\\s*:\\s*)"(warn|off)"`),
|
|
138
|
+
'$1"error"'
|
|
139
|
+
);
|
|
140
|
+
writeFileSync(resolve3(cwd, strictName), strict);
|
|
141
|
+
try {
|
|
142
|
+
const output = run(command.replace("{config}", strictName)).output;
|
|
143
|
+
return countMatches(output, new RegExp(`\\(${escapeForRegex(rule)}\\)`));
|
|
144
|
+
} finally {
|
|
145
|
+
rmSync(resolve3(cwd, strictName), { force: true });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// src/counters/runtime-code.ts
|
|
151
|
+
var DEFAULT_PATTERNS = ["^(apps|packages)/[^/]+/src/"];
|
|
152
|
+
var NOT_RUNTIME = /(\.test\.|\.spec\.|__tests__\/|__fixtures__\/|\.d\.ts$)/;
|
|
153
|
+
var runtimeCodeShipped = {
|
|
154
|
+
id: "runtimeCodeShipped",
|
|
155
|
+
run: async ({ params, run }) => {
|
|
156
|
+
const command = stringParam(
|
|
157
|
+
"runtimeCodeShipped",
|
|
158
|
+
params,
|
|
159
|
+
"command",
|
|
160
|
+
"git diff --cached --name-only HEAD"
|
|
161
|
+
);
|
|
162
|
+
const patterns = stringsParam(params, "patterns", DEFAULT_PATTERNS).map(
|
|
163
|
+
(one) => new RegExp(one)
|
|
164
|
+
);
|
|
165
|
+
const shipped = run(command).output.split("\n").map((line) => line.trim()).filter((line) => line.length > 0 && !NOT_RUNTIME.test(line)).some((line) => patterns.some((pattern) => pattern.test(line)));
|
|
166
|
+
return shipped ? 0 : 1;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// src/counters/sum-of-counts.ts
|
|
171
|
+
var sumOfCounts = {
|
|
172
|
+
id: "sumOfCounts",
|
|
173
|
+
run: async ({ params, run }) => {
|
|
174
|
+
const command = stringParam("sumOfCounts", params, "command");
|
|
175
|
+
const match = new RegExp(stringParam("sumOfCounts", params, "match", ":(\\d+)$"), "gm");
|
|
176
|
+
return [...run(command).output.matchAll(match)].map(([, captured]) => Number(captured ?? 0)).filter((count) => Number.isFinite(count)).reduce((sum, count) => sum + count, 0);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// src/counters/tests.ts
|
|
181
|
+
var FAILED = /(\d+)\s+fail(?:ed|ing|s)?\b/;
|
|
182
|
+
var PASSED = /(\d+)\s+pass(?:ed|ing|es)?\b/;
|
|
183
|
+
var testFailures = {
|
|
184
|
+
id: "testFailures",
|
|
185
|
+
run: async ({ params, run }) => {
|
|
186
|
+
const command = stringParam("testFailures", params, "command", "npx vitest run");
|
|
187
|
+
const output = run(command).output;
|
|
188
|
+
const failed = output.match(FAILED)?.[1];
|
|
189
|
+
if (failed !== void 0) return Number(failed);
|
|
190
|
+
if (PASSED.test(output)) return 0;
|
|
191
|
+
throw new CounterError(
|
|
192
|
+
"testFailures",
|
|
193
|
+
`no pass/fail summary in the runner output:
|
|
194
|
+
${output.trim().slice(-500)}`
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// src/counters/typecheck.ts
|
|
200
|
+
var typecheckErrors = {
|
|
201
|
+
id: "typecheckErrors",
|
|
202
|
+
run: async ({ params, run }) => {
|
|
203
|
+
const command = stringParam("typecheckErrors", params, "command", "npx tsc --noEmit");
|
|
204
|
+
return countMatches(run(command).output, /error TS/);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// src/counters/index.ts
|
|
209
|
+
var COUNTERS = [
|
|
210
|
+
archViolations,
|
|
211
|
+
boundaryIssues,
|
|
212
|
+
cloneCount,
|
|
213
|
+
knipIssues,
|
|
214
|
+
lawLineCount,
|
|
215
|
+
oxlintErrors,
|
|
216
|
+
oxlintRule,
|
|
217
|
+
oxlintWarnings,
|
|
218
|
+
runtimeCodeShipped,
|
|
219
|
+
sumOfCounts,
|
|
220
|
+
testFailures,
|
|
221
|
+
typecheckErrors,
|
|
222
|
+
unformattedFiles
|
|
223
|
+
];
|
|
224
|
+
var counterById = (id) => {
|
|
225
|
+
const counter = COUNTERS.find((one) => one.id === id);
|
|
226
|
+
if (counter === void 0) {
|
|
227
|
+
throw new Error(
|
|
228
|
+
`no counter implements "${id}" \u2014 known ids: ${COUNTERS.map((one) => one.id).join(", ")}`
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
return counter;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
// src/config.ts
|
|
235
|
+
import { existsSync as existsSync4, readFileSync as readFileSync3 } from "fs";
|
|
236
|
+
import { resolve as resolve4 } from "path";
|
|
237
|
+
var CONFIG_FILE = "geonosis.ratchet.json";
|
|
238
|
+
var keyOf = (entry) => entry.key ?? entry.counter;
|
|
239
|
+
var loadConfig = (cwd) => {
|
|
240
|
+
const path = resolve4(cwd, CONFIG_FILE);
|
|
241
|
+
if (!existsSync4(path)) {
|
|
242
|
+
throw new Error(`no ${CONFIG_FILE} in ${cwd} \u2014 the ratchet has nothing to count`);
|
|
243
|
+
}
|
|
244
|
+
const parsed = JSON.parse(readFileSync3(path, "utf8"));
|
|
245
|
+
if (!Array.isArray(parsed.counters)) {
|
|
246
|
+
throw new Error(`${CONFIG_FILE} has no "counters" array`);
|
|
247
|
+
}
|
|
248
|
+
for (const entry of parsed.counters) {
|
|
249
|
+
if (typeof entry.counter !== "string") {
|
|
250
|
+
throw new Error(`${CONFIG_FILE}: every counter entry needs a "counter" id`);
|
|
251
|
+
}
|
|
252
|
+
if (entry.tiers !== void 0) {
|
|
253
|
+
const named = Array.isArray(entry.tiers) && entry.tiers.length > 0 && entry.tiers.every((tier) => typeof tier === "string" && tier.length > 0);
|
|
254
|
+
if (!named) {
|
|
255
|
+
throw new Error(
|
|
256
|
+
`${CONFIG_FILE}: "${keyOf(entry)}" has "tiers" that is not a non-empty list of tier names`
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const keys = parsed.counters.map(keyOf);
|
|
262
|
+
const duplicate = keys.find((key, at) => keys.indexOf(key) !== at);
|
|
263
|
+
if (duplicate !== void 0) {
|
|
264
|
+
throw new Error(
|
|
265
|
+
`${CONFIG_FILE}: two counters both write "${duplicate}" \u2014 give one of them a distinct "key"`
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
return { baseline: parsed.baseline ?? "gate-baseline.json", counters: parsed.counters };
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// src/shell.ts
|
|
272
|
+
import { execSync } from "child_process";
|
|
273
|
+
var ANSI = /\[[0-9;]*m/g;
|
|
274
|
+
var runCommand = (cwd, counterId) => (command) => {
|
|
275
|
+
try {
|
|
276
|
+
const output = execSync(`${command} 2>&1`, {
|
|
277
|
+
cwd,
|
|
278
|
+
encoding: "utf8",
|
|
279
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
280
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
281
|
+
});
|
|
282
|
+
return { code: 0, output: output.replaceAll(ANSI, "") };
|
|
283
|
+
} catch (error) {
|
|
284
|
+
const failed = error;
|
|
285
|
+
const output = `${failed.stdout ?? ""}${failed.stderr ?? ""}`.replaceAll(ANSI, "");
|
|
286
|
+
const code = failed.status ?? -1;
|
|
287
|
+
if (code === 126 || code === 127 || code === -1) {
|
|
288
|
+
throw new CounterError(
|
|
289
|
+
counterId,
|
|
290
|
+
`command did not run (exit ${code}): ${command}
|
|
291
|
+
${output.trim()}`
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
return { code, output };
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// src/ratchet.ts
|
|
299
|
+
import { existsSync as existsSync5, readFileSync as readFileSync4, writeFileSync as writeFileSync2 } from "fs";
|
|
300
|
+
import { resolve as resolve5 } from "path";
|
|
301
|
+
var verdictOf = (now, baseline) => {
|
|
302
|
+
if (now > baseline) return "grew";
|
|
303
|
+
if (now < baseline) return "shrank";
|
|
304
|
+
return "held";
|
|
305
|
+
};
|
|
306
|
+
var outsideTier = (entry, tier) => entry.tiers !== void 0 && !entry.tiers.includes(tier);
|
|
307
|
+
var runRatchet = async ({
|
|
308
|
+
counters,
|
|
309
|
+
cwd,
|
|
310
|
+
tier
|
|
311
|
+
}) => {
|
|
312
|
+
const config = loadConfig(cwd);
|
|
313
|
+
const baselinePath = resolve5(cwd, config.baseline);
|
|
314
|
+
if (!existsSync5(baselinePath)) {
|
|
315
|
+
throw new Error(`no ${config.baseline} in ${cwd} \u2014 nothing to ratchet against`);
|
|
316
|
+
}
|
|
317
|
+
const baseline = JSON.parse(readFileSync4(baselinePath, "utf8"));
|
|
318
|
+
const byId = new Map(counters.map((one) => [one.id, one]));
|
|
319
|
+
const measurements = [];
|
|
320
|
+
for (const entry of config.counters) {
|
|
321
|
+
const counter = byId.get(entry.counter);
|
|
322
|
+
if (counter === void 0) {
|
|
323
|
+
throw new Error(
|
|
324
|
+
`no counter implements "${entry.counter}" \u2014 known ids: ${[...byId.keys()].toSorted().join(", ")}`
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
const key = keyOf(entry);
|
|
328
|
+
if (tier !== void 0 && outsideTier(entry, tier)) {
|
|
329
|
+
measurements.push({ key, tier, verdict: "skipped" });
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
const limit = baseline[key];
|
|
333
|
+
if (typeof limit !== "number") {
|
|
334
|
+
throw new Error(
|
|
335
|
+
`${config.baseline} has no number for "${key}" \u2014 add it before enabling the counter`
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
const now = await counter.run({
|
|
339
|
+
cwd,
|
|
340
|
+
key,
|
|
341
|
+
params: entry,
|
|
342
|
+
run: runCommand(cwd, entry.counter)
|
|
343
|
+
});
|
|
344
|
+
measurements.push({ baseline: limit, key, now, verdict: verdictOf(now, limit) });
|
|
345
|
+
}
|
|
346
|
+
const grew = measurements.some((one) => one.verdict === "grew");
|
|
347
|
+
const shrank = measurements.some((one) => one.verdict === "shrank");
|
|
348
|
+
if (shrank && !grew) {
|
|
349
|
+
const next = { ...baseline };
|
|
350
|
+
for (const one of measurements) {
|
|
351
|
+
if (one.verdict !== "skipped") next[one.key] = one.now;
|
|
352
|
+
}
|
|
353
|
+
writeFileSync2(baselinePath, `${JSON.stringify(next, null, 2)}
|
|
354
|
+
`);
|
|
355
|
+
return { measurements, rewritten: true };
|
|
356
|
+
}
|
|
357
|
+
return { measurements, rewritten: false };
|
|
358
|
+
};
|
|
359
|
+
var WIDTH = 28;
|
|
360
|
+
var formatReport = ({ measurements, rewritten }) => {
|
|
361
|
+
const lines = measurements.map((one) => {
|
|
362
|
+
if (one.verdict === "skipped") {
|
|
363
|
+
return ` SKIP ${one.key}: not measured by --tier ${one.tier}`;
|
|
364
|
+
}
|
|
365
|
+
const head = ` ${one.key.padEnd(WIDTH)} ${String(one.now).padStart(5)} (baseline ${one.baseline})`;
|
|
366
|
+
if (one.verdict === "grew") return `${head} <-- REGRESSED +${one.now - one.baseline}`;
|
|
367
|
+
if (one.verdict === "shrank") return `${head} <-- improved -${one.baseline - one.now}`;
|
|
368
|
+
return head;
|
|
369
|
+
});
|
|
370
|
+
const grew = measurements.filter((one) => one.verdict === "grew");
|
|
371
|
+
if (grew.length > 0) {
|
|
372
|
+
lines.push(
|
|
373
|
+
"",
|
|
374
|
+
"ratchet FAIL \u2014 debt grew. Fix it, or say in the commit message why the baseline goes up."
|
|
375
|
+
);
|
|
376
|
+
} else if (rewritten) {
|
|
377
|
+
lines.push("", "ratchet PASS \u2014 debt shrank; the baseline was lowered to lock the win in.");
|
|
378
|
+
} else {
|
|
379
|
+
lines.push("", "ratchet PASS \u2014 no regression.");
|
|
380
|
+
}
|
|
381
|
+
return `${lines.join("\n")}
|
|
382
|
+
`;
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
export {
|
|
386
|
+
CounterError,
|
|
387
|
+
COUNTERS,
|
|
388
|
+
counterById,
|
|
389
|
+
CONFIG_FILE,
|
|
390
|
+
keyOf,
|
|
391
|
+
loadConfig,
|
|
392
|
+
runCommand,
|
|
393
|
+
runRatchet,
|
|
394
|
+
formatReport
|
|
395
|
+
};
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
COUNTERS,
|
|
3
|
+
formatReport,
|
|
4
|
+
runRatchet
|
|
5
|
+
} from "./chunk-VX2PL7QG.js";
|
|
6
|
+
|
|
7
|
+
// src/cli.ts
|
|
8
|
+
var cwdFlag = process.argv.indexOf("--cwd");
|
|
9
|
+
var cwd = cwdFlag === -1 ? process.cwd() : process.argv[cwdFlag + 1] ?? process.cwd();
|
|
10
|
+
var tierFlag = process.argv.indexOf("--tier");
|
|
11
|
+
var tier = tierFlag === -1 ? void 0 : process.argv[tierFlag + 1];
|
|
12
|
+
try {
|
|
13
|
+
if (tierFlag !== -1 && (tier === void 0 || tier.startsWith("--"))) {
|
|
14
|
+
throw new Error("--tier needs a tier name");
|
|
15
|
+
}
|
|
16
|
+
const result = await runRatchet({ counters: COUNTERS, cwd, tier });
|
|
17
|
+
process.stdout.write(formatReport(result));
|
|
18
|
+
process.exit(result.measurements.some((one) => one.verdict === "grew") ? 1 : 0);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
process.stderr.write(`geonosis-ratchet: ${error.message}
|
|
21
|
+
`);
|
|
22
|
+
process.exit(2);
|
|
23
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CONFIG_FILE,
|
|
3
|
+
COUNTERS,
|
|
4
|
+
CounterError,
|
|
5
|
+
counterById,
|
|
6
|
+
formatReport,
|
|
7
|
+
keyOf,
|
|
8
|
+
loadConfig,
|
|
9
|
+
runCommand,
|
|
10
|
+
runRatchet
|
|
11
|
+
} from "./chunk-VX2PL7QG.js";
|
|
12
|
+
export {
|
|
13
|
+
CONFIG_FILE,
|
|
14
|
+
COUNTERS,
|
|
15
|
+
CounterError,
|
|
16
|
+
counterById,
|
|
17
|
+
formatReport,
|
|
18
|
+
keyOf,
|
|
19
|
+
loadConfig,
|
|
20
|
+
runCommand,
|
|
21
|
+
runRatchet
|
|
22
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@geonosis/ratchet",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Debt as a number that may only shrink — one ratchet, pluggable counters.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ratchet",
|
|
7
|
+
"baseline",
|
|
8
|
+
"technical-debt",
|
|
9
|
+
"gate",
|
|
10
|
+
"ci",
|
|
11
|
+
"lint",
|
|
12
|
+
"migration"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/microcompanies/geonosis/tree/main/packages/ratchet",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/microcompanies/geonosis.git",
|
|
18
|
+
"directory": "packages/ratchet"
|
|
19
|
+
},
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "dist/index.js",
|
|
23
|
+
"bin": {
|
|
24
|
+
"geonosis-ratchet": "./bin/geonosis-ratchet.mjs"
|
|
25
|
+
},
|
|
26
|
+
"exports": {
|
|
27
|
+
".": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"bin",
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"typecheck": "tsc --noEmit"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=22"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
}
|
|
43
|
+
}
|