draft: refactor project so it sits on top of a standalone app
This commit is contained in:
+83
@@ -0,0 +1,83 @@
|
||||
import {IOptions as GlobOptions} from 'glob';
|
||||
|
||||
declare namespace del {
|
||||
interface Options extends Readonly<GlobOptions> {
|
||||
/**
|
||||
Allow deleting the current working directory and outside.
|
||||
|
||||
@default false
|
||||
*/
|
||||
readonly force?: boolean;
|
||||
|
||||
/**
|
||||
See what would be deleted.
|
||||
|
||||
@default false
|
||||
|
||||
@example
|
||||
```
|
||||
import del = require('del');
|
||||
|
||||
(async () => {
|
||||
const deletedPaths = await del(['tmp/*.js'], {dryRun: true});
|
||||
|
||||
console.log('Files and folders that would be deleted:\n', deletedPaths.join('\n'));
|
||||
})();
|
||||
```
|
||||
*/
|
||||
readonly dryRun?: boolean;
|
||||
|
||||
/**
|
||||
Concurrency limit. Minimum: `1`.
|
||||
|
||||
@default Infinity
|
||||
*/
|
||||
readonly concurrency?: number;
|
||||
}
|
||||
}
|
||||
|
||||
declare const del: {
|
||||
/**
|
||||
Delete files and folders 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 A promise for an array of deleted paths.
|
||||
|
||||
@example
|
||||
```
|
||||
import del = require('del');
|
||||
|
||||
(async () => {
|
||||
const deletedPaths = await del(['tmp/*.js', '!tmp/unicorn.js']);
|
||||
|
||||
console.log('Deleted files and folders:\n', deletedPaths.join('\n'));
|
||||
})();
|
||||
```
|
||||
*/
|
||||
(
|
||||
patterns: string | ReadonlyArray<string>,
|
||||
options?: del.Options
|
||||
): Promise<string[]>;
|
||||
|
||||
/**
|
||||
Synchronously delete files and folders 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.
|
||||
*/
|
||||
sync(
|
||||
patterns: string | ReadonlyArray<string>,
|
||||
options?: del.Options
|
||||
): string[];
|
||||
|
||||
// TODO: Remove this for the next major release
|
||||
default: typeof del;
|
||||
};
|
||||
|
||||
export = del;
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
'use strict';
|
||||
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);
|
||||
|
||||
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.');
|
||||
}
|
||||
}
|
||||
|
||||
const del = (patterns, options) => {
|
||||
options = Object.assign({}, options);
|
||||
|
||||
const {force, dryRun} = options;
|
||||
delete options.force;
|
||||
delete options.dryRun;
|
||||
|
||||
const mapper = file => {
|
||||
if (!force) {
|
||||
safeCheck(file);
|
||||
}
|
||||
|
||||
file = path.resolve(options.cwd || '', file);
|
||||
|
||||
if (dryRun) {
|
||||
return file;
|
||||
}
|
||||
|
||||
return rimrafP(file, {glob: false}).then(() => file);
|
||||
};
|
||||
|
||||
return globby(patterns, options).then(files => 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;
|
||||
|
||||
return globby.sync(patterns, options).map(file => {
|
||||
if (!force) {
|
||||
safeCheck(file);
|
||||
}
|
||||
|
||||
file = path.resolve(options.cwd || '', file);
|
||||
|
||||
if (!dryRun) {
|
||||
rimraf.sync(file, {glob: false});
|
||||
}
|
||||
|
||||
return file;
|
||||
});
|
||||
};
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
{
|
||||
"_from": "del@^4.1.1",
|
||||
"_id": "del@4.1.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-QwGuEUouP2kVwQenAsOof5Fv8K9t3D8Ca8NxcXKrIpEHjTXK5J2nXLdP+ALI1cgv8wj7KuwBhTwBkOZSJKM5XQ==",
|
||||
"_location": "/del",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "del@^4.1.1",
|
||||
"name": "del",
|
||||
"escapedName": "del",
|
||||
"rawSpec": "^4.1.1",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^4.1.1"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/webpack-dev-server"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/del/-/del-4.1.1.tgz",
|
||||
"_shasum": "9e8f117222ea44a31ff3a156c049b99052a9f0b4",
|
||||
"_spec": "del@^4.1.1",
|
||||
"_where": "O:\\zero\\zero.Web\\node_modules\\webpack-dev-server",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/sindresorhus/del/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"@types/glob": "^7.1.1",
|
||||
"globby": "^6.1.0",
|
||||
"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"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "Delete files and folders",
|
||||
"devDependencies": {
|
||||
"ava": "^1.4.1",
|
||||
"make-dir": "^2.1.0",
|
||||
"tempy": "^0.2.1",
|
||||
"tsd": "^0.7.1",
|
||||
"xo": "^0.24.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"homepage": "https://github.com/sindresorhus/del#readme",
|
||||
"keywords": [
|
||||
"delete",
|
||||
"files",
|
||||
"folders",
|
||||
"directories",
|
||||
"del",
|
||||
"remove",
|
||||
"destroy",
|
||||
"trash",
|
||||
"unlink",
|
||||
"clean",
|
||||
"cleaning",
|
||||
"cleanup",
|
||||
"rm",
|
||||
"rmrf",
|
||||
"rimraf",
|
||||
"rmdir",
|
||||
"glob",
|
||||
"gulpfriendly",
|
||||
"file",
|
||||
"folder",
|
||||
"directory",
|
||||
"dir",
|
||||
"fs",
|
||||
"filesystem"
|
||||
],
|
||||
"license": "MIT",
|
||||
"name": "del",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/sindresorhus/del.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"version": "4.1.1"
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
# del [](https://travis-ci.org/sindresorhus/del) [](https://github.com/xojs/xo)
|
||||
|
||||
> Delete files and folders 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
|
||||
|
||||
```
|
||||
$ npm install del
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const del = require('del');
|
||||
|
||||
(async () => {
|
||||
const deletedPaths = await del(['tmp/*.js', '!tmp/unicorn.js']);
|
||||
|
||||
console.log('Deleted files and folders:\n', deletedPaths.join('\n'));
|
||||
})();
|
||||
```
|
||||
|
||||
|
||||
## 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']);
|
||||
```
|
||||
|
||||
Suggestions on how to improve this welcome!
|
||||
|
||||
|
||||
## API
|
||||
|
||||
### del(patterns, [options])
|
||||
|
||||
Returns a promise for an array of deleted paths.
|
||||
|
||||
### del.sync(patterns, [options])
|
||||
|
||||
Returns an array of deleted paths.
|
||||
|
||||
#### patterns
|
||||
|
||||
Type: `string` `string[]`
|
||||
|
||||
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)
|
||||
|
||||
#### options
|
||||
|
||||
Type: `Object`
|
||||
|
||||
See the [`glob` options](https://github.com/isaacs/node-glob#options).
|
||||
|
||||
##### force
|
||||
|
||||
Type: `boolean`<br>
|
||||
Default: `false`
|
||||
|
||||
Allow deleting the current working directory and outside.
|
||||
|
||||
##### dryRun
|
||||
|
||||
Type: `boolean`<br>
|
||||
Default: `false`
|
||||
|
||||
See what would be deleted.
|
||||
|
||||
```js
|
||||
const del = require('del');
|
||||
|
||||
(async () => {
|
||||
const deletedPaths = await del(['tmp/*.js'], {dryRun: true});
|
||||
|
||||
console.log('Files and folders that would be deleted:\n', deletedPaths.join('\n'));
|
||||
})();
|
||||
```
|
||||
|
||||
##### concurrency
|
||||
|
||||
Type: `number`<br>
|
||||
Default: `Infinity`<br>
|
||||
Minimum: `1`
|
||||
|
||||
Concurrency limit.
|
||||
|
||||
|
||||
## 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.
|
||||
|
||||
|
||||
## 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
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Sindre Sorhus](https://sindresorhus.com)
|
||||
Reference in New Issue
Block a user