diff --git a/.travis.yml b/.travis.yml index e155464..f98fed0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,3 @@ node_js: - '12' - '10' - '8' - - '6' diff --git a/index.d.ts b/index.d.ts index 11fdc2c..82b01b6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,9 +19,9 @@ declare namespace del { import del = require('del'); (async () => { - const deletedPaths = await del(['tmp/*.js'], {dryRun: true}); + const deletedPaths = await del(['temp/*.js'], {dryRun: true}); - console.log('Files and folders that would be deleted:\n', deletedPaths.join('\n')); + console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n')); })(); ``` */ @@ -38,46 +38,43 @@ declare namespace del { declare const del: { /** - Delete files and folders using glob patterns. + Delete files and directories using glob patterns. - @param patterns - See supported minimatch [patterns](https://github.com/isaacs/minimatch#usage). + @param patterns - See the supported [`minimatch` patterns](https://github.com/isaacs/minimatch#usage). - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) @param options - See the [`glob` options](https://github.com/isaacs/node-glob#options). - @returns A promise for an array of deleted paths. + @returns The deleted paths. @example ``` import del = require('del'); (async () => { - const deletedPaths = await del(['tmp/*.js', '!tmp/unicorn.js']); + const deletedPaths = await del(['temp/*.js', '!temp/unicorn.js']); - console.log('Deleted files and folders:\n', deletedPaths.join('\n')); + console.log('Deleted files and directories:\n', deletedPaths.join('\n')); })(); ``` */ ( - patterns: string | ReadonlyArray, + patterns: string | readonly string[], options?: del.Options ): Promise; /** - Synchronously delete files and folders using glob patterns. + Synchronously delete files and directories using glob patterns. @param patterns - See supported minimatch [patterns](https://github.com/isaacs/minimatch#usage). - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) @param options - See the [`glob` options](https://github.com/isaacs/node-glob#options). - @returns An array of deleted paths. + @returns The deleted paths. */ sync( - patterns: string | ReadonlyArray, + patterns: string | readonly string[], options?: del.Options ): string[]; - - // TODO: Remove this for the next major release - default: typeof del; }; export = del; diff --git a/index.js b/index.js index e953403..dcb45c7 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,13 @@ 'use strict'; +const {promisify} = require('util'); const path = require('path'); const globby = require('globby'); const isPathCwd = require('is-path-cwd'); const isPathInCwd = require('is-path-in-cwd'); -const pify = require('pify'); const rimraf = require('rimraf'); const pMap = require('p-map'); -const rimrafP = pify(rimraf); +const rimrafP = promisify(rimraf); function safeCheck(file) { if (isPathCwd(file)) { @@ -15,45 +15,31 @@ function safeCheck(file) { } if (!isPathInCwd(file)) { - throw new Error('Cannot delete files/folders outside the current working directory. Can be overridden with the `force` option.'); + throw new Error('Cannot delete files/directories outside the current working directory. Can be overridden with the `force` option.'); } } -const del = (patterns, options) => { - options = Object.assign({}, options); +module.exports = async (patterns, {force, dryRun, ...options} = {}) => { + const files = await globby(patterns, options); - const {force, dryRun} = options; - delete options.force; - delete options.dryRun; - - const mapper = file => { + const mapper = async file => { if (!force) { safeCheck(file); } file = path.resolve(options.cwd || '', file); - if (dryRun) { - return file; + if (!dryRun) { + await rimrafP(file, {glob: false}); } - return rimrafP(file, {glob: false}).then(() => file); + return file; }; - return globby(patterns, options).then(files => pMap(files, mapper, options)); + return pMap(files, mapper, options); }; -module.exports = del; -// TODO: Remove this for the next major release -module.exports.default = del; - -module.exports.sync = (patterns, options) => { - options = Object.assign({}, options); - - const {force, dryRun} = options; - delete options.force; - delete options.dryRun; - +module.exports.sync = (patterns, {force, dryRun, ...options} = {}) => { return globby.sync(patterns, options).map(file => { if (!force) { safeCheck(file); diff --git a/index.test-d.ts b/index.test-d.ts index e41d5b7..bff5f02 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,11 +1,13 @@ import {expectType} from 'tsd'; import del = require('.'); -import {sync as delSync} from '.'; -let paths = ['tmp/*.js', '!tmp/unicorn.js']; +const paths = [ + 'temp/*.js', + '!temp/unicorn.js' +]; // Del -expectType>(del('tmp/*.js')); +expectType>(del('temp/*.js')); expectType>(del(paths)); expectType>(del(paths, {force: true})); @@ -14,10 +16,10 @@ expectType>(del(paths, {concurrency: 20})); expectType>(del(paths, {cwd: ''})); // Del (sync) -expectType(delSync('tmp/*.js')); -expectType(delSync(paths)); +expectType(del.sync('tmp/*.js')); +expectType(del.sync(paths)); -expectType(delSync(paths, {force: true})); -expectType(delSync(paths, {dryRun: true})); -expectType(delSync(paths, {concurrency: 20})); -expectType(delSync(paths, {cwd: ''})); +expectType(del.sync(paths, {force: true})); +expectType(del.sync(paths, {dryRun: true})); +expectType(del.sync(paths, {concurrency: 20})); +expectType(del.sync(paths, {cwd: ''})); diff --git a/package.json b/package.json index 1f83f03..b663962 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "del", "version": "4.1.1", - "description": "Delete files and folders", + "description": "Delete files and directories", "license": "MIT", "repository": "sindresorhus/del", "author": { @@ -10,7 +10,7 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=6" + "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" @@ -24,7 +24,6 @@ "files", "folders", "directories", - "del", "remove", "destroy", "trash", @@ -41,7 +40,6 @@ "file", "folder", "directory", - "dir", "fs", "filesystem" ], @@ -51,14 +49,13 @@ "is-path-cwd": "^2.0.0", "is-path-in-cwd": "^2.0.0", "p-map": "^2.0.0", - "pify": "^4.0.1", "rimraf": "^2.6.3" }, "devDependencies": { "ava": "^1.4.1", - "make-dir": "^2.1.0", - "tempy": "^0.2.1", - "tsd": "^0.7.1", + "make-dir": "^3.0.0", + "tempy": "^0.3.0", + "tsd": "^0.7.3", "xo": "^0.24.0" } } diff --git a/readme.md b/readme.md index 1f401ab..494c9bb 100644 --- a/readme.md +++ b/readme.md @@ -1,16 +1,9 @@ # del [![Build Status](https://travis-ci.org/sindresorhus/del.svg?branch=master)](https://travis-ci.org/sindresorhus/del) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo) -> Delete files and folders using [globs](https://github.com/isaacs/minimatch#usage) +> Delete files and directories using [globs](https://github.com/isaacs/minimatch#usage) 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. ---- - -

🐶

-

Support this project and improve your JavaScript skills with this great ES6 course by Wes Bos.
Try his free JavaScript 30 course for a taste of what to expect. You might also like his React and Sublime course.

- ---- - ## Install @@ -25,9 +18,9 @@ $ npm install del const del = require('del'); (async () => { - const deletedPaths = await del(['tmp/*.js', '!tmp/unicorn.js']); + const deletedPaths = await del(['temp/*.js', '!temp/unicorn.js']); - console.log('Deleted files and folders:\n', deletedPaths.join('\n')); + console.log('Deleted files and directories:\n', deletedPaths.join('\n')); })(); ``` @@ -53,26 +46,26 @@ Suggestions on how to improve this welcome! ## API -### del(patterns, [options]) +### del(patterns, options?) -Returns a promise for an array of deleted paths. +Returns `Promise` with the deleted paths. ### del.sync(patterns, [options]) -Returns an array of deleted paths. +Returns `string[]` with the deleted paths. #### patterns -Type: `string` `string[]` +Type: `string | string[]` -See supported minimatch [patterns](https://github.com/isaacs/minimatch#usage). +See the supported [`minimatch` patterns](https://github.com/isaacs/minimatch#usage). - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) #### options -Type: `Object` +Type: `object` See the [`glob` options](https://github.com/isaacs/node-glob#options). @@ -94,9 +87,9 @@ See what would be deleted. const del = require('del'); (async () => { - const deletedPaths = await del(['tmp/*.js'], {dryRun: true}); + const deletedPaths = await del(['temp/*.js'], {dryRun: true}); - console.log('Files and folders that would be deleted:\n', deletedPaths.join('\n')); + console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n')); })(); ``` @@ -118,8 +111,3 @@ See [del-cli](https://github.com/sindresorhus/del-cli) for a CLI for this module - [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 - - -## License - -MIT © [Sindre Sorhus](https://sindresorhus.com)