clear-path
v1.4.0
Published
A node library for deleting a path if exists using del
Maintainers
Readme
clear-path
A Node.js library for deleting paths if they exist, built on top of del. It provides a simple CLI and programmatic API for cleaning up files and directories using glob patterns.
Table of Contents
Installation
Install clear-path as a development dependency:
npm install clear-path --save-devUsage
CLI
Add scripts to your package.json and run them via npm:
{
"scripts": {
"clean": "clear-path"
},
"clearpath": "dist"
}The clearpath value can be a string or an array of glob pattern strings.
Run the script:
npm run cleanCLI 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
{
"clearpath": [
"dist/*.png",
"dist/*.jpg"
]
}Using .clearpathrc
dist/*.png
dist/*.jpgProgrammatic
Use clear-path in your JavaScript code:
const clearPath = require('clear-path');
clearPath('dist');Options
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
Examples
Basic Usage
Delete a directory:
clear-path distMultiple Routines
Define multiple cleaning routines in package.json:
{
"scripts": {
"clean": "clear-path --routine=all",
"clean:dist": "clear-path --routine=dist"
},
"clearpath": {
"routine": {
"all": [
"dist",
"public"
],
"dist": "dist"
}
}
}Or in .clearpathrc:
routine:
all:
- dist
- public
dist: distProgrammatic with Callback
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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
