iofs/index.js

306 lines
6.4 KiB
JavaScript
Raw Permalink Normal View History

2017-02-26 17:46:04 +08:00
/**
2020-09-19 16:43:57 +08:00
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/19 16:39:59
2017-02-26 17:46:04 +08:00
*/
const FS = require('fs')
const PATH = require('path')
2017-02-26 17:46:04 +08:00
class Stats {
isFile() {
return false
}
isDirectory() {
return false
}
isSocket() {
return false
}
isSymbolicLink() {
return false
}
}
const VERSION = +process.versions.node
.split('.')
.slice(0, 2)
.join('.')
const EMPTY_STAT = new Stats()
2020-01-08 21:20:25 +08:00
2019-07-16 15:03:53 +08:00
const Iofs = {
origin: FS,
2017-11-20 01:21:33 +08:00
/**
* [cat 文件读取]
* @param {String} file [文件路径]
* @param {Function} cb [回调] 可选
*/
cat(file, debug) {
2017-11-20 01:21:33 +08:00
try {
return FS.readFileSync(file)
2017-11-20 01:21:33 +08:00
} catch (err) {
debug && console.error('call cat(): ', err + '')
2017-11-20 01:21:33 +08:00
return null
2017-02-26 17:46:04 +08:00
}
2019-07-16 15:03:53 +08:00
},
2017-11-20 01:21:33 +08:00
/**
* [ls 读取整个目录(不遍历子目录)]
* @param {string} dir [目标路径]
2020-01-08 21:20:25 +08:00
* @param {boolean} recursive [是否递归遍历子目录]
2017-11-20 01:21:33 +08:00
* @return {array} [返回目标目录所有文件名和子目录名, 不包括'.''..']
*/
ls(dir, recursive, debug) {
2017-11-20 01:21:33 +08:00
try {
2019-07-16 15:03:53 +08:00
var list = FS.readdirSync(dir)
2019-07-16 15:03:53 +08:00
list.forEach((it, i) => {
list[i] = PATH.resolve(dir, it)
})
2017-11-20 01:21:33 +08:00
2020-01-08 21:20:25 +08:00
if (recursive) {
2019-07-16 15:03:53 +08:00
var tmp = list.concat()
tmp.forEach(it => {
if (this.isdir(it)) {
list = list.concat(this.ls(it))
2019-07-16 15:03:53 +08:00
}
})
}
2017-11-20 01:21:33 +08:00
return list
} catch (err) {
debug && console.error('call ls(): ', err + '')
2017-11-20 01:21:33 +08:00
return null
2017-02-26 17:46:04 +08:00
}
2019-07-16 15:03:53 +08:00
},
2017-11-20 01:21:33 +08:00
/**
* [echo 写文件]
* @param {String|Buffer|Number} data [要写入的数据]
* @param {String} file [要写的文件]
* @param {Boolean} append [是否在后面追加默认否]
* @param {String} encode [编码, 默认utf8]
*/
echo(data, file, append, encode, debug) {
2017-11-20 01:21:33 +08:00
if (!file) {
return data
2017-02-26 17:46:04 +08:00
}
2019-07-16 15:03:53 +08:00
var updir = PATH.parse(file).dir
var opt = {}
2017-11-20 01:21:33 +08:00
if (!this.isdir(updir)) {
this.mkdir(updir)
}
2017-02-26 17:46:04 +08:00
2017-11-20 01:21:33 +08:00
if (append && typeof append === 'string') {
encode = append
append = false
opt.encoding = encode
} else {
if (typeof encode === 'string') {
opt.encoding = encode
}
}
2017-02-26 17:46:04 +08:00
2020-01-08 21:20:25 +08:00
try {
if (!!append) {
FS.appendFileSync(file, data, opt)
} else {
FS.writeFileSync(file, data, opt)
}
return true
} catch (err) {
debug && console.error('call echo(): ', err + '')
2020-01-08 21:20:25 +08:00
return false
2017-02-26 17:46:04 +08:00
}
2019-07-16 15:03:53 +08:00
},
2017-11-20 01:21:33 +08:00
//修改权限
chmod(path, mode, debug) {
2020-01-08 21:20:25 +08:00
try {
FS.chmodSync(path, mode)
return true
} catch (err) {
debug && console.error('call chmod(): ', err + '')
2020-01-08 21:20:25 +08:00
return false
}
},
//修改所属用户
chown(path, uid, gid, debug) {
2020-01-08 21:20:25 +08:00
try {
FS.chownSync(path, uid, gid)
return true
} catch (err) {
debug && console.error('call chown(): ', err + '')
2020-01-08 21:20:25 +08:00
return false
}
2019-07-16 15:03:53 +08:00
},
2017-11-20 01:21:33 +08:00
/**
2020-01-08 21:20:25 +08:00
* [mv 移动文件&目录,兼具重命名功能]
* @param {String} origin [原路径/原名]
* @param {String} target [目标路径/新名]
2017-11-20 01:21:33 +08:00
*/
mv(origin, target, debug) {
2019-07-16 15:03:53 +08:00
var updir = PATH.parse(target).dir
if (!this.isdir(updir)) {
this.mkdir(updir)
}
2017-11-20 01:21:33 +08:00
2020-01-08 21:20:25 +08:00
try {
FS.renameSync(origin, target)
} catch (err) {
if (~err.message.indexOf('cross-device')) {
if (this.cp(origin, target)) {
return this.rm(origin)
}
return false
2019-07-16 15:03:53 +08:00
}
debug && console.error('call mv(): ', err + '')
2020-01-08 21:20:25 +08:00
return false
}
2019-07-16 15:03:53 +08:00
},
2017-02-26 17:46:04 +08:00
2020-01-08 21:20:25 +08:00
/**
* [cp 复制文件&目录]
* @param {String} origin [原路径]
* @param {String} target [目标路径]
*/
cp(origin, target, debug) {
2020-01-08 21:20:25 +08:00
try {
// 如果是目录, 则递归操作
if (this.isdir(origin)) {
this.mkdir(target)
var list = this.ls(origin)
list.forEach(val => {
let name = PATH.parse(val).base
this.cp(val, PATH.join(target, name))
})
} else {
var updir = PATH.parse(target).dir
if (!this.isdir(updir)) {
this.mkdir(updir)
}
2017-02-26 17:46:04 +08:00
2020-01-08 21:20:25 +08:00
var rs = FS.createReadStream(origin)
var ws = FS.createWriteStream(target)
rs.pipe(ws)
}
2017-11-20 01:21:33 +08:00
2020-01-08 21:20:25 +08:00
return true
} catch (err) {
debug && console.error('call cp(): ', err + '')
2020-01-08 21:20:25 +08:00
return false
}
2019-07-16 15:03:53 +08:00
},
2017-11-20 01:21:33 +08:00
/**
* [rm 删除文件/目录]
* @param {[type]} origin [源文件/目录路径]
2017-11-20 01:21:33 +08:00
*/
rm(origin, debug) {
2020-01-08 21:20:25 +08:00
try {
if (this.isdir(origin)) {
if (VERSION > 12.1) {
FS.rmdirSync(origin, { recursive: true })
} else {
var list = this.ls(origin)
list.forEach(it => this.rm(it))
FS.rmdirSync(origin)
}
} else {
FS.unlinkSync(origin)
}
return true
} catch (err) {
debug && console.error('call rm(): ', err + '')
2020-01-08 21:20:25 +08:00
return false
2017-02-26 17:46:04 +08:00
}
2019-07-16 15:03:53 +08:00
},
2017-11-20 01:21:33 +08:00
/**
* [stat 返回文件/目录的状态信息]
2020-01-18 18:53:36 +08:00
* @param {[string]} path [目标路径]
* @param {[boolean]} debug [是否静默检测, 是否不打印错误日志]
2017-11-20 01:21:33 +08:00
*/
stat(path, debug) {
2017-11-20 01:21:33 +08:00
try {
return FS.statSync(path)
2017-11-20 01:21:33 +08:00
} catch (err) {
debug && console.error('call stat(): ', err + '')
return EMPTY_STAT
2017-02-26 17:46:04 +08:00
}
2019-07-16 15:03:53 +08:00
},
2017-11-20 01:21:33 +08:00
/**
* [isdir 判断目标是否为目录]
* @param {String} path [目标路径]
*/
isdir(path) {
try {
return this.stat(path).isDirectory()
} catch (err) {
return false
}
},
isfile(path) {
try {
return this.stat(path).isFile()
2017-11-20 01:21:33 +08:00
} catch (err) {
return false
2017-02-26 17:46:04 +08:00
}
2019-07-16 15:03:53 +08:00
},
2017-11-20 01:21:33 +08:00
/**
* [mkdir 新建目录]
* @param {String} dir [目标路径]
2020-01-08 21:20:25 +08:00
* @param {Number} mode [目录权限, node v10.12起支持]
2017-11-20 01:21:33 +08:00
*/
mkdir(dir, mode = 0o755, debug) {
2020-01-08 21:20:25 +08:00
try {
if (VERSION > 10.12) {
FS.mkdirSync(dir, { recursive: true, mode: mode })
} else {
var updir = PATH.parse(dir).dir
if (!updir) {
debug && console.error('call mkdir(): ', 'Wrong dir path')
2020-01-08 21:20:25 +08:00
return false
}
2017-11-20 01:21:33 +08:00
2020-01-08 21:20:25 +08:00
if (!this.isdir(updir)) {
this.mkdir(updir)
}
2017-02-26 17:46:04 +08:00
2020-01-08 21:20:25 +08:00
FS.mkdirSync(dir)
this.chmod(dir, mode)
}
return true
} catch (err) {
debug && console.error('call mkdir(): ', err + '')
2020-01-08 21:20:25 +08:00
return false
}
2019-07-16 15:03:53 +08:00
},
2017-11-20 01:21:33 +08:00
/**
* [exists 判断目标(文件/目录)是否存在]
* @param {String} file [目标路径]
*/
exists(file) {
return this.is(file, FS.constants.R_OK)
},
// 是否可读写
is(file, mode) {
try {
FS.accessSync(file, mode)
return true
} catch (e) {
return false
}
2017-11-20 01:21:33 +08:00
}
2017-02-26 17:46:04 +08:00
}
2019-07-16 15:03:53 +08:00
module.exports = Iofs