specgate-cli 0.0.0-stage → 0.1.4

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,348 @@
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
+ # run without installing — no npm account, no registry token
62
+ npx --yes --package=https://github.com/aazh2026/specgate/releases/download/v0.1.4/specgate-0.1.4.tgz specgate
63
+
64
+ # or straight from the git tag
65
+ npx --yes github:aazh2026/specgate
66
+
67
+ # or with Homebrew
68
+ brew install aazh2026/specgate/specgate
69
+
70
+ # or as a container image (no local Node needed)
71
+ docker run --rm -v "$PWD:/workspace" -w /workspace ghcr.io/aazh2026/specgate:0.1.4
72
+
73
+ # or clone and run it directly
74
+ git clone https://github.com/aazh2026/specgate
75
+ cd specgate && node bin/specgate.js
76
+ ```
77
+
78
+ Requires Node.js 20+. No runtime dependencies.
79
+
80
+ > **Coming to npm.** The publish pipeline is wired up, but the package is not
81
+ > on npmjs.org yet — `npx specgate` will 404 until it is. Use the release
82
+ > tarball form above in the meantime; it runs the exact same published
83
+ > tarball and needs no npm account.
84
+
85
+ ### GitHub Packages (fallback, needs a token)
86
+
87
+ The same tarball is also published to GitHub Packages as
88
+ `@aazh2026/specgate`. Be aware of one registry rule before you use it:
89
+ **`npm.pkg.github.com` requires an auth token for every read**, even though
90
+ the package is publicly visible. An anonymous install returns
91
+ `401 authentication token not provided`.
92
+
93
+ ```bash
94
+ npm install @aazh2026/specgate --registry https://npm.pkg.github.com
95
+ ```
96
+
97
+ with a `.npmrc` carrying a GitHub token that has the `read:packages` scope:
98
+
99
+ ```
100
+ //npm.pkg.github.com/:_authToken=ghp_xxx
101
+ ```
102
+
103
+ This is a fallback for people who already have a GitHub token — not a
104
+ replacement for a public npmjs.org package. The two zero-token paths above
105
+ (tarball, `npx github:`) are the ones that need no credentials at all.
106
+
107
+ ## Quick start
108
+
109
+ If your project has no written spec yet, scaffold one:
110
+
111
+ ```bash
112
+ npx --yes --package=https://github.com/aazh2026/specgate/releases/download/v0.1.4/specgate-0.1.4.tgz specgate init
113
+ ```
114
+
115
+ That writes a starter `SPEC.md` with the acceptance criteria already structured,
116
+ plus a `.github/workflows/requirements.yml` CI gate. It detects your stack
117
+ (language, test runner, package manager) so the scaffold matches your project.
118
+
119
+ Then edit the criteria, tag your tests, and scan:
120
+
121
+ ```bash
122
+ npx --yes --package=https://github.com/aazh2026/specgate/releases/download/v0.1.4/specgate-0.1.4.tgz specgate
123
+ ```
124
+
125
+ ## Usage
126
+
127
+ ```bash
128
+ specgate # auto-discover the spec, print a report
129
+ specgate --spec docs/SPEC.md # point it at a specific spec
130
+ specgate --format json # machine-readable output
131
+ specgate --min 80 # fail CI below 80% coverage
132
+ specgate --fail-uncovered # fail if anything is uncovered
133
+ specgate --sarif-out specgate.sarif # upload to GitHub code scanning
134
+ ```
135
+
136
+ | Flag | Meaning |
137
+ |---|---|
138
+ | `--spec <file>` | Spec file to read. Repeatable. |
139
+ | `--format <fmt>` | `text` (default), `json`, or `sarif` |
140
+ | `--min <n>` | Exit 1 if the coverage score is below `n` |
141
+ | `--fail-uncovered` | Exit 1 if any criterion has no evidence |
142
+ | `--fail-untested` | Exit 1 if any criterion is implemented but untested |
143
+ | `--json-out <file>` | Write the JSON report to a file |
144
+ | `--sarif-out <file>` | Write a SARIF report for GitHub code scanning |
145
+ | `--force` | With `init`: overwrite existing files |
146
+ | `--name <name>` | With `init`: project name for the spec title |
147
+ | `-v`, `--verbose` | Show every criterion, not just the gaps |
148
+
149
+ ## How it works
150
+
151
+ `specgate` does four things, all locally:
152
+
153
+ **1. Parse the spec.** It finds your acceptance criteria and gives each one a
154
+ stable id. If you wrote ids, it keeps them. If you didn't, it mints `AC-1`,
155
+ `AC-2`, … in document order, so the tool still works on a plain bullet list.
156
+
157
+ ```markdown
158
+ ## Acceptance Criteria
159
+
160
+ - [ ] AC-1: User can export invoices as CSV
161
+ - [ ] [AC-2] Export respects the date range filter
162
+ - [ ] Export fails loudly when the storage backend is unavailable
163
+ ```
164
+
165
+ It also reads **Gherkin/Cucumber `.feature` files** — a feature file is a spec
166
+ by construction, so every scenario becomes an acceptance criterion:
167
+
168
+ ```gherkin
169
+ Feature: Invoice export
170
+
171
+ @AC-1
172
+ Scenario: Export invoices as CSV
173
+ Given an invoice exists
174
+ When the user requests an export
175
+ Then a CSV file is produced
176
+ ```
177
+
178
+ A scenario tagged `@AC-1` keeps the same id as the markdown criterion, so a
179
+ `SPEC.md` and a feature file can describe the same requirement without
180
+ double-counting it. Untagged scenarios get `SC-1`, `SC-2`, … in document
181
+ order. Scenarios are matched against their steps as well as their title, so a
182
+ step definition like `Given("the user requests an export")` counts as evidence
183
+ even when the scenario title says something else.
184
+
185
+ **2. Index the code.** It walks your repo for source and test files, skipping
186
+ `node_modules`, `dist`, and friends. Identifier boundaries are respected:
187
+ `resetPassword`, `export_csv`, and `HTTPResponse` are all split correctly, so a
188
+ criterion written in English prose can match code written in camelCase.
189
+
190
+ **3. Find evidence, in tiers.**
191
+
192
+ | Tier | Status | What it means |
193
+ |---|---|---|
194
+ | `tagged` | `covered` | An explicit marker — `// AC-1`, `# AC-1`, `[AC-1]` — points at the criterion. |
195
+ | `named` | `inferred` | A test name matches the criterion's distinctive words. A hint, not a guarantee. |
196
+ | `text` | `untested` | Only implementation code matched. Nothing pins the behaviour down. |
197
+ | — | `uncovered` | No evidence anywhere. |
198
+
199
+ **4. Score.** Evidence tiers are weighted, because they are not equally
200
+ trustworthy:
201
+
202
+ ```
203
+ score = 100 × (covered×1.0 + inferred×0.75 + untested×0.4 + uncovered×0.0) / total
204
+ ```
205
+
206
+ `inferred` is discounted on purpose. A test named `resetPassword` might test
207
+ something else entirely. Only an explicit tag is worth full credit.
208
+
209
+ ## Using it in CI
210
+
211
+ The quickest way in:
212
+
213
+ ```yaml
214
+ # .github/workflows/requirements.yml
215
+ name: Requirement coverage
216
+ on: [pull_request]
217
+
218
+ jobs:
219
+ specgate:
220
+ runs-on: ubuntu-latest
221
+ steps:
222
+ - uses: actions/checkout@v4
223
+ - uses: aazh2026/specgate@v0.1.4
224
+ with:
225
+ min: 80
226
+ fail-uncovered: true
227
+ ```
228
+
229
+ The action exposes the score as an output:
230
+
231
+ ```yaml
232
+ - id: specgate
233
+ uses: aazh2026/specgate@v0.1.4
234
+ - run: echo "Coverage was ${{ steps.specgate.outputs.score }}%"
235
+ ```
236
+
237
+ Or run the CLI directly:
238
+
239
+ ```yaml
240
+ - uses: actions/setup-node@v4
241
+ with: { node-version: 20 }
242
+ - run: npx --yes github:aazh2026/specgate --min 80 --fail-uncovered
243
+ ```
244
+
245
+ The gate is the point. A spec that says "the user can export invoices as CSV"
246
+ should stop a pull request that doesn't implement it.
247
+
248
+ For GitHub code scanning, upload the SARIF report:
249
+
250
+ ```yaml
251
+ - run: npx --yes github:aazh2026/specgate --sarif-out specgate.sarif --format sarif
252
+ - uses: github/codeql-action/upload-sarif@v3
253
+ with:
254
+ sarif_file: specgate.sarif
255
+ ```
256
+
257
+ Or let the action do it:
258
+
259
+ ```yaml
260
+ - uses: aazh2026/specgate@v0.1.4
261
+ with:
262
+ sarif: specgate.sarif
263
+ - uses: github/codeql-action/upload-sarif@v3
264
+ with:
265
+ sarif_file: specgate.sarif
266
+ ```
267
+
268
+ ## Spec format
269
+
270
+ Anything Markdown works. `specgate` looks for a heading matching
271
+ `acceptance criteria`, `acceptance`, `requirements`, `user stories`, `criteria`
272
+ (or `验收标准` / `需求` / `用户故事`), and treats the bullets under it as criteria.
273
+
274
+ Criteria without an explicit id still get one. If your team prefers plain
275
+ prose, this is enough:
276
+
277
+ ```markdown
278
+ ## Acceptance Criteria
279
+
280
+ - User can export invoices as CSV
281
+ - Export respects the date range filter
282
+ ```
283
+
284
+ If your team already numbers things, use it:
285
+
286
+ ```markdown
287
+ ## Acceptance Criteria
288
+
289
+ - [ ] AC-1: User can export invoices as CSV
290
+ - [ ] REQ-12: Export respects the date range filter
291
+ ```
292
+
293
+ ### Adding explicit tags
294
+
295
+ To move a criterion from `inferred` to `covered`, point at it:
296
+
297
+ ```js
298
+ // AC-1: User can export invoices as CSV
299
+ test('exports invoices as CSV', () => { /* ... */ });
300
+ ```
301
+
302
+ ```python
303
+ # AC-2: Export respects the date range filter
304
+ def test_export_respects_date_range():
305
+ ...
306
+ ```
307
+
308
+ ## API
309
+
310
+ ```js
311
+ import { runSpecgate } from 'specgate';
312
+
313
+ const report = runSpecgate({ root: process.cwd(), specFiles: ['docs/SPEC.md'] });
314
+
315
+ report.summary; // { score, total, covered, inferred, untested, uncovered }
316
+ report.criteria; // [{ id, text, status, evidence: { tier, file, line } }]
317
+ report.stats; // { filesScanned, testFiles }
318
+ ```
319
+
320
+ ## Limitations
321
+
322
+ Rust test detection is file-level. A `#[cfg(test)] mod` marks a mixed source
323
+ and test file as a test file; it does not restrict evidence to the test module.
324
+
325
+ Being honest about what this is:
326
+
327
+ - **It matches text, not meaning.** A criterion written as *"the operator can
328
+ revoke a session"* will not match a function called `deleteToken` unless the
329
+ words overlap. Use explicit tags for anything that matters.
330
+ - **`inferred` is a heuristic.** It is weighted down for that reason, but it is
331
+ not proof.
332
+ - **English and Chinese prose both tokenise**, but a spec mixing languages
333
+ works best when criteria are written in the language that matches the code
334
+ identifiers.
335
+ - **It is not a test runner.** It does not know whether your tests pass. Pair it
336
+ with your existing test suite.
337
+
338
+ ## Contributing
339
+
340
+ Issues and pull requests are welcome. Run the tests with:
341
+
342
+ ```bash
343
+ npm test
344
+ ```
345
+
346
+ ## License
347
+
348
+ 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());