Skip to content

Codemap & Cleanup Commands

Command reference for architecture documentation and code cleanup.

Overview

CommandPurposeMain Tools
/jikime:codemapAST analysis-based architecture documentationts-morph, madge
/jikime:cleanupDead code detection and safe removalknip, depcheck, ts-prune

/jikime:codemap

AST Analysis-Based Architecture Mapping

ItemDescription
DescriptionAutomatically generates architecture documentation from codebase
TypeUtility (Type B)
Contextsync.md
Skilljikime-workflow-codemap
Standalone Use✅ High - Can be run independently anytime

Usage

bash
# Generate full architecture map
/jikime:codemap all

# Generate specific areas only
/jikime:codemap frontend
/jikime:codemap backend
/jikime:codemap database
/jikime:codemap integrations

# Include AST analysis (TypeScript/JavaScript)
/jikime:codemap all --ast

# Generate dependency graph
/jikime:codemap all --deps

# Force regeneration
/jikime:codemap all --refresh

# JSON output (for automation)
/jikime:codemap all --json

Options

OptionDescription
allGenerate codemap for all areas
frontendFrontend architecture only
backendBackend/API architecture only
databaseDatabase schema/models
integrationsExternal service integrations
--astEnable AST analysis with ts-morph
--depsGenerate dependency graph with madge
--refreshForce regeneration ignoring cache
--jsonJSON output for automation

Analysis Tools

1. ts-morph (AST Analysis)

Structural analysis for TypeScript/JavaScript projects:

typescript
// Extracted information
- All exported functions, classes, types
- import/export relationships
- Module dependencies
- Route definitions (Next.js, Express, etc.)

2. madge (Dependency Graph)

bash
# Generate SVG graph
npx madge --image docs/CODEMAPS/assets/dependency-graph.svg src/

# Detect circular dependencies
npx madge --circular src/

Framework Detection

Detection FileFrameworkCodemap Focus
next.config.*Next.jsApp Router, API Routes, Pages
vite.config.*ViteComponents, Modules
angular.jsonAngularModules, Services, Components
nuxt.config.*NuxtPages, Plugins, Modules
package.json + expressExpressRoutes, Middleware
go.modGoPackages, Handlers
Cargo.tomlRustCrates, Modules
pyproject.tomlPythonPackages, Modules

Output Structure

docs/
├── CODEMAPS/
│   ├── INDEX.md              # Architecture overview
│   ├── frontend.md           # Frontend structure
│   ├── backend.md            # Backend/API structure
│   ├── database.md           # Database schema
│   ├── integrations.md       # External services
│   └── assets/
│       ├── dependency-graph.svg
│       └── architecture-diagram.svg

Codemap File Format

markdown
# [Area] Codemap

**Last Updated:** YYYY-MM-DD
**Version:** X.Y.Z
**Entry Points:** [List of main entry points]

## Overview
[Brief description of the area]

## Architecture
[ASCII diagram showing component relationships]

## Key Modules

| Module | Purpose | Exports | Dependencies |
|--------|---------|---------|--------------||
| ... | ... | ... | ... |

## Data Flow
[Description of data flow through this area]

## External Dependencies
- package@version - purpose
- ...

## Related Codemaps
- [Related Area](./related.md)

Process

Phase 1: Discovery

  Detect framework, language, project type

  Identify entry points and core files

Phase 2: Analysis

  AST parsing (ts-morph for TS/JS)

  Dependency graph (madge)

  Pattern recognition (MVC, Clean, etc.)

Phase 3: Generation

  Generate structured codemap

  Create ASCII diagrams

  Build relationship tables

Phase 4: Validation

  Verify path existence

  Validate link targets

  Report coverage statistics

/jikime:cleanup

Dead Code Detection and Safe Removal

ItemDescription
DescriptionComprehensive dead code analysis with DELETION_LOG tracking
TypeUtility (Type B)
Contextdev.md
Agentrefactorer
Standalone Use✅ High - Can be run independently anytime

Usage

bash
# Dead code scan (analysis only, no changes)
/jikime:cleanup scan

# Remove SAFE items only (low risk)
/jikime:cleanup remove --safe

# Include CAREFUL items (medium risk, confirmation required)
/jikime:cleanup remove --careful

# Target specific categories
/jikime:cleanup remove --deps      # Unused npm dependencies
/jikime:cleanup remove --exports   # Unused exports
/jikime:cleanup remove --files     # Unused files

# Dry run (shows what would be removed)
/jikime:cleanup scan --dry-run

# Check deletion history
/jikime:cleanup log

# Generate comprehensive cleanup report
/jikime:cleanup report

Options

