fix `fs.rm` bug

master 1.0.2
宇天 2017-11-20 01:21:33 +08:00
parent 891a6cca41
commit 9ebfac29f2
3 changed files with 198 additions and 215 deletions

View File

@ -1,3 +1,7 @@
1.0.2 / 2017-11-20
* [fixed] Fixed remove the dir not empty call error.
1.0.1 / 2017-04-24 1.0.1 / 2017-04-24
* [add] ls function can recur list child folder with second para set to be true. * [add] ls function can recur list child folder with second para set to be true.

View File

@ -4,18 +4,16 @@
* @date 2015-12-28 14:28:38 * @date 2015-12-28 14:28:38
* *
*/ */
"use strict"; 'use strict'
const fs = require('fs'), const fs = require('fs'),
path = require('path'); path = require('path')
class Iofs { class Iofs {
constructor() { constructor() {
this.self = fs this.self = fs
} }
/** /**
* [cat 文件读取] * [cat 文件读取]
* @param {String} file [文件路径] * @param {String} file [文件路径]
@ -40,16 +38,18 @@ class Iofs {
try { try {
let list = fs.readdirSync(dir) let list = fs.readdirSync(dir)
if(!child) if (!child) return list
return list
let tmp = Array.from(list) let tmp = Array.from(list)
tmp.forEach(it => { tmp.forEach(it => {
let childdir = path.join(dir, it) let childdir = path.join(dir, it)
if (this.isdir(childdir)) { if (this.isdir(childdir)) {
list = Array.prototype.concat.apply(list, this.ls(childdir, true).map(sub => { list = Array.prototype.concat.apply(
list,
this.ls(childdir, true).map(sub => {
return path.join(it, sub) return path.join(it, sub)
})) })
)
} }
}) })
return list return list
@ -59,8 +59,6 @@ class Iofs {
} }
} }
/** /**
* [echo 写文件] * [echo 写文件]
* @param {String|Buffer|Number} data [要写入的数据] * @param {String|Buffer|Number} data [要写入的数据]
@ -70,19 +68,19 @@ class Iofs {
*/ */
echo(data, file, append, encode) { echo(data, file, append, encode) {
if (!file) { if (!file) {
return data; return data
} }
let updir = path.parse(file).dir, let updir = path.parse(file).dir,
opt = {}; opt = {}
if (!this.isdir(updir)) { if (!this.isdir(updir)) {
this.mkdir(updir) this.mkdir(updir)
} }
if (append && typeof append === 'string') { if (append && typeof append === 'string') {
encode = append; encode = append
append = false; append = false
opt.encoding = encode; opt.encoding = encode
} else { } else {
if (typeof encode === 'string') { if (typeof encode === 'string') {
opt.encoding = encode opt.encoding = encode
@ -100,8 +98,6 @@ class Iofs {
} }
} }
//修改权限 //修改权限
chmod(path, mode) { chmod(path, mode) {
try { try {
@ -111,23 +107,20 @@ class Iofs {
} }
} }
/** /**
* [mv 移动文件,兼具重命名功能] * [mv 移动文件,兼具重命名功能]
* @param {String} from [原路径/原名] * @param {String} from [原路径/原名]
* @param {String} to [目标路径/新名] * @param {String} to [目标路径/新名]
*/ */
mv(from, to) { mv(from, to) {
let updir = path.parse(to).dir let updir = path.parse(to).dir
if(!this.isdir(updir)) if (!this.isdir(updir)) this.mkdir(updir)
this.mkdir(updir)
try { try {
fs.renameSync(from, to) fs.renameSync(from, to)
} catch (e) { } catch (e) {
let rs = fs.createReadStream(from), let rs = fs.createReadStream(from),
ws = fs.createWriteStream(to); ws = fs.createWriteStream(to)
rs.pipe(ws) rs.pipe(ws)
rs.on('end', err => { rs.on('end', err => {
@ -136,8 +129,6 @@ class Iofs {
} }
} }
cp(from, to) { cp(from, to) {
let updir = path.parse(to).dir let updir = path.parse(to).dir
if (!this.isdir(updir)) { if (!this.isdir(updir)) {
@ -145,14 +136,12 @@ class Iofs {
} }
let rs = fs.createReadStream(from), let rs = fs.createReadStream(from),
ws = fs.createWriteStream(to); ws = fs.createWriteStream(to)
rs.pipe(ws) rs.pipe(ws)
rs.on('end', err => console.error(err)) rs.on('end', err => console.error(err))
} }
/** /**
* [rm 删除文件/目录] * [rm 删除文件/目录]
* @param {[type]} from [源文件/目录路径] * @param {[type]} from [源文件/目录路径]
@ -161,17 +150,20 @@ class Iofs {
rm(from, recursion) { rm(from, recursion) {
try { try {
if (!!recursion) { if (!!recursion) {
let list = this.ls(from)
list.forEach(it => {
it = path.resolve(from, it)
this.rm(it, this.isdir(it))
})
fs.rmdirSync(from) fs.rmdirSync(from)
} else { } else {
fs.unlinkSync(from) fs.unlinkSync(from)
} }
} catch (err) { } catch (err) {
console.error(err) console.error(err)
} }
} }
/** /**
* [stat 返回文件/目录的状态信息] * [stat 返回文件/目录的状态信息]
* @param {[type]} path [目标路径] * @param {[type]} path [目标路径]
@ -184,7 +176,6 @@ class Iofs {
} }
} }
/** /**
* [isdir 判断目标是否为目录] * [isdir 判断目标是否为目录]
* @param {String} path [目标路径] * @param {String} path [目标路径]
@ -197,15 +188,13 @@ class Iofs {
} }
} }
/** /**
* [mkdir 新建目录] * [mkdir 新建目录]
* @param {String} dir [目标路径] * @param {String} dir [目标路径]
*/ */
mkdir(dir) { mkdir(dir) {
let updir = path.parse(dir).dir let updir = path.parse(dir).dir
if(!updir) if (!updir) return
return
if (!this.isdir(updir)) { if (!this.isdir(updir)) {
this.mkdir(updir) this.mkdir(updir)
@ -225,10 +214,6 @@ class Iofs {
exists(file) { exists(file) {
return fs.existsSync(file) return fs.existsSync(file)
} }
} }
module.exports = new Iofs()
module.exports = new Iofs

View File

@ -1,19 +1,13 @@
{ {
"name": "iofs", "name": "iofs",
"version": "1.0.1", "version": "1.0.2",
"description": "Base on native fs module, for easy using.", "description": "Base on native fs module, for easy using.",
"main": "index.js", "main": "index.js",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/yutent/iofs.git" "url": "https://github.com/yutent/iofs.git"
}, },
"keywords": [ "keywords": ["dojs", "fs", "iofs", "fs.io", "file"],
"dojs",
"fs",
"iofs",
"fs.io",
"file"
],
"author": "yutent", "author": "yutent",
"license": "MIT" "license": "MIT"
} }