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.

381
index.js
View File

@ -1,234 +1,219 @@
/** /**
* *
* @authors yutent (yutent@doui.cc) * @authors yutent (yutent@doui.cc)
* @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() {
this.self = fs
}
constructor(){ /**
this.self = fs * [cat 文件读取]
* @param {String} file [文件路径]
* @param {Function} cb [回调] 可选
*/
cat(file) {
try {
return fs.readFileSync(file)
} catch (err) {
console.error(err)
return null
} }
}
/**
* [ls 读取整个目录(不遍历子目录)]
* @param {string} dir [目标路径]
* @param {boolean} child [是否遍历子目录]
* @return {array} [返回目标目录所有文件名和子目录名, 不包括'.''..']
*/
ls(dir, child) {
try {
let list = fs.readdirSync(dir)
/** if (!child) return list
* [cat 文件读取]
* @param {String} file [文件路径]
* @param {Function} cb [回调] 可选
*/
cat(file){
try{
return fs.readFileSync(file)
}catch(err){
console.error(err)
return null
}
}
/** let tmp = Array.from(list)
* [ls 读取整个目录(不遍历子目录)] tmp.forEach(it => {
* @param {string} dir [目标路径] let childdir = path.join(dir, it)
* @param {boolean} child [是否遍历子目录] if (this.isdir(childdir)) {
* @return {array} [返回目标目录所有文件名和子目录名, 不包括'.''..'] list = Array.prototype.concat.apply(
*/ list,
ls(dir, child){ this.ls(childdir, true).map(sub => {
try{ return path.join(it, sub)
let list = fs.readdirSync(dir)
if(!child)
return list
let tmp = Array.from(list)
tmp.forEach(it => {
let childdir = path.join(dir, it)
if(this.isdir(childdir)){
list = Array.prototype.concat.apply(list, this.ls(childdir, true).map(sub => {
return path.join(it, sub)
}))
}
}) })
return list )
}catch(err){
console.error(err)
return null
} }
})
return list
} catch (err) {
console.error(err)
return null
}
}
/**
* [echo 写文件]
* @param {String|Buffer|Number} data [要写入的数据]
* @param {String} file [要写的文件]
* @param {Boolean} append [是否在后面追加默认否]
* @param {String} encode [编码, 默认utf8]
*/
echo(data, file, append, encode) {
if (!file) {
return data
} }
let updir = path.parse(file).dir,
opt = {}
/** if (!this.isdir(updir)) {
* [echo 写文件] this.mkdir(updir)
* @param {String|Buffer|Number} data [要写入的数据]
* @param {String} file [要写的文件]
* @param {Boolean} append [是否在后面追加默认否]
* @param {String} encode [编码, 默认utf8]
*/
echo(data, file, append, encode){
if(!file){
return data;
}
let updir = path.parse(file).dir,
opt = {};
if(!this.isdir(updir)){
this.mkdir(updir)
}
if(append && typeof append === 'string'){
encode = append;
append = false;
opt.encoding = encode;
}else{
if(typeof encode === 'string'){
opt.encoding = encode
}
}
try{
if(!!append){
fs.appendFileSync(file, data, opt)
}else{
fs.writeFileSync(file, data, opt)
}
}catch(err){
console.error(err)
}
} }
if (append && typeof append === 'string') {
encode = append
//修改权限 append = false
chmod(path, mode){ opt.encoding = encode
try{ } else {
fs.chmodSync(path, mode) if (typeof encode === 'string') {
}catch(err){ opt.encoding = encode
console.error(err) }
}
}
/**
* [mv 移动文件,兼具重命名功能]
* @param {String} from [原路径/原名]
* @param {String} to [目标路径/新名]
*/
mv(from, to){
let updir = path.parse(to).dir
if(!this.isdir(updir))
this.mkdir(updir)
try{
fs.renameSync(from, to)
}catch(e){
let rs = fs.createReadStream(from),
ws = fs.createWriteStream(to);
rs.pipe(ws)
rs.on('end', err => {
this.rm(from)
})
}
} }
try {
if (!!append) {
fs.appendFileSync(file, data, opt)
} else {
fs.writeFileSync(file, data, opt)
}
} catch (err) {
console.error(err)
}
}
//修改权限
chmod(path, mode) {
try {
fs.chmodSync(path, mode)
} catch (err) {
console.error(err)
}
}
cp(from, to){ /**
let updir = path.parse(to).dir * [mv 移动文件,兼具重命名功能]
if(!this.isdir(updir)){ * @param {String} from [原路径/原名]
this.mkdir(updir) * @param {String} to [目标路径/新名]
} */
mv(from, to) {
let updir = path.parse(to).dir
if (!this.isdir(updir)) this.mkdir(updir)
let rs = fs.createReadStream(from), try {
ws = fs.createWriteStream(to); fs.renameSync(from, to)
} catch (e) {
let rs = fs.createReadStream(from),
ws = fs.createWriteStream(to)
rs.pipe(ws) rs.pipe(ws)
rs.on('end', err => console.error(err)) rs.on('end', err => {
this.rm(from)
})
}
}
cp(from, to) {
let updir = path.parse(to).dir
if (!this.isdir(updir)) {
this.mkdir(updir)
} }
let rs = fs.createReadStream(from),
ws = fs.createWriteStream(to)
/** rs.pipe(ws)
* [rm 删除文件/目录] rs.on('end', err => console.error(err))
* @param {[type]} from [源文件/目录路径] }
* @param {[type]} recursion [是否递归删除若删除目录此值须为true]
*/ /**
rm(from, recursion){ * [rm 删除文件/目录]
try{ * @param {[type]} from [源文件/目录路径]
if(!!recursion){ * @param {[type]} recursion [是否递归删除若删除目录此值须为true]
fs.rmdirSync(from) */
}else{ rm(from, recursion) {
fs.unlinkSync(from) try {
} if (!!recursion) {
}catch(err){ let list = this.ls(from)
console.error(err) list.forEach(it => {
it = path.resolve(from, it)
} this.rm(it, this.isdir(it))
})
fs.rmdirSync(from)
} else {
fs.unlinkSync(from)
}
} catch (err) {
console.error(err)
}
}
/**
* [stat 返回文件/目录的状态信息]
* @param {[type]} path [目标路径]
*/
stat(path) {
try {
return fs.statSync(path)
} catch (err) {
return null
}
}
/**
* [isdir 判断目标是否为目录]
* @param {String} path [目标路径]
*/
isdir(path) {
try {
return this.stat(path).isDirectory()
} catch (err) {
return false
}
}
/**
* [mkdir 新建目录]
* @param {String} dir [目标路径]
*/
mkdir(dir) {
let updir = path.parse(dir).dir
if (!updir) return
if (!this.isdir(updir)) {
this.mkdir(updir)
} }
try {
/** fs.mkdirSync(dir)
* [stat 返回文件/目录的状态信息] } catch (err) {
* @param {[type]} path [目标路径] console.error(err)
*/
stat(path){
try{
return fs.statSync(path)
}catch(err){
return null
}
} }
}
/**
/** * [exists 判断目标(文件/目录)是否存在]
* [isdir 判断目标是否为目录] * @param {String} file [目标路径]
* @param {String} path [目标路径] */
*/ exists(file) {
isdir(path){ return fs.existsSync(file)
try{ }
return this.stat(path).isDirectory()
}catch(err){
return false
}
}
/**
* [mkdir 新建目录]
* @param {String} dir [目标路径]
*/
mkdir(dir){
let updir = path.parse(dir).dir
if(!updir)
return
if(!this.isdir(updir)){
this.mkdir(updir)
}
try{
fs.mkdirSync(dir)
}catch(err){
console.error(err)
}
}
/**
* [exists 判断目标(文件/目录)是否存在]
* @param {String} file [目标路径]
*/
exists(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", "author": "yutent",
"fs", "license": "MIT"
"iofs",
"fs.io",
"file"
],
"author": "yutent",
"license": "MIT"
} }