New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

cleansole

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cleansole

CLI to remove all console statements from JS/TS files

latest
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

🧹 cleansole

A CLI tool to automatically remove all console.* statements (log, warn, error, etc.) from JavaScript and TypeScript code.

Clean your code before production. No more forgotten console.logs in your deployed apps.

🚀 Features

  • 🔍 Detects and removes all console.* calls using AST (Abstract Syntax Tree)
  • 📁 Works with both single files and entire directories (recursively)
  • ⚡ Fast and reliable — doesn’t touch anything except console statements
  • 🔧 Supports .js and .ts files out of the box
  • 🧼 Keeps formatting and comments intact

📦 Installation

You can install the package locally in your projects depending on your use case.

Installation Commands:

npm install cleansole
npm i cleansole

🧑‍💻 Usage

Syntax:

npx cleansole <path>
ArgumentDescription
<path>Path to a JavaScript/TypeScript file or a directory

Examples:

  • Clean a single file:
npx cleansole test.js
  • Clean multiple files:
npx cleansole test.js test1.ts
  • Clean selective consoles
npx cleansole --warn --error test.js test1.ts
  • Clean all files inside a folder recursively:
npx cleansole ./folder-name
  • Clean a deeply nested file:
npx cleansole ./main-folder/inside-folder/test.js

🔒 Your files will be updated in-place, removing all console calls. Always version your code or back up before batch operations.

🧠 How It Works — The Logic Explained

Instead of using risky regular expressions to remove console.log statements, cleansole uses a safe and robust AST-based approach with Babel.

📝 Step-by-Step Breakdown:

1. Parsing the Source Code

Using @babel/parser, each file is parsed into an Abstract Syntax Tree (AST). This tree represents your code structure in a programmable format.

const ast = parser.parse(code, {
  sourceType: "module",
  plugins: ["jsx", "typescript"]
});

2. Traversing the AST

We walk through the AST using @babel/traverse to find:

console.log(...)
console.warn(...)
console.error(...)

The logic looks for:

  • ExpressionStatements
  • Where the callee is console and a valid method like log, warn, etc.
if (
  t.isExpressionStatement(path.node) &&
  t.isMemberExpression(path.node.expression) &&
  path.node.expression.object.name === 'console'
) {
  path.remove();
}

3. Generating Cleaned Code

After modifying the AST (removing console.* calls), the cleaned code is generated using @babel/generator.

const output = generate(ast, {}, code);

4. Writing to File

The updated source code is written back to the same file using fs.writeFileSync.

📁 Project Structure

cleansole/
├── index.js         # CLI entry point
├── test.js          # Sample file
├── package.json
├── node_modules
└── lib/
    └── clean.js     # Console cleanup logic lives here

📌 Supported Console Methods

The tool removes all of the following:

  • console.log()
  • console.warn()
  • console.error()
  • console.debug()
  • console.info()
  • console.trace()

🧱 Built With

  • Node.js (https://nodejs.org/en)
  • @babel/parser (https://babeljs.io/)
  • @babel/traverse (https://babeljs.io/)
  • @babel/generator (https://babeljs.io/)
  • fs / path modules (https://nodejs.org/en)

🙋‍♂️ Author

Made with ❤️ by [https://github.com/just-bk] — feedback welcome!

Keywords

console

FAQs

Package last updated on 08 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts