@cleartrip/frontguard 0.3.5 → 1.0.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 CHANGED
@@ -1,291 +1,336 @@
1
1
  # FrontGuard
2
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.
3
+ Automated frontend code quality checks for every pull request. Runs in CI, catches issues before merge, and posts a clean report comment with a link to the full interactive HTML report.
4
4
 
5
5
  ---
6
6
 
7
- ## Quick Start
7
+ ## What it checks
8
+
9
+ | Check | What it does | Runs on |
10
+ |:------|:------------|:--------|
11
+ | **eslint** | Linting with your project's ESLint config | Changed files |
12
+ | **prettier** | Formatting check against your Prettier config | Changed files |
13
+ | **typescript** | `tsc --noEmit` type checking | Whole project |
14
+ | **secrets** | Regex scan for leaked tokens, keys, private keys | Changed files |
15
+ | **pr-hygiene** | PR description length, required sections, AI disclosure | PR metadata |
16
+ | **pr-size** | Flags PRs that are too large to review effectively | PR diff |
17
+ | **ts-any-delta** | Counts new `any` usage introduced in this diff | PR diff |
18
+ | **cycles** | Circular import detection via `madge` | Source tree |
19
+ | **dead-code** | Unused exports via `ts-prune` | Whole project |
20
+ | **bundle** | Bundle size measured after build, compared to a baseline | Build output |
21
+ | **core-web-vitals** | Static JSX/TSX hints for LCP, CLS, FID patterns | Changed files |
22
+ | **react-native** | Metro config, SwiftLint, native file hints (RN projects only) | Changed files |
23
+ | **custom-rules** | Your own file/content checks defined in config | Changed files |
24
+ | **ai-assisted-strict** | Stricter checks on AI-generated code regions | Opt-in |
25
+
26
+ All checks are **enabled by default** except `ai-assisted-strict` and `llm` (both off by default). Every check can be disabled or tuned individually.
27
+
28
+ ---
29
+
30
+ ## Quick start
8
31
 
9
32
  ### 1. Install
10
33
 
11
34
  ```bash
12
- # yarn
13
- yarn add -D @cleartrip/frontguard
14
-
15
- # npm
16
35
  npm install -D @cleartrip/frontguard
17
-
18
- # pnpm
36
+ # or
37
+ yarn add -D @cleartrip/frontguard
38
+ # or
19
39
  pnpm add -D @cleartrip/frontguard
20
40
  ```
21
41
 
22
- ### 2. Initialize
42
+ ### 2. Initialise
23
43
 
24
44
  ```bash
25
45
  npx frontguard init
26
46
  ```
27
47
 
28
- This does three things:
29
-
30
- - **Creates `frontguard.config.mjs`** (ESM) with all available options as commented examples — works even when `package.json` is not `"type": "module"`
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.
48
+ This creates three files:
35
49
 
36
- ### 3. Configure
50
+ - **`frontguard.config.mjs`** — config file with every option documented as comments
51
+ - **`pull_request_template.md`** — PR template with AI disclosure checkboxes
52
+ - **`bitbucket-pipelines.yml`** — creates or updates the pipeline with a FrontGuard step
37
53
 
38
- Open `frontguard.config.mjs` and uncomment the checks you want. Every option is documented inline.
54
+ ### 3. Run locally
39
55
 
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
56
+ ```bash
57
+ npx frontguard run
51
58
  ```
52
59
 
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
60
+ Run the same checks locally before pushing. No CI environment needed.
57
61
 
58
62
  ---
59
63
 
60
- ## Checks
61
-
62
- | Check | What it does | Default |
63
- |-------|-------------|---------|
64
- | **eslint** | Runs ESLint with your project config (skips `frontguard.config.*` by default) | Enabled |
65
- | **prettier** | Verifies Prettier formatting (skips `frontguard.config.*` by default) | 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 |
64
+ ## Configuration
93
65
 
94
- ### Examples
66
+ Config lives in `frontguard.config.mjs` (recommended), `frontguard.config.cjs`, or `frontguard.config.js` at your project root.
95
67
 
96
- **Next.js (auto-detected):**
97
68
  ```js
98
- // frontguard.config.mjs — no strategy override needed
99
- bundle: {
100
- buildCommand: 'yarn build',
101
- maxDeltaBytes: 50_000,
102
- }
103
- ```
69
+ // frontguard.config.mjs
70
+ import { defineConfig } from '@cleartrip/frontguard'
104
71
 