OptionDescription
scanAnalyze codebase for dead code (no changes)
removeRemove detected dead code
reportGenerate comprehensive cleanup report
logCheck DELETION_LOG.md history
--safeRemove low-risk items only
--carefulInclude medium-risk items (verification required)
--depsTarget unused dependencies
--exportsTarget unused exports
--filesTarget unused files
--dry-runShow what would be removed

Analysis Tools

1. knip - Comprehensive Dead Code Detection

bash
# Installation
npm install -D knip

# Full analysis
npx knip

# JSON report
npx knip --reporter json > .jikime/cleanup/knip-report.json

Detection Items:

  • Unused files
  • Unused exports
  • Unused dependencies
  • Unused devDependencies
  • Unused types

2. depcheck - Dependency Analysis

bash
# Installation
npm install -D depcheck

# Analysis
npx depcheck

# JSON report
npx depcheck --json > .jikime/cleanup/depcheck-report.json

Detection Items:

  • Unused dependencies
  • Missing dependencies
  • Phantom dependencies

3. ts-prune - TypeScript Export Analysis

bash
# Installation
npm install -D ts-prune

# Analysis
npx ts-prune

# Filtering
npx ts-prune | grep -v "used in module"

Detection Items:

  • Unused exports
  • Unused types
  • Dead code paths

4. ESLint - Unused Directives

bash
# Check unused eslint-disable comments
npx eslint . --report-unused-disable-directives

Risk Classification System

SAFE (Automatically Removable)

CategoryRiskDetection MethodVerification
Unused npm depsLowdepcheckNo imports confirmed
Unused devDepsLowdepcheckNot used in scripts
Commented codeLowRegex patternsVisual confirmation
Unused importsLowESLint + knipNo references
Unused eslint-disableLowESLint reportDirective check

CAREFUL (Confirmation Required)

CategoryRiskDetection MethodVerification
Unused exportsMediumts-prune + knipGrep + git history
Unused filesMediumknipCheck dynamic imports
Unused typesMediumts-pruneCheck type inference
Dead branchesMediumCoverage reportRuntime tests

RISKY (Manual Review Required)

CategoryRiskDetection MethodVerification
Public APIHighAPI testsIntegration tests
Shared utilitiesHighCross-project searchStakeholder review
Dynamic importsHighString pattern searchRuntime tests
Reflection codeHighPattern analysisFull test suite

DDD-Aligned Workflow

Phase 1: ANALYZE
    └─ Run all detection tools in parallel
    └─ Aggregate results with risk classification
    └─ Check test coverage for affected code
    └─ Review git history for context

Phase 2: PRESERVE
    └─ Ensure characterization tests exist for affected code
    └─ Create backup branch: cleanup/YYYY-MM-DD-HHMM
    └─ Document current behavior if no tests exist

Phase 3: IMPROVE
    └─ Remove by category (safest first):
        a. Unused npm dependencies
        b. Unused devDependencies
        c. Unused imports
        d. Unused exports
        e. Unused files
    └─ After each category:
        - Run build
        - Run full test suite
        - Commit if passing
        - Update DELETION_LOG.md

DELETION_LOG.md Format

All deletions are tracked in docs/DELETION_LOG.md:

markdown
# Code Deletion Log

Audit trail for code cleanup operations.

---

## [YYYY-MM-DD HH:MM] Cleanup Session

**Operator**: J.A.R.V.I.S. / refactorer agent
**Branch**: cleanup/YYYY-MM-DD-HHMM
**Commit**: abc123def
**Tools**: knip v5.x, depcheck v1.x, ts-prune v0.x

### Summary

| Category | Items | Lines | Size Impact |
|----------|-------|-------|-------------|
| Dependencies | 5 | - | -120 KB |
| DevDependencies | 3 | - | -45 KB |
| Files | 12 | 1,450 | -45 KB |
| Exports | 23 | 89 | - |
| Imports | 45 | 45 | - |
| **Total** | **88** | **1,584** | **-210 KB** |

### Dependencies Removed

| Package | Version | Reason | Alternative |
|---------|---------|--------|-------------|
| lodash | 4.17.21 | Not imported | Use native methods |
| moment | 2.29.4 | Deprecated | date-fns already used |

### Files Deleted

| Path | Lines | Last Modified | Replaced By |
|------|-------|---------------|-------------|
| src/utils/old-helpers.ts | 120 | 2023-08-15 | N/A (unused) |
| src/components/LegacyButton.tsx | 85 | 2023-09-01 | Button.tsx |

### Verification Results

- [x] TypeScript compiles: `npx tsc --noEmit`
- [x] Build succeeds: `npm run build`
- [x] Tests pass: 47/47 (100%)
- [x] No lint errors: `npm run lint`
- [x] Bundle size verified

### Recovery Instructions

