Skip to content

Interactive CLI to find and selectively remove console statements from your codebase

Notifications You must be signed in to change notification settings

thebiltheory/console-clean

Repository files navigation

console-clean

Interactive CLI tool to find and selectively remove console statements from your codebase.

Think git add -p but for cleaning up debug logs.

Installation

# Run directly with npx
npx console-clean

# Or install globally
npm install -g console-clean

# Or install as dev dependency
npm install --save-dev console-clean

Usage

Interactive Mode (Default)

Scan your codebase and interactively select which console statements to remove:

console-clean
# or
console-clean ./src

This will:

  1. Scan all JS/TS files
  2. Show each console statement with context
  3. Let you choose: Remove, Keep, Remove all of type, etc.
  4. Preview changes before applying

Scan Mode

List all console statements without removing them:

# Table output
console-clean scan

# JSON output (for CI/tooling)
console-clean scan --json

# Scan specific directory
console-clean scan --path ./src

Clean Mode

Remove all console statements without interaction:

# Remove all (with confirmation)
console-clean clean

# Skip confirmation
console-clean clean --force

# Dry run (show what would be removed)
console-clean clean --dry-run

Configuration

Create a config file in your project root:

.consolecleanrc (JSON)

{
  "include": ["src/**/*", "lib/**/*"],
  "exclude": ["**/*.test.*", "**/*.spec.*"],
  "types": ["log", "debug", "info"],
  "keep": ["error", "warn"],
  "respectGitignore": true,
  "ignoreComments": ["console-clean-ignore", "keep-console"]
}

console-clean.config.js (ESM)

export default {
  include: ['src/**/*'],
  exclude: ['**/*.test.*'],
  types: ['log', 'debug'],
  keep: ['error', 'warn'],
};

package.json

{
  "consoleclean": {
    "types": ["log", "debug"],
    "keep": ["error", "warn"]
  }
}

Ignore Comments

Mark console statements to preserve:

// console-clean-ignore
console.log('This will be skipped');

console.log('This too'); // console-clean-ignore

/* console-clean-ignore */
console.warn('Block comment works too');

Options

Global Flags

  • --path, -p - Directory to scan (default: .)
  • --config, -c - Path to config file

Interactive Mode Flags

  • --no-preview - Skip diff preview before applying

Scan Mode Flags

  • --json - Output as JSON

Clean Mode Flags

  • --force, -f - Skip confirmation prompt
  • --dry-run - Show changes without applying

Examples

# Interactive mode in src directory
console-clean ./src

# Scan and output JSON
console-clean scan --json > console-report.json

# Remove all console.log and console.debug (keep errors/warnings)
console-clean clean

# Dry run to preview changes
console-clean clean --dry-run

# Force remove without confirmation
console-clean clean --force

How It Works

  1. Scanner: Uses fast-glob to find JS/TS files, respects .gitignore
  2. Parser: Uses Babel to parse files and detect console statements
  3. Modifier: Uses magic-string for surgical removal without breaking code
  4. Interactive: Uses inquirer for smooth terminal UI

Features

  • ✓ Detects all console.* methods (log, warn, error, info, debug, etc.)
  • ✓ Interactive selection with context preview
  • ✓ Safe removal preserving code structure
  • ✓ Respects .gitignore patterns
  • ✓ Supports ignore comments
  • ✓ TypeScript and JSX support
  • ✓ Dry run mode
  • ✓ JSON output for CI/tooling

Supported Console Methods

All standard console methods:

  • log, warn, error, info, debug, trace
  • table, dir, group, groupEnd, groupCollapsed
  • time, timeEnd, timeLog
  • assert, count, countReset
  • clear, profile, profileEnd

License

MIT

Author

Nabil Benhamou

Contributing

Issues and PRs welcome at https://github.com/thebiltheory/console-clean

About

Interactive CLI to find and selectively remove console statements from your codebase

Resources

Stars

Watchers

Forks

Packages

No packages published