2017-02-26 17:46:04 +08:00
|
|
|
|
/**
|
2017-11-20 01:21:33 +08:00
|
|
|
|
*
|
2017-02-26 17:46:04 +08:00
|
|
|
|
* @authors yutent (yutent@doui.cc)
|
|
|
|
|
* @date 2015-12-28 14:28:38
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-11-20 01:21:33 +08:00
|
|
|
|
'use strict'
|
2017-02-26 17:46:04 +08:00
|
|
|
|
|
2018-05-24 23:36:51 +08:00
|
|
|
|
const FS = require('fs')
|
|
|
|
|
const PATH = require('path')
|
2017-02-26 17:46:04 +08:00
|
|
|
|
|
|
|
|
|
class Iofs {
|
2017-11-20 01:21:33 +08:00
|
|
|
|
constructor() {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
this.origin = FS
|
2017-11-20 01:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [cat 文件读取]
|
|
|
|
|
* @param {String} file [文件路径]
|
|
|
|
|
* @param {Function} cb [回调] 可选
|
|
|
|
|
*/
|
|
|
|
|
cat(file) {
|
|
|
|
|
try {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
return FS.readFileSync(file)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
} catch (err) {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err + '')
|
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
return null
|
2017-02-26 17:46:04 +08:00
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [ls 读取整个目录(不遍历子目录)]
|
|
|
|
|
* @param {string} dir [目标路径]
|
|
|
|
|
* @param {boolean} child [是否遍历子目录]
|
|
|
|
|
* @return {array} [返回目标目录所有文件名和子目录名, 不包括'.'和'..']
|
|
|
|
|
*/
|
|
|
|
|
ls(dir, child) {
|
|
|
|
|
try {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
let list = FS.readdirSync(dir)
|
|
|
|
|
|
|
|
|
|
list = list.map(it => {
|
|
|
|
|
return PATH.resolve(dir, it)
|
|
|
|
|
})
|
2017-11-20 01:21:33 +08:00
|
|
|
|
|
2018-05-24 23:36:51 +08:00
|
|
|
|
if (!child) {
|
|
|
|
|
return list
|
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
|
2018-05-24 23:36:51 +08:00
|
|
|
|
let tmp = list.concat()
|
2017-11-20 01:21:33 +08:00
|
|
|
|
tmp.forEach(it => {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
if (this.isdir(it)) {
|
|
|
|
|
list = list.concat(this.ls(it, true))
|
2017-02-26 17:46:04 +08:00
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
})
|
|
|
|
|
return list
|
|
|
|
|
} catch (err) {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err + '')
|
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
return null
|
2017-02-26 17:46:04 +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) {
|
|
|
|
|
if (!file) {
|
|
|
|
|
return data
|
2017-02-26 17:46:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-24 23:36:51 +08:00
|
|
|
|
let updir = PATH.parse(file).dir
|
|
|
|
|
let 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
|
|
|
|
|
2017-11-20 01:21:33 +08:00
|
|
|
|
try {
|
|
|
|
|
if (!!append) {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
FS.appendFileSync(file, data, opt)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
} else {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
FS.writeFileSync(file, data, opt)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err + '')
|
|
|
|
|
}
|
2017-02-26 17:46:04 +08:00
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//修改权限
|
|
|
|
|
chmod(path, mode) {
|
|
|
|
|
try {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
FS.chmodSync(path, mode)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
} catch (err) {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err + '')
|
|
|
|
|
}
|
2017-02-26 17:46:04 +08:00
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [mv 移动文件,兼具重命名功能]
|
2018-05-24 23:36:51 +08:00
|
|
|
|
* @param {String} origin [原路径/原名]
|
|
|
|
|
* @param {String} target [目标路径/新名]
|
2017-11-20 01:21:33 +08:00
|
|
|
|
*/
|
2018-05-24 23:36:51 +08:00
|
|
|
|
mv(origin, target) {
|
|
|
|
|
let updir = PATH.parse(target).dir
|
|
|
|
|
if (!this.isdir(updir)) {
|
|
|
|
|
this.mkdir(updir)
|
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
|
|
|
|
|
try {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
FS.renameSync(origin, target)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
} catch (e) {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
let rs = FS.createReadStream(origin)
|
|
|
|
|
let ws = FS.createWriteStream(target)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
|
|
|
|
|
rs.pipe(ws)
|
|
|
|
|
rs.on('end', err => {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
this.rm(origin)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
})
|
2017-02-26 17:46:04 +08:00
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
}
|
2017-02-26 17:46:04 +08:00
|
|
|
|
|
2018-05-24 23:36:51 +08:00
|
|
|
|
cp(origin, target) {
|
|
|
|
|
let updir = PATH.parse(target).dir
|
2017-11-20 01:21:33 +08:00
|
|
|
|
if (!this.isdir(updir)) {
|
|
|
|
|
this.mkdir(updir)
|
2017-02-26 17:46:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-24 23:36:51 +08:00
|
|
|
|
let rs = FS.createReadStream(origin)
|
|
|
|
|
let ws = FS.createWriteStream(target)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
|
|
|
|
|
rs.pipe(ws)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [rm 删除文件/目录]
|
2018-05-24 23:36:51 +08:00
|
|
|
|
* @param {[type]} origin [源文件/目录路径]
|
2017-11-20 01:21:33 +08:00
|
|
|
|
* @param {[type]} recursion [是否递归删除,若删除目录,此值须为true]
|
|
|
|
|
*/
|
2018-05-24 23:36:51 +08:00
|
|
|
|
rm(origin, recursion) {
|
2017-11-20 01:21:33 +08:00
|
|
|
|
try {
|
|
|
|
|
if (!!recursion) {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
let list = this.ls(origin)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
list.forEach(it => {
|
|
|
|
|
this.rm(it, this.isdir(it))
|
|
|
|
|
})
|
2018-05-24 23:36:51 +08:00
|
|
|
|
FS.rmdirSync(origin)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
} else {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
FS.unlinkSync(origin)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err + '')
|
|
|
|
|
}
|
2017-02-26 17:46:04 +08:00
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [stat 返回文件/目录的状态信息]
|
|
|
|
|
* @param {[type]} path [目标路径]
|
|
|
|
|
*/
|
|
|
|
|
stat(path) {
|
|
|
|
|
try {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
return FS.statSync(path)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
return null
|
2017-02-26 17:46:04 +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
|
2017-02-26 17:46:04 +08:00
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [mkdir 新建目录]
|
|
|
|
|
* @param {String} dir [目标路径]
|
|
|
|
|
*/
|
|
|
|
|
mkdir(dir) {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
let updir = PATH.parse(dir).dir
|
|
|
|
|
if (!updir) {
|
|
|
|
|
return
|
|
|
|
|
}
|
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
|
|
|
|
try {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
FS.mkdirSync(dir)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
} catch (err) {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err + '')
|
|
|
|
|
}
|
2017-11-20 01:21:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [exists 判断目标(文件/目录)是否存在]
|
|
|
|
|
* @param {String} file [目标路径]
|
|
|
|
|
*/
|
|
|
|
|
exists(file) {
|
2018-05-24 23:36:51 +08:00
|
|
|
|
return FS.existsSync(file)
|
2017-11-20 01:21:33 +08:00
|
|
|
|
}
|
2017-02-26 17:46:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 01:21:33 +08:00
|
|
|
|
module.exports = new Iofs()
|