diff --git a/History.md b/History.md index 9eb1ec0..15d734e 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,7 @@ +# 1.3.2 / 2020-01-18 +* [U] 增加静默参数, 可以不打印错误日志(用于某些特殊场景) + + # 1.3.1 / 2020-01-09 * [U] 优化异常输出 diff --git a/Readme.md b/Readme.md index f3cf987..f021cb7 100644 --- a/Readme.md +++ b/Readme.md @@ -33,6 +33,7 @@ ## APIs +> 所有API均支持在最后传入一个 `silently`参数(v1.3.2新增), 用于静默执行, 不打印错误日志 ### .cat(file) > 读取文件, 返回一个`Buffer对象` diff --git a/index.js b/index.js index 7a7da2e..4418ce2 100644 --- a/index.js +++ b/index.js @@ -22,11 +22,11 @@ const Iofs = { * @param {String} file [文件路径] * @param {Function} cb [回调] 可选 */ - cat(file) { + cat(file, silently) { try { return FS.readFileSync(file) } catch (err) { - console.error(err + '') + !silently && console.error('call cat(): ', err + '') return null } }, @@ -37,7 +37,7 @@ const Iofs = { * @param {boolean} recursive [是否递归遍历子目录] * @return {array} [返回目标目录所有文件名和子目录名, 不包括'.'和'..'] */ - ls(dir, recursive) { + ls(dir, recursive, silently) { try { var list = FS.readdirSync(dir) @@ -55,7 +55,7 @@ const Iofs = { } return list } catch (err) { - console.error(err + '') + !silently && console.error('call ls(): ', err + '') return null } }, @@ -67,7 +67,7 @@ const Iofs = { * @param {Boolean} append [是否在后面追加,默认否] * @param {String} encode [编码, 默认utf8] */ - echo(data, file, append, encode) { + echo(data, file, append, encode, silently) { if (!file) { return data } @@ -96,29 +96,29 @@ const Iofs = { } return true } catch (err) { - console.error(err + '') + !silently && console.error('call echo(): ', err + '') return false } }, //修改权限 - chmod(path, mode) { + chmod(path, mode, silently) { try { FS.chmodSync(path, mode) return true } catch (err) { - console.error(err + '') + !silently && console.error('call chmod(): ', err + '') return false } }, //修改所属用户 - chown(path, uid, gid) { + chown(path, uid, gid, silently) { try { FS.chownSync(path, uid, gid) return true } catch (err) { - console.error(err + '') + !silently && console.error('call chown(): ', err + '') return false } }, @@ -128,7 +128,7 @@ const Iofs = { * @param {String} origin [原路径/原名] * @param {String} target [目标路径/新名] */ - mv(origin, target) { + mv(origin, target, silently) { var updir = PATH.parse(target).dir if (!this.isdir(updir)) { this.mkdir(updir) @@ -143,7 +143,7 @@ const Iofs = { } return false } - console.error(err + '') + !silently && console.error('call mv(): ', err + '') return false } }, @@ -153,7 +153,7 @@ const Iofs = { * @param {String} origin [原路径] * @param {String} target [目标路径] */ - cp(origin, target) { + cp(origin, target, silently) { try { // 如果是目录, 则递归操作 if (this.isdir(origin)) { @@ -176,7 +176,7 @@ const Iofs = { return true } catch (err) { - console.error(err + '') + !silently && console.error('call cp(): ', err + '') return false } }, @@ -185,7 +185,7 @@ const Iofs = { * [rm 删除文件/目录] * @param {[type]} origin [源文件/目录路径] */ - rm(origin) { + rm(origin, silently) { try { if (this.isdir(origin)) { if (VERSION > 12.1) { @@ -200,21 +200,22 @@ const Iofs = { } return true } catch (err) { - console.error(err + '') + !silently && console.error('call rm(): ', err + '') return false } }, /** * [stat 返回文件/目录的状态信息] - * @param {[type]} path [目标路径] + * @param {[string]} path [目标路径] + * @param {[boolean]} silently [是否静默检测, 是否不打印错误日志] */ - stat(path) { + stat(path, silently) { try { return FS.statSync(path) } catch (err) { - console.error(err + '') - return null + !silently && console.error('call stat(): ', err + '') + return Object.create(null) } }, @@ -224,9 +225,8 @@ const Iofs = { */ isdir(path) { try { - return this.stat(path).isDirectory() + return this.stat(path, true).isDirectory() } catch (err) { - console.error(err + '') return false } }, @@ -236,14 +236,14 @@ const Iofs = { * @param {String} dir [目标路径] * @param {Number} mode [目录权限, node v10.12起支持] */ - mkdir(dir, mode = 0o755) { + mkdir(dir, mode = 0o755, silently) { try { if (VERSION > 10.12) { FS.mkdirSync(dir, { recursive: true, mode: mode }) } else { var updir = PATH.parse(dir).dir if (!updir) { - console.error('Wrong dir path') + !silently && console.error('call mkdir(): ', 'Wrong dir path') return false } @@ -256,7 +256,7 @@ const Iofs = { } return true } catch (err) { - console.error(err + '') + !silently && console.error('call mkdir(): ', err + '') return false } }, diff --git a/package.json b/package.json index 2167185..77ff66d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iofs", - "version": "1.3.1", + "version": "1.3.2", "description": "Base on native fs module, for easy using.", "main": "index.js", "repository": {