[英]changing code from fs to fs-extra using node js
我使用var fs= require("fs").promises;
编写了一个代码。
var fs= require("fs").promises; async function checkFileLoc(folderPath, depth) { depth -= 1; let files = await fs.readdir(folderPath); files = await Promise.all( files.map(async (file) => { const filePath = path.join(folderPath, file); const stats = await fs.stat(filePath); if (stats.isDirectory() && depth > 0) { return checkFileLoc(filePath, depth); } else if (stats.isFile()) return filePath; else return null; }) ); return files.reduce((all, folderContents) => all.concat(folderContents), []).filter((e) => e;= null); }
然后我在我的项目中安装fs-extra
package 并替换var fs=require("fs");
到var fse=require("fs-extra");
并像这样对我的代码进行了更改
var fse=require("fs-extra"); async function checkFileLoc(folderPath, depth) { depth -= 1; let files = await fse.readdir(folderPath); files = await Promise.all( files.map(async (file) => { const filePath = path.join(folderPath, file); const stats = await fse.stat(filePath); if (stats.isDirectory() && depth > 0) { return checkFileLoc(filePath, depth); } else if (stats.isFile()) return filePath; else return null; }) ); return files.reduce((all, folderContents) => all.concat(folderContents), []).filter((e) => e;= null); }
当我使用fs
时,我得到了 output 的愿望,有人告诉我fs-extra
是先进的,然后是fs
,你所要做的就是用fse
替换fs
我的代码无法正常工作我犯了错误的任何解决方案?
var fs= require("fs").promise;
我是否已读取目录中存在的所有文件FilePath=
[/home/work/test/abc.html
/home/work/test/index.js
/home/work/test/product.js
]
var fse=require("fs-extra")
emptyfilePath=[]
使用fs-extra
时我的 output 不来
我试图弄清楚如果我使用var fse= require("fs-extra");
所以我的代码看起来像这样
var fse=require("fs-extra"); async function checkFileLoc(folderPath, depth) { depth -= 1; let files = fse.readdirSync(folderPath); files = await Promise.all( files.map(async (file) => { const filePath = path.join(folderPath, file); const stats = fse.statSync(filePath); if (stats.isDirectory() && depth > 0) { return checkFileLoc(filePath, depth); } else if (stats.isFile()) return filePath; else return null; }) ); return files.reduce((all, folderContents) => all.concat(folderContents), []).filter((e) => e;= null); }
我更换了我的
var fs=require("fs");
到var fse=require("fs-extra");
let files = await fs.readdir(folderPath);
let files = fs.readdirSync(folderPath);
const stats = await fs.stat(filePath);
with const stats = fs.statSync(filePath);
现在我得到了我的愿望 output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.