specgate-cli 0.0.0-stage → 0.1.7

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 aazh2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,351 @@
1
- # Temporary Holding Version
1
+ # specgate
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **Requirement coverage for your codebase. Like test coverage, but for acceptance criteria.**
4
+
5
+ [![CI](https://github.com/aazh2026/specgate/actions/workflows/ci.yml/badge.svg)](https://github.com/aazh2026/specgate/actions/workflows/ci.yml)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
7
+ [![Node >= 20](https://img.shields.io/badge/node-%3E%3D20-brightgreen.svg)](https://nodejs.org)
8
+
9
+ Your tests pass. Your lint passes. But does the code actually do what the spec says?
10
+
11
+ `specgate` reads your acceptance criteria, finds the evidence for each one in your
12
+ codebase, and tells you which requirements are unproven. It is deterministic —
13
+ no LLM calls at runtime, no API key, no network. Same repo, same spec, same
14
+ score. Every time.
15
+
16
+ ![specgate demo](assets/demo.gif)
17
+
18
+ ```bash
19
+ npx --yes --package=https://github.com/aazh2026/specgate/releases/download/v0.1.4/specgate-0.1.4.tgz specgate
20
+ ```
21
+
22
+ ```
23
+ Requirement coverage: 80% (5 criteria)
24
+
25
+ ✔ covered 4
26
+ ~ inferred 0
27
+ ! untested 0
28
+ ✗ uncovered 1
29
+
30
+ Criterion Status Evidence
31
+ ------------------------------------------------------------------------------
32
+ AC-1 covered #test/export.test.js:12
33
+ User can export invoices as CSV
34
+ AC-2 covered #test/export.test.js:21
35
+ Export respects the date range filter
36
+ AC-5 uncovered —
37
+ Admin can schedule a recurring monthly export
38
+
39
+ Gaps:
40
+ AC-5 Admin can schedule a recurring monthly export
41
+ ```
42
+
43
+ ## Why
44
+
45
+ AI coding agents write code fast. The spec is where the truth lives — and it
46
+ rots first. Three months later nobody can tell whether `AC-12` is implemented,
47
+ half-implemented, or silently broken. Test coverage tells you *which lines ran*.
48
+ It tells you nothing about *which requirements exist*.
49
+
50
+ `specgate` closes that gap:
51
+
52
+ | | test coverage | requirement coverage |
53
+ |---|---|---|
54
+ | unit of measure | line / branch | acceptance criterion |
55
+ | answers | "is this code exercised?" | "is this requirement met?" |
56
+ | misses | a whole unimplemented feature | — |
57
+
58
+ ## Install
59
+
60
+ ```bash
61
+ # from npmjs — no account, no token
62
+ npx --yes specgate-cli
63
+
64
+ # or install it
65
+ npm install --save-dev specgate-cli
66
+
67
+ # or straight from the git tag
68
+ npx --yes github:aazh2026/specgate
69
+
70
+ # or with Homebrew
71
+ brew install aazh2026/specgate/specgate
72
+
73
+ # or as a container image (no local Node needed)
74
+ docker run --rm -v "$PWD:/workspace" -w /workspace ghcr.io/aazh2026/specgate:0.1.4
75
+
76
+ # or clone and run it directly
77
+ git clone https://github.com/aazh2026/specgate
78
+ cd specgate && node bin/specgate.js
79
+ ```
80
+
81
+ Requires Node.js 20+. No runtime dependencies.
82
+
83
+ > **On the npm package name.** The package is published as `specgate-cli`,
84
+ > not `specgate`. The unscoped `specgate` name is refused by the npm registry
85
+ > because an unrelated package named `spec-gate` already exists and npm's
86
+ > name-confusion rule blocks near-identical names. `specgate-cli` is the same
87
+ > code with the same `specgate` command; only the package name differs.
88
+
89
+ ### GitHub Packages (fallback, needs a token)
90
+
91
+ The same tarball is also published to GitHub Packages as
92
+ `@aazh2026/specgate`. Be aware of one registry rule before you use it:
93
+ **`npm.pkg.github.com` requires an auth token for every read**, even though
94
+ the package is publicly visible. An anonymous install returns
95
+ `401 authentication token not provided`.
96
+
97
+ ```bash
98
+ npm install @aazh2026/specgate --registry https://npm.pkg.github.com
99
+ ```
100
+
101
+ with a `.npmrc` carrying a GitHub token that has the `read:packages` scope:
102
+
103
+ ```
104
+ //npm.pkg.github.com/:_authToken=ghp_xxx
105
+ ```
106
+
107
+ This is a fallback for people who already have a GitHub token — npmjs
108
+ (`specgate-cli`) needs no credentials at all.
109
+
110
+ ## Quick start
111
+
112
+ If your project has no written spec yet, scaffold one:
113
+
114
+ ```bash
115
+ npx --yes --package=https://github.com/aazh2026/specgate/releases/download/v0.1.4/specgate-0.1.4.tgz specgate init
116
+ ```
117
+
118
+ That writes a starter `SPEC.md` with the acceptance criteria already structured,
119
+ plus a `.github/workflows/requirements.yml` CI gate. It detects your stack
120
+ (language, test runner, package manager) so the scaffold matches your project.
121
+
122
+ Then edit the criteria, tag your tests, and scan:
123
+
124
+ ```bash
125
+ npx --yes --package=https://github.com/aazh2026/specgate/releases/download/v0.1.4/specgate-0.1.4.tgz specgate
126
+ ```
127
+
128
+ ## Usage
129
+
130
+ ```bash
131
+ specgate # auto-discover the spec, print a report
132
+ specgate --spec docs/SPEC.md # point it at a specific spec
133
+ specgate --format json # machine-readable output
134
+ specgate --min 80 # fail CI below 80% coverage
135
+ specgate --fail-uncovered # fail if anything is uncovered
136
+ specgate --sarif-out specgate.sarif # upload to GitHub code scanning
137
+ ```
138
+
139
+ | Flag | Meaning |
140
+ |---|---|
141
+ | `--spec <file>` | Spec file to read. Repeatable. |
142
+ | `--format <fmt>` | `text` (default), `json`, or `sarif` |
143
+ | `--min <n>` | Exit 1 if the coverage score is below `n` |
144
+ | `--fail-uncovered` | Exit 1 if any criterion has no evidence |
145
+ | `--fail-untested` | Exit 1 if any criterion is implemented but untested |
146
+ | `--json-out <file>` | Write the JSON report to a file |
147
+ | `--sarif-out <file>` | Write a SARIF report for GitHub code scanning |
148
+ | `--force` | With `init`: overwrite existing files |
149
+ | `--name <name>` | With `init`: project name for the spec title |
150
+ | `-v`, `--verbose` | Show every criterion, not just the gaps |
151
+
152
+ ## How it works
153
+
154
+ `specgate` does four things, all locally:
155
+
156
+ **1. Parse the spec.** It finds your acceptance criteria and gives each one a
157
+ stable id. If you wrote ids, it keeps them. If you didn't, it mints `AC-1`,
158
+ `AC-2`, … in document order, so the tool still works on a plain bullet list.
159
+
160
+ ```markdown
161
+ ## Acceptance Criteria
162
+
163
+ - [ ] AC-1: User can export invoices as CSV
164
+ - [ ] [AC-2] Export respects the date range filter
165
+ - [ ] Export fails loudly when the storage backend is unavailable
166
+ ```
167
+
168
+ It also reads **Gherkin/Cucumber `.feature` files** — a feature file is a spec
169
+ by construction, so every scenario becomes an acceptance criterion:
170
+
171
+ ```gherkin
172
+ Feature: Invoice export
173
+
174
+ @AC-1
175
+ Scenario: Export invoices as CSV
176
+ Given an invoice exists
177
+ When the user requests an export
178
+ Then a CSV file is produced
179
+ ```
180
+
181
+ A scenario tagged `@AC-1` keeps the same id as the markdown criterion, so a
182
+ `SPEC.md` and a feature file can describe the same requirement without
183
+ double-counting it. Untagged scenarios get `SC-1`, `SC-2`, … in document
184
+ order. Scenarios are matched against their steps as well as their title, so a
185
+ step definition like `Given("the user requests an export")` counts as evidence
186
+ even when the scenario title says something else.
187
+
188
+ **2. Index the code.** It walks your repo for source and test files, skipping
189
+ `node_modules`, `dist`, and friends. Identifier boundaries are respected:
190
+ `resetPassword`, `export_csv`, and `HTTPResponse` are all split correctly, so a
191
+ criterion written in English prose can match code written in camelCase.
192
+
193
+ **3. Find evidence, in tiers.**
194
+
195
+ | Tier | Status | What it means |
196
+ |---|---|---|
197
+ | `tagged` | `covered` | An explicit marker — `// AC-1`, `# AC-1`, `[AC-1]` — points at the criterion. |
198
+ | `named` | `inferred` | A test name matches the criterion's distinctive words. A hint, not a guarantee. |
199
+ | `text` | `untested` | Only implementation code matched. Nothing pins the behaviour down. |
200
+ | — | `uncovered` | No evidence anywhere. |
201
+
202
+ **4. Score.** Evidence tiers are weighted, because they are not equally
203
+ trustworthy:
204
+
205
+ ```
206
+ score = 100 × (covered×1.0 + inferred×0.75 + untested×0.4 + uncovered×0.0) / total
207
+ ```
208
+
209
+ `inferred` is discounted on purpose. A test named `resetPassword` might test
210
+ something else entirely. Only an explicit tag is worth full credit.
211
+
212
+ ## Using it in CI
213
+
214
+ The quickest way in:
215
+
216
+ ```yaml
217
+ # .github/workflows/requirements.yml
218
+ name: Requirement coverage
219
+ on: [pull_request]
220
+
221
+ jobs:
222
+ specgate:
223
+ runs-on: ubuntu-latest
224
+ steps:
225
+ - uses: actions/checkout@v4
226
+ - uses: aazh2026/specgate@v0.1.4
227
+ with:
228
+ min: 80
229
+ fail-uncovered: true
230
+ ```
231
+
232
+ The action exposes the score as an output:
233
+
234
+ ```yaml
235
+ - id: specgate
236
+ uses: aazh2026/specgate@v0.1.4
237
+ - run: echo "Coverage was ${{ steps.specgate.outputs.score }}%"
238
+ ```
239
+
240
+ Or run the CLI directly:
241
+
242
+ ```yaml
243
+ - uses: actions/setup-node@v4
244
+ with: { node-version: 20 }
245
+ - run: npx --yes github:aazh2026/specgate --min 80 --fail-uncovered
246
+ ```
247
+
248
+ The gate is the point. A spec that says "the user can export invoices as CSV"
249
+ should stop a pull request that doesn't implement it.
250
+
251
+ For GitHub code scanning, upload the SARIF report:
252
+
253
+ ```yaml
254
+ - run: npx --yes github:aazh2026/specgate --sarif-out specgate.sarif --format sarif
255
+ - uses: github/codeql-action/upload-sarif@v3
256
+ with:
257
+ sarif_file: specgate.sarif
258
+ ```
259
+
260
+ Or let the action do it:
261
+
262
+ ```yaml
263
+ - uses: aazh2026/specgate@v0.1.4
264
+ with:
265
+ sarif: specgate.sarif
266
+ - uses: github/codeql-action/upload-sarif@v3
267
+ with:
268
+ sarif_file: specgate.sarif
269
+ ```
270
+
271
+ ## Spec format
272
+
273
+ Anything Markdown works. `specgate` looks for a heading matching
274
+ `acceptance criteria`, `acceptance`, `requirements`, `user stories`, `criteria`
275
+ (or `验收标准` / `需求` / `用户故事`), and treats the bullets under it as criteria.
276
+
277
+ Criteria without an explicit id still get one. If your team prefers plain
278
+ prose, this is enough:
279
+
280
+ ```markdown
281
+ ## Acceptance Criteria
282
+
283
+ - User can export invoices as CSV
284
+ - Export respects the date range filter
285
+ ```
286
+
287
+ If your team already numbers things, use it:
288
+
289
+ ```markdown
290
+ ## Acceptance Criteria
291
+
292
+ - [ ] AC-1: User can export invoices as CSV
293
+ - [ ] REQ-12: Export respects the date range filter
294
+ ```
295
+
296
+ ### Adding explicit tags
297
+
298
+ To move a criterion from `inferred` to `covered`, point at it:
299
+
300
+ ```js
301
+ // AC-1: User can export invoices as CSV
302
+ test('exports invoices as CSV', () => { /* ... */ });
303
+ ```
304
+
305
+ ```python
306
+ # AC-2: Export respects the date range filter
307
+ def test_export_respects_date_range():
308
+ ...
309
+ ```
310
+
311
+ ## API
312
+
313
+ ```js
314
+ import { runSpecgate } from 'specgate';
315
+
316
+ const report = runSpecgate({ root: process.cwd(), specFiles: ['docs/SPEC.md'] });
317
+
318
+ report.summary; // { score, total, covered, inferred, untested, uncovered }
319
+ report.criteria; // [{ id, text, status, evidence: { tier, file, line } }]
320
+ report.stats; // { filesScanned, testFiles }
321
+ ```
322
+
323
+ ## Limitations
324
+
325
+ Rust test detection is file-level. A `#[cfg(test)] mod` marks a mixed source
326
+ and test file as a test file; it does not restrict evidence to the test module.
327
+
328
+ Being honest about what this is:
329
+
330
+ - **It matches text, not meaning.** A criterion written as *"the operator can
331
+ revoke a session"* will not match a function called `deleteToken` unless the
332
+ words overlap. Use explicit tags for anything that matters.
333
+ - **`inferred` is a heuristic.** It is weighted down for that reason, but it is
334
+ not proof.
335
+ - **English and Chinese prose both tokenise**, but a spec mixing languages
336
+ works best when criteria are written in the language that matches the code
337
+ identifiers.
338
+ - **It is not a test runner.** It does not know whether your tests pass. Pair it
339
+ with your existing test suite.
340
+
341
+ ## Contributing
342
+
343
+ Issues and pull requests are welcome. Run the tests with:
344
+
345
+ ```bash
346
+ npm test
347
+ ```
348
+
349
+ ## License
350
+
351
+ MIT
package/action.yml ADDED
@@ -0,0 +1,83 @@
1
+ name: 'Requirement coverage'
2
+ description: 'Check that your acceptance criteria are actually implemented. Like test coverage, but for requirements.'
3
+ author: 'aazh2026'
4
+
5
+ branding:
6
+ icon: 'check-square'
7
+ color: 'green'
8
+
9
+ inputs:
10
+ min:
11
+ description: 'Minimum coverage score required to pass. Set to 0 to disable the score gate.'
12
+ required: false
13
+ default: '0'
14
+ fail-uncovered:
15
+ description: 'Fail the build if any acceptance criterion has no evidence.'
16
+ required: false
17
+ default: 'false'
18
+ fail-untested:
19
+ description: 'Fail the build if any acceptance criterion is implemented but untested.'
20
+ required: false
21
+ default: 'false'
22
+ spec:
23
+ description: 'Spec file to read. Leave empty to auto-discover.'
24
+ required: false
25
+ default: ''
26
+ sarif:
27
+ description: 'Path to write a SARIF report for GitHub code scanning.'
28
+ required: false
29
+ default: ''
30
+
31
+ outputs:
32
+ score:
33
+ description: 'Requirement coverage score, 0-100.'
34
+ value: ${{ steps.run.outputs.score }}
35
+ total:
36
+ description: 'Total number of acceptance criteria found.'
37
+ value: ${{ steps.run.outputs.total }}
38
+ uncovered:
39
+ description: 'Number of acceptance criteria with no evidence.'
40
+ value: ${{ steps.run.outputs.uncovered }}
41
+
42
+ runs:
43
+ using: 'composite'
44
+ steps:
45
+ - name: Set up Node
46
+ uses: actions/setup-node@v4
47
+ with:
48
+ node-version: '20'
49
+
50
+ - name: Run specgate
51
+ id: run
52
+ shell: bash
53
+ env:
54
+ SPEC: ${{ inputs.spec }}
55
+ MIN: ${{ inputs.min }}
56
+ FAIL_UNCOVERED: ${{ inputs.fail-uncovered }}
57
+ FAIL_UNTESTED: ${{ inputs.fail-untested }}
58
+ SARIF_OUT: ${{ inputs.sarif }}
59
+ run: |
60
+ set -euo pipefail
61
+
62
+ ARGS=()
63
+ if [ -n "${SPEC}" ]; then ARGS+=(--spec "${SPEC}"); fi
64
+ if [ -n "${MIN}" ] && [ "${MIN}" != "0" ]; then ARGS+=(--min "${MIN}"); fi
65
+ if [ "${FAIL_UNCOVERED}" = "true" ]; then ARGS+=(--fail-uncovered); fi
66
+ if [ "${FAIL_UNTESTED}" = "true" ]; then ARGS+=(--fail-untested); fi
67
+ if [ -n "${SARIF_OUT}" ]; then ARGS+=(--sarif-out "${SARIF_OUT}"); fi
68
+
69
+ # Write the JSON report so the outputs can be read.
70
+ JSON_OUT="$(mktemp).json"
71
+ node "$GITHUB_ACTION_PATH/bin/specgate.js" "${ARGS[@]+"${ARGS[@]}"}" --json-out "${JSON_OUT}"
72
+
73
+ # Read with JSON.parse: `require` on an extension-less file would try to
74
+ # parse it as JavaScript.
75
+ SCORE="$(node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1],'utf8')).score)" "${JSON_OUT}")"
76
+ TOTAL="$(node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1],'utf8')).summary.total)" "${JSON_OUT}")"
77
+ UNCOVERED="$(node -e "console.log(JSON.parse(require('fs').readFileSync(process.argv[1],'utf8')).summary.uncovered)" "${JSON_OUT}")"
78
+
79
+ {
80
+ echo "score=${SCORE}"
81
+ echo "total=${TOTAL}"
82
+ echo "uncovered=${UNCOVERED}"
83
+ } >> "$GITHUB_OUTPUT"
Binary file
@@ -0,0 +1,221 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * specgate — requirement coverage for your codebase.
4
+ */
5
+
6
+ import fs from 'node:fs';
7
+ import path from 'node:path';
8
+ import { fileURLToPath } from 'node:url';
9
+
10
+ import { runSpecgate } from '../src/index.js';
11
+ import { renderText, renderJson, renderSarif } from '../src/report.js';
12
+ import { init, renderInitResult } from '../src/scaffold/init.js';
13
+
14
+ // Read the version from package.json so it can never drift from the published
15
+ // package. Falls back to a literal if package.json is missing.
16
+ function readVersion() {
17
+ try {
18
+ const here = path.dirname(fileURLToPath(import.meta.url));
19
+ const pkg = JSON.parse(fs.readFileSync(path.join(here, '..', 'package.json'), 'utf8'));
20
+ return pkg.version || '0.0.0';
21
+ } catch {
22
+ return '0.0.0';
23
+ }
24
+ }
25
+
26
+ const VERSION = readVersion();
27
+
28
+ const USAGE = `
29
+ specgate ${VERSION} — requirement coverage for your codebase
30
+
31
+ Usage:
32
+ specgate [options] [path]
33
+ specgate init [options] [path]
34
+
35
+ Commands:
36
+ (default) Scan the project and report requirement coverage.
37
+ init Create a starter SPEC.md and a CI gate for this project.
38
+
39
+ Options:
40
+ --spec <file> Spec file to read (repeatable). Default: auto-discover.
41
+ --scope <dir> Only consider files under <dir> as evidence. Specs are
42
+ still discovered across the whole repository. Useful in
43
+ a monorepo, where one package holds the spec and the
44
+ implementation lives in another.
45
+ --format <fmt> text | json | sarif (default: text)
46
+ --min <n> Fail (exit 1) if coverage score is below n (default: none)
47
+ --fail-uncovered Fail if any criterion is uncovered
48
+ --fail-untested Fail if any criterion is untested
49
+ --json-out <file> Also write the JSON report to a file
50
+ --sarif-out <file> Write a SARIF report (for GitHub code scanning)
51
+ --verbose, -v Show every criterion, not just the gaps
52
+ --quiet Print nothing; rely on exit code
53
+ --force With init: overwrite existing files
54
+ --name <name> With init: project name for the spec title
55
+ --version, -V Print version
56
+ --help, -h Show this help
57
+
58
+ Examples:
59
+ specgate
60
+ specgate init --min 70
61
+ specgate --spec docs/SPEC.md --format json
62
+ specgate --min 80 --fail-untested
63
+ specgate --sarif-out specgate.sarif --format sarif
64
+ `;
65
+
66
+ function parseArgs(argv) {
67
+ const opts = {
68
+ path: '.',
69
+ command: 'scan',
70
+ specs: [],
71
+ format: 'text',
72
+ min: null,
73
+ failUncovered: false,
74
+ failUntested: false,
75
+ jsonOut: null,
76
+ sarifOut: null,
77
+ verbose: false,
78
+ quiet: false,
79
+ force: false,
80
+ name: null,
81
+ scope: null,
82
+ help: false,
83
+ version: false,
84
+ };
85
+ const rest = [];
86
+
87
+ for (let i = 0; i < argv.length; i++) {
88
+ const a = argv[i];
89
+ switch (a) {
90
+ case '-h':
91
+ case '--help':
92
+ opts.help = true;
93
+ break;
94
+ case '-V':
95
+ case '--version':
96
+ opts.version = true;
97
+ break;
98
+ case '-v':
99
+ case '--verbose':
100
+ opts.verbose = true;
101
+ break;
102
+ case '--quiet':
103
+ opts.quiet = true;
104
+ break;
105
+ case '--force':
106
+ opts.force = true;
107
+ break;
108
+ case '--name':
109
+ opts.name = argv[++i];
110
+ break;
111
+ case 'init':
112
+ opts.command = 'init';
113
+ break;
114
+ case '--spec':
115
+ opts.specs.push(argv[++i]);
116
+ break;
117
+ case '--scope':
118
+ opts.scope = argv[++i];
119
+ break;
120
+ case '--format':
121
+ opts.format = argv[++i];
122
+ break;
123
+ case '--min':
124
+ opts.min = Number(argv[++i]);
125
+ break;
126
+ case '--json-out':
127
+ opts.jsonOut = argv[++i];
128
+ break;
129
+ case '--sarif-out':
130
+ opts.sarifOut = argv[++i];
131
+ break;
132
+ case '--fail-uncovered':
133
+ opts.failUncovered = true;
134
+ break;
135
+ case '--fail-untested':
136
+ opts.failUntested = true;
137
+ break;
138
+ default:
139
+ if (a.startsWith('-')) {
140
+ console.error(`specgate: unknown option ${a}`);
141
+ process.exit(2);
142
+ }
143
+ rest.push(a);
144
+ }
145
+ }
146
+
147
+ if (rest.length) opts.path = rest[0];
148
+ return opts;
149
+ }
150
+
151
+ function main() {
152
+ const opts = parseArgs(process.argv.slice(2));
153
+
154
+ if (opts.help) {
155
+ console.log(USAGE);
156
+ return 0;
157
+ }
158
+ if (opts.version) {
159
+ console.log(VERSION);
160
+ return 0;
161
+ }
162
+ if (!['text', 'json', 'sarif'].includes(opts.format)) {
163
+ console.error(`specgate: unknown format "${opts.format}" (use text, json or sarif)`);
164
+ return 2;
165
+ }
166
+
167
+ const root = path.resolve(opts.path);
168
+ if (!fs.existsSync(root)) {
169
+ console.error(`specgate: path not found: ${root}`);
170
+ return 2;
171
+ }
172
+
173
+ if (opts.command === 'init') {
174
+ const result = init(root, {
175
+ force: opts.force,
176
+ name: opts.name,
177
+ min: opts.min,
178
+ failUncovered: opts.failUncovered,
179
+ });
180
+ if (!opts.quiet) console.log(renderInitResult(result));
181
+ return 0;
182
+ }
183
+
184
+ const report = runSpecgate({
185
+ root,
186
+ specFiles: opts.specs,
187
+ scope: opts.scope,
188
+ });
189
+
190
+ if (!opts.quiet) {
191
+ if (opts.format === 'json') console.log(renderJson(report));
192
+ else if (opts.format === 'sarif') console.log(renderSarif(report));
193
+ else console.log(renderText(report, { verbose: opts.verbose }));
194
+ }
195
+
196
+ if (opts.jsonOut) {
197
+ fs.mkdirSync(path.dirname(path.resolve(opts.jsonOut)), { recursive: true });
198
+ fs.writeFileSync(path.resolve(opts.jsonOut), renderJson(report));
199
+ }
200
+ if (opts.sarifOut) {
201
+ fs.mkdirSync(path.dirname(path.resolve(opts.sarifOut)), { recursive: true });
202
+ fs.writeFileSync(path.resolve(opts.sarifOut), renderSarif(report));
203
+ }
204
+
205
+ const { score, uncovered, untested } = report.summary;
206
+ if (opts.min !== null && !Number.isNaN(opts.min) && score < opts.min) {
207
+ console.error(`specgate: coverage ${score}% is below the required ${opts.min}%`);
208
+ return 1;
209
+ }
210
+ if (opts.failUncovered && uncovered > 0) {
211
+ console.error(`specgate: ${uncovered} acceptance criteria are uncovered`);
212
+ return 1;
213
+ }
214
+ if (opts.failUntested && untested > 0) {
215
+ console.error(`specgate: ${untested} acceptance criteria are untested`);
216
+ return 1;
217
+ }
218
+ return 0;
219
+ }
220
+
221
+ process.exit(main());