@cleartrip/frontguard 0.2.9 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,268 @@
1
+ # FrontGuard
2
+
3
+ Org-wide frontend PR guardrails for Bitbucket Cloud. Runs automated checks on every pull request — linting, type safety, bundle size, secrets, PR hygiene, and more — then posts a summary comment with a link to the full interactive report.
4
+
5
+ ---
6
+
7
+ ## Quick Start
8
+
9
+ ### 1. Install
10
+
11
+ ```bash
12
+ # yarn
13
+ yarn add -D @cleartrip/frontguard
14
+
15
+ # npm
16
+ npm install -D @cleartrip/frontguard
17
+
18
+ # pnpm
19
+ pnpm add -D @cleartrip/frontguard
20
+ ```
21
+
22
+ ### 2. Initialize
23
+
24
+ ```bash
25
+ npx frontguard init
26
+ ```
27
+
28
+ This does three things:
29
+
30
+ - **Creates `frontguard.config.js`** with all available options as commented examples
31
+ - **Creates `pull_request_template.md`** with AI disclosure checkboxes
32
+ - **Merges the FrontGuard step** into your existing `bitbucket-pipelines.yml` (or creates one if missing)
33
+
34
+ > If your repo already has a `pull-requests:` section in the pipeline, `init` appends the FrontGuard step to it without touching your `image`, `clone`, or other pipeline config.
35
+
36
+ ### 3. Configure
37
+
38
+ Open `frontguard.config.js` and uncomment the checks you want. Every option is documented inline.
39
+
40
+ ### 4. Set Up CI
41
+
42
+ Add `BITBUCKET_ACCESS_TOKEN` as a **secured variable** in your Bitbucket repo settings. The token needs permission to **post pull request comments**.
43
+
44
+ ---
45
+
46
+ ## How It Works
47
+
48
+ ```
49
+ PR opened → Bitbucket pipeline triggers → FrontGuard runs all enabled checks
50
+ → Generates HTML report → Uploads to FreeKit → Posts Markdown comment on PR
51
+ ```
52
+
53
+ The PR comment includes:
54
+ - **RISK score** (LOW / MEDIUM / HIGH) based on findings and PR size
55
+ - **Table of failing checks** (only checks with issues — clean checks are omitted)
56
+ - **Link to full interactive HTML report** hosted on FreeKit
57
+
58
+ ---
59
+
60
+ ## Checks
61
+
62
+ | Check | What it does | Default |
63
+ |-------|-------------|---------|
64
+ | **eslint** | Runs ESLint with your project config | Enabled |
65
+ | **prettier** | Verifies Prettier formatting | Enabled |
66
+ | **typescript** | Runs `tsc --noEmit` for type errors | Enabled |
67
+ | **secrets** | Scans for leaked tokens, keys, passwords | Enabled |
68
+ | **pr-hygiene** | Validates PR description, sections, AI disclosure | Enabled |
69
+ | **pr-size** | Flags oversized PRs by line count | Enabled |
70
+ | **ts-any-delta** | Counts new `any` usage in the diff | Enabled |
71
+ | **cycles** | Detects circular imports via madge | Enabled |
72
+ | **dead-code** | Finds unused exports via ts-prune | Enabled |
73
+ | **bundle** | Measures bundle size, compares to baseline | Enabled |
74
+ | **core-web-vitals** | Static CWV hints in JSX/TSX | Enabled |
75
+ | **ai-assisted-strict** | Static heuristics on AI-marked code / AI PRs | **Off** (under development) |
76
+ | **custom-rules** | Your own pattern checks defined in config | Enabled |
77
+ | **llm** | Automated diff review / fix hints | **Off** (under development) |
78
+
79
+ ---
80
+
81
+ ## Bundle Size Strategies
82
+
83
+ The bundle check supports multiple strategies for extracting the size metric:
84
+
85
+ | Strategy | How it works |
86
+ |----------|-------------|
87
+ | `auto` | Auto-detects from `package.json` — Next.js, Vite, CRA, or falls back to glob |
88
+ | `next` | Parses `next build` stdout for "First Load JS shared by all" |
89
+ | `vite` | Sums JS files under `dist/assets/` after build |
90
+ | `cra` | Parses `react-scripts build` stdout for gzipped JS total |
91
+ | `glob` | Sums raw bytes under `measureGlobs` (generic fallback) |
92
+ | `custom` | Runs your `bundleSizeCommand`, reads a single number (bytes) from stdout |
93
+
94
+ ### Examples
95
+
96
+ **Next.js (auto-detected):**
97
+ ```js
98
+ // frontguard.config.js — no strategy override needed
99
+ bundle: {
100
+ buildCommand: 'yarn build',
101
+ maxDeltaBytes: 50_000,
102
+ }
103
+ ```
104
+
105
+ **Custom script:**
106
+ ```js
107
+ bundle: {
108
+ bundleSizeStrategy: 'custom',
109
+ bundleSizeCommand: 'node scripts/bundle-size.js',
110
+ buildCommand: 'yarn build',
111
+ }
112
+ ```
113
+
114
+ **Baseline file** (`.frontguard/bundle-baseline.json`):
115
+ ```json
116
+ { "totalBytes": 245760 }
117
+ ```
118
+
119
+ Commit this to your base branch. FrontGuard compares the current build against it.
120
+
121
+ ---
122
+
123
+ ## Configuration
124
+
125
+ All configuration lives in `frontguard.config.js` at your project root:
126
+
127
+ ```js
128
+ import { defineConfig } from '@cleartrip/frontguard'
129
+
130
+ export default defineConfig({
131
+ mode: 'warn', // 'warn' or 'enforce'
132
+ checks: {
133
+ bundle: {
134
+ buildCommand: 'yarn build',
135
+ bundleSizeStrategy: 'auto',
136
+ maxDeltaBytes: 50_000,
137
+ },
138
+ prSize: {
139
+ tiers: [
140
+ { minLines: 1000, severity: 'block', message: 'PR too large (${lines} lines)' },
141
+ { minLines: 500, severity: 'warn', message: 'Consider splitting (${lines} lines)' },
142
+ ],
143
+ },
144
+ // AI-assisted + LLM are WIP — leave off unless you opt in explicitly:
145
+ // aiAssistedReview: { enabled: true },
146
+ // llm: { enabled: true, provider: 'ollama', model: 'llama3.2' },
147
+ },
148
+ })
149
+ ```
150
+
151
+ Run `frontguard init` to generate a config file with every option documented as comments.
152
+
153
+ ### Shared Org Config
154
+
155
+ Publish a shared config package and extend it:
156
+
157
+ ```js
158
+ export default defineConfig({
159
+ extends: '@your-org/frontguard-config/base',
160
+ checks: {
161
+ // project-specific overrides
162
+ },
163
+ })
164
+ ```
165
+
166
+ ---
167
+
168
+ ## CLI
169
+
170
+ ```bash
171
+ # Initialize project (config + pipeline + PR template)
172
+ frontguard init
173
+
174
+ # Run all checks (console + markdown output)
175
+ frontguard run
176
+
177
+ # Run with markdown-only output
178
+ frontguard run --markdown
179
+
180
+ # Run in enforce mode (exit 1 on blocking findings)
181
+ frontguard run --enforce
182
+
183
+ # Generate HTML report + PR comment markdown
184
+ frontguard run --htmlOut report.html --prCommentOut comment.md
185
+
186
+ # Append manual review notes
187
+ frontguard run --append ./notes.md
188
+ ```
189
+
190
+ ---
191
+
192
+ ## Pipeline Integration
193
+
194
+ ### How `init` handles your pipeline
195
+
196
+ | Scenario | What `init` does |
197
+ |----------|-----------------|
198
+ | No `bitbucket-pipelines.yml` | Creates one with `image`, `clone`, and the FrontGuard step |
199
+ | File exists, no `pull-requests:` section | Appends a `pull-requests:` section with the FrontGuard step |
200
+ | File exists, `pull-requests:` with existing steps | Appends the FrontGuard step after your existing steps |
201
+ | FrontGuard step already present | Skips (no duplicate) |
202
+
203
+ Your `image`, `clone`, `definitions`, and other pipeline sections are **never modified**.
204
+
205
+ ### Required Variables
206
+
207
+ | Variable | Type | Purpose |
208
+ |----------|------|---------|
209
+ | `BITBUCKET_ACCESS_TOKEN` | Secured | Post PR comments |
210
+ | `FREEKIT_BASE_URL` | Optional | Override report hosting endpoint |
211
+
212
+ ---
213
+
214
+ ## Risk Scoring
215
+
216
+ The composite risk score is a heuristic combining:
217
+
218
+ | Signal | LOW | MEDIUM | HIGH |
219
+ |--------|-----|--------|------|
220
+ | Blocking findings | 0 | — | 1+ |
221
+ | Warning count | < 3 | 3–7 | 8+ |
222
+ | PR size (lines) | < 400 | 400–799 | 800+ |
223
+ | Changed files | < 25 | — | 25+ |
224
+
225
+ ---
226
+
227
+ ## AI-assisted review and LLM (under development)
228
+
229
+ These features are **implemented in code but turned off by default** so day-to-day CI stays predictable.
230
+
231
+ | Feature | Config | Default |
232
+ |---------|--------|---------|
233
+ | **AI-assisted strict** (`ai-assisted-strict` check) | `checks.aiAssistedReview.enabled` | `false` |
234
+ | **LLM appendix + Cursor rules context** | `checks.llm.enabled` | `false` |
235
+
236
+ When you are ready to try them:
237
+
238
+ 1. Set `checks.aiAssistedReview.enabled: true` for static heuristics on `@frontguard-ai` regions or AI-disclosed PRs (see `frontguard.config.js` comments).
239
+ 2. Set `checks.llm.enabled: true` and choose `provider: 'ollama' | 'openai' | 'anthropic'` for automated review text.
240
+
241
+ **PR template:** The init flow still adds an **AI disclosure** section to `pull_request_template.md` for human reviewers. With AI-assisted review off, that disclosure is recorded as an info finding and does not enable extra static checks until you opt in.
242
+
243
+ ### Cursor rules (LLM only)
244
+
245
+ When **`checks.llm.enabled`** is true, FrontGuard loads `.cursor/rules/*.{md,mdc}`, `.cursorrules`, and `AGENTS.md` (up to ~32 kB) and injects them into the LLM prompts. With LLM off, those files are not read.
246
+
247
+ ---
248
+
249
+ ## Local Development
250
+
251
+ ```bash
252
+ # Run checks locally (same as CI)
253
+ npx frontguard run
254
+ ```
255
+
256
+ ---
257
+
258
+ ## Requirements
259
+
260
+ - **Node.js** >= 18
261
+ - **Bitbucket Cloud** pipelines
262
+ - `BITBUCKET_ACCESS_TOKEN` for PR comments
263
+
264
+ ---
265
+
266
+ ## License
267
+
268
+ MIT