繁体   English   中英

使用 node js 将代码从 fs 更改为 fs-extra

[英]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我的代码无法正常工作我犯了错误的任何解决方案?

  • 我的 output 使用var fs= require("fs").promise; 我是否已读取目录中存在的所有文件
FilePath=
[/home/work/test/abc.html
/home/work/test/index.js
/home/work/test/product.js
]
  • 我的 output 使用var fse=require("fs-extra") empty
filePath=[]

使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM