# clear-path

[![npm version](https://badge.fury.io/js/clear-path.svg)](https://badge.fury.io/js/clear-path)
[![License](https://img.shields.io/npm/l/clear-path)](https://github.com/your-repo/clear-path/blob/main/LICENSE)

A Node.js library for deleting paths if they exist, built on top of [del](https://www.npmjs.com/package/del). It provides a simple CLI and programmatic API for cleaning up files and directories using glob patterns.

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
  - [CLI](#cli)
  - [Configuration](#configuration)
  - [Programmatic](#programmatic)
- [API](#api)
- [Examples](#examples)
- [Contributing](#contributing)
- [License](#license)

## Installation

Install `clear-path` as a development dependency:

```bash
npm install clear-path --save-dev
```

## Usage

### CLI

Add scripts to your `package.json` and run them via npm:

```json
{
  "scripts": {
    "clean": "clear-path"
  },
  "clearpath": "dist"
}
```

The `clearpath` value can be a string or an array of glob pattern strings.

Run the script:

```bash
npm run clean
```

#### CLI Options

- `--silent`: Suppress console output of deleted files.
- `--routine:{name}`: Run a specific routine defined in configuration.

### Configuration

Configure paths to delete either in `package.json` or a `.clearpathrc` file.

#### Using `package.json`

```json
{
  "clearpath": [
    "dist/*.png",
    "dist/*.jpg"
  ]
}
```

#### Using `.clearpathrc`

```yaml
dist/*.png
dist/*.jpg
```

### Programmatic

Use `clear-path` in your JavaScript code:

```js
const clearPath = require('clear-path');

clearPath('dist');
```

#### Options

```js
clearPath('dist', {
  silent: true,
  callback: (files) => {
    console.log('Deleted these files:', files);
  }
});
```

##### `options.silent`

- **Type:** `boolean`
- **Default:** `false`
- **Description:** If set to `true`, suppresses console logging of deleted files.

You can activate this from CLI using `clear-path --silent` or `clear-path --routine:{routine_name} --silent`.

##### `options.callback`

- **Type:** `function`
- **Description:** A function that executes after deleting files, receiving an array of deleted file paths.

## API

### `clearPath(paths, options?)`

Deletes the specified paths using glob patterns.

- **Parameters:**
  - `paths` (string | string[]): Path(s) or glob pattern(s) to delete.
  - `options` (object, optional): Configuration options.
    - `silent` (boolean): Suppress output.
    - `callback` (function): Callback after deletion.

- **Returns:** Promise<void>

## Examples

### Basic Usage

Delete a directory:

```bash
clear-path dist
```

### Multiple Routines

Define multiple cleaning routines in `package.json`:

```json
{
  "scripts": {
    "clean": "clear-path --routine=all",
    "clean:dist": "clear-path --routine=dist"
  },
  "clearpath": {
    "routine": {
      "all": [
        "dist",
        "public"
      ],
      "dist": "dist"
    }
  }
}
```

Or in `.clearpathrc`:

```yaml
routine:
  all:
    - dist
    - public
  dist: dist
```

### Programmatic with Callback

```js
const clearPath = require('clear-path');

clearPath(['dist/*.log', 'temp/'], {
  silent: false,
  callback: (files) => {
    console.log(`Cleaned up ${files.length} files.`);
  }
});
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
