优化ls方法;增加错误信息输出;this.self改名为this.origin

master 1.1.0
宇天 2018-05-24 23:36:51 +08:00
parent 81e0439161
commit 0c79c47b19
3 changed files with 85 additions and 62 deletions

View File

@ -1,3 +1,11 @@
# 1.1.0 / 2018-05-24
* [add] add error log output
* optimize `ls` method
* rename `this.self` to `this.origin`
# 1.0.3 / 2017-12-23 # 1.0.3 / 2017-12-23
* [del] delete the log output * [del] delete the log output

135
index.js
View File

@ -6,12 +6,12 @@
*/ */
'use strict' 'use strict'
const fs = require('fs') const FS = require('fs')
const path = require('path') const PATH = require('path')
class Iofs { class Iofs {
constructor() { constructor() {
this.self = fs this.origin = FS
} }
/** /**
@ -21,9 +21,11 @@ class Iofs {
*/ */
cat(file) { cat(file) {
try { try {
return fs.readFileSync(file) return FS.readFileSync(file)
} catch (err) { } catch (err) {
console.error(err) if (err) {
console.error(err + '')
}
return null return null
} }
} }
@ -36,25 +38,27 @@ class Iofs {
*/ */
ls(dir, child) { ls(dir, child) {
try { try {
let list = fs.readdirSync(dir) let list = FS.readdirSync(dir)
if (!child) return list list = list.map(it => {
return PATH.resolve(dir, it)
})
let tmp = Array.from(list) if (!child) {
return list
}
let tmp = list.concat()
tmp.forEach(it => { tmp.forEach(it => {
let childdir = path.join(dir, it) if (this.isdir(it)) {
if (this.isdir(childdir)) { list = list.concat(this.ls(it, true))
list = Array.prototype.concat.apply(
list,
this.ls(childdir, true).map(sub => {
return path.join(it, sub)
})
)
} }
}) })
return list return list
} catch (err) { } catch (err) {
console.error(err) if (err) {
console.error(err + '')
}
return null return null
} }
} }
@ -71,8 +75,8 @@ class Iofs {
return data return data
} }
let updir = path.parse(file).dir, let updir = PATH.parse(file).dir
opt = {} let opt = {}
if (!this.isdir(updir)) { if (!this.isdir(updir)) {
this.mkdir(updir) this.mkdir(updir)
} }
@ -89,77 +93,84 @@ class Iofs {
try { try {
if (!!append) { if (!!append) {
fs.appendFileSync(file, data, opt) FS.appendFileSync(file, data, opt)
} else { } else {
fs.writeFileSync(file, data, opt) FS.writeFileSync(file, data, opt)
} }
} catch (err) { } catch (err) {
console.error(err) if (err) {
console.error(err + '')
}
} }
} }
//修改权限 //修改权限
chmod(path, mode) { chmod(path, mode) {
try { try {
fs.chmodSync(path, mode) FS.chmodSync(path, mode)
} catch (err) { } catch (err) {
console.error(err) if (err) {
console.error(err + '')
}
} }
} }
/** /**
* [mv 移动文件,兼具重命名功能] * [mv 移动文件,兼具重命名功能]
* @param {String} from [原路径/原名] * @param {String} origin [原路径/原名]
* @param {String} to [目标路径/新名] * @param {String} target [目标路径/新名]
*/ */
mv(from, to) { mv(origin, target) {
let updir = path.parse(to).dir let updir = PATH.parse(target).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)
})
}
}
cp(from, to) {
let updir = path.parse(to).dir
if (!this.isdir(updir)) { if (!this.isdir(updir)) {
this.mkdir(updir) this.mkdir(updir)
} }
let rs = fs.createReadStream(from), try {
ws = fs.createWriteStream(to) FS.renameSync(origin, target)
} catch (e) {
let rs = FS.createReadStream(origin)
let ws = FS.createWriteStream(target)
rs.pipe(ws)
rs.on('end', err => {
this.rm(origin)
})
}
}
cp(origin, target) {
let updir = PATH.parse(target).dir
if (!this.isdir(updir)) {
this.mkdir(updir)
}
let rs = FS.createReadStream(origin)
let ws = FS.createWriteStream(target)
rs.pipe(ws) rs.pipe(ws)
} }
/** /**
* [rm 删除文件/目录] * [rm 删除文件/目录]
* @param {[type]} from [源文件/目录路径] * @param {[type]} origin [源文件/目录路径]
* @param {[type]} recursion [是否递归删除若删除目录此值须为true] * @param {[type]} recursion [是否递归删除若删除目录此值须为true]
*/ */
rm(from, recursion) { rm(origin, recursion) {
try { try {
if (!!recursion) { if (!!recursion) {
let list = this.ls(from) let list = this.ls(origin)
list.forEach(it => { list.forEach(it => {
it = path.resolve(from, it)
this.rm(it, this.isdir(it)) this.rm(it, this.isdir(it))
}) })
fs.rmdirSync(from) FS.rmdirSync(origin)
} else { } else {
fs.unlinkSync(from) FS.unlinkSync(origin)
} }
} catch (err) { } catch (err) {
console.error(err) if (err) {
console.error(err + '')
}
} }
} }
@ -169,7 +180,7 @@ class Iofs {
*/ */
stat(path) { stat(path) {
try { try {
return fs.statSync(path) return FS.statSync(path)
} catch (err) { } catch (err) {
return null return null
} }
@ -192,17 +203,21 @@ class Iofs {
* @param {String} dir [目标路径] * @param {String} dir [目标路径]
*/ */
mkdir(dir) { mkdir(dir) {
let updir = path.parse(dir).dir let updir = PATH.parse(dir).dir
if (!updir) return if (!updir) {
return
}
if (!this.isdir(updir)) { if (!this.isdir(updir)) {
this.mkdir(updir) this.mkdir(updir)
} }
try { try {
fs.mkdirSync(dir) FS.mkdirSync(dir)
} catch (err) { } catch (err) {
console.error(err) if (err) {
console.error(err + '')
}
} }
} }
@ -211,7 +226,7 @@ class Iofs {
* @param {String} file [目标路径] * @param {String} file [目标路径]
*/ */
exists(file) { exists(file) {
return fs.existsSync(file) return FS.existsSync(file)
} }
} }

View File

@ -1,13 +1,13 @@
{ {
"name": "iofs", "name": "iofs",
"version": "1.0.3", "version": "1.1.0",
"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": ["dojs", "fs", "iofs", "fs.io", "file"], "keywords": ["fivejs", "fs", "iofs", "fs.io", "file"],
"author": "yutent", "author": "yutent",
"license": "MIT" "license": "MIT"
} }