@llbbl/uncov 0.1.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 +121 -0
- package/README.md +240 -0
- package/dist/cli.js +900 -0
- package/package.json +28 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
Creative Commons Legal Code
|
|
2
|
+
|
|
3
|
+
CC0 1.0 Universal
|
|
4
|
+
|
|
5
|
+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
|
6
|
+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
|
7
|
+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
|
8
|
+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
|
9
|
+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
|
10
|
+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
|
11
|
+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
|
12
|
+
HEREUNDER.
|
|
13
|
+
|
|
14
|
+
Statement of Purpose
|
|
15
|
+
|
|
16
|
+
The laws of most jurisdictions throughout the world automatically confer
|
|
17
|
+
exclusive Copyright and Related Rights (defined below) upon the creator
|
|
18
|
+
and subsequent owner(s) (each and all, an "owner") of an original work of
|
|
19
|
+
authorship and/or a database (each, a "Work").
|
|
20
|
+
|
|
21
|
+
Certain owners wish to permanently relinquish those rights to a Work for
|
|
22
|
+
the purpose of contributing to a commons of creative, cultural and
|
|
23
|
+
scientific works ("Commons") that the public can reliably and without fear
|
|
24
|
+
of later claims of infringement build upon, modify, incorporate in other
|
|
25
|
+
works, reuse and redistribute as freely as possible in any form whatsoever
|
|
26
|
+
and for any purposes, including without limitation commercial purposes.
|
|
27
|
+
These owners may contribute to the Commons to promote the ideal of a free
|
|
28
|
+
culture and the further production of creative, cultural and scientific
|
|
29
|
+
works, or to gain reputation or greater distribution for their Work in
|
|
30
|
+
part through the use and efforts of others.
|
|
31
|
+
|
|
32
|
+
For these and/or other purposes and motivations, and without any
|
|
33
|
+
expectation of additional consideration or compensation, the person
|
|
34
|
+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
|
35
|
+
is an owner of Copyright and Related Rights in the Work, voluntarily
|
|
36
|
+
elects to apply CC0 to the Work and publicly distribute the Work under its
|
|
37
|
+
terms, with knowledge of his or her Copyright and Related Rights in the
|
|
38
|
+
Work and the meaning and intended legal effect of CC0 on those rights.
|
|
39
|
+
|
|
40
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
41
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
|
42
|
+
Related Rights"). Copyright and Related Rights include, but are not
|
|
43
|
+
limited to, the following:
|
|
44
|
+
|
|
45
|
+
i. the right to reproduce, adapt, distribute, perform, display,
|
|
46
|
+
communicate, and translate a Work;
|
|
47
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
|
48
|
+
iii. publicity and privacy rights pertaining to a person's image or
|
|
49
|
+
likeness depicted in a Work;
|
|
50
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
|
51
|
+
subject to the limitations in paragraph 4(a), below;
|
|
52
|
+
v. rights protecting the extraction, dissemination, use and reuse of data
|
|
53
|
+
in a Work;
|
|
54
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
55
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
|
56
|
+
protection of databases, and under any national implementation
|
|
57
|
+
thereof, including any amended or successor version of such
|
|
58
|
+
directive); and
|
|
59
|
+
vii. other similar, equivalent or corresponding rights throughout the
|
|
60
|
+
world based on applicable law or treaty, and any national
|
|
61
|
+
implementations thereof.
|
|
62
|
+
|
|
63
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention
|
|
64
|
+
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
|
65
|
+
irrevocably and unconditionally waives, abandons, and surrenders all of
|
|
66
|
+
Affirmer's Copyright and Related Rights and associated claims and causes
|
|
67
|
+
of action, whether now known or unknown (including existing as well as
|
|
68
|
+
future claims and causes of action), in the Work (i) in all territories
|
|
69
|
+
worldwide, (ii) for the maximum duration provided by applicable law or
|
|
70
|
+
treaty (including future time extensions), (iii) in any current or future
|
|
71
|
+
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
|
72
|
+
including without limitation commercial, advertising or promotional
|
|
73
|
+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
|
74
|
+
member of the public at large and to the detriment of Affirmer's heirs and
|
|
75
|
+
successors, fully intending that such Waiver shall not be subject to
|
|
76
|
+
revocation, rescission, cancellation, termination, or any other legal or
|
|
77
|
+
equitable action to disrupt the quiet enjoyment of the Work by the public
|
|
78
|
+
as contemplated by Affirmer's express Statement of Purpose.
|
|
79
|
+
|
|
80
|
+
3. Public License Fallback. Should any part of the Waiver for any reason
|
|
81
|
+
be judged legally invalid or ineffective under applicable law, then the
|
|
82
|
+
Waiver shall be preserved to the maximum extent permitted taking into
|
|
83
|
+
account Affirmer's express Statement of Purpose. In addition, to the
|
|
84
|
+
extent the Waiver is so judged Affirmer hereby grants to each affected
|
|
85
|
+
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
|
86
|
+
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
|
87
|
+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
|
88
|
+
maximum duration provided by applicable law or treaty (including future
|
|
89
|
+
time extensions), (iii) in any current or future medium and for any number
|
|
90
|
+
of copies, and (iv) for any purpose whatsoever, including without
|
|
91
|
+
limitation commercial, advertising or promotional purposes (the
|
|
92
|
+
"License"). The License shall be deemed effective as of the date CC0 was
|
|
93
|
+
applied by Affirmer to the Work. Should any part of the License for any
|
|
94
|
+
reason be judged legally invalid or ineffective under applicable law, such
|
|
95
|
+
partial invalidity or ineffectiveness shall not invalidate the remainder
|
|
96
|
+
of the License, and in such case Affirmer hereby affirms that he or she
|
|
97
|
+
will not (i) exercise any of his or her remaining Copyright and Related
|
|
98
|
+
Rights in the Work or (ii) assert any associated claims and causes of
|
|
99
|
+
action with respect to the Work, in either case contrary to Affirmer's
|
|
100
|
+
express Statement of Purpose.
|
|
101
|
+
|
|
102
|
+
4. Limitations and Disclaimers.
|
|
103
|
+
|
|
104
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
105
|
+
surrendered, licensed or otherwise affected by this document.
|
|
106
|
+
b. Affirmer offers the Work as-is and makes no representations or
|
|
107
|
+
warranties of any kind concerning the Work, express, implied,
|
|
108
|
+
statutory or otherwise, including without limitation warranties of
|
|
109
|
+
title, merchantability, fitness for a particular purpose, non
|
|
110
|
+
infringement, or the absence of latent or other defects, accuracy, or
|
|
111
|
+
the present or absence of errors, whether or not discoverable, all to
|
|
112
|
+
the greatest extent permissible under applicable law.
|
|
113
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
|
114
|
+
that may apply to the Work or any use thereof, including without
|
|
115
|
+
limitation any person's Copyright and Related Rights in the Work.
|
|
116
|
+
Further, Affirmer disclaims responsibility for obtaining any necessary
|
|
117
|
+
consents, permissions or other rights required for any use of the
|
|
118
|
+
Work.
|
|
119
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
120
|
+
party to this document and has no duty or obligation with respect to
|
|
121
|
+
this CC0 or use of the Work.
|
package/README.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# uncov
|
|
2
|
+
|
|
3
|
+
[](https://github.com/llbbl/uncov/actions/workflows/ci.yml)
|
|
4
|
+
[](https://codecov.io/gh/llbbl/uncov)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
A zero-dependency CLI tool that reports files with low test coverage from Vitest/Istanbul JSON output.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Zero dependencies** - Single binary, instant startup
|
|
12
|
+
- **Zero config** - Just run `uncov` in any project with coverage output
|
|
13
|
+
- **Cross-platform** - Binaries for macOS (arm64, x64), Linux (x64), Windows (x64)
|
|
14
|
+
- **CI-friendly** - JSON output, exit codes for automation
|
|
15
|
+
- **Optional setup** - Bootstrap coverage scripts with `uncov init`
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
### Quick Install (Recommended)
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
curl -fsSL https://raw.githubusercontent.com/llbbl/uncov/main/install.sh | bash
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
This automatically detects your platform, downloads the latest binary, verifies the checksum, and installs to your PATH.
|
|
26
|
+
|
|
27
|
+
### npm / pnpm / bun
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
# npm
|
|
31
|
+
npm install -g uncov
|
|
32
|
+
|
|
33
|
+
# pnpm
|
|
34
|
+
pnpm add -g uncov
|
|
35
|
+
|
|
36
|
+
# bun
|
|
37
|
+
bun add -g uncov
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Download Binary
|
|
41
|
+
|
|
42
|
+
Download the latest release for your platform from [GitHub Releases](https://github.com/llbbl/uncov/releases).
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
# macOS (Apple Silicon)
|
|
46
|
+
curl -fsSL https://github.com/llbbl/uncov/releases/latest/download/uncov-darwin-arm64 -o uncov
|
|
47
|
+
chmod +x uncov
|
|
48
|
+
sudo mv uncov /usr/local/bin/
|
|
49
|
+
|
|
50
|
+
# macOS (Intel)
|
|
51
|
+
curl -fsSL https://github.com/llbbl/uncov/releases/latest/download/uncov-darwin-x64 -o uncov
|
|
52
|
+
chmod +x uncov
|
|
53
|
+
sudo mv uncov /usr/local/bin/
|
|
54
|
+
|
|
55
|
+
# Linux
|
|
56
|
+
curl -fsSL https://github.com/llbbl/uncov/releases/latest/download/uncov-linux-x64 -o uncov
|
|
57
|
+
chmod +x uncov
|
|
58
|
+
sudo mv uncov /usr/local/bin/
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Build from Source
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
git clone https://github.com/llbbl/uncov.git
|
|
65
|
+
cd uncov
|
|
66
|
+
bun install
|
|
67
|
+
bun run build:local
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Quick Start
|
|
71
|
+
|
|
72
|
+
1. Run your tests with coverage:
|
|
73
|
+
```bash
|
|
74
|
+
pnpm test:coverage
|
|
75
|
+
# or
|
|
76
|
+
vitest run --coverage
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
2. Report low coverage files:
|
|
80
|
+
```bash
|
|
81
|
+
uncov
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Output:
|
|
85
|
+
```
|
|
86
|
+
Files at or below 10% line coverage: 3
|
|
87
|
+
|
|
88
|
+
0.00% LH 0/LF 120 src/renderer.ts
|
|
89
|
+
5.26% LH 2/LF 38 src/bootstrap.ts
|
|
90
|
+
10.00% LH 10/LF 100 src/engine.ts
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Commands
|
|
94
|
+
|
|
95
|
+
### `uncov` (default)
|
|
96
|
+
|
|
97
|
+
Report files below coverage threshold.
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
uncov # Files at or below 10% coverage (default)
|
|
101
|
+
uncov --threshold 50 # Files at or below 50% coverage
|
|
102
|
+
uncov --threshold 0 # Only 0% coverage files
|
|
103
|
+
uncov --fail # Exit 1 if any files below threshold
|
|
104
|
+
uncov --json # JSON output for scripting
|
|
105
|
+
uncov --verbose # Show debug information
|
|
106
|
+
uncov --no-color # Disable colorized output
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### `uncov init`
|
|
110
|
+
|
|
111
|
+
Bootstrap coverage configuration in your project.
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
uncov init # Interactive setup
|
|
115
|
+
uncov init --force # Overwrite existing config
|
|
116
|
+
uncov init --dry-run # Preview changes without modifying files
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
This command:
|
|
120
|
+
1. Detects your package manager (pnpm, bun, npm, yarn)
|
|
121
|
+
2. Adds scripts to `package.json`:
|
|
122
|
+
- `test:coverage` - Run tests with coverage
|
|
123
|
+
- `coverage:low` - Run uncov
|
|
124
|
+
3. Creates `vitest.config.ts` with coverage settings
|
|
125
|
+
4. Adds `coverage/` to `.gitignore`
|
|
126
|
+
|
|
127
|
+
### `uncov check`
|
|
128
|
+
|
|
129
|
+
Verify coverage setup is correct.
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
uncov check # Verify setup
|
|
133
|
+
uncov check --verbose # Show detailed check information
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Checks:
|
|
137
|
+
- Vitest config exists
|
|
138
|
+
- Coverage config is present
|
|
139
|
+
- `coverage-summary.json` exists
|
|
140
|
+
- Required scripts are configured
|
|
141
|
+
|
|
142
|
+
## Configuration
|
|
143
|
+
|
|
144
|
+
### uncov.config.json
|
|
145
|
+
|
|
146
|
+
```json
|
|
147
|
+
{
|
|
148
|
+
"threshold": 10,
|
|
149
|
+
"exclude": ["**/*.test.ts", "**/index.ts"],
|
|
150
|
+
"failOnLow": false,
|
|
151
|
+
"coveragePath": "coverage/coverage-summary.json"
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### package.json
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
{
|
|
159
|
+
"uncov": {
|
|
160
|
+
"threshold": 20,
|
|
161
|
+
"failOnLow": true
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Configuration Priority
|
|
167
|
+
|
|
168
|
+
1. CLI flags (highest)
|
|
169
|
+
2. `uncov.config.json`
|
|
170
|
+
3. `package.json` uncov field
|
|
171
|
+
4. Defaults (lowest)
|
|
172
|
+
|
|
173
|
+
## Exit Codes
|
|
174
|
+
|
|
175
|
+
| Code | Meaning |
|
|
176
|
+
|------|---------|
|
|
177
|
+
| 0 | Success (or low coverage files found but `--fail` not set) |
|
|
178
|
+
| 1 | Low coverage files found and `--fail` set |
|
|
179
|
+
| 2 | Missing coverage-summary.json or config error |
|
|
180
|
+
|
|
181
|
+
## Development
|
|
182
|
+
|
|
183
|
+
### Setup
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
git clone https://github.com/llbbl/uncov.git
|
|
187
|
+
cd uncov
|
|
188
|
+
bun install
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Commands
|
|
192
|
+
|
|
193
|
+
Using [Just](https://github.com/casey/just) (recommended):
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
just # Show available commands
|
|
197
|
+
just install # Install dependencies
|
|
198
|
+
just dev # Run CLI locally
|
|
199
|
+
just dev --help # Run CLI with args
|
|
200
|
+
just test # Run tests
|
|
201
|
+
just lint # Lint with Biome
|
|
202
|
+
just lint-fix # Fix lint issues
|
|
203
|
+
just build # Build local binary
|
|
204
|
+
just build-all # Build all platform binaries
|
|
205
|
+
just check # Run lint + tests
|
|
206
|
+
just clean # Remove build artifacts
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Or using bun directly:
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
bun run dev # Run CLI locally
|
|
213
|
+
bun test # Run tests
|
|
214
|
+
bun run lint # Lint with Biome
|
|
215
|
+
bun run lint:fix # Fix lint issues
|
|
216
|
+
bun run build:local # Build local binary
|
|
217
|
+
bun run build # Build all platform binaries
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Project Structure
|
|
221
|
+
|
|
222
|
+
```
|
|
223
|
+
uncov/
|
|
224
|
+
├── src/
|
|
225
|
+
│ ├── cli.ts # Entry point, arg parsing
|
|
226
|
+
│ ├── commands/ # Command implementations
|
|
227
|
+
│ ├── lib/ # Core logic
|
|
228
|
+
│ └── utils/ # Helpers
|
|
229
|
+
├── test/ # Tests
|
|
230
|
+
├── scripts/
|
|
231
|
+
│ └── build.ts # Cross-platform build
|
|
232
|
+
└── docs/
|
|
233
|
+
└── spec.md # Full specification
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
See [docs/spec.md](docs/spec.md) for the full specification.
|
|
237
|
+
|
|
238
|
+
## License
|
|
239
|
+
|
|
240
|
+
MIT
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,900 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
|
|
4
|
+
// src/cli.ts
|
|
5
|
+
import { parseArgs } from "util";
|
|
6
|
+
// package.json
|
|
7
|
+
var package_default = {
|
|
8
|
+
name: "@llbbl/uncov",
|
|
9
|
+
version: "0.1.1",
|
|
10
|
+
description: "Report files with low test coverage from Vitest/Istanbul output",
|
|
11
|
+
type: "module",
|
|
12
|
+
main: "./dist/cli.js",
|
|
13
|
+
bin: {
|
|
14
|
+
uncov: "./dist/cli.js"
|
|
15
|
+
},
|
|
16
|
+
files: ["dist"],
|
|
17
|
+
scripts: {
|
|
18
|
+
dev: "bun run src/cli.ts",
|
|
19
|
+
test: "bun test",
|
|
20
|
+
lint: "biome check .",
|
|
21
|
+
"lint:fix": "biome check --write .",
|
|
22
|
+
build: "bun run scripts/build.ts",
|
|
23
|
+
"build:local": "bun build --compile --outfile=uncov src/cli.ts",
|
|
24
|
+
"build:npm": "bun build src/cli.ts --outdir=dist --target=node",
|
|
25
|
+
"test:coverage": "vitest run --coverage",
|
|
26
|
+
"coverage:low": "uncov"
|
|
27
|
+
},
|
|
28
|
+
devDependencies: {
|
|
29
|
+
"@biomejs/biome": "^1.9.4",
|
|
30
|
+
"@types/bun": "^1.2.4"
|
|
31
|
+
},
|
|
32
|
+
keywords: ["coverage", "vitest", "testing", "cli"],
|
|
33
|
+
license: "MIT"
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// src/lib/constants.ts
|
|
37
|
+
var DEFAULT_COVERAGE_PATH = "coverage/coverage-summary.json";
|
|
38
|
+
var VITEST_CONFIG_FILES = [
|
|
39
|
+
{ file: "vitest.config.ts", type: "ts" },
|
|
40
|
+
{ file: "vitest.config.js", type: "js" },
|
|
41
|
+
{ file: "vitest.config.mts", type: "mts" },
|
|
42
|
+
{ file: "vitest.config.mjs", type: "mjs" },
|
|
43
|
+
{ file: "vite.config.ts", type: "ts" },
|
|
44
|
+
{ file: "vite.config.js", type: "js" },
|
|
45
|
+
{ file: "vite.config.mts", type: "mts" },
|
|
46
|
+
{ file: "vite.config.mjs", type: "mjs" }
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
// src/utils/fs.ts
|
|
50
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
51
|
+
import { dirname, isAbsolute, relative, resolve } from "node:path";
|
|
52
|
+
function validatePath(targetPath, baseDir = process.cwd()) {
|
|
53
|
+
const resolved = resolve(baseDir, targetPath);
|
|
54
|
+
const relativePath = relative(baseDir, resolved);
|
|
55
|
+
if (relativePath.startsWith("..") || isAbsolute(relativePath)) {
|
|
56
|
+
throw new Error(`Path "${targetPath}" is outside allowed directory`);
|
|
57
|
+
}
|
|
58
|
+
return resolved;
|
|
59
|
+
}
|
|
60
|
+
function fileExists(path) {
|
|
61
|
+
return existsSync(path);
|
|
62
|
+
}
|
|
63
|
+
function readJson(path, options) {
|
|
64
|
+
const resolvedPath = options?.baseDir ? validatePath(path, options.baseDir) : path;
|
|
65
|
+
let content;
|
|
66
|
+
try {
|
|
67
|
+
content = readFileSync(resolvedPath, "utf-8");
|
|
68
|
+
} catch (error) {
|
|
69
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
70
|
+
throw new Error(`Failed to read file "${path}": ${message}`);
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
return JSON.parse(content);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
const message = error instanceof Error ? error.message : "Invalid JSON";
|
|
76
|
+
throw new Error(`Failed to parse JSON in "${path}": ${message}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function writeJson(path, data, options) {
|
|
80
|
+
const resolvedPath = options?.baseDir ? validatePath(path, options.baseDir) : path;
|
|
81
|
+
ensureDir(dirname(resolvedPath));
|
|
82
|
+
const content = `${JSON.stringify(data, null, 2)}
|
|
83
|
+
`;
|
|
84
|
+
try {
|
|
85
|
+
writeFileSync(resolvedPath, content, "utf-8");
|
|
86
|
+
} catch (error) {
|
|
87
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
88
|
+
throw new Error(`Failed to write JSON to "${path}": ${message}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function readText(path, options) {
|
|
92
|
+
const resolvedPath = options?.baseDir ? validatePath(path, options.baseDir) : path;
|
|
93
|
+
try {
|
|
94
|
+
return readFileSync(resolvedPath, "utf-8");
|
|
95
|
+
} catch (error) {
|
|
96
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
97
|
+
throw new Error(`Failed to read file "${path}": ${message}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function writeText(path, content, options) {
|
|
101
|
+
const resolvedPath = options?.baseDir ? validatePath(path, options.baseDir) : path;
|
|
102
|
+
ensureDir(dirname(resolvedPath));
|
|
103
|
+
try {
|
|
104
|
+
writeFileSync(resolvedPath, content, "utf-8");
|
|
105
|
+
} catch (error) {
|
|
106
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
107
|
+
throw new Error(`Failed to write to "${path}": ${message}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function ensureDir(path) {
|
|
111
|
+
if (!path || path === "." || path === "/") {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
if (!existsSync(path)) {
|
|
116
|
+
mkdirSync(path, { recursive: true });
|
|
117
|
+
}
|
|
118
|
+
} catch (error) {
|
|
119
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
120
|
+
throw new Error(`Failed to create directory "${path}": ${message}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function appendLine(path, line, options) {
|
|
124
|
+
const resolvedPath = options?.baseDir ? validatePath(path, options.baseDir) : path;
|
|
125
|
+
ensureDir(dirname(resolvedPath));
|
|
126
|
+
try {
|
|
127
|
+
if (existsSync(resolvedPath)) {
|
|
128
|
+
const content = readFileSync(resolvedPath, "utf-8");
|
|
129
|
+
const needsNewlineBefore = content.length > 0 && !content.endsWith(`
|
|
130
|
+
`);
|
|
131
|
+
const lineToAppend = needsNewlineBefore ? `
|
|
132
|
+
${line}
|
|
133
|
+
` : `${line}
|
|
134
|
+
`;
|
|
135
|
+
appendFileSync(resolvedPath, lineToAppend, "utf-8");
|
|
136
|
+
} else {
|
|
137
|
+
writeFileSync(resolvedPath, `${line}
|
|
138
|
+
`, "utf-8");
|
|
139
|
+
}
|
|
140
|
+
} catch (error) {
|
|
141
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
142
|
+
throw new Error(`Failed to append to "${path}": ${message}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function resolvePath(relativePath, cwd) {
|
|
146
|
+
const base = cwd ?? process.cwd();
|
|
147
|
+
return resolve(base, relativePath);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/lib/detect.ts
|
|
151
|
+
var LOCKFILE_MAP = [
|
|
152
|
+
{ file: "pnpm-lock.yaml", manager: "pnpm" },
|
|
153
|
+
{ file: "bun.lockb", manager: "bun" },
|
|
154
|
+
{ file: "package-lock.json", manager: "npm" },
|
|
155
|
+
{ file: "yarn.lock", manager: "yarn" }
|
|
156
|
+
];
|
|
157
|
+
function detectPackageManager(cwd) {
|
|
158
|
+
for (const { file, manager } of LOCKFILE_MAP) {
|
|
159
|
+
const lockfilePath = resolvePath(file, cwd);
|
|
160
|
+
if (fileExists(lockfilePath)) {
|
|
161
|
+
return manager;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return "npm";
|
|
165
|
+
}
|
|
166
|
+
function detectVitestConfig(cwd) {
|
|
167
|
+
for (const { file, type } of VITEST_CONFIG_FILES) {
|
|
168
|
+
const configPath = resolvePath(file, cwd);
|
|
169
|
+
if (fileExists(configPath)) {
|
|
170
|
+
return { path: configPath, type };
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
function detectGitignore(cwd) {
|
|
176
|
+
const gitignorePath = resolvePath(".gitignore", cwd);
|
|
177
|
+
if (!fileExists(gitignorePath)) {
|
|
178
|
+
return { exists: false, hasCoverage: false };
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
const content = readText(gitignorePath);
|
|
182
|
+
const lines = content.split(`
|
|
183
|
+
`).map((line) => line.trim());
|
|
184
|
+
const hasCoverage = lines.some((line) => {
|
|
185
|
+
if (line.startsWith("#") || line === "") {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
return /^[/*]*coverage[/*]*$/.test(line) || line.startsWith("coverage") || line.endsWith("/coverage") || line.endsWith("/coverage/");
|
|
189
|
+
});
|
|
190
|
+
return { exists: true, hasCoverage };
|
|
191
|
+
} catch {
|
|
192
|
+
return { exists: true, hasCoverage: false };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// src/lib/package-json.ts
|
|
197
|
+
function readPackageJson(path) {
|
|
198
|
+
const resolvedPath = path ?? resolvePath("package.json");
|
|
199
|
+
if (!fileExists(resolvedPath)) {
|
|
200
|
+
throw new Error(`package.json not found at: ${resolvedPath}`);
|
|
201
|
+
}
|
|
202
|
+
return readJson(resolvedPath);
|
|
203
|
+
}
|
|
204
|
+
function writePackageJson(pkg, path) {
|
|
205
|
+
const resolvedPath = path ?? resolvePath("package.json");
|
|
206
|
+
writeJson(resolvedPath, pkg);
|
|
207
|
+
}
|
|
208
|
+
function addScripts(scripts, path) {
|
|
209
|
+
const resolvedPath = path ?? resolvePath("package.json");
|
|
210
|
+
const pkg = readPackageJson(resolvedPath);
|
|
211
|
+
const added = [];
|
|
212
|
+
const skipped = [];
|
|
213
|
+
if (!pkg.scripts) {
|
|
214
|
+
pkg.scripts = {};
|
|
215
|
+
}
|
|
216
|
+
for (const [name, command] of Object.entries(scripts)) {
|
|
217
|
+
if (pkg.scripts[name] !== undefined) {
|
|
218
|
+
skipped.push(name);
|
|
219
|
+
} else {
|
|
220
|
+
pkg.scripts[name] = command;
|
|
221
|
+
added.push(name);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (added.length > 0) {
|
|
225
|
+
writePackageJson(pkg, resolvedPath);
|
|
226
|
+
}
|
|
227
|
+
return { added, skipped };
|
|
228
|
+
}
|
|
229
|
+
function hasScript(name, path) {
|
|
230
|
+
const resolvedPath = path ?? resolvePath("package.json");
|
|
231
|
+
if (!fileExists(resolvedPath)) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
const pkg = readPackageJson(resolvedPath);
|
|
236
|
+
return pkg.scripts?.[name] !== undefined;
|
|
237
|
+
} catch {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// src/lib/vitest-config.ts
|
|
243
|
+
function hasVitestConfig(cwd) {
|
|
244
|
+
for (const { file } of VITEST_CONFIG_FILES) {
|
|
245
|
+
const configPath = resolvePath(file, cwd);
|
|
246
|
+
if (fileExists(configPath)) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
function hasCoverageConfig(configPath) {
|
|
253
|
+
if (!fileExists(configPath)) {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
try {
|
|
257
|
+
const content = readText(configPath);
|
|
258
|
+
const hasCoverageBlock = /coverage\s*:\s*\{/.test(content);
|
|
259
|
+
if (hasCoverageBlock) {
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
const hasCoverageProperty = /coverage\s*:\s*[a-zA-Z]/.test(content);
|
|
263
|
+
return hasCoverageProperty;
|
|
264
|
+
} catch {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
function createVitestConfig() {
|
|
269
|
+
return `import { defineConfig } from 'vitest/config';
|
|
270
|
+
|
|
271
|
+
export default defineConfig({
|
|
272
|
+
test: {
|
|
273
|
+
coverage: {
|
|
274
|
+
provider: 'v8',
|
|
275
|
+
reporter: ['text', 'json-summary'],
|
|
276
|
+
reportsDirectory: './coverage',
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
`;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// src/utils/colors.ts
|
|
284
|
+
function isColorSupported(noColor = false) {
|
|
285
|
+
if (noColor) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
if (process.env.NO_COLOR !== undefined) {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
return Boolean(process.stdout.isTTY);
|
|
292
|
+
}
|
|
293
|
+
function makeColor(code, enabled) {
|
|
294
|
+
if (!enabled) {
|
|
295
|
+
return (s) => s;
|
|
296
|
+
}
|
|
297
|
+
return (s) => `\x1B[${code}m${s}\x1B[0m`;
|
|
298
|
+
}
|
|
299
|
+
function createColors(noColor = false) {
|
|
300
|
+
const enabled = isColorSupported(noColor);
|
|
301
|
+
return {
|
|
302
|
+
red: makeColor("31", enabled),
|
|
303
|
+
green: makeColor("32", enabled),
|
|
304
|
+
yellow: makeColor("33", enabled),
|
|
305
|
+
cyan: makeColor("36", enabled),
|
|
306
|
+
bold: makeColor("1", enabled),
|
|
307
|
+
dim: makeColor("2", enabled)
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// src/utils/logger.ts
|
|
312
|
+
function createLogger(verbose) {
|
|
313
|
+
return {
|
|
314
|
+
verbose: (msg) => {
|
|
315
|
+
if (verbose) {
|
|
316
|
+
console.error(`[verbose] ${msg}`);
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
info: (msg) => console.log(msg),
|
|
320
|
+
error: (msg) => console.error(msg)
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// src/commands/check.ts
|
|
325
|
+
async function checkCommand(options = {}) {
|
|
326
|
+
const colors = createColors(options.noColor);
|
|
327
|
+
const log = createLogger(options.verbose ?? false);
|
|
328
|
+
const cwd = process.cwd();
|
|
329
|
+
const checks = [];
|
|
330
|
+
log.verbose(`Working directory: ${cwd}`);
|
|
331
|
+
log.verbose("Checking for vitest config...");
|
|
332
|
+
const vitestConfig = detectVitestConfig(cwd);
|
|
333
|
+
if (vitestConfig) {
|
|
334
|
+
log.verbose(`Found vitest config: ${vitestConfig.path}`);
|
|
335
|
+
checks.push({
|
|
336
|
+
name: "vitest-config",
|
|
337
|
+
passed: true,
|
|
338
|
+
message: `Vitest config found: ${vitestConfig.path}`
|
|
339
|
+
});
|
|
340
|
+
log.verbose("Checking for coverage config in vitest config...");
|
|
341
|
+
const hasCoverage = hasCoverageConfig(vitestConfig.path);
|
|
342
|
+
if (hasCoverage) {
|
|
343
|
+
log.verbose("Coverage config detected");
|
|
344
|
+
checks.push({
|
|
345
|
+
name: "coverage-config",
|
|
346
|
+
passed: true,
|
|
347
|
+
message: "Coverage config detected"
|
|
348
|
+
});
|
|
349
|
+
} else {
|
|
350
|
+
log.verbose("Coverage config not found");
|
|
351
|
+
checks.push({
|
|
352
|
+
name: "coverage-config",
|
|
353
|
+
passed: false,
|
|
354
|
+
message: "Coverage config not found in vitest config",
|
|
355
|
+
hint: "Add coverage configuration to your vitest.config.ts"
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
} else {
|
|
359
|
+
log.verbose("No vitest config found");
|
|
360
|
+
checks.push({
|
|
361
|
+
name: "vitest-config",
|
|
362
|
+
passed: false,
|
|
363
|
+
message: "Vitest config not found",
|
|
364
|
+
hint: "Run 'uncov init' to create a vitest.config.ts"
|
|
365
|
+
});
|
|
366
|
+
checks.push({
|
|
367
|
+
name: "coverage-config",
|
|
368
|
+
passed: false,
|
|
369
|
+
message: "Coverage config not checked (no vitest config)",
|
|
370
|
+
hint: "Create a vitest config first"
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
const coveragePath = resolvePath(DEFAULT_COVERAGE_PATH, cwd);
|
|
374
|
+
log.verbose(`Checking for coverage summary at: ${coveragePath}`);
|
|
375
|
+
if (fileExists(coveragePath)) {
|
|
376
|
+
log.verbose("Coverage summary found");
|
|
377
|
+
checks.push({
|
|
378
|
+
name: "coverage-summary",
|
|
379
|
+
passed: true,
|
|
380
|
+
message: `Coverage summary exists: ${DEFAULT_COVERAGE_PATH}`
|
|
381
|
+
});
|
|
382
|
+
} else {
|
|
383
|
+
log.verbose("Coverage summary not found");
|
|
384
|
+
checks.push({
|
|
385
|
+
name: "coverage-summary",
|
|
386
|
+
passed: false,
|
|
387
|
+
message: `Coverage summary not found: ${DEFAULT_COVERAGE_PATH}`,
|
|
388
|
+
hint: "Run 'pnpm test:coverage' to generate coverage data"
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
log.verbose("Checking for test:coverage script in package.json...");
|
|
392
|
+
const hasTestCoverage = hasScript("test:coverage");
|
|
393
|
+
if (hasTestCoverage) {
|
|
394
|
+
log.verbose("test:coverage script found");
|
|
395
|
+
checks.push({
|
|
396
|
+
name: "scripts",
|
|
397
|
+
passed: true,
|
|
398
|
+
message: "Scripts configured: test:coverage"
|
|
399
|
+
});
|
|
400
|
+
} else {
|
|
401
|
+
log.verbose("test:coverage script not found");
|
|
402
|
+
checks.push({
|
|
403
|
+
name: "scripts",
|
|
404
|
+
passed: false,
|
|
405
|
+
message: "Script not found: test:coverage",
|
|
406
|
+
hint: "Run 'uncov init' to add required scripts"
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
const failedChecks = checks.filter((c) => !c.passed);
|
|
410
|
+
const passedChecks = checks.filter((c) => c.passed);
|
|
411
|
+
for (const check of passedChecks) {
|
|
412
|
+
console.log(`${colors.green("[ok]")} ${check.message}`);
|
|
413
|
+
}
|
|
414
|
+
for (const check of failedChecks) {
|
|
415
|
+
console.log(`${colors.red("[fail]")} ${check.message}`);
|
|
416
|
+
if (check.hint) {
|
|
417
|
+
console.log(` ${colors.dim(check.hint)}`);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
console.log("");
|
|
421
|
+
if (failedChecks.length === 0) {
|
|
422
|
+
console.log(colors.green("All checks passed!"));
|
|
423
|
+
return 0;
|
|
424
|
+
}
|
|
425
|
+
console.log(colors.red(`${failedChecks.length} check${failedChecks.length === 1 ? "" : "s"} failed`));
|
|
426
|
+
return 1;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/commands/init.ts
|
|
430
|
+
var SCRIPTS_TO_ADD = {
|
|
431
|
+
"test:coverage": "vitest run --coverage",
|
|
432
|
+
"coverage:low": "uncov"
|
|
433
|
+
};
|
|
434
|
+
async function initCommand(options) {
|
|
435
|
+
const colors = createColors(options.noColor);
|
|
436
|
+
const log = createLogger(options.verbose ?? false);
|
|
437
|
+
const dryRun = options.dryRun ?? false;
|
|
438
|
+
const cwd = process.cwd();
|
|
439
|
+
if (dryRun) {
|
|
440
|
+
console.log(colors.cyan("Dry run - no files will be modified"));
|
|
441
|
+
console.log("");
|
|
442
|
+
}
|
|
443
|
+
log.verbose(`Working directory: ${cwd}`);
|
|
444
|
+
const packageJsonPath = resolvePath("package.json", cwd);
|
|
445
|
+
log.verbose(`Checking for package.json at: ${packageJsonPath}`);
|
|
446
|
+
if (!fileExists(packageJsonPath)) {
|
|
447
|
+
console.error(colors.red("Error: No package.json found in current directory."));
|
|
448
|
+
console.error("Run 'npm init' or 'pnpm init' first to create a package.json.");
|
|
449
|
+
return 2;
|
|
450
|
+
}
|
|
451
|
+
try {
|
|
452
|
+
readPackageJson(packageJsonPath);
|
|
453
|
+
log.verbose("package.json is valid JSON");
|
|
454
|
+
} catch (error) {
|
|
455
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
456
|
+
console.error(colors.red(`Error: Failed to read package.json: ${message}`));
|
|
457
|
+
return 2;
|
|
458
|
+
}
|
|
459
|
+
log.verbose("Detecting package manager from lockfiles...");
|
|
460
|
+
const packageManager = detectPackageManager(cwd);
|
|
461
|
+
if (dryRun) {
|
|
462
|
+
console.log(`Would detect package manager: ${colors.bold(packageManager)}`);
|
|
463
|
+
} else {
|
|
464
|
+
console.log(`${colors.green("[ok]")} Detected package manager: ${packageManager}`);
|
|
465
|
+
}
|
|
466
|
+
log.verbose("Checking package.json scripts...");
|
|
467
|
+
if (dryRun) {
|
|
468
|
+
for (const script of Object.keys(SCRIPTS_TO_ADD)) {
|
|
469
|
+
console.log(`Would add script: ${colors.bold(script)}`);
|
|
470
|
+
}
|
|
471
|
+
} else {
|
|
472
|
+
const scriptsResult = addScripts(SCRIPTS_TO_ADD, packageJsonPath);
|
|
473
|
+
for (const script of scriptsResult.added) {
|
|
474
|
+
console.log(`${colors.green("[ok]")} Added script: ${script}`);
|
|
475
|
+
}
|
|
476
|
+
for (const script of scriptsResult.skipped) {
|
|
477
|
+
console.log(`${colors.yellow("[skip]")} Script already exists: ${script}`);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
log.verbose("Checking for existing vitest config...");
|
|
481
|
+
const existingConfig = detectVitestConfig(cwd);
|
|
482
|
+
const vitestConfigPath = resolvePath("vitest.config.ts", cwd);
|
|
483
|
+
if (dryRun) {
|
|
484
|
+
if (existingConfig && !options.force) {
|
|
485
|
+
console.log(`Would skip vitest config (already exists: ${existingConfig.path})`);
|
|
486
|
+
} else if (existingConfig && options.force) {
|
|
487
|
+
console.log(`Would overwrite: ${colors.bold("vitest.config.ts")}`);
|
|
488
|
+
} else if (!hasVitestConfig(cwd)) {
|
|
489
|
+
console.log(`Would create: ${colors.bold("vitest.config.ts")}`);
|
|
490
|
+
}
|
|
491
|
+
} else {
|
|
492
|
+
if (existingConfig && !options.force) {
|
|
493
|
+
console.log(`${colors.yellow("[skip]")} Vitest config already exists: ${existingConfig.path}`);
|
|
494
|
+
console.log(" Use --force to overwrite");
|
|
495
|
+
} else {
|
|
496
|
+
if (existingConfig && options.force) {
|
|
497
|
+
const configContent = createVitestConfig();
|
|
498
|
+
writeText(vitestConfigPath, configContent);
|
|
499
|
+
console.log(`${colors.green("[ok]")} Overwrote vitest.config.ts with coverage settings`);
|
|
500
|
+
} else if (!hasVitestConfig(cwd)) {
|
|
501
|
+
const configContent = createVitestConfig();
|
|
502
|
+
writeText(vitestConfigPath, configContent);
|
|
503
|
+
console.log(`${colors.green("[ok]")} Created vitest.config.ts with coverage settings`);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
log.verbose("Checking .gitignore...");
|
|
508
|
+
const gitignoreResult = detectGitignore(cwd);
|
|
509
|
+
const gitignorePath = resolvePath(".gitignore", cwd);
|
|
510
|
+
if (dryRun) {
|
|
511
|
+
if (gitignoreResult.hasCoverage) {
|
|
512
|
+
console.log("Would skip .gitignore (coverage/ already present)");
|
|
513
|
+
} else if (gitignoreResult.exists) {
|
|
514
|
+
console.log(`Would append to .gitignore: ${colors.bold("coverage/")}`);
|
|
515
|
+
} else {
|
|
516
|
+
console.log(`Would create .gitignore with: ${colors.bold("coverage/")}`);
|
|
517
|
+
}
|
|
518
|
+
} else {
|
|
519
|
+
if (gitignoreResult.hasCoverage) {
|
|
520
|
+
console.log(`${colors.yellow("[skip]")} coverage/ already in .gitignore`);
|
|
521
|
+
} else {
|
|
522
|
+
appendLine(gitignorePath, "coverage/");
|
|
523
|
+
if (gitignoreResult.exists) {
|
|
524
|
+
console.log(`${colors.green("[ok]")} Added coverage/ to .gitignore`);
|
|
525
|
+
} else {
|
|
526
|
+
console.log(`${colors.green("[ok]")} Created .gitignore with coverage/`);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
console.log("");
|
|
531
|
+
if (dryRun) {
|
|
532
|
+
console.log(colors.cyan("Run without --dry-run to apply these changes."));
|
|
533
|
+
} else {
|
|
534
|
+
console.log(`Run '${packageManager} test:coverage' to generate coverage, then 'uncov' to see low coverage files.`);
|
|
535
|
+
}
|
|
536
|
+
return 0;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// src/lib/config.ts
|
|
540
|
+
var DEFAULT_CONFIG = {
|
|
541
|
+
threshold: 10,
|
|
542
|
+
exclude: [],
|
|
543
|
+
failOnLow: false,
|
|
544
|
+
coveragePath: "coverage/coverage-summary.json"
|
|
545
|
+
};
|
|
546
|
+
var CONFIG_FILE_NAME = "uncov.config.json";
|
|
547
|
+
function findConfigFile(cwd) {
|
|
548
|
+
const configPath = resolvePath(CONFIG_FILE_NAME, cwd);
|
|
549
|
+
return fileExists(configPath) ? configPath : null;
|
|
550
|
+
}
|
|
551
|
+
function readPackageJsonConfig(cwd) {
|
|
552
|
+
const packagePath = resolvePath("package.json", cwd);
|
|
553
|
+
if (!fileExists(packagePath)) {
|
|
554
|
+
return null;
|
|
555
|
+
}
|
|
556
|
+
try {
|
|
557
|
+
const pkg = readJson(packagePath);
|
|
558
|
+
const uncovConfig = pkg.uncov;
|
|
559
|
+
if (typeof uncovConfig === "object" && uncovConfig !== null) {
|
|
560
|
+
return validatePartialConfig(uncovConfig);
|
|
561
|
+
}
|
|
562
|
+
return null;
|
|
563
|
+
} catch {
|
|
564
|
+
return null;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
function validatePartialConfig(obj) {
|
|
568
|
+
const result = {};
|
|
569
|
+
if (typeof obj.threshold === "number" && obj.threshold >= 0 && obj.threshold <= 100) {
|
|
570
|
+
result.threshold = obj.threshold;
|
|
571
|
+
}
|
|
572
|
+
if (Array.isArray(obj.exclude) && obj.exclude.every((e) => typeof e === "string")) {
|
|
573
|
+
result.exclude = obj.exclude;
|
|
574
|
+
}
|
|
575
|
+
if (typeof obj.failOnLow === "boolean") {
|
|
576
|
+
result.failOnLow = obj.failOnLow;
|
|
577
|
+
}
|
|
578
|
+
if (typeof obj.coveragePath === "string") {
|
|
579
|
+
result.coveragePath = obj.coveragePath;
|
|
580
|
+
}
|
|
581
|
+
return result;
|
|
582
|
+
}
|
|
583
|
+
function mergeConfigs(...configs) {
|
|
584
|
+
const result = { ...DEFAULT_CONFIG };
|
|
585
|
+
for (const config of configs) {
|
|
586
|
+
if (config.threshold !== undefined) {
|
|
587
|
+
result.threshold = config.threshold;
|
|
588
|
+
}
|
|
589
|
+
if (config.exclude !== undefined) {
|
|
590
|
+
result.exclude = config.exclude;
|
|
591
|
+
}
|
|
592
|
+
if (config.failOnLow !== undefined) {
|
|
593
|
+
result.failOnLow = config.failOnLow;
|
|
594
|
+
}
|
|
595
|
+
if (config.coveragePath !== undefined) {
|
|
596
|
+
result.coveragePath = config.coveragePath;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return result;
|
|
600
|
+
}
|
|
601
|
+
function loadConfig(cliOverrides, cwd) {
|
|
602
|
+
const configs = [];
|
|
603
|
+
const pkgConfig = readPackageJsonConfig(cwd);
|
|
604
|
+
if (pkgConfig) {
|
|
605
|
+
configs.push(pkgConfig);
|
|
606
|
+
}
|
|
607
|
+
const configFilePath = findConfigFile(cwd);
|
|
608
|
+
if (configFilePath) {
|
|
609
|
+
try {
|
|
610
|
+
const fileConfig = readJson(configFilePath);
|
|
611
|
+
configs.push(validatePartialConfig(fileConfig));
|
|
612
|
+
} catch {}
|
|
613
|
+
}
|
|
614
|
+
if (cliOverrides) {
|
|
615
|
+
configs.push(cliOverrides);
|
|
616
|
+
}
|
|
617
|
+
return mergeConfigs(...configs);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// src/lib/coverage.ts
|
|
621
|
+
function isValidCoverageMetric(obj) {
|
|
622
|
+
if (typeof obj !== "object" || obj === null)
|
|
623
|
+
return false;
|
|
624
|
+
const metric = obj;
|
|
625
|
+
return typeof metric.total === "number" && typeof metric.covered === "number" && typeof metric.pct === "number";
|
|
626
|
+
}
|
|
627
|
+
function isValidFileCoverage(obj) {
|
|
628
|
+
if (typeof obj !== "object" || obj === null)
|
|
629
|
+
return false;
|
|
630
|
+
const coverage = obj;
|
|
631
|
+
return isValidCoverageMetric(coverage.lines);
|
|
632
|
+
}
|
|
633
|
+
function validateCoverageSummary(data) {
|
|
634
|
+
if (typeof data !== "object" || data === null) {
|
|
635
|
+
throw new Error("Coverage summary must be an object");
|
|
636
|
+
}
|
|
637
|
+
const summary = data;
|
|
638
|
+
if (!isValidFileCoverage(summary.total)) {
|
|
639
|
+
throw new Error('Coverage summary missing valid "total" field');
|
|
640
|
+
}
|
|
641
|
+
for (const [key, value] of Object.entries(summary)) {
|
|
642
|
+
if (key !== "total" && !isValidFileCoverage(value)) {
|
|
643
|
+
throw new Error(`Invalid coverage data for file: ${key}`);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return summary;
|
|
647
|
+
}
|
|
648
|
+
function parseCoverageSummary(coveragePath) {
|
|
649
|
+
const data = readJson(coveragePath);
|
|
650
|
+
return validateCoverageSummary(data);
|
|
651
|
+
}
|
|
652
|
+
function filterBelowThreshold(summary, threshold) {
|
|
653
|
+
const result = [];
|
|
654
|
+
for (const [path, coverage] of Object.entries(summary)) {
|
|
655
|
+
if (path === "total")
|
|
656
|
+
continue;
|
|
657
|
+
const linesPct = coverage.lines.pct;
|
|
658
|
+
if (linesPct <= threshold) {
|
|
659
|
+
result.push({
|
|
660
|
+
path,
|
|
661
|
+
linesPct,
|
|
662
|
+
linesCovered: coverage.lines.covered,
|
|
663
|
+
linesTotal: coverage.lines.total
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
return result;
|
|
668
|
+
}
|
|
669
|
+
function sortByPercentage(files) {
|
|
670
|
+
return [...files].sort((a, b) => a.linesPct - b.linesPct);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
// src/utils/format.ts
|
|
674
|
+
function formatPercent(pct) {
|
|
675
|
+
return `${pct.toFixed(2).padStart(6)}%`;
|
|
676
|
+
}
|
|
677
|
+
function formatLines(covered, total) {
|
|
678
|
+
const coveredStr = covered.toString().padStart(4);
|
|
679
|
+
const totalStr = total.toString().padStart(4);
|
|
680
|
+
return `LH ${coveredStr}/LF ${totalStr}`;
|
|
681
|
+
}
|
|
682
|
+
function formatFileLine(file, colors) {
|
|
683
|
+
const pct = formatPercent(file.linesPct);
|
|
684
|
+
const lines = formatLines(file.linesCovered, file.linesTotal);
|
|
685
|
+
const line = `${pct} ${lines} ${file.path}`;
|
|
686
|
+
if (!colors) {
|
|
687
|
+
return line;
|
|
688
|
+
}
|
|
689
|
+
if (file.linesPct === 0) {
|
|
690
|
+
return colors.red(line);
|
|
691
|
+
}
|
|
692
|
+
return colors.yellow(line);
|
|
693
|
+
}
|
|
694
|
+
function formatReport(files, threshold, colors) {
|
|
695
|
+
if (files.length === 0) {
|
|
696
|
+
const msg = `No files at or below ${threshold}% line coverage.`;
|
|
697
|
+
return colors ? colors.green(msg) : msg;
|
|
698
|
+
}
|
|
699
|
+
const header = `Files at or below ${threshold}% line coverage: ${files.length}
|
|
700
|
+
`;
|
|
701
|
+
const fileLines = files.map((f) => ` ${formatFileLine(f, colors)}`).join(`
|
|
702
|
+
`);
|
|
703
|
+
return `${header}
|
|
704
|
+
${fileLines}`;
|
|
705
|
+
}
|
|
706
|
+
function formatReportJson(files, threshold) {
|
|
707
|
+
return JSON.stringify({
|
|
708
|
+
threshold,
|
|
709
|
+
count: files.length,
|
|
710
|
+
files: files.map((f) => ({
|
|
711
|
+
path: f.path,
|
|
712
|
+
linesPct: f.linesPct,
|
|
713
|
+
linesCovered: f.linesCovered,
|
|
714
|
+
linesTotal: f.linesTotal
|
|
715
|
+
}))
|
|
716
|
+
}, null, 2);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// src/commands/report.ts
|
|
720
|
+
async function reportCommand(options) {
|
|
721
|
+
const colors = createColors(options.noColor);
|
|
722
|
+
const log = createLogger(options.verbose ?? false);
|
|
723
|
+
log.verbose("Loading configuration...");
|
|
724
|
+
const config = loadConfig({
|
|
725
|
+
threshold: options.threshold,
|
|
726
|
+
failOnLow: options.fail,
|
|
727
|
+
coveragePath: options.coveragePath
|
|
728
|
+
});
|
|
729
|
+
log.verbose(`Config loaded: threshold=${config.threshold}, failOnLow=${config.failOnLow}`);
|
|
730
|
+
const coveragePath = resolvePath(config.coveragePath);
|
|
731
|
+
log.verbose(`Coverage path: ${coveragePath}`);
|
|
732
|
+
if (!fileExists(coveragePath)) {
|
|
733
|
+
if (options.json) {
|
|
734
|
+
console.log(JSON.stringify({
|
|
735
|
+
error: "Coverage file not found",
|
|
736
|
+
path: coveragePath
|
|
737
|
+
}, null, 2));
|
|
738
|
+
} else {
|
|
739
|
+
console.error(colors.red(`Error: Coverage file not found: ${coveragePath}`));
|
|
740
|
+
console.error("Run your test coverage command first (e.g., 'pnpm test:coverage')");
|
|
741
|
+
}
|
|
742
|
+
return 2;
|
|
743
|
+
}
|
|
744
|
+
let summary;
|
|
745
|
+
try {
|
|
746
|
+
log.verbose("Parsing coverage summary...");
|
|
747
|
+
summary = parseCoverageSummary(coveragePath);
|
|
748
|
+
log.verbose(`Parsed ${Object.keys(summary).length - 1} files from coverage summary`);
|
|
749
|
+
} catch (error) {
|
|
750
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
751
|
+
if (options.json) {
|
|
752
|
+
console.log(JSON.stringify({
|
|
753
|
+
error: "Failed to parse coverage file",
|
|
754
|
+
details: message
|
|
755
|
+
}, null, 2));
|
|
756
|
+
} else {
|
|
757
|
+
console.error(colors.red(`Error: Failed to parse coverage file: ${message}`));
|
|
758
|
+
}
|
|
759
|
+
return 2;
|
|
760
|
+
}
|
|
761
|
+
log.verbose(`Filtering files at or below ${config.threshold}% threshold...`);
|
|
762
|
+
const filesBelow = filterBelowThreshold(summary, config.threshold);
|
|
763
|
+
log.verbose(`Found ${filesBelow.length} files below threshold`);
|
|
764
|
+
log.verbose("Sorting files by coverage percentage...");
|
|
765
|
+
const sortedFiles = sortByPercentage(filesBelow);
|
|
766
|
+
if (options.json) {
|
|
767
|
+
console.log(formatReportJson(sortedFiles, config.threshold));
|
|
768
|
+
} else {
|
|
769
|
+
console.log(formatReport(sortedFiles, config.threshold, colors));
|
|
770
|
+
}
|
|
771
|
+
if (sortedFiles.length > 0 && config.failOnLow) {
|
|
772
|
+
return 1;
|
|
773
|
+
}
|
|
774
|
+
return 0;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// src/lib/constants.ts
|
|
778
|
+
var DEFAULT_COVERAGE_PATH2 = "coverage/coverage-summary.json";
|
|
779
|
+
|
|
780
|
+
// src/cli.ts
|
|
781
|
+
var VERSION = package_default.version;
|
|
782
|
+
var HELP = `
|
|
783
|
+
uncov - Report files with low test coverage
|
|
784
|
+
|
|
785
|
+
Usage: uncov [command] [options]
|
|
786
|
+
|
|
787
|
+
Commands:
|
|
788
|
+
(default) Report files below coverage threshold
|
|
789
|
+
init Bootstrap coverage configuration
|
|
790
|
+
check Verify coverage setup
|
|
791
|
+
|
|
792
|
+
Options:
|
|
793
|
+
--threshold <n> Coverage threshold percentage (default: 10)
|
|
794
|
+
--fail Exit 1 if files below threshold
|
|
795
|
+
--json Output as JSON
|
|
796
|
+
--no-color Disable colorized output
|
|
797
|
+
--verbose, -V Show debug information
|
|
798
|
+
--dry-run Show what would happen without making changes (init only)
|
|
799
|
+
--help, -h Show this help
|
|
800
|
+
--version, -v Show version
|
|
801
|
+
`.trim();
|
|
802
|
+
function parseCliArgs(args) {
|
|
803
|
+
const { values, positionals } = parseArgs({
|
|
804
|
+
args,
|
|
805
|
+
options: {
|
|
806
|
+
help: { type: "boolean", short: "h", default: false },
|
|
807
|
+
version: { type: "boolean", short: "v", default: false },
|
|
808
|
+
threshold: { type: "string", short: "t", default: "10" },
|
|
809
|
+
fail: { type: "boolean", short: "f", default: false },
|
|
810
|
+
json: { type: "boolean", short: "j", default: false },
|
|
811
|
+
"coverage-path": {
|
|
812
|
+
type: "string",
|
|
813
|
+
short: "c",
|
|
814
|
+
default: DEFAULT_COVERAGE_PATH2
|
|
815
|
+
},
|
|
816
|
+
force: { type: "boolean", default: false },
|
|
817
|
+
"no-color": { type: "boolean", default: false },
|
|
818
|
+
verbose: { type: "boolean", short: "V", default: false },
|
|
819
|
+
"dry-run": { type: "boolean", default: false }
|
|
820
|
+
},
|
|
821
|
+
allowPositionals: true
|
|
822
|
+
});
|
|
823
|
+
const command = positionals[0];
|
|
824
|
+
let parsedCommand = "report";
|
|
825
|
+
if (command === "init") {
|
|
826
|
+
parsedCommand = "init";
|
|
827
|
+
} else if (command === "check") {
|
|
828
|
+
parsedCommand = "check";
|
|
829
|
+
} else if (command && command !== "report") {
|
|
830
|
+
console.error(`Unknown command: ${command}`);
|
|
831
|
+
console.error("Run 'uncov --help' for usage information.");
|
|
832
|
+
process.exit(2);
|
|
833
|
+
}
|
|
834
|
+
const thresholdRaw = values.threshold ?? "10";
|
|
835
|
+
const threshold = Number.parseInt(thresholdRaw, 10);
|
|
836
|
+
if (Number.isNaN(threshold)) {
|
|
837
|
+
console.error(`Error: Invalid threshold value "${thresholdRaw}". Must be a number.`);
|
|
838
|
+
process.exit(2);
|
|
839
|
+
}
|
|
840
|
+
if (threshold < 0 || threshold > 100) {
|
|
841
|
+
console.error(`Error: Threshold must be between 0 and 100, got ${threshold}.`);
|
|
842
|
+
process.exit(2);
|
|
843
|
+
}
|
|
844
|
+
return {
|
|
845
|
+
command: parsedCommand,
|
|
846
|
+
help: values.help ?? false,
|
|
847
|
+
version: values.version ?? false,
|
|
848
|
+
threshold,
|
|
849
|
+
fail: values.fail ?? false,
|
|
850
|
+
json: values.json ?? false,
|
|
851
|
+
coveragePath: values["coverage-path"] ?? DEFAULT_COVERAGE_PATH2,
|
|
852
|
+
force: values.force ?? false,
|
|
853
|
+
noColor: values["no-color"] ?? false,
|
|
854
|
+
verbose: values.verbose ?? false,
|
|
855
|
+
dryRun: values["dry-run"] ?? false
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
async function main() {
|
|
859
|
+
const args = parseCliArgs(process.argv.slice(2));
|
|
860
|
+
if (args.version) {
|
|
861
|
+
console.log(VERSION);
|
|
862
|
+
process.exit(0);
|
|
863
|
+
}
|
|
864
|
+
if (args.help) {
|
|
865
|
+
console.log(HELP);
|
|
866
|
+
process.exit(0);
|
|
867
|
+
}
|
|
868
|
+
let exitCode = 0;
|
|
869
|
+
switch (args.command) {
|
|
870
|
+
case "report":
|
|
871
|
+
exitCode = await reportCommand({
|
|
872
|
+
threshold: args.threshold,
|
|
873
|
+
fail: args.fail,
|
|
874
|
+
json: args.json,
|
|
875
|
+
coveragePath: args.coveragePath,
|
|
876
|
+
noColor: args.noColor,
|
|
877
|
+
verbose: args.verbose
|
|
878
|
+
});
|
|
879
|
+
break;
|
|
880
|
+
case "init":
|
|
881
|
+
exitCode = await initCommand({
|
|
882
|
+
force: args.force,
|
|
883
|
+
noColor: args.noColor,
|
|
884
|
+
verbose: args.verbose,
|
|
885
|
+
dryRun: args.dryRun
|
|
886
|
+
});
|
|
887
|
+
break;
|
|
888
|
+
case "check":
|
|
889
|
+
exitCode = await checkCommand({
|
|
890
|
+
noColor: args.noColor,
|
|
891
|
+
verbose: args.verbose
|
|
892
|
+
});
|
|
893
|
+
break;
|
|
894
|
+
}
|
|
895
|
+
process.exit(exitCode);
|
|
896
|
+
}
|
|
897
|
+
main().catch((error) => {
|
|
898
|
+
console.error("Error:", error.message);
|
|
899
|
+
process.exit(2);
|
|
900
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@llbbl/uncov",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Report files with low test coverage from Vitest/Istanbul output",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/cli.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"uncov": "./dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"files": ["dist"],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"dev": "bun run src/cli.ts",
|
|
13
|
+
"test": "bun test",
|
|
14
|
+
"lint": "biome check .",
|
|
15
|
+
"lint:fix": "biome check --write .",
|
|
16
|
+
"build": "bun run scripts/build.ts",
|
|
17
|
+
"build:local": "bun build --compile --outfile=uncov src/cli.ts",
|
|
18
|
+
"build:npm": "bun build src/cli.ts --outdir=dist --target=node",
|
|
19
|
+
"test:coverage": "vitest run --coverage",
|
|
20
|
+
"coverage:low": "uncov"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@biomejs/biome": "^1.9.4",
|
|
24
|
+
"@types/bun": "^1.2.4"
|
|
25
|
+
},
|
|
26
|
+
"keywords": ["coverage", "vitest", "testing", "cli"],
|
|
27
|
+
"license": "MIT"
|
|
28
|
+
}
|