wishthis/node_modules/del/readme.md

152 lines
3.9 KiB
Markdown
Raw Normal View History

2023-08-17 09:47:40 +00:00
# del
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
> Delete files and directories using [globs](https://github.com/sindresorhus/globby#globbing-patterns)
2022-01-21 08:28:41 +00:00
Similar to [rimraf](https://github.com/isaacs/rimraf), but with a Promise API and support for multiple files and globbing. It also protects you against deleting the current working directory and above.
## Install
```
2023-08-17 09:47:40 +00:00
$ npm install del
2022-01-21 08:28:41 +00:00
```
## Usage
```js
const del = require('del');
2023-08-17 09:47:40 +00:00
(async () => {
const deletedFilePaths = await del(['temp/*.js', '!temp/unicorn.js']);
const deletedDirectoryPaths = await del(['temp', 'public']);
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
console.log('Deleted files:\n', deletedFilePaths.join('\n'));
console.log('\n\n');
console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n'));
})();
```
2022-01-21 08:28:41 +00:00
## Beware
The glob pattern `**` matches all children and *the parent*.
So this won't work:
```js
del.sync(['public/assets/**', '!public/assets/goat.png']);
```
You have to explicitly ignore the parent directories too:
```js
del.sync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
```
2023-08-17 09:47:40 +00:00
To delete all subdirectories inside `public/`, you can do:
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
```js
del.sync(['public/*/']);
```
Suggestions on how to improve this welcome!
2022-01-21 08:28:41 +00:00
## API
2023-08-17 09:47:40 +00:00
Note that glob patterns can only contain forward-slashes, not backward-slashes. Windows file paths can use backward-slashes as long as the path does not contain any glob-like characters, otherwise use `path.posix.join()` instead of `path.join()`.
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
### del(patterns, options?)
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
Returns `Promise<string[]>` with the deleted paths.
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
### del.sync(patterns, options?)
Returns `string[]` with the deleted paths.
2022-01-21 08:28:41 +00:00
#### patterns
2023-08-17 09:47:40 +00:00
Type: `string | string[]`
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
- [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
2022-01-21 08:28:41 +00:00
- [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
#### options
2023-08-17 09:47:40 +00:00
Type: `object`
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
You can specify any of the [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the below options. In contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default.
2022-01-21 08:28:41 +00:00
##### force
2023-08-17 09:47:40 +00:00
Type: `boolean`\
2022-01-21 08:28:41 +00:00
Default: `false`
Allow deleting the current working directory and outside.
##### dryRun
2023-08-17 09:47:40 +00:00
Type: `boolean`\
2022-01-21 08:28:41 +00:00
Default: `false`
See what would be deleted.
```js
const del = require('del');
2023-08-17 09:47:40 +00:00
(async () => {
const deletedPaths = await del(['temp/*.js'], {dryRun: true});
console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
})();
2022-01-21 08:28:41 +00:00
```
##### concurrency
2023-08-17 09:47:40 +00:00
Type: `number`\
Default: `Infinity`\
2022-01-21 08:28:41 +00:00
Minimum: `1`
Concurrency limit.
2023-08-17 09:47:40 +00:00
##### onProgress
Type: `(progress: ProgressData) => void`
Called after each file or directory is deleted.
```js
import del from 'del';
await del(patterns, {
onProgress: progress => {
// …
}});
```
###### ProgressData
```js
{
totalCount: number,
deletedCount: number,
percent: number
}
```
- `percent` is a value between `0` and `1`
2022-01-21 08:28:41 +00:00
## CLI
See [del-cli](https://github.com/sindresorhus/del-cli) for a CLI for this module and [trash-cli](https://github.com/sindresorhus/trash-cli) for a safe version that is suitable for running by hand.
2023-08-17 09:47:40 +00:00
## del for enterprise
Available as part of the Tidelift Subscription.
The maintainers of del and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-del?utm_source=npm-del&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
2022-01-21 08:28:41 +00:00
## Related
- [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed
- [globby](https://github.com/sindresorhus/globby) - User-friendly glob matching