105
- **Custom script:**
106
- ```js
107
- bundle: {
108
- bundleSizeStrategy: 'custom',
109
- bundleSizeCommand: 'node scripts/bundle-size.js',
110
- buildCommand: 'yarn build',
111
- }
112
- ```
72
+ export default defineConfig({
73
+ // 'warn' = advisory only, CI never fails (default)
74
+ // 'enforce' = CI exits non-zero on any block-severity finding
75
+ mode: 'warn',
113
76
 
114
- **Baseline file** (`.frontguard/bundle-baseline.json`):
115
- ```json
116
- { "totalBytes": 245760 }
77
+ checks: {
78
+ // Tune any check by key — only set what you want to override
79
+ prSize: {
80
+ tiers: [
81
+ { minLines: 800, severity: 'block', message: 'PR too large (${lines} lines). Split it up.' },
82
+ { minLines: 400, severity: 'warn', message: 'Large PR (${lines} lines). Consider splitting.' },
83
+ ],
84
+ },
85
+ bundle: {
86
+ buildCommand: 'yarn build',
87
+ maxDeltaBytes: 50_000, // fail if bundle grows more than 50 kB
88
+ },
89
+ // Disable a check entirely
90
+ deadCode: { enabled: false },
91
+ // Custom rules — plain JS functions
92
+ // (see Custom rules section below)
93
+ },
94
+ })
117
95
  ```
118
96
 
119
- Commit this to your base branch. FrontGuard compares the current build against it.
97
+ Run `frontguard init` to generate the full annotated template.
120
98
 
121
- ---
99
+ ### Severity levels
122
100
 
123
- ## Configuration
101
+ | Level | Meaning |
102
+ |:------|:--------|
103
+ | `info` | Informational note — never fails CI |
104
+ | `warn` | Advisory — visible in report, CI passes unless `mode: 'enforce'` |
105
+ | `block` | Failing — CI exits non-zero when `mode: 'enforce'` is set |
124
106
 
125
- FrontGuard loads the first file that exists, in order: **`frontguard.config.mjs`** → **`frontguard.config.cjs`** → **`frontguard.config.js`**.
107
+ ### Gate modes
126
108
 
127
- - **`.mjs`** ESM (`import` / `export default`). Use this in CommonJS-only repos (no `"type": "module"` needed). **Recommended.**
128
- - **`.cjs`** — CommonJS without importing the package (plain object merges like `defineConfig`):
109
+ Most checks have a `gate` option that controls the severity of their findings:
129
110
 
130
111
  ```js
131
- // frontguard.config.cjs
132
- module.exports = {
133
- checks: {
134
- bundle: { buildCommand: 'yarn build:prod', bundleSizeStrategy: 'next' },
135
- },
136
- }
112
+ cycles: { gate: 'block' } // circular deps now fail CI in enforce mode
113
+ tsAnyDelta: { gate: 'warn' } // any usage is advisory (default)
137
114
  ```
138
115
 
139
- - **`.js`** — Only reliable as ESM if your `package.json` has `"type": "module"`; otherwise Node may refuse to load it (you will see an ESM warning and defaults will apply).
116
+ ---
140
117
 
141
- **CI / monorepos:** Run `frontguard` with `cwd` set to the app root that contains `package.json` and the config file. **Git:** If you see `fatal: Needed a single revision`, increase clone depth or fetch the PR destination branch (e.g. `git fetch origin main:refs/remotes/origin/main`) so diff and baseline reads can resolve `origin/<branch>`.
118
+ ## Custom rules
142
119
 
143
- All configuration lives in one of those files at your project root (same directory you run `frontguard` from):
120
+ Define your own checks inline in the config file. The function receives `{ path, content }` for each file and returns `true` when the rule is violated.
144
121
 
145
122
  ```js
146
- import { defineConfig } from '@cleartrip/frontguard'
147
-
148
123
  export default defineConfig({
149
- mode: 'warn', // 'warn' or 'enforce'
150
- checks: {
151
- bundle: {
152
- buildCommand: 'yarn build',
153
- bundleSizeStrategy: 'auto',
154
- maxDeltaBytes: 50_000,
124
+ rules: {
125
+ 'no-console-log': {
126
+ severity: 'warn',
127
+ message: 'Remove console.log before merging',
128
+ check: (file) =>
129
+ /\.(ts|tsx|js|jsx)$/.test(file.path) && /console\.log\(/.test(file.content),
155
130
  },
156
- prSize: {
157
- tiers: [
158
- { minLines: 1000, severity: 'block', message: 'PR too large (${lines} lines)' },
159
- { minLines: 500, severity: 'warn', message: 'Consider splitting (${lines} lines)' },
160
- ],
131
+ 'no-todo-comments': {
132
+ severity: 'info',
133
+ message: 'Unresolved TODO comment',
134
+ check: (file) => /\bTODO\b/.test(file.content),
135
+ },
136
+ 'no-inline-styles': {
137
+ severity: 'info',
138
+ message: 'Prefer CSS classes over inline style objects',
139
+ check: (file) => /\.tsx$/.test(file.path) && file.content.includes('style={{'),
161
140
  },
162
- // AI-assisted + LLM are WIP — leave off unless you opt in explicitly:
163
- // aiAssistedReview: { enabled: true },
164
- // llm: { enabled: true, provider: 'ollama', model: 'llama3.2' },
165
141
  },
166
142
  })
167
143
  ```
168
144
 
169
- Run `frontguard init` to generate a config file with every option documented as comments.
145
+ ---
146
+
147
+ ## Bundle size
148
+
149
+ The bundle check builds your project and compares the output size against a baseline file you commit to your repo.
150
+
151
+ ### Setup
152
+
153
+ 1. Run your build once and create the baseline:
154
+ ```bash
155
+ echo '{ "totalBytes": 245760 }' > .frontguard/bundle-baseline.json
156
+ git add .frontguard/bundle-baseline.json
157
+ ```
158
+
159
+ 2. Configure the check:
160
+ ```js
161
+ bundle: {
162
+ buildCommand: 'yarn build',
163
+ maxDeltaBytes: 50_000, // warn/block if bundle grows > 50 kB
164
+ maxTotalBytes: null, // or set an absolute cap
165
+ }
166
+ ```
167
+
168
+ 3. FrontGuard reads the baseline from `baselineRef` (default: `main`) when the file is not on disk, so it works in PR pipelines automatically.
169
+
170
+ ### Size strategies
171
+
172
+ | Strategy | Use when |
173
+ |:---------|:---------|
174
+ | `auto` | Most projects — auto-detects Next.js, Vite, CRA, or falls back to glob |
175
+ | `next` | Next.js — reads "First Load JS shared by all" from build output |
176
+ | `vite` | Vite — sums `dist/assets/*.js` |
177
+ | `cra` | Create React App — reads gzipped JS total from build output |
178
+ | `glob` | Custom build tools — sums files matching `measureGlobs` |
179
+ | `custom` | Anything else — runs `bundleSizeCommand`, reads bytes from stdout |
180
+
181
+ ---
182
+
183
+ ## PR hygiene
170
184
 
171
- ### ESLint / Prettier vs `frontguard.config.*`
185
+ Validates the PR description when running in CI (requires `FRONTGUARD_PR_BODY` env var):
172
186
 
173
- Defaults exclude `frontguard.config.mjs`, `frontguard.config.js`, and `frontguard.config.cjs` via `checks.eslint.ignorePatterns` and `checks.prettier.ignorePatterns` (picomatch globs, repo-relative). That keeps tool config from being judged by product rules. Set `ignorePatterns: []` on either check if you want those files linted/formatted. To extend the list in shared config, spread `DEFAULT_FRONTGUARD_CONFIG_IGNORE_PATTERNS` from `@cleartrip/frontguard`.
187
+ ```js
188
+ prHygiene: {
189
+ minBodyLength: 80, // minimum characters in description
190
+ requireSections: true, // require headers from sectionHints
191
+ sectionHints: ['what', 'why', 'test', 'screenshot'],
192
+ requireAiDisclosureSection: true, // expect an ## AI disclosure section
193
+ }
194
+ ```
174
195
 
175
- ### Bitbucket PR comment table
196
+ The `pull_request_template.md` created by `frontguard init` includes the AI disclosure section automatically.
176
197
 
177
- The short PR comment lists **only checks that reported findings**. Skipped checks stay out of that table; the full HTML report still shows every check, including skips.
198
+ ---
178
199
 
179
- ### Shared Org Config
200
+ ## Shared org config
180
201
 
181
- Publish a shared config package and extend it:
202
+ Publish defaults to an npm package and extend them in each repo:
182
203
 
183
204
  ```js
205
+ // frontguard.config.mjs
184
206
  export default defineConfig({
185
- extends: '@your-org/frontguard-config/base',
207
+ extends: '@your-org/frontguard-config',
186
208
  checks: {
187
- // project-specific overrides
209
+ // repo-specific overrides
188
210
  },
189
211
  })
190
212
  ```
191
213
 
214
+ The org config is deep-merged as a base layer. Individual repos only need to set what differs.
215
+
192
216
  ---
193
217
 
194
- ## CLI
218
+ ## CLI reference
195
219
 
196
220
  ```bash
197
- # Initialize project (config + pipeline + PR template)
221
+ # Scaffold config, PR template, and pipeline step
198
222
  frontguard init
199
223
 
200
- # Run all checks (console + markdown output)
224
+ # Run all checks (console output + markdown report)
201
225
  frontguard run
202
226
 
203
- # Run with markdown-only output
227
+ # Fail CI on block-severity findings
228
+ frontguard run --enforce
229
+
230
+ # Output markdown only (good for capturing in CI logs)
204
231
  frontguard run --markdown
205
232
 
206
- # Run in enforce mode (exit 1 on blocking findings)
207
- frontguard run --enforce
233
+ # Generate HTML report file
234
+ frontguard run --htmlOut report.html
208
235
 
209
- # Generate HTML report + PR comment markdown
210
- frontguard run --htmlOut report.html --prCommentOut comment.md
236
+ # Generate Bitbucket PR comment markdown
237
+ frontguard run --prCommentOut comment.md
211
238
 
212
- # Append manual review notes
239
+ # Append manual review notes to the report
213
240
  frontguard run --append ./notes.md
214
241
  ```
215
242
 
216
243
  ---
217
244
 
218
- ## Pipeline Integration
245
+ ## CI integration (Bitbucket)
219
246
 
220
- ### How `init` handles your pipeline
247
+ `frontguard init` creates or updates `bitbucket-pipelines.yml` with a ready-to-use step. It:
221
248
 
222
- | Scenario | What `init` does |
223
- |----------|-----------------|
224
- | No `bitbucket-pipelines.yml` | Creates one with `image`, `clone`, and the FrontGuard step |
225
- | File exists, no `pull-requests:` section | Appends a `pull-requests:` section with the FrontGuard step |
226
- | File exists, `pull-requests:` with existing steps | Appends the FrontGuard step after your existing steps |
227
- | FrontGuard step already present | Skips (no duplicate) |
249
+ 1. Runs all checks
250
+ 2. Generates an HTML report
251
+ 3. Uploads it to [FreeKit](https://freekit.dev) (static hosting, free, no login)
252
+ 4. Posts a comment on the PR with the risk score and a link to the full report
228
253
 
229
- Your `image`, `clone`, `definitions`, and other pipeline sections are **never modified**.
254
+ ### Required variables
230
255
 
231
- ### Required Variables
256
+ Add these as secured variables in your Bitbucket repo settings:
232
257
 
233
- | Variable | Type | Purpose |
234
- |----------|------|---------|
235
- | `BITBUCKET_ACCESS_TOKEN` | Secured | Post PR comments |
236
- | `FREEKIT_BASE_URL` | Optional | Override report hosting endpoint |
237
-
238
- ---
258
+ | Variable | Required | Purpose |
259
+ |:---------|:---------|:--------|
260
+ | `BITBUCKET_ACCESS_TOKEN` | Yes | Post PR comments |
261
+ | `FREEKIT_BASE_URL` | No | Override report hosting (default: `https://freekit.dev`) |
262
+ | `FRONTGUARD_PR_BODY` | No | Pass PR description to `pr-hygiene` check |
239
263
 
240
- ## Risk Scoring
264
+ ### Pipeline init scenarios
241
265
 
242
- The composite risk score is a heuristic combining:
266
+ | Situation | What `init` does |
267
+ |:----------|:----------------|
268
+ | No `bitbucket-pipelines.yml` | Creates one with `image`, `clone`, and the FrontGuard step |
269
+ | File exists, no `pull-requests:` section | Appends a `pull-requests:` section |
270
+ | File exists, `pull-requests:` present | Appends the FrontGuard step after existing steps |
271
+ | FrontGuard step already present | Skips (no duplicates) |
243
272
 
244
- | Signal | LOW | MEDIUM | HIGH |
245
- |--------|-----|--------|------|
246
- | Blocking findings | 0 | — | 1+ |
247
- | Warning count | < 3 | 3–7 | 8+ |
248
- | PR size (lines) | < 400 | 400–799 | 800+ |
249
- | Changed files | < 25 | — | 25+ |
273
+ Your `image`, `clone`, `definitions`, and other pipeline config are never modified.
250
274
 
251
275
  ---
252
276
 
253
- ## AI-assisted review and LLM (under development)
277
+ ## Risk scoring
254
278
 
255
- These features are **implemented in code but turned off by default** so day-to-day CI stays predictable.
279
+ The risk score is a heuristic shown at the top of every report.
256
280
 
257
- | Feature | Config | Default |
258
- |---------|--------|---------|
259
- | **AI-assisted strict** (`ai-assisted-strict` check) | `checks.aiAssistedReview.enabled` | `false` |
260
- | **LLM appendix + Cursor rules context** | `checks.llm.enabled` | `false` |
281
+ | Score | When |
282
+ |:------|:-----|
283
+ | 🔴 HIGH | 1+ blocking findings, or 8+ warnings, or 800+ changed lines |
284
+ | 🟡 MEDIUM | 3–7 warnings, or 400–799 changed lines, or 25+ files changed |
285
+ | 🟢 LOW | No significant findings |
261
286
 
262
- When you are ready to try them:
287
+ The score is informational it does not affect CI pass/fail by itself.
263
288
 
264
- 1. Set `checks.aiAssistedReview.enabled: true` for static heuristics on `@frontguard-ai` regions or AI-disclosed PRs (see `frontguard.config.mjs` comments).
265
- 2. Set `checks.llm.enabled: true` and choose `provider: 'ollama' | 'openai' | 'anthropic'` for automated review text.
289
+ ---
266
290
 
267
- **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.
291
+ ## React Native
268
292
 
269
- ### Cursor rules (LLM only)
293
+ The `react-native` check runs automatically when `react-native` is in your dependencies.
270
294
 
271
- 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.
295
+ | Sub-check | Default | What it does |
296
+ |:----------|:--------|:-------------|
297
+ | Metro config | On | Warns if `metro.config.*` is missing |
298
+ | SwiftLint | On | Runs `swiftlint` when PR touches `.swift` files |
299
+ | Android hint | On | Reminds to run Android Lint when `.kt`/`.java` change |
300
+ | `@rnx-kit/align-deps` | Off | Validates RN dependency alignment (opt-in, needs network) |
301
+ | `react-native doctor` | Off | Runs the RN doctor CLI (opt-in, slow) |
272
302
 
273
303
  ---
274
304
 
275
- ## Local Development
305
+ ## AI-assisted review (opt-in)
276
306
 
277
- ```bash
278
- # Run checks locally (same as CI)
279
- npx frontguard run
307
+ Stricter checks for code written by AI tools. Disabled by default.
308
+
309
+ ```js
310
+ aiAssistedReview: {
311
+ enabled: true,
312
+ gate: 'warn',
313
+ strictScanMode: 'decorator', // 'decorator' | 'pr-disclosure' | 'both'
314
+ }
280
315
  ```
281
316
 
317
+ Mark AI-generated code with inline comments:
318
+
319
+ ```ts
320
+ // @frontguard-ai:start
321
+ const result = eval(userInput) // flagged: eval in AI region
322
+ // @frontguard-ai:end
323
+ ```
324
+
325
+ Detects: `eval()`, `dangerouslySetInnerHTML`, `@ts-ignore`, empty `catch`, `new Function()`, plain HTTP URLs, tokens in `localStorage`, dynamic SQL, and more.
326
+
282
327
  ---
283
328
 
284
329
  ## Requirements
285
330
 
286
- - **Node.js** >= 18
287
- - **Bitbucket Cloud** pipelines
288
- - `BITBUCKET_ACCESS_TOKEN` for PR comments
331
+ - **Node.js** 18
332
+ - Git (for diff and baseline operations)
333
+ - Bitbucket Cloud pipelines (for PR context and comment posting)
289
334
 
290
335
  ---
291
336