shutil.js
v0.3.1
Published
A Lightweight File Operating Library
Downloads
15
Maintainers
Readme
shutil.js
A Lightweight File Operating Library
Installation
Using npm:
$ npm i -g shutil.js
$ npm i shutil.js
Note: add --save if you are using npm < 5.0.0
In Node.js:
const shutil = require('shutil.js')
let pwd = "."
for (let [root, dirs, files] of await shutil.walk(pwd)) {
//
}
Why shutil?
This FileSystem
module in Node.JS is pretty simple but we want more.
Try this
const fs = require('fs')
fs.rmdirSync("..") //Woops Error: ENOTEMPTY: directory not empty, rmdir ".."
Or this
const fs = require('fs')
fs.mkdirSync("../a/b/c") //Woops Again Error: ENOENT: no such file or directory, mkdir "./a/b/c"
Or this
const fs = require('fs')
//fs.walk(".") //Woops How to do this?
shutil will make all this things easier.
More important, shutil will do nothing more beyond pure file system operations.
API Documents
shutil.copy: (src, dest[, options]) -> Promise
/**
* Copy a file/directory to another directory/file
* directory -> directory will do things recursively
* @argument
* {src}:string the path,
* {dest}:string the path,
* {options}:object
* {override}:bool, if true, the same file in `dest` path will be overrided.
* default false
* {aswhole}:bool, if true, the `src` directory will seems as a whole part to move.
* default false (means only move all contained files in the `src` path)
* @returns
* {Promise}: nothing fulfilled.
* @short
* same as `shutil.cp`
*/
const shutil = require("shutil.js")
await shutil.copy("./from", "./to", {override: true, aswhole: false}) // `/from` copy to `/to/from`
await shutil.cp("./from", "./to", {override: true, aswhole: true}) // `/from` copy to `/to`
shutil.exists: (src) -> Promise
/**
* Check a dir/file is exists (or accessable)
* @argument
* {src}:string the path to check
* @returns
* {Promise}: fulfilled with true is the `src` path is exists, otherwise false.
*/
const shutil = require("shutil.js")
if (await shutil.exists(".")) {
//exists
}
shutil.isdir: (src) -> Promise
/**
* Check `src` path is exist as a directory
* @argument
* {src}:string the path
* @returns
* {Promise}: fulfilled with true if `src` is a exist directory , otherwise false
*/
const shutil = require("shutil.js")
if (await shutil.isdir("./dir")) {
//exists
}
shutil.isfile: (src) -> Promise
/**
* Check `src` path is exist as a file
* @argument
* {src}:string the path
* @returns
* {Promise}: fulfilled with true if `src` is a exist file , otherwise false
*/
const shutil = require("shutil.js")
if (await shutil.isfile("./file")) {
//exists
}
shutil.listdir: (src) -> Promise
/**
* List all files/dirs contains in `src` path
* @argument
* {src}:string the path to list
* @returns
* {Promise}: fulfilled with a name list; if the `src` is not a exists directory, fulfilled with empty array `[]`
* @short
* exactly same as `shutil.ls`
*/
const shutil = require("shutil.js")
for (let all of await shutil.listdir(".")) {
}
//shot usage
console.log(`show dirs ${await shutil.ls('.')}`)
shutil.mkdirp: (src) -> Promise
/**
* Create a directory
* @argument
* {src}:string the path,
* @returns
* {Promise}: nothing fulfilled
* @short
* same as `shutil.md`
*/
const shutil = require("shutil.js")
await shutil.mkdir("any/valid/path/will/be/created")
await shutil.md("any/valid/path/will/be/created")
shutil.move: (src, dest[, options]) -> Promise
/**
* Move a file/directory to another directory/file
* file -> file means rename
* file -> directory means move
* directory -> directory will do things recursively
* @argument
* {src}:string the path,
* {dest}:string the path,
* {options}:object
* {override}:bool, if true, the same file in `dest` path will be overrided.
* default false
* {aswhole}:bool, if true, the `src` directory will seems as a whole part to move.
* default false (means only move all contained files in the `src` path)
* @returns
* {Promise}: nothing fulfilled.
* @short
* same as `shutil.mv`
*/
const shutil = require("shutil.js")
await shutil.move("./from", "./to", {override: true, aswhole: false}) // `/from` move to `/to/from`
await shutil.mv("./from", "./to", {override: true, aswhole: true}) // `/from` rename to `/to`
shutil.rmtree: (src) -> Promise
/**
* Remove a directory, everything will be removed.
* @argument
* {src}:string the path,
* @returns
* {Promise}: nothing fulfilled
* @short
* same as `shutil.rd`
*/
const shutil = require("shutil.js")
await shutil.rmtree("any")
await shutil.md("any")
shutil.walk: (src) -> Promise
/**
* Travers a directory
* @argument
* {src}:string the path,
* @returns
* {Promise}: fulfilled with a [root, dirs, files] array if `src` path is exists, otherwise fulfilled with empty array `[]`
*/
const shutil = require("shutil.js")
for (let [root, dirs, files] of await shutil.walk(".")){
}
Todos
- filters
- excludes