```bash
# If issues occur after this cleanup:
git log --oneline | head -5  # Find cleanup commit
git revert <commit-sha>       # Revert specific commit
npm install                   # Reinstall dependencies
npm run build && npm test     # Verify recovery

### Protected Items

Manage items that should not be removed in `.jikime/cleanup/protected.yaml`:

```yaml
# Items that should not be removed
protected:
  dependencies:
    - "@types/*"  # Type definitions
    - "eslint-*"  # Linting infrastructure

  files:
    - "src/polyfills/*"  # Browser compatibility
    - "src/lib/dynamic-*"  # Dynamic import targets

  exports:
    - "src/api/public.ts:*"  # Public API
    - "src/sdk/index.ts:*"   # SDK exports

  patterns:
    - "**/index.ts"  # Barrel files (may appear unused)
    - "**/__tests__/*"  # Test utilities

Safety Checklist

Pre-removal Verification:

  • [ ] All detection tools run
  • [ ] Risk classification complete
  • [ ] Backup branch created
  • [ ] Characterization tests exist (or created)
  • [ ] Git history reviewed for context
  • [ ] Dynamic import patterns checked
  • [ ] Public API impact assessed

Post-removal Verification:

  • [ ] TypeScript compiles without errors
  • [ ] Build succeeds
  • [ ] All tests pass
  • [ ] No console errors
  • [ ] Bundle size measured
  • [ ] DELETION_LOG.md updated
  • [ ] Commit message detailed

TRUST 5 Integration

PrincipleCodemapCleanup
TestedVerify generated document pathsRun tests after each removal
ReadableClear structure with ASCII diagramsRemove noise, improve signal/noise ratio
UnifiedConsistent document formatConsolidate duplicates
SecuredPrevent sensitive information exposureRemove unused deps with vulnerabilities
TrackableTimestamp and version controlDELETION_LOG.md audit trail

J.A.R.V.I.S. / F.R.I.D.A.Y. Output Format

J.A.R.V.I.S. (Development)

markdown
## J.A.R.V.I.S.: Codemap Generation Complete

### Generated Files
| File | Lines | Modules Documented |
|------|-------|-------------------|
| docs/CODEMAPS/INDEX.md | 120 | 5 entry points |
| docs/CODEMAPS/frontend.md | 85 | 12 components |
| docs/CODEMAPS/backend.md | 95 | 8 endpoints |

### Coverage
- Files analyzed: 47
- Modules documented: 25
- Dependencies mapped: 32
- Circular dependencies: 0

### Predictive Suggestions
- Consider documenting workers/ directory
- API rate limiting not documented
markdown
## J.A.R.V.I.S.: Cleanup Scan Complete

### Dead Code Summary
| Category | Found | Risk | Action |
|----------|-------|------|--------|
| Dependencies | 5 | SAFE | Auto-remove |
| Exports | 23 | CAREFUL | Review |
| Files | 12 | CAREFUL | Review |
| Dynamic refs | 2 | RISKY | Skip |

### Recommended Actions

**Immediate (SAFE)**:
1. Remove 5 unused dependencies (-120 KB)
2. Remove 15 unused imports

**Review Required (CAREFUL)**:
1. 12 files appear unused but check git history
2. 23 exports not directly referenced

### Estimated Impact
- Bundle size: -165 KB (~8% reduction)
- Lines of code: -1,539
- Files: -12

Proceed with --safe removal? Use: /jikime:cleanup remove --safe

F.R.I.D.A.Y. (Migration)

markdown
## F.R.I.D.A.Y.: Migration Cleanup

### Legacy Code Status
| Module | Dead Code | Migrated | Clean |
|--------|-----------|----------|-------|
| Auth | 5 items | Yes | No |
| Users | 0 items | Yes | Yes |
| Products | 12 items | Yes | No |

### Migration-Safe Removal
Only removing code that has been:
- Fully migrated to target framework
- Verified by characterization tests
- Not referenced in migration artifacts

Command Comparison

SituationRecommended Command
Need architecture documentation/jikime:codemap all
Visualize dependency graph/jikime:codemap all --deps
Assess dead code status/jikime:cleanup scan
Safe cleanup operation/jikime:cleanup remove --safe
Check cleanup history/jikime:cleanup log
Cleanup before refactoring/jikime:cleanup scan/jikime:refactor

  • /jikime:docs - Documentation update and synchronization
  • /jikime:refactor - DDD-based code refactoring
  • /jikime:3-sync - SPEC completion and documentation sync
  • /jikime:learn - Codebase exploration and learning

  • jikime-workflow-codemap - Codemap generation workflow
  • jikime-workflow-ddd - DDD methodology (ANALYZE-PRESERVE-IMPROVE)
  • jikime-foundation-quality - TRUST 5 quality framework

Version: 1.0.0 Last Updated: 2026-01-25 Integration: AST analysis (ts-morph), Dependency graphs (madge), Dead code detection (knip, depcheck, ts-prune)

Released under the MIT License.