Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 27, 2019
1 parent 535d775 commit 42e67a8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 80 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ node_js:
- '12'
- '10'
- '8'
- '6'
25 changes: 11 additions & 14 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
})();
```
*/
Expand All @@ -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<string>,
patterns: string | readonly string[],
options?: del.Options
): Promise<string[]>;

/**
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<string>,
patterns: string | readonly string[],
options?: del.Options
): string[];

// TODO: Remove this for the next major release
default: typeof del;
};

export = del;
36 changes: 11 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,45 @@
'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)) {
throw new Error('Cannot delete the current working directory. Can be overridden with the `force` option.');
}

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);
Expand Down
20 changes: 11 additions & 9 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<Promise<string[]>>(del('tmp/*.js'));
expectType<Promise<string[]>>(del('temp/*.js'));
expectType<Promise<string[]>>(del(paths));

expectType<Promise<string[]>>(del(paths, {force: true}));
Expand All @@ -14,10 +16,10 @@ expectType<Promise<string[]>>(del(paths, {concurrency: 20}));
expectType<Promise<string[]>>(del(paths, {cwd: ''}));

// Del (sync)
expectType<string[]>(delSync('tmp/*.js'));
expectType<string[]>(delSync(paths));
expectType<string[]>(del.sync('tmp/*.js'));
expectType<string[]>(del.sync(paths));

expectType<string[]>(delSync(paths, {force: true}));
expectType<string[]>(delSync(paths, {dryRun: true}));
expectType<string[]>(delSync(paths, {concurrency: 20}));
expectType<string[]>(delSync(paths, {cwd: ''}));
expectType<string[]>(del.sync(paths, {force: true}));
expectType<string[]>(del.sync(paths, {dryRun: true}));
expectType<string[]>(del.sync(paths, {concurrency: 20}));
expectType<string[]>(del.sync(paths, {cwd: ''}));
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -24,7 +24,6 @@
"files",
"folders",
"directories",
"del",
"remove",
"destroy",
"trash",
Expand All @@ -41,7 +40,6 @@
"file",
"folder",
"directory",
"dir",
"fs",
"filesystem"
],
Expand All @@ -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"
}
}
34 changes: 11 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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.

---

<p align="center">🐶</p>
<p align="center"><b>Support this project and improve your JavaScript skills with this great <a href="https://ES6.io/friend/AWESOME">ES6 course</a> by Wes Bos.</b><br>Try his free <a href="https://javascript30.com/friend/AWESOME">JavaScript 30 course</a> for a taste of what to expect. You might also like his <a href="https://ReactForBeginners.com/friend/AWESOME">React</a> and <a href="https://SublimeTextBook.com/friend/AWESOME">Sublime</a> course.</p>

---


## Install

Expand All @@ -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'));
})();
```

Expand All @@ -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<string[]>` 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).

Expand All @@ -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'));
})();
```

Expand All @@ -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)

0 comments on commit 42e67a8

Please sign in to comment.