在使用node开发过程中很多时候会遇到对文件系统做各种处理操纵
-
文件处理开发中常用的内置模块
- path:处理文件路径
- fs:操作文件系统
- child_process:新建子进程
- process: 进程
-
比较好用的第三方模块
- glob: 使用shell命令的模式匹配文件
- trash: 文件放到回收站
下面通过一个文件遍历例子 来描述下node处理文件的各种实现方式
1. 递归遍历
很多时候如果不知道其它方式处理的话第一反应就是手写递归处理遍历文件夹
下面我写了几个简约的列子来看下
我们要处理文件的目录结构如下:
使用递归处理的代码实现
```
const fs = require('fs');
const path = require('path');
const files = [];
const findDirectory = function (dir) {
const fdList = fs.readdirSync(dir);
fdList.forEach(fd => {
const fdPath = path.resolve(dir, fd);
const stat = fs.statSync(fdPath);
if (stat.isFile()) {
files.push(fdPath)
} else if (stat.isDirectory()) {
findDirectory(fdPath);
}
});
};
const filesDisplay = function (filePath) {
const fdPath = path.resolve(filePath);
const stat = fs.statSync(fdPath);
if (stat.isDirectory()) {
findDirectory(fdPath);
} else if (stat.isFile()) {
files.push(fdPath)
}
return files
};
console.log(filesDisplay('/Users/fengshi/Documents/tix/test'))
运行返回结果:
[ '/Users/fengshi/Documents/tix/test/2.7.7/WARemoteDebug.js', '/Users/fengshi/Documents/tix/test/2.7.7/appservice.js', '/Users/fengshi/Documents/tix/test/2.7.7/base/WAService.js', '/Users/fengshi/Documents/tix/test/2.7.7/base/WAWebview.js', '/Users/fengshi/Documents/tix/test/2.7.7/bridge.js', '/Users/fengshi/Documents/tix/test/2.7.7/context.js', '/Users/fengshi/Documents/tix/test/2.7.7/documentstart.js', '/Users/fengshi/Documents/tix/test/2.7.7/hls.js', '/Users/fengshi/Documents/tix/test/2.7.7/pageframe.js', '/Users/fengshi/Documents/tix/test/2.7.7/virtualdom.js' ]
可以看出使用递归操作我们最少需要使用fs path模块和一些fs模块的方法
- fs.statSync: 获取文件信息对象Stats
- stat.isDirectory():判断是否是文件夹
- stat.isFile():判断是否是文件
- path.resolve(path):一个路径或路径片段解析成一个绝对路径返回解析后的路径字符串
- fs.readdirSync(dir): 读取目录下面的文件 返回目录下的文件列表对象
2. 使用glob模块
glob很方便功能也很强大
因为他是基于shell命令的模式 如果想深入研究的话可以参考glob
使用glob模块的话很简洁实现我们想要的效果
const path = require('path');
const glob = require('glob');
const files = glob.sync(path.resolve('/Users/fengshi/Documents/tix/test/2.7.7', '**'), { nodir: true});
console.log(files)
运行结果: [ '/Users/fengshi/Documents/tix/test/2.7.7/appservice.js', '/Users/fengshi/Documents/tix/test/2.7.7/base/WAService.js', '/Users/fengshi/Documents/tix/test/2.7.7/base/WAWebview.js', '/Users/fengshi/Documents/tix/test/2.7.7/bridge.js', '/Users/fengshi/Documents/tix/test/2.7.7/context.js', '/Users/fengshi/Documents/tix/test/2.7.7/documentstart.js', '/Users/fengshi/Documents/tix/test/2.7.7/hls.js', '/Users/fengshi/Documents/tix/test/2.7.7/pageframe.js', '/Users/fengshi/Documents/tix/test/2.7.7/virtualdom.js', '/Users/fengshi/Documents/tix/test/2.7.7/WARemoteDebug.js' ]
- nodir:不匹配目录,只匹配文件。(注意:要只匹配目录,只需在模式末尾加上)
参数对象中比较常用的
- ignore:添加一个模式或一个glob模式数组来排除匹配
const path = require('path');
const glob = require('glob');
const ignore = [];
ignore.push(path.resolve('/Users/fengshi/Documents/tix/test/2.7.7', 'base', '*'));
ignore.push(path.resolve('/Users/fengshi/Documents/tix/test/2.7.7', 'hls.js'));
const files = glob.sync(path.resolve('/Users/fengshi/Documents/tix/test/2.7.7', '**'), {
ignore,
nodir: true
});
console.log(files)
运行结果: [ '/Users/fengshi/Documents/tix/test/2.7.7/appservice.js', '/Users/fengshi/Documents/tix/test/2.7.7/bridge.js', '/Users/fengshi/Documents/tix/test/2.7.7/context.js', '/Users/fengshi/Documents/tix/test/2.7.7/documentstart.js', '/Users/fengshi/Documents/tix/test/2.7.7/pageframe.js', '/Users/fengshi/Documents/tix/test/2.7.7/virtualdom.js', '/Users/fengshi/Documents/tix/test/2.7.7/WARemoteDebug.js' ]
可以看到base文件夹下和hls.js都忽略了 后续我们可以对文件组合过滤等处理都很方便
灵活的使用shell模式匹配操作文件 可以看出让代码可读性和简约大大提高
3. 使用child_process子进程
child_process模块非常强大和重要
灵活运用它可以让你的node水平提高一个水平
child_process 模块让我们在一个子进程内运行一些能够进入操作系统的命令
使我们可以控制子进程的输入流,操作它的输出流 就像在linux系统里面一样操作
这里不详细讲解关于child_process的点 主要就是运行各种命令 来实现我们的结果
大致我们使用到的一些方法主要是
-
创建同步的进程(为了书写方便这里采用同步写法 当然都有对应的异步操作)
- child_process.execFileSync(file[, args][, options])
- child_process.execSync(command[, options])
- child_process.spawnSync(command[, args][, options])
这种方式需要对shell脚本有一定的基础会方便很多 我们先创建一个shell脚本文件fileHandle
-
#!/bin/bash for file in `ls $1` do if [ -d $1"/"$file ] then read_dir $1"/"$file else echo $1"/"$file fi done
然后在js里面调用
const childProcess = require('child_process');
const dir = '/Users/fengshi/Documents/tix/test/2.7.7'
const files = childProcess.execFileSync('/Users/fengshi/Documents/tix/test/fileHandle', [dir], {encoding: 'utf8'});
console.log(files)
运行结果: /Users/fengshi/Documents/tix/test/2.7.7/WARemoteDebug.js /Users/fengshi/Documents/tix/test/2.7.7/appservice.js /Users/fengshi/Documents/tix/test/2.7.7/base/WAService.js /Users/fengshi/Documents/tix/test/2.7.7/base/WAWebview.js /Users/fengshi/Documents/tix/test/2.7.7/bridge.js /Users/fengshi/Documents/tix/test/2.7.7/context.js /Users/fengshi/Documents/tix/test/2.7.7/documentstart.js /Users/fengshi/Documents/tix/test/2.7.7/hls.js /Users/fengshi/Documents/tix/test/2.7.7/pageframe.js /Users/fengshi/Documents/tix/test/2.7.7/virtualdom.js
child_process模块让我们可以脱离在node环境下 使用shell脚本可以实现我们想做的很多事