master 1.3.2
宇天 2020-01-18 18:53:36 +08:00
parent ec54db65d8
commit f43d707188
4 changed files with 31 additions and 26 deletions

View File

@ -1,3 +1,7 @@
# 1.3.2 / 2020-01-18
* [U] 增加静默参数, 可以不打印错误日志(用于某些特殊场景)
# 1.3.1 / 2020-01-09 # 1.3.1 / 2020-01-09
* [U] 优化异常输出 * [U] 优化异常输出

View File

@ -33,6 +33,7 @@
## APIs ## APIs
> 所有API均支持在最后传入一个 `silently<Boolean>`参数(v1.3.2新增), 用于静默执行, 不打印错误日志
### .cat(file) ### .cat(file)
> 读取文件, 返回一个`Buffer对象` > 读取文件, 返回一个`Buffer对象`

View File

@ -22,11 +22,11 @@ const Iofs = {
* @param {String} file [文件路径] * @param {String} file [文件路径]
* @param {Function} cb [回调] 可选 * @param {Function} cb [回调] 可选
*/ */
cat(file) { cat(file, silently) {
try { try {
return FS.readFileSync(file) return FS.readFileSync(file)
} catch (err) { } catch (err) {
console.error(err + '') !silently && console.error('call cat(): ', err + '')
return null return null
} }
}, },
@ -37,7 +37,7 @@ const Iofs = {
* @param {boolean} recursive [是否递归遍历子目录] * @param {boolean} recursive [是否递归遍历子目录]
* @return {array} [返回目标目录所有文件名和子目录名, 不包括'.''..'] * @return {array} [返回目标目录所有文件名和子目录名, 不包括'.''..']
*/ */
ls(dir, recursive) { ls(dir, recursive, silently) {
try { try {
var list = FS.readdirSync(dir) var list = FS.readdirSync(dir)
@ -55,7 +55,7 @@ const Iofs = {
} }
return list return list
} catch (err) { } catch (err) {
console.error(err + '') !silently && console.error('call ls(): ', err + '')
return null return null
} }
}, },
@ -67,7 +67,7 @@ const Iofs = {
* @param {Boolean} append [是否在后面追加默认否] * @param {Boolean} append [是否在后面追加默认否]
* @param {String} encode [编码, 默认utf8] * @param {String} encode [编码, 默认utf8]
*/ */
echo(data, file, append, encode) { echo(data, file, append, encode, silently) {
if (!file) { if (!file) {
return data return data
} }
@ -96,29 +96,29 @@ const Iofs = {
} }
return true return true
} catch (err) { } catch (err) {
console.error(err + '') !silently && console.error('call echo(): ', err + '')
return false return false
} }
}, },
//修改权限 //修改权限
chmod(path, mode) { chmod(path, mode, silently) {
try { try {
FS.chmodSync(path, mode) FS.chmodSync(path, mode)
return true return true
} catch (err) { } catch (err) {
console.error(err + '') !silently && console.error('call chmod(): ', err + '')
return false return false
} }
}, },
//修改所属用户 //修改所属用户
chown(path, uid, gid) { chown(path, uid, gid, silently) {
try { try {
FS.chownSync(path, uid, gid) FS.chownSync(path, uid, gid)
return true return true
} catch (err) { } catch (err) {
console.error(err + '') !silently && console.error('call chown(): ', err + '')
return false return false
} }
}, },
@ -128,7 +128,7 @@ const Iofs = {
* @param {String} origin [原路径/原名] * @param {String} origin [原路径/原名]
* @param {String} target [目标路径/新名] * @param {String} target [目标路径/新名]
*/ */
mv(origin, target) { mv(origin, target, silently) {
var updir = PATH.parse(target).dir var updir = PATH.parse(target).dir
if (!this.isdir(updir)) { if (!this.isdir(updir)) {
this.mkdir(updir) this.mkdir(updir)
@ -143,7 +143,7 @@ const Iofs = {
} }
return false return false
} }
console.error(err + '') !silently && console.error('call mv(): ', err + '')
return false return false
} }
}, },
@ -153,7 +153,7 @@ const Iofs = {
* @param {String} origin [原路径] * @param {String} origin [原路径]
* @param {String} target [目标路径] * @param {String} target [目标路径]
*/ */
cp(origin, target) { cp(origin, target, silently) {
try { try {
// 如果是目录, 则递归操作 // 如果是目录, 则递归操作
if (this.isdir(origin)) { if (this.isdir(origin)) {
@ -176,7 +176,7 @@ const Iofs = {
return true return true
} catch (err) { } catch (err) {
console.error(err + '') !silently && console.error('call cp(): ', err + '')
return false return false
} }
}, },
@ -185,7 +185,7 @@ const Iofs = {
* [rm 删除文件/目录] * [rm 删除文件/目录]
* @param {[type]} origin [源文件/目录路径] * @param {[type]} origin [源文件/目录路径]
*/ */
rm(origin) { rm(origin, silently) {
try { try {
if (this.isdir(origin)) { if (this.isdir(origin)) {
if (VERSION > 12.1) { if (VERSION > 12.1) {
@ -200,21 +200,22 @@ const Iofs = {
} }
return true return true
} catch (err) { } catch (err) {
console.error(err + '') !silently && console.error('call rm(): ', err + '')
return false return false
} }
}, },
/** /**
* [stat 返回文件/目录的状态信息] * [stat 返回文件/目录的状态信息]
* @param {[type]} path [目标路径] * @param {[string]} path [目标路径]
* @param {[boolean]} silently [是否静默检测, 是否不打印错误日志]
*/ */
stat(path) { stat(path, silently) {
try { try {
return FS.statSync(path) return FS.statSync(path)
} catch (err) { } catch (err) {
console.error(err + '') !silently && console.error('call stat(): ', err + '')
return null return Object.create(null)
} }
}, },
@ -224,9 +225,8 @@ const Iofs = {
*/ */
isdir(path) { isdir(path) {
try { try {
return this.stat(path).isDirectory() return this.stat(path, true).isDirectory()
} catch (err) { } catch (err) {
console.error(err + '')
return false return false
} }
}, },
@ -236,14 +236,14 @@ const Iofs = {
* @param {String} dir [目标路径] * @param {String} dir [目标路径]
* @param {Number} mode [目录权限, node v10.12起支持] * @param {Number} mode [目录权限, node v10.12起支持]
*/ */
mkdir(dir, mode = 0o755) { mkdir(dir, mode = 0o755, silently) {
try { try {
if (VERSION > 10.12) { if (VERSION > 10.12) {
FS.mkdirSync(dir, { recursive: true, mode: mode }) FS.mkdirSync(dir, { recursive: true, mode: mode })
} else { } else {
var updir = PATH.parse(dir).dir var updir = PATH.parse(dir).dir
if (!updir) { if (!updir) {
console.error('Wrong dir path') !silently && console.error('call mkdir(): ', 'Wrong dir path')
return false return false
} }
@ -256,7 +256,7 @@ const Iofs = {
} }
return true return true
} catch (err) { } catch (err) {
console.error(err + '') !silently && console.error('call mkdir(): ', err + '')
return false return false
} }
}, },

View File

@ -1,6 +1,6 @@
{ {
"name": "iofs", "name": "iofs",
"version": "1.3.1", "version": "1.3.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": {