Compare commits
22 Commits
Author | SHA1 | Date |
---|---|---|
|
a986df0fa7 | |
|
bfbc829cd9 | |
|
bf62ec06b9 | |
|
724e49c949 | |
|
901f356bfd | |
|
11d37d5d9b | |
|
de6182576d | |
|
3b3c285d0c | |
|
37e44605a9 | |
![]() |
f43d707188 | |
![]() |
ec54db65d8 | |
![]() |
5c66a1b883 | |
![]() |
61dd8da228 | |
![]() |
f44be49fbc | |
![]() |
5453b999be | |
![]() |
4c949fadd7 | |
![]() |
af4d9faf0f | |
![]() |
2222b0d005 | |
![]() |
d82c4e04b4 | |
![]() |
0c79c47b19 | |
![]() |
81e0439161 | |
![]() |
9ebfac29f2 |
11
History.md
11
History.md
|
@ -1,11 +0,0 @@
|
||||||
1.0.1 / 2017-04-24
|
|
||||||
* [add] ls function can recur list child folder with second para set to be true.
|
|
||||||
|
|
||||||
|
|
||||||
1.0.0 / 2017-02-26
|
|
||||||
==================
|
|
||||||
|
|
||||||
* new project
|
|
||||||
|
|
||||||
|
|
||||||
|
|
227
Readme.md
227
Readme.md
|
@ -1,120 +1,219 @@
|
||||||

|

|
||||||
|
|
||||||
# iofs
|
# iofs
|
||||||
> `iofs` is a bash-like module for reading and writing files on Node.js. It base on Node.js's native `fs` module.
|
> `iofs`是一个基于原生`fs`模块封装的工具, 旨在提供更加方便实用一些常用的API方法(同步), API习惯参考了`bash shell`, 习惯用命令行的朋友, 可能会比较亲切。
|
||||||
|
|
||||||
## property
|
|
||||||
|
|
||||||
### self
|
|
||||||
> It return the native `fs` module for more requests.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
+ props
|
||||||
|
- [origin](#origin)
|
||||||
|
+ methods
|
||||||
|
- [.cat(file)](#catfile)
|
||||||
|
- [.ls(path, recursive)](#lspath-recursive)
|
||||||
|
- [.echo(data, file, append, encode)](#echodata-file-append-encode)
|
||||||
|
- [.chmod(path, mode)](#chmodpath-mode)
|
||||||
|
- [.chown(path, uid, gid)](#chownpath-uid-gid)
|
||||||
|
- [.mv(origin, target)](#mvorigin-target)
|
||||||
|
- [.cp(origin, target)](#cporigin-target)
|
||||||
|
- [.rm(origin)](#rmorigin)
|
||||||
|
- [.stat(path)](#statpath)
|
||||||
|
- [.isdir(path)](#isdirpath)
|
||||||
|
- [.isfile(path)](#isfilepath)
|
||||||
|
- [.mkdir(dir, mode)](#mkdirdir-mode)
|
||||||
|
- [.exists(path)](#existspath)
|
||||||
|
- [.is(path)](#isspath-mode)
|
||||||
|
|
||||||
### cat(file)
|
|
||||||
- file `<String>`
|
|
||||||
|
|
||||||
> Just like bash's cat, it will read a file and return a Buffer.
|
## 属性
|
||||||
|
|
||||||
|
### origin
|
||||||
|
> 返回原生的`fs`模块对象, 方便调用一些未封装的额外功能
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### ls(path, child)
|
## APIs
|
||||||
- path `<String>`
|
> 所有API均支持在最后传入一个 `debug<Boolean>`参数(v1.3.2新增), 用于打印错误日志
|
||||||
- child `<Boolean>`
|
|
||||||
|
|
||||||
> List all files and folders of the path given exclude '.' and '..'. I t return an array.
|
### .cat(file)
|
||||||
> If para `child` is set to be ture, it will recur list all files of child dir.
|
> 读取文件, 返回一个`Buffer对象`
|
||||||
|
|
||||||
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| file | `<String>` | 是 | 要读取的文件路径 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
### echo(data, file[, append][, encode])
|
### .ls(path, recursive)
|
||||||
- data `<String>` | `<Buffer>` | `<Number>`
|
> 列出指定目录下的所有文件&目录, 不包括 '.' and '..'. 结果返回一个数组.
|
||||||
- file `<String>`
|
|
||||||
- append `<Boolean>` optional
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
- encode `<String>` optional
|
| :--: | :--: | :--: | -- |
|
||||||
|
| path | `<String>` | 是 | 要读取的目录 |
|
||||||
|
| recursive | `<String>` | 否 | 是否递归读取 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### .echo(data, file, append, encode)
|
||||||
|
> 写数据到指定文件中. 如果指定文件不存在, 则自动生成.
|
||||||
|
|
||||||
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| data | `<String>` `<Buffer>` `<Number>` | 是 | 要写入的数据, 可以字符串、Buffer对象, 数字 |
|
||||||
|
| file | `<String>` | 是 | 要写入的文件名, 不存在会自动创建, 如存在会覆盖 |
|
||||||
|
| append | `<Boolean>` | 否 | 是否在文件后追加数据, 默认否, 即会整个文件替换 |
|
||||||
|
| encode | `<String>` | 否 | 指定保存的编码, 默认utf8 |
|
||||||
|
|
||||||
> Write/Append data to a file. creating the file if it does not yet exist.
|
|
||||||
> If `append` is set true, it will append data to the file.
|
|
||||||
> Default `encode` is utf8.
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
let fs = require('iofs')
|
var fs = require('iofs')
|
||||||
|
|
||||||
fs.echo('hello ', 'test.txt') // replacing test.txt if it exists.
|
fs.echo('hello ', 'test.txt') // 如果test.txt存在, 则覆盖.
|
||||||
|
fs.echo('world', 'test.txt', true) // 不会覆盖, 只会追加到 test.txt中
|
||||||
fs.echo('world', 'test.txt', true) // append the data to test.txt
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
### .chmod(path, mode)
|
||||||
|
> 修改文件&目录的权限.
|
||||||
|
|
||||||
### chmod(file, mode)
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
- file `<String>` | `<Buffer>`
|
| :--: | :--: | :--: | -- |
|
||||||
- mode `<Integer>`
|
| path | `<String>`| 是 | 要修改的文件&目录路径 |
|
||||||
|
| mode | `<Number>` | 是 | 权限码 `0o000 - 0o777` |
|
||||||
|
|
||||||
> Changes the mode of the file specified whose pathname is given.
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
fs.chmod('test.txt', 777) // replacing test.txt if it exists.
|
fs.chmod('test.txt', 0o777)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
### mv(from, to)
|
|
||||||
- from `<String>`
|
|
||||||
- to `<String>`
|
|
||||||
|
|
||||||
> Move a fil to the target location. It can also use to renaming a file.
|
### .chown(path, uid, gid)
|
||||||
|
> 修改文件&目录的归属。
|
||||||
|
>> `v1.3.0 新增`
|
||||||
|
|
||||||
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| path | `<String>`| 是 | 要修改的文件&目录路径 |
|
||||||
|
| uid | `<Number>` | 是 | 用户ID |
|
||||||
|
| gid | `<Number>` | 是 | 用户组ID |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### cp(from, to)
|
---
|
||||||
- from `<String>`
|
|
||||||
- to `<String>`
|
|
||||||
|
|
||||||
> Copy a fil to the target location.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### rm(path, recursion)
|
|
||||||
- path `<String>`
|
|
||||||
- recursion `<Boolean>`
|
|
||||||
|
|
||||||
> Delete a file or a folder. If path is a folder, `recursion` must be set to true.
|
### .mv(origin, target)
|
||||||
|
> 移动文件&目录, 支持跨磁盘移动; 同时具备重命名功能。
|
||||||
|
>> `v1.3.0 之后支持对目录进行操作`
|
||||||
|
|
||||||
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| origin | `<String>`| 是 | 要移动或重命名的文件&目录 |
|
||||||
|
| target | `<String>` | 是 | 目标文件名&目录名 |
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
### .cp(origin, target)
|
||||||
|
> 复制文件&目录, 支持跨磁盘复制。
|
||||||
|
>> `v1.3.0 之后支持对目录进行操作`
|
||||||
|
|
||||||
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| origin | `<String>`| 是 | 要复制的文件&目录 |
|
||||||
|
| target | `<String>` | 是 | 目标文件名&目录名 |
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
### .rm(origin)
|
||||||
|
> 删除文件&目录
|
||||||
|
>> `v1.3.0 之后取消第2个参数, 改为自动判断是否目录, 是否自动递归删除`
|
||||||
|
|
||||||
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| origin | `<String>`| 是 | 要删除的文件&目录 |
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
fs.rm('./foo/test.txt')
|
fs.rm('./foo/test.txt')
|
||||||
|
fs.rm('./foo') // 整个目录删除
|
||||||
fs.rm('./foo', true)
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
### stat(path)
|
|
||||||
- path `<String>`
|
|
||||||
|
|
||||||
> Returns an instance of fs.Stats.
|
|
||||||
|
|
||||||
|
|
||||||
### isdir(path)
|
|
||||||
- path `<String>`
|
|
||||||
|
|
||||||
> Return true if the path is a folder, and false when it is a file or not yet exists.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### mkdir(path)
|
### .stat(path)
|
||||||
- path `<String>`
|
> 返回文件&目录的状态信息, 如修改时间, 文件大小等
|
||||||
|
|
||||||
> Build a folder in where you want.
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| path | `<String>`| 是 | 要读取的目录&文件 |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### exists(path)
|
|
||||||
- path `<String>`
|
|
||||||
|
|
||||||
> Return true if the path exists, and false not.
|
---
|
||||||
|
|
||||||
|
|
||||||
|
### .isdir(path)
|
||||||
|
> 判断指定目录是否为一个目录, 路径不存在或者不是目录都会返回 false.
|
||||||
|
|
||||||
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| path | `<String>`| 是 | 要读取的目录路径 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### .isfile(path)
|
||||||
|
> 判断指定目录是否为一个文件, 路径不存在或者不是文件都会返回 false
|
||||||
|
|
||||||
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| path | `<String>`| 是 | 要读取的文件 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### .mkdir(dir)
|
||||||
|
> 创建目录, 会自动创建上级目录(如不存在)
|
||||||
|
|
||||||
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| dir | `<String>`| 是 | 要创建的目录名 |
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### .exists(path)
|
||||||
|
> 判断文件&目录是否存在
|
||||||
|
|
||||||
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| path | `<String>`| 是 | 要读取的目录&文件 |
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### .is(path, mode)
|
||||||
|
> 判断文件&目录是否存在
|
||||||
|
|
||||||
|
| 参数 | 类型 | 是否必须 | 说明 |
|
||||||
|
| :--: | :--: | :--: | -- |
|
||||||
|
| path | `<String>`| 是 | 要读取的目录&文件 |
|
||||||
|
| mode | `<Number>`| 否 | 如 r: 4, w: 2, rw: 6 |
|
||||||
|
|
||||||
|
|
||||||
|
|
522
index.js
522
index.js
|
@ -1,234 +1,302 @@
|
||||||
/**
|
/**
|
||||||
*
|
* @author yutent<yutent.io@gmail.com>
|
||||||
* @authors yutent (yutent@doui.cc)
|
* @date 2020/09/19 16:39:59
|
||||||
* @date 2015-12-28 14:28:38
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
"use strict";
|
|
||||||
|
|
||||||
const fs = require('fs'),
|
|
||||||
path = require('path');
|
|
||||||
|
|
||||||
class Iofs {
|
|
||||||
|
|
||||||
constructor(){
|
|
||||||
this.self = fs
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [cat 文件读取]
|
|
||||||
* @param {String} file [文件路径]
|
|
||||||
* @param {Function} cb [回调] 可选
|
|
||||||
*/
|
|
||||||
cat(file){
|
|
||||||
try{
|
|
||||||
return fs.readFileSync(file)
|
|
||||||
}catch(err){
|
|
||||||
console.error(err)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [ls 读取整个目录(不遍历子目录)]
|
|
||||||
* @param {string} dir [目标路径]
|
|
||||||
* @param {boolean} child [是否遍历子目录]
|
|
||||||
* @return {array} [返回目标目录所有文件名和子目录名, 不包括'.'和'..']
|
|
||||||
*/
|
|
||||||
ls(dir, child){
|
|
||||||
try{
|
|
||||||
let list = fs.readdirSync(dir)
|
|
||||||
|
|
||||||
if(!child)
|
|
||||||
return list
|
|
||||||
|
|
||||||
let tmp = Array.from(list)
|
|
||||||
tmp.forEach(it => {
|
|
||||||
let childdir = path.join(dir, it)
|
|
||||||
if(this.isdir(childdir)){
|
|
||||||
list = Array.prototype.concat.apply(list, this.ls(childdir, true).map(sub => {
|
|
||||||
return path.join(it, sub)
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return list
|
|
||||||
}catch(err){
|
|
||||||
console.error(err)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [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;
|
|
||||||
}
|
|
||||||
|
|
||||||
let updir = path.parse(file).dir,
|
|
||||||
opt = {};
|
|
||||||
if(!this.isdir(updir)){
|
|
||||||
this.mkdir(updir)
|
|
||||||
}
|
|
||||||
|
|
||||||
if(append && typeof append === 'string'){
|
|
||||||
encode = append;
|
|
||||||
append = false;
|
|
||||||
opt.encoding = encode;
|
|
||||||
}else{
|
|
||||||
if(typeof encode === 'string'){
|
|
||||||
opt.encoding = encode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try{
|
|
||||||
if(!!append){
|
|
||||||
fs.appendFileSync(file, data, opt)
|
|
||||||
}else{
|
|
||||||
fs.writeFileSync(file, data, opt)
|
|
||||||
}
|
|
||||||
}catch(err){
|
|
||||||
console.error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//修改权限
|
|
||||||
chmod(path, mode){
|
|
||||||
try{
|
|
||||||
fs.chmodSync(path, mode)
|
|
||||||
}catch(err){
|
|
||||||
console.error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [mv 移动文件,兼具重命名功能]
|
|
||||||
* @param {String} from [原路径/原名]
|
|
||||||
* @param {String} to [目标路径/新名]
|
|
||||||
*/
|
|
||||||
mv(from, to){
|
|
||||||
|
|
||||||
let updir = path.parse(to).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)){
|
|
||||||
this.mkdir(updir)
|
|
||||||
}
|
|
||||||
|
|
||||||
let rs = fs.createReadStream(from),
|
|
||||||
ws = fs.createWriteStream(to);
|
|
||||||
|
|
||||||
rs.pipe(ws)
|
|
||||||
rs.on('end', err => console.error(err))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [rm 删除文件/目录]
|
|
||||||
* @param {[type]} from [源文件/目录路径]
|
|
||||||
* @param {[type]} recursion [是否递归删除,若删除目录,此值须为true]
|
|
||||||
*/
|
|
||||||
rm(from, recursion){
|
|
||||||
try{
|
|
||||||
if(!!recursion){
|
|
||||||
fs.rmdirSync(from)
|
|
||||||
}else{
|
|
||||||
fs.unlinkSync(from)
|
|
||||||
}
|
|
||||||
}catch(err){
|
|
||||||
console.error(err)
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [stat 返回文件/目录的状态信息]
|
|
||||||
* @param {[type]} path [目标路径]
|
|
||||||
*/
|
|
||||||
stat(path){
|
|
||||||
try{
|
|
||||||
return fs.statSync(path)
|
|
||||||
}catch(err){
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [isdir 判断目标是否为目录]
|
|
||||||
* @param {String} path [目标路径]
|
|
||||||
*/
|
|
||||||
isdir(path){
|
|
||||||
try{
|
|
||||||
return this.stat(path).isDirectory()
|
|
||||||
}catch(err){
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [mkdir 新建目录]
|
|
||||||
* @param {String} dir [目标路径]
|
|
||||||
*/
|
|
||||||
mkdir(dir){
|
|
||||||
let updir = path.parse(dir).dir
|
|
||||||
if(!updir)
|
|
||||||
return
|
|
||||||
|
|
||||||
if(!this.isdir(updir)){
|
|
||||||
this.mkdir(updir)
|
|
||||||
}
|
|
||||||
|
|
||||||
try{
|
|
||||||
fs.mkdirSync(dir)
|
|
||||||
}catch(err){
|
|
||||||
console.error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [exists 判断目标(文件/目录)是否存在]
|
|
||||||
* @param {String} file [目标路径]
|
|
||||||
*/
|
|
||||||
exists(file){
|
|
||||||
return fs.existsSync(file)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
const FS = require('fs')
|
||||||
|
const PATH = require('path')
|
||||||
|
|
||||||
|
class Stats {
|
||||||
|
isFile() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
isDirectory() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
isSocket() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
isSymbolicLink() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const VERSION = +process.versions.node.split('.').slice(0, 2).join('.')
|
||||||
|
|
||||||
module.exports = new Iofs
|
const EMPTY_STAT = new Stats()
|
||||||
|
|
||||||
|
const Iofs = {
|
||||||
|
origin: FS,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [cat 文件读取]
|
||||||
|
* @param {String} file [文件路径]
|
||||||
|
* @param {Function} cb [回调] 可选
|
||||||
|
*/
|
||||||
|
cat(file, debug) {
|
||||||
|
try {
|
||||||
|
return FS.readFileSync(file)
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call cat(): ', err + '')
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ls 读取整个目录(不遍历子目录)]
|
||||||
|
* @param {string} dir [目标路径]
|
||||||
|
* @param {boolean} recursive [是否递归遍历子目录]
|
||||||
|
* @return {array} [返回目标目录所有文件名和子目录名, 不包括'.'和'..']
|
||||||
|
*/
|
||||||
|
ls(dir, recursive, debug) {
|
||||||
|
try {
|
||||||
|
var list = FS.readdirSync(dir)
|
||||||
|
|
||||||
|
list.forEach((it, i) => {
|
||||||
|
list[i] = PATH.resolve(dir, it)
|
||||||
|
})
|
||||||
|
|
||||||
|
if (recursive) {
|
||||||
|
var tmp = list.concat()
|
||||||
|
tmp.forEach(it => {
|
||||||
|
if (this.isdir(it)) {
|
||||||
|
list = list.concat(this.ls(it, recursive))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call ls(): ', err + '')
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [echo 写文件]
|
||||||
|
* @param {String|Buffer|Number} data [要写入的数据]
|
||||||
|
* @param {String} file [要写的文件]
|
||||||
|
* @param {Boolean} append [是否在后面追加,默认否]
|
||||||
|
* @param {String} encode [编码, 默认utf8]
|
||||||
|
*/
|
||||||
|
echo(data, file, append, encode, debug) {
|
||||||
|
if (!file) {
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
var updir = PATH.parse(file).dir
|
||||||
|
var opt = {}
|
||||||
|
if (!this.isdir(updir)) {
|
||||||
|
this.mkdir(updir)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (append && typeof append === 'string') {
|
||||||
|
encode = append
|
||||||
|
append = false
|
||||||
|
opt.encoding = encode
|
||||||
|
} else {
|
||||||
|
if (typeof encode === 'string') {
|
||||||
|
opt.encoding = encode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!!append) {
|
||||||
|
FS.appendFileSync(file, data, opt)
|
||||||
|
} else {
|
||||||
|
FS.writeFileSync(file, data, opt)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call echo(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
//修改权限
|
||||||
|
chmod(path, mode, debug) {
|
||||||
|
try {
|
||||||
|
FS.chmodSync(path, mode)
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call chmod(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
//修改所属用户
|
||||||
|
chown(path, uid, gid, debug) {
|
||||||
|
try {
|
||||||
|
FS.chownSync(path, uid, gid)
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call chown(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [mv 移动文件&目录,兼具重命名功能]
|
||||||
|
* @param {String} origin [原路径/原名]
|
||||||
|
* @param {String} target [目标路径/新名]
|
||||||
|
*/
|
||||||
|
mv(origin, target, debug) {
|
||||||
|
var updir = PATH.parse(target).dir
|
||||||
|
if (!this.isdir(updir)) {
|
||||||
|
this.mkdir(updir)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
FS.renameSync(origin, target)
|
||||||
|
} catch (err) {
|
||||||
|
if (~err.message.indexOf('cross-device')) {
|
||||||
|
if (this.cp(origin, target)) {
|
||||||
|
return this.rm(origin)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
debug && console.error('call mv(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [cp 复制文件&目录]
|
||||||
|
* @param {String} origin [原路径]
|
||||||
|
* @param {String} target [目标路径]
|
||||||
|
*/
|
||||||
|
cp(origin, target, debug) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
var rs = FS.createReadStream(origin)
|
||||||
|
var ws = FS.createWriteStream(target)
|
||||||
|
rs.pipe(ws)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call cp(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [rm 删除文件/目录]
|
||||||
|
* @param {[type]} origin [源文件/目录路径]
|
||||||
|
*/
|
||||||
|
rm(origin, debug) {
|
||||||
|
try {
|
||||||
|
if (VERSION >= 14.14) {
|
||||||
|
FS.rmSync(origin, { recursive: true })
|
||||||
|
} else {
|
||||||
|
if (this.isdir(origin)) {
|
||||||
|
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 + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [stat 返回文件/目录的状态信息]
|
||||||
|
* @param {[string]} path [目标路径]
|
||||||
|
* @param {[boolean]} debug [是否静默检测, 是否不打印错误日志]
|
||||||
|
*/
|
||||||
|
stat(path, debug) {
|
||||||
|
try {
|
||||||
|
return FS.statSync(path)
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call stat(): ', err + '')
|
||||||
|
return EMPTY_STAT
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [isdir 判断目标是否为目录]
|
||||||
|
* @param {String} path [目标路径]
|
||||||
|
*/
|
||||||
|
isdir(path) {
|
||||||
|
try {
|
||||||
|
return this.stat(path).isDirectory()
|
||||||
|
} catch (err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
isfile(path) {
|
||||||
|
try {
|
||||||
|
return this.stat(path).isFile()
|
||||||
|
} catch (err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [mkdir 新建目录]
|
||||||
|
* @param {String} dir [目标路径]
|
||||||
|
* @param {Number} mode [目录权限, node v10.12起支持]
|
||||||
|
*/
|
||||||
|
mkdir(dir, mode = 0o755, debug) {
|
||||||
|
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')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.isdir(updir)) {
|
||||||
|
this.mkdir(updir)
|
||||||
|
}
|
||||||
|
|
||||||
|
FS.mkdirSync(dir)
|
||||||
|
this.chmod(dir, mode)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call mkdir(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [exists 判断目标(文件/目录)是否存在]
|
||||||
|
* @param {String} file [目标路径]
|
||||||
|
*/
|
||||||
|
exists(file) {
|
||||||
|
return this.is(file, FS.constants.F_OK)
|
||||||
|
},
|
||||||
|
|
||||||
|
// 是否可读写
|
||||||
|
is(file, mode) {
|
||||||
|
try {
|
||||||
|
FS.accessSync(file, mode)
|
||||||
|
return true
|
||||||
|
} catch (e) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Iofs
|
||||||
|
|
|
@ -0,0 +1,300 @@
|
||||||
|
/**
|
||||||
|
* @author yutent<yutent.io@gmail.com>
|
||||||
|
* @date 2020/09/19 16:39:59
|
||||||
|
*/
|
||||||
|
|
||||||
|
import FS from 'fs'
|
||||||
|
import PATH from 'path'
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
export default {
|
||||||
|
origin: FS,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [cat 文件读取]
|
||||||
|
* @param {String} file [文件路径]
|
||||||
|
* @param {Function} cb [回调] 可选
|
||||||
|
*/
|
||||||
|
cat(file, debug) {
|
||||||
|
try {
|
||||||
|
return FS.readFileSync(file)
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call cat(): ', err + '')
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ls 读取整个目录(不遍历子目录)]
|
||||||
|
* @param {string} dir [目标路径]
|
||||||
|
* @param {boolean} recursive [是否递归遍历子目录]
|
||||||
|
* @return {array} [返回目标目录所有文件名和子目录名, 不包括'.'和'..']
|
||||||
|
*/
|
||||||
|
ls(dir, recursive, debug) {
|
||||||
|
try {
|
||||||
|
var list = FS.readdirSync(dir)
|
||||||
|
|
||||||
|
list.forEach((it, i) => {
|
||||||
|
list[i] = PATH.resolve(dir, it)
|
||||||
|
})
|
||||||
|
|
||||||
|
if (recursive) {
|
||||||
|
var tmp = list.concat()
|
||||||
|
tmp.forEach(it => {
|
||||||
|
if (this.isdir(it)) {
|
||||||
|
list = list.concat(this.ls(it, recursive))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call ls(): ', err + '')
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [echo 写文件]
|
||||||
|
* @param {String|Buffer|Number} data [要写入的数据]
|
||||||
|
* @param {String} file [要写的文件]
|
||||||
|
* @param {Boolean} append [是否在后面追加,默认否]
|
||||||
|
* @param {String} encode [编码, 默认utf8]
|
||||||
|
*/
|
||||||
|
echo(data, file, append, encode, debug) {
|
||||||
|
if (!file) {
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
var updir = PATH.parse(file).dir
|
||||||
|
var opt = {}
|
||||||
|
if (!this.isdir(updir)) {
|
||||||
|
this.mkdir(updir)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (append && typeof append === 'string') {
|
||||||
|
encode = append
|
||||||
|
append = false
|
||||||
|
opt.encoding = encode
|
||||||
|
} else {
|
||||||
|
if (typeof encode === 'string') {
|
||||||
|
opt.encoding = encode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!!append) {
|
||||||
|
FS.appendFileSync(file, data, opt)
|
||||||
|
} else {
|
||||||
|
FS.writeFileSync(file, data, opt)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call echo(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
//修改权限
|
||||||
|
chmod(path, mode, debug) {
|
||||||
|
try {
|
||||||
|
FS.chmodSync(path, mode)
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call chmod(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
//修改所属用户
|
||||||
|
chown(path, uid, gid, debug) {
|
||||||
|
try {
|
||||||
|
FS.chownSync(path, uid, gid)
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call chown(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [mv 移动文件&目录,兼具重命名功能]
|
||||||
|
* @param {String} origin [原路径/原名]
|
||||||
|
* @param {String} target [目标路径/新名]
|
||||||
|
*/
|
||||||
|
mv(origin, target, debug) {
|
||||||
|
var updir = PATH.parse(target).dir
|
||||||
|
if (!this.isdir(updir)) {
|
||||||
|
this.mkdir(updir)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
FS.renameSync(origin, target)
|
||||||
|
} catch (err) {
|
||||||
|
if (~err.message.indexOf('cross-device')) {
|
||||||
|
if (this.cp(origin, target)) {
|
||||||
|
return this.rm(origin)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
debug && console.error('call mv(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [cp 复制文件&目录]
|
||||||
|
* @param {String} origin [原路径]
|
||||||
|
* @param {String} target [目标路径]
|
||||||
|
*/
|
||||||
|
cp(origin, target, debug) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
var rs = FS.createReadStream(origin)
|
||||||
|
var ws = FS.createWriteStream(target)
|
||||||
|
rs.pipe(ws)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call cp(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [rm 删除文件/目录]
|
||||||
|
* @param {[type]} origin [源文件/目录路径]
|
||||||
|
*/
|
||||||
|
rm(origin, debug) {
|
||||||
|
try {
|
||||||
|
if (VERSION >= 14.14) {
|
||||||
|
FS.rmSync(origin, { recursive: true })
|
||||||
|
} else {
|
||||||
|
if (this.isdir(origin)) {
|
||||||
|
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 + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [stat 返回文件/目录的状态信息]
|
||||||
|
* @param {[string]} path [目标路径]
|
||||||
|
* @param {[boolean]} debug [是否静默检测, 是否不打印错误日志]
|
||||||
|
*/
|
||||||
|
stat(path, debug) {
|
||||||
|
try {
|
||||||
|
return FS.statSync(path)
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call stat(): ', err + '')
|
||||||
|
return EMPTY_STAT
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [isdir 判断目标是否为目录]
|
||||||
|
* @param {String} path [目标路径]
|
||||||
|
*/
|
||||||
|
isdir(path) {
|
||||||
|
try {
|
||||||
|
return this.stat(path).isDirectory()
|
||||||
|
} catch (err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
isfile(path) {
|
||||||
|
try {
|
||||||
|
return this.stat(path).isFile()
|
||||||
|
} catch (err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [mkdir 新建目录]
|
||||||
|
* @param {String} dir [目标路径]
|
||||||
|
* @param {Number} mode [目录权限, node v10.12起支持]
|
||||||
|
*/
|
||||||
|
mkdir(dir, mode = 0o755, debug) {
|
||||||
|
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')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.isdir(updir)) {
|
||||||
|
this.mkdir(updir)
|
||||||
|
}
|
||||||
|
|
||||||
|
FS.mkdirSync(dir)
|
||||||
|
this.chmod(dir, mode)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
debug && console.error('call mkdir(): ', err + '')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [exists 判断目标(文件/目录)是否存在]
|
||||||
|
* @param {String} file [目标路径]
|
||||||
|
*/
|
||||||
|
exists(file) {
|
||||||
|
return this.is(file, FS.constants.F_OK)
|
||||||
|
},
|
||||||
|
|
||||||
|
// 是否可读写
|
||||||
|
is(file, mode) {
|
||||||
|
try {
|
||||||
|
FS.accessSync(file, mode)
|
||||||
|
return true
|
||||||
|
} catch (e) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
38
package.json
38
package.json
|
@ -1,19 +1,23 @@
|
||||||
{
|
{
|
||||||
"name": "iofs",
|
"name": "iofs",
|
||||||
"version": "1.0.1",
|
"version": "1.5.3",
|
||||||
"description": "Base on native fs module, for easy using.",
|
"description": "iofs是一个基于原生fs模块封装的工具, 旨在提供更加方便实用一些常用的API方法(同步), API习惯参考了bash shell。",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"repository": {
|
"exports": {
|
||||||
"type": "git",
|
"require": "./index.js",
|
||||||
"url": "https://github.com/yutent/iofs.git"
|
"import": "./index.mjs"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"repository": {
|
||||||
"dojs",
|
"type": "git",
|
||||||
"fs",
|
"url": "https://github.com/bytedo/iofs.git"
|
||||||
"iofs",
|
},
|
||||||
"fs.io",
|
"keywords": [
|
||||||
"file"
|
"fivejs",
|
||||||
],
|
"fs",
|
||||||
"author": "yutent",
|
"iofs",
|
||||||
"license": "MIT"
|
"fs.io",
|
||||||
|
"file"
|
||||||
|
],
|
||||||
|
"author": "yutent",
|
||||||
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue