一个微型(247B)工具类,用于递归删除项目

一个微型(247B)工具类,用于递归删除项目

Node.js 其它杂项

访问GitHub主页

共42Star

详细介绍

premove Build Status

A tiny (247B) utility to remove items recursively

This is a Promise-based utility that recursively removes files and directories.
It's effectively rm -rf for Node.js

Available in these formats:

  • ES Module: dist/premove.mjs
  • CommonJS: dist/premove.js

Important: Requires Node 8.x or later – uses async functions.

Install

$ npm install --save premove

Usage

import { resolve } from 'path';
import premove from 'premove';

// Async/await
try {
  await premove('./foobar');
} catch (err) {
  //
}

// Promise
premove('./foobar').then(val => {
  console.log(val); //=> undefined
}).catch(err => {
  //
});

// Using `cwd` option
const dir = resolve('./foo/bar');
await premove('hello.txt', { cwd: dir });

API

premove(str, opts={})

Returns: Promise<undefined> or false

Returns a Promise that resolves to undefined once complete.
Returns false immediately if the initial filepath (str) does not exist.

str

Type: String

The filepath to remove – may be a file or a directory.
An initial existence check is made for this filepath.

Important: This value is resolved to a full path.
Please be aware of how and from where the Node.js file system is resolving your path!

options.cwd

Type: String
Default: .

The directory to resolve your str from.
Defaults to the process.cwd() – aka, the directory that your command is run within.

Related

  • mk-dirs – Create directories recursively
  • del - Delete files and folders using globs

License

MIT © Luke Edwards