|
1 | 1 | # CLAUDE.md |
2 | 2 |
|
3 | | -🚨 **CRITICAL**: This file contains MANDATORY guidelines for Claude Code (claude.ai/code). You MUST follow these guidelines EXACTLY as specified. Act as a principal-level software engineer with deep expertise in TypeScript, Node.js, and CLI development. |
| 3 | +🚨 **MANDATORY**: Act as principal-level engineer with deep expertise in TypeScript, Node.js, and CLI development. |
4 | 4 |
|
5 | 5 | ## 📚 SHARED STANDARDS |
6 | 6 |
|
7 | | -**This project follows Socket's unified development standards.** For comprehensive guidelines on: |
8 | | -- Code style (imports, sorting, __proto__ patterns, comments) |
9 | | -- Git workflow (GitHub Actions, CI, commit messages) |
10 | | -- Error handling standards and message patterns |
11 | | -- Cross-platform compatibility |
12 | | -- Testing best practices (Vitest memory optimization) |
13 | | -- Dependency alignment |
14 | | -- Changelog management |
| 7 | +**See canonical reference:** `../socket-registry/CLAUDE.md` |
15 | 8 |
|
16 | | -**See the canonical reference:** `socket-registry/CLAUDE.md` (in sibling repository) |
| 9 | +For all shared Socket standards (git workflow, testing, code style, imports, sorting, error handling, cross-platform, CI, etc.), refer to socket-registry/CLAUDE.md. |
17 | 10 |
|
18 | | -This file contains **Socket CLI-specific** rules and patterns. When in doubt, consult socket-registry/CLAUDE.md first. |
19 | | - |
20 | | -## 🎯 YOUR ROLE |
21 | | - |
22 | | -You are a **Principal Software Engineer** responsible for production-quality code, architectural decisions, and system reliability. |
23 | | - |
24 | | -## 🔍 PRE-ACTION PROTOCOL |
25 | | - |
26 | | -- **🚨 MANDATORY**: Before ANY action, review both this file AND socket-registry/CLAUDE.md |
27 | | -- Check before you act - ensure approach follows established patterns |
28 | | -- No exceptions for code changes, commits, documentation, testing, file operations |
29 | | - |
30 | | -## 🛡️ ABSOLUTE RULES |
31 | | - |
32 | | -- 🚨 **NEVER** create files unless absolutely necessary |
33 | | -- 🚨 **ALWAYS** prefer editing existing files |
34 | | -- 🚨 **FORBIDDEN** to proactively create documentation files unless explicitly requested |
35 | | -- 🚨 **REQUIRED** to do exactly what was asked - nothing more, nothing less |
| 11 | +--- |
36 | 12 |
|
37 | | -## 🏗️ ARCHITECTURE |
| 13 | +## 🏗️ CLI-SPECIFIC |
38 | 14 |
|
39 | | -### CLI Tool for Socket.dev Security Analysis |
40 | | -Built with TypeScript using .mts extensions. |
| 15 | +### Architecture |
| 16 | +CLI tool for Socket.dev security analysis - TypeScript with .mts extensions |
41 | 17 |
|
42 | | -### Core Structure |
43 | | -- **Entry point**: `src/cli.mts` - Main CLI entry with meow subcommands |
| 18 | +**Core Structure**: |
| 19 | +- **Entry**: `src/cli.mts` - Main CLI with meow subcommands |
44 | 20 | - **Commands**: `src/commands.mts` - Exports all command definitions |
45 | | -- **Command modules**: `src/commands/*/` - Each feature has cmd-*, handle-*, output-* files |
46 | | -- **Utilities**: `src/utils/` - Shared utilities |
47 | | -- **Constants**: `src/constants.mts` - Application constants |
48 | | -- **Types**: `src/types.mts` - TypeScript type definitions |
| 21 | +- **Modules**: `src/commands/*/` - Each feature: `cmd-*`, `handle-*`, `output-*`, `fetch-*` |
| 22 | +- **Utils**: `src/utils/` - Shared utilities |
| 23 | +- **Constants**: `src/constants.mts` |
| 24 | +- **Types**: `src/types.mts` |
49 | 25 |
|
50 | | -### Command Architecture Pattern |
51 | | -Each command follows consistent pattern: |
52 | | -- `cmd-*.mts` - Command definition and CLI interface |
53 | | -- `handle-*.mts` - Business logic and processing |
54 | | -- `output-*.mts` - Output formatting (JSON, markdown, etc.) |
55 | | -- `fetch-*.mts` - API calls (where applicable) |
| 26 | +**Command Pattern**: `cmd-*.mts` (CLI interface), `handle-*.mts` (business logic), `output-*.mts` (formatting), `fetch-*.mts` (API calls) |
56 | 27 |
|
57 | | -### Key Command Categories |
58 | | -- **npm/npx wrapping**: `socket npm`, `socket npx` - Wraps with security scanning |
59 | | -- **Scanning**: `socket scan` - Create and manage security scans |
60 | | -- **Organization management**: `socket organization` - Manage org settings |
61 | | -- **Package analysis**: `socket package` - Analyze package scores |
62 | | -- **Optimization**: `socket optimize` - Apply Socket registry overrides |
63 | | -- **Configuration**: `socket config` - Manage CLI configuration |
| 28 | +**Categories**: npm/npx wrapping, scanning, org management, package analysis, optimization, configuration |
64 | 29 |
|
65 | | -### Build System |
66 | | -- Uses Rollup for building distribution files |
67 | | -- TypeScript compilation with tsgo (preferred) or tsc |
68 | | -- Individual file compilation for better maintainability |
69 | | -- Multiple environment configs (.env.local, .env.test, .env.dist) |
| 30 | +**Shadow Binaries**: `shadow-bin/npm`, `shadow-bin/npx` - Wrappers for `socket npm`, `socket npx` |
70 | 31 |
|
71 | | -### Shadow Binaries |
72 | | -- **`shadow-bin/`** - Contains wrapper scripts for npm/npx commands |
73 | | - - `shadow-bin/npm` - Wraps npm with Socket security scanning |
74 | | - - `shadow-bin/npx` - Wraps npx with Socket security scanning |
75 | | - - Enables `socket npm` and `socket npx` functionality |
76 | | - |
77 | | -## ⚡ COMMANDS |
| 32 | +### Build System |
| 33 | +- Rollup for distribution |
| 34 | +- TypeScript with tsgo (preferred) or tsc |
| 35 | +- Individual file compilation |
| 36 | +- Env configs: `.env.local`, `.env.test`, `.env.dist` |
78 | 37 |
|
79 | | -### Development Commands |
80 | | -- **Build**: `pnpm run build` (alias for `pnpm run build:dist`) |
| 38 | +### Commands |
| 39 | +- **Build**: `pnpm run build` (alias for `build:dist`) |
81 | 40 | - **Build source**: `pnpm run build:dist:src` |
82 | 41 | - **Build types**: `pnpm run build:dist:types` |
83 | | -- **Test**: `pnpm run test` |
84 | | -- **Test unit**: `pnpm run test:unit` |
| 42 | +- **Test**: `pnpm run test`, `pnpm run test:unit` |
85 | 43 | - **Lint**: `pnpm run check:lint` |
86 | 44 | - **Type check**: `pnpm run check:tsc` (uses tsgo) |
87 | 45 | - **Check all**: `pnpm run check` |
88 | | -- **Commit without tests**: `git commit --no-verify` |
89 | 46 |
|
90 | | -### Running CLI Locally |
91 | | -- **Build and run**: `pnpm run build && pnpm exec socket` |
92 | | -- **Quick build + run**: `pnpm run bs` (builds source only, then runs socket) |
93 | | -- **Run without build**: `pnpm run s` (runs socket directly) |
94 | | -- **Native TypeScript**: `./sd` (Node.js native TypeScript support on Node 22+) |
| 47 | +**Run locally**: |
| 48 | +- `pnpm run build && pnpm exec socket` |
| 49 | +- `pnpm run bs` (builds source, runs socket) |
| 50 | +- `pnpm run s` (runs socket directly) |
| 51 | +- `./sd` (native TS on Node 22+) |
95 | 52 |
|
96 | | -### Testing Best Practices |
97 | | -- **🚨 NEVER USE `--` BEFORE TEST FILE PATHS** - Runs ALL tests! |
98 | | -- **Always build before testing**: Run `pnpm run build:dist:src` first |
99 | | -- **Test single file**: ✅ CORRECT: `pnpm run test:unit src/commands/specific/cmd-file.test.mts` |
100 | | - - ❌ WRONG: `pnpm run test:unit -- src/commands/specific/cmd-file.test.mts` |
101 | | -- **Update snapshots**: |
102 | | - - All tests: `pnpm run testu` |
103 | | - - Single file: ✅ CORRECT: `pnpm run testu src/commands/specific/cmd-file.test.mts` |
104 | | - - ❌ WRONG: `pnpm run testu -- src/commands/specific/cmd-file.test.mts` |
105 | | -- **🚨 MANDATORY Coverage Requirements**: Before pushing commits, ensure test coverage is maintained or improved |
106 | | - - **Never decrease coverage**: All changes MUST maintain or increase existing coverage percentages |
107 | | - - **Check before push**: Run `pnpm run test` to verify coverage thresholds are met |
108 | | - - **Fix coverage drops**: If coverage decreases, add tests to restore or improve coverage before pushing |
109 | | - - **Rationale**: Declining coverage indicates untested code paths, which increases risk of bugs and regressions |
110 | | - |
111 | | -### CI Testing Infrastructure |
112 | | -- **🚨 MANDATORY**: Use `SocketDev/socket-registry/.github/workflows/ci.yml@<SHA>` with full commit SHA (not @main) |
113 | | -- **🚨 CRITICAL**: GitHub Actions require full-length commit SHAs. Format: `@662bbcab1b7533e24ba8e3446cffd8a7e5f7617e # main` |
114 | | -- **Reusable workflows**: Socket-registry provides centralized, reusable workflows for lint/type-check/test/coverage |
115 | | -- **Benefits**: Parallel execution, consistent configuration, cross-platform testing |
116 | | -- **Documentation**: See `docs/CI_TESTING.md` and `socket-registry/docs/CI_TESTING_TOOLS.md` |
| 53 | +### CLI-Specific Patterns |
117 | 54 |
|
118 | | -## 🎨 CLI-SPECIFIC CODE PATTERNS |
| 55 | +#### Command Structure |
| 56 | +🚨 MANDATORY - Each command: `cmd-*.mts`, `handle-*.mts`, `output-*.mts` |
119 | 57 |
|
120 | | -### File Structure |
121 | | -- **File extensions**: `.mts` for TypeScript module files |
122 | | -- **Naming**: kebab-case (e.g., `cmd-scan-create.mts`, `handle-create-new-scan.mts`) |
123 | | -- **Module headers**: 🚨 MANDATORY - All modules MUST have `@fileoverview` headers |
| 58 | +#### File Structure |
| 59 | +- **Extensions**: `.mts` |
| 60 | +- **Naming**: kebab-case (`cmd-scan-create.mts`, `handle-create-new-scan.mts`) |
| 61 | +- **Module headers**: 🚨 MANDATORY `@fileoverview` headers |
124 | 62 |
|
125 | | -### CLI-Specific Patterns |
126 | | -- **Command structure**: 🚨 MANDATORY - Each command MUST have `cmd-*.mts`, `handle-*.mts`, `output-*.mts` |
127 | | -- **Flags**: Use `MeowFlags` type with descriptive help text |
128 | | -- **GitHub API**: Use Octokit instances from `src/utils/github.mts` instead of raw fetch |
129 | | -- **Null-prototype objects**: |
130 | | - - ✅ CORRECT: `{ __proto__: null, key: 'value' }` (object literal with properties) |
131 | | - - ✅ CORRECT: `{ __proto__: null, ...options }` (spread pattern) |
132 | | - - ✅ CORRECT: `const obj = Object.create(null)` (empty object, populate separately) |
133 | | - - ❌ WRONG: `const obj = { __proto__: null }` (empty object literal - use `Object.create(null)` instead) |
134 | | - - **Rationale**: Use `Object.create(null)` only for empty null-prototype objects; object literals with `__proto__: null` are fine when they have properties |
| 63 | +#### CLI Patterns |
| 64 | +- **Flags**: Use `MeowFlags` with descriptive help |
| 65 | +- **GitHub API**: Use Octokit from `src/utils/github.mts`, not raw fetch |
| 66 | +- **Null-prototype**: `{ __proto__: null, key: val }` or `Object.create(null)` for empty |
135 | 67 |
|
136 | | -### Error Handling |
| 68 | +#### Error Handling |
137 | 69 | - **Input validation**: Use `InputError` from `src/utils/errors.mts` |
138 | 70 | - **Authentication**: Use `AuthError` from `src/utils/errors.mts` |
139 | | -- **Result pattern**: Use `CResult<T>` type for functions that can fail |
140 | | -- **Examples**: |
141 | | - - ✅ CORRECT: `throw new InputError('No .socket directory found')` |
142 | | - - ✅ CORRECT: `throw new AuthError('Invalid API token')` |
143 | | - - ❌ WRONG: `logger.error('Error occurred'); return` |
144 | | - |
145 | | -### File Operations (SECURITY CRITICAL) |
146 | | -- **Scripts/Build**: Use `trash` package ONLY in scripts and build files |
147 | | -- **Source code**: In `/src/`, use `fs.rm()` with proper error handling |
148 | | -- **NO rmSync**: 🚨 ABSOLUTELY FORBIDDEN - NEVER use `fs.rmSync()` or `rm -rf` |
149 | | - |
150 | | -## 📦 PACKAGE STRUCTURE |
151 | | - |
152 | | -### Distribution |
153 | | -- **Binary entries**: `socket`, `socket-npm`, `socket-npx` (in `bin/` directory) |
154 | | -- **Distribution**: Built files go to `dist/` directory |
155 | | -- **External dependencies**: Bundled in `dist/external/` directory |
156 | | -- **Test fixtures**: Located in `test/fixtures/` |
157 | | - |
158 | | -### External Dependencies |
159 | | -- Bundles external dependencies in `dist/external/` directory |
160 | | -- Uses Socket registry overrides for security |
161 | | -- Custom patches applied to dependencies in `patches/` |
162 | | - |
163 | | -## 🧪 TESTING |
164 | | - |
165 | | -### Test Organization |
166 | | -- **Modular test files**: Split large test files by functionality |
167 | | -- **Test file naming**: Use descriptive names reflecting module being tested |
168 | | -- **Test directory structure**: 🚨 MANDATORY |
169 | | - ``` |
170 | | - test/ |
171 | | - ├── unit/ # Unit tests |
172 | | - ├── integration/ # Integration tests |
173 | | - ├── fixtures/ # Test fixtures |
174 | | - └── utils/ # Test utilities |
175 | | - ├── environment.mts # Test environment setup |
176 | | - ├── fixtures.mts # Test data configurations |
177 | | - ├── mock-helpers.mts # Mock setup utilities |
178 | | - └── constants.mts # Test constants |
179 | | - ``` |
180 | | - |
181 | | -### Test Utilities Organization |
182 | | -- ✅ CORRECT: `import { setupTestEnvironment } from './utils/environment.mts'` |
183 | | -- ✅ CORRECT: `import { TEST_PACKAGE_CONFIGS } from './utils/fixtures.mts'` |
184 | | -- ❌ OLD PATTERN: `import { setupTestEnvironment } from './test-utils.mts'` |
185 | | - |
186 | | -## 📝 CHANGELOG MANAGEMENT |
187 | | - |
188 | | -### Content Guidelines |
189 | | -Focus on **user-facing changes** only: |
190 | | -- **Added**: New features, commands, flags users can access |
191 | | -- **Changed**: Modifications to existing behavior users will notice |
192 | | -- **Fixed**: Bug fixes that resolve user-reported issues |
193 | | -- **Removed**: Features, flags, commands no longer available |
194 | | - |
195 | | -### Writing Style |
196 | | -Use **marketing voice** emphasizing user benefits while staying **concise**: |
197 | | -- Focus on what users can accomplish vs technical implementation |
198 | | -- Highlight improvements in UX and productivity |
199 | | -- Use active, positive language showcasing value |
200 | | -- Keep entries brief |
201 | | - |
202 | | -### Exclude Internal Changes |
203 | | -- Dependency updates (unless security fixes or user features) |
204 | | -- Code refactoring and cleanup |
205 | | -- Internal constant reorganization |
206 | | -- Test snapshot updates |
207 | | -- Build system improvements |
208 | | -- Developer tooling changes |
209 | | -- GitHub workflow and CI/CD changes |
210 | | -- Third-party integration updates (unless user-visible features) |
211 | | - |
212 | | -### Third-Party Integrations |
213 | | -Socket CLI integrates with: |
214 | | -- **@coana-tech/cli**: Static analysis for reachability |
215 | | -- **cdxgen**: CycloneDX BOM generator |
216 | | -- **synp**: Convert between yarn.lock and package-lock.json |
217 | | - |
218 | | -## 🔧 GIT WORKFLOW |
219 | | - |
220 | | -### Commit Messages |
221 | | -- **🚨 ABSOLUTELY FORBIDDEN**: NEVER add Claude Code attribution to commit messages |
222 | | - - ❌ WRONG: Adding "🤖 Generated with [Claude Code]..." or "Co-Authored-By: Claude" |
223 | | - - ✅ CORRECT: Write commit messages without any AI attribution or signatures |
224 | | - - **Rationale**: This is a professional project and commit messages should not contain AI tool attributions |
225 | | - |
226 | | -### Pre-Commit Quality Checks |
227 | | -- **🚨 MANDATORY**: Always run these commands before committing: |
228 | | - - `pnpm run fix` - Fix linting and formatting issues |
229 | | - - `pnpm run check` - Run all checks (lint, type-check, tests) |
230 | | - - **Rationale**: Ensures code quality regardless of whether hooks run |
231 | | - |
232 | | -### Commit Strategy with --no-verify |
233 | | -- **--no-verify usage**: Use `--no-verify` flag for commits that don't require pre-commit hooks |
234 | | - - ✅ **Safe to skip hooks**: Scripts (scripts/), GitHub Actions workflows (.github/workflows/), tests (test/), documentation (*.md), configuration files |
235 | | - - ❌ **Always run hooks**: Source code (src/), published package code, CLI command implementations |
236 | | - - **Important**: Even when using `--no-verify`, you MUST still run `pnpm run fix` and `pnpm run check` manually first |
237 | | - - **Rationale**: Pre-commit hooks run linting and type-checking which are critical for CLI source code but less critical for non-published files |
238 | | - |
239 | | -### Batch Commits Strategy |
240 | | -- **When making many changes**: Break large changesets into small, logical commits |
241 | | -- **First commit with tests**: Run full test suite (hooks) for the first commit only |
242 | | -- **Subsequent commits with --no-verify**: Use `--no-verify` for follow-up commits |
243 | | -- **Example workflow**: |
244 | | - 1. Make all changes and ensure `pnpm run fix && pnpm run check` passes |
245 | | - 2. Stage and commit core changes with hooks: `git commit -m "message"` |
246 | | - 3. Stage and commit related changes: `git commit --no-verify -m "message"` |
247 | | - 4. Stage and commit cleanup: `git commit --no-verify -m "message"` |
248 | | - 5. Stage and commit docs: `git commit --no-verify -m "message"` |
249 | | -- **Rationale**: Reduces commit time while maintaining code quality through initial validation |
250 | | - |
251 | | -### Git SHA Management (CRITICAL) |
252 | | -- **🚨 NEVER GUESS OR MAKE UP GIT SHAs**: Always retrieve the exact full SHA using `git rev-parse` |
253 | | - - ✅ CORRECT: `cd /path/to/repo && git rev-parse HEAD` or `git rev-parse main` |
254 | | - - ❌ WRONG: Guessing the rest of a SHA after seeing only the short version (e.g., `43a668e1`) |
255 | | - - **Why this matters**: GitHub Actions workflow references require exact, full 40-character SHAs |
256 | | - - **Consequences of wrong SHA**: Workflow failures with "workflow was not found" errors |
257 | | -- **Updating workflow SHA references**: When updating SHA references in workflow files: |
258 | | - 1. Get the exact full SHA: `cd repo && git rev-parse HEAD` |
259 | | - 2. Use the FULL 40-character SHA in sed commands |
260 | | - 3. Verify the SHA exists: `git show <sha> --stat` |
261 | | -- **Rationale**: Using incorrect SHAs breaks CI/CD pipelines and wastes debugging time |
262 | | - |
263 | | -## 🔍 DEBUGGING |
264 | | - |
265 | | -### CI vs Local |
266 | | -- CI uses published packages, not local versions |
267 | | -- Be defensive with @socketsecurity/registry |
268 | | - |
269 | | -### Package Detection |
270 | | -- Use `existsSync()` not `fs.access()` for consistency |
271 | | - |
272 | | -## 📝 SCRATCH DOCUMENTS |
273 | | - |
274 | | -### Working Documents Directory |
275 | | -- **Location**: `.claude/` directory (gitignored) |
276 | | -- **Purpose**: Store scratch documents, planning notes, analysis reports, and temporary documentation |
277 | | -- **🚨 CRITICAL**: NEVER commit files in `.claude/` to version control |
278 | | -- **Examples of scratch documents**: |
279 | | - - Working notes and implementation plans |
280 | | - - Analysis reports from codebase investigations |
281 | | - - Temporary documentation and TODO lists |
282 | | - - Any files not intended for production use |
283 | | - |
284 | | ---- |
285 | | - |
286 | | -**For all other standards not covered here, refer to `socket-registry/CLAUDE.md` (in sibling repository)** |
| 71 | +- **Result pattern**: Use `CResult<T>` for fallible functions |
| 72 | +- Examples: |
| 73 | + - ✅ `throw new InputError('No .socket directory found')` |
| 74 | + - ✅ `throw new AuthError('Invalid API token')` |
| 75 | + - ❌ `logger.error('Error occurred'); return` |
| 76 | + |
| 77 | +### Testing |
| 78 | +- **🚨 NEVER USE `--` before test paths** - runs ALL tests |
| 79 | +- **Build first**: `pnpm run build:dist:src` |
| 80 | +- **Test single file**: ✅ `pnpm run test:unit src/commands/specific/cmd-file.test.mts` |
| 81 | +- **Update snapshots**: ✅ `pnpm run testu src/commands/specific/cmd-file.test.mts` |
| 82 | +- **Structure**: `test/unit/`, `test/integration/`, `test/fixtures/`, `test/utils/` |
| 83 | +- **Utils**: `environment.mts`, `fixtures.mts`, `mock-helpers.mts`, `constants.mts` |
| 84 | + |
| 85 | +### CI Testing |
| 86 | +- **🚨 MANDATORY**: `SocketDev/socket-registry/.github/workflows/ci.yml@<SHA>` with full SHA |
| 87 | +- **Format**: `@662bbcab1b7533e24ba8e3446cffd8a7e5f7617e # main` |
| 88 | +- **Docs**: `docs/CI_TESTING.md`, `socket-registry/docs/CI_TESTING_TOOLS.md` |
| 89 | + |
| 90 | +### Package Structure |
| 91 | +- **Binary entries**: `socket`, `socket-npm`, `socket-npx` (in `bin/`) |
| 92 | +- **Distribution**: `dist/` directory |
| 93 | +- **External deps**: Bundled in `dist/external/` |
| 94 | +- **Test fixtures**: `test/fixtures/` |
| 95 | +- **Custom patches**: `patches/` |
| 96 | + |
| 97 | +### Changelog Management |
| 98 | +**Content**: Focus on user-facing changes only |
| 99 | +- **Include**: New features/commands/flags, behavior changes, bug fixes, removed features |
| 100 | +- **Exclude**: Deps (unless security/features), refactoring, internal constants, test snapshots, build system, dev tooling, CI/CD, third-party integration updates |
| 101 | + |
| 102 | +**Style**: Marketing voice emphasizing user benefits, concise |
| 103 | +- Focus on what users can accomplish |
| 104 | +- Highlight UX/productivity improvements |
| 105 | +- Active, positive language |
| 106 | +- Keep brief |
| 107 | + |
| 108 | +**Third-party integrations**: @coana-tech/cli, cdxgen, synp |
| 109 | + |
| 110 | +### Debugging |
| 111 | +- **CI vs Local**: CI uses published packages, not local |
| 112 | +- **Package detection**: Use `existsSync()` not `fs.access()` |
0 commit comments