2

I wrote a code by using var fs= require("fs").promises;

  • to read all my files in directories and sub-directories

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);
}

Then I install fs-extra package in my project and replace var fs=require("fs");to var fse=require("fs-extra"); and made changes in my code like this

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);
}

when I was using fs i was getting the desire output and someone told me fs-extra is advance then fs and all you have to do is replace fs with fse my code not working properly any solution where i have made mistake ?

  • My output while using var fs= require("fs").promise; is has i made to read all the files present inside directories
FilePath=
[/home/work/test/abc.html
/home/work/test/index.js
/home/work/test/product.js
]
  • My output while using var fse=require("fs-extra") empty
filePath=[]

My output don't come while using fs-extra

Aakash
  • 225
  • 13
  • "*someone told me fs-extra is advance*" - ask that someone then? I don't understand why you use fs-extra at all - you got a proper working code with native fs, why use an extra library? – Bergi May 27 '21 at 10:48
  • as in project everything is written in `fs-extra` that's why need to change @Bergi – Aakash May 27 '21 at 11:02
  • 1
    ok then, but how exactly is the code "*not working properly*"? – Bergi May 27 '21 at 11:05
  • code working properly by using `fs` but need to change my code from `fs` to`fs-extra` – Aakash May 27 '21 at 11:08
  • 1
    What *exactly* is going wrong? Error message? Please provide sample input & wrong output and expected output... etc. – trincot May 27 '21 at 12:59
  • Hello @trincot i updated my question and provided the Output which come while using `fs` and `fs-extra` I only have to read all my files using `fs-extra` method – Aakash May 27 '21 at 13:08
  • Well, I took your above (working) snippet and replaced `var fs = require('fs').promises` with `var fs = require('fs-extra')` and it produced the same output both times – derpirscher May 27 '21 at 13:40
  • I made some changes now its working fine I have posted code in ans if you need you can review it :) and thank you for your time :) – Aakash May 27 '21 at 16:02
  • Why would you even need "fs-extra" for this piece of code? Yes, "fs-extra" has some nice additional functionality, especially the support for promises was a nice feature before nodejs v10. But with recent versions of node and for this particular usecase it does not provide any advantages over the builtin fs module from nodejs. – derpirscher May 27 '21 at 19:10
  • 1
    @derpirscher Apparently (deleted comments) their team lead asked them to use it, for consistent usage across the whole project. – Bergi May 27 '21 at 20:20

1 Answers1

0

I tried to figure it out what changes i have to made if I am using var fse= require("fs-extra");

so my code will look like this

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);
}

I replaced my

  • var fs=require("fs"); to var fse=require("fs-extra");
  • let files = await fs.readdir(folderPath); with let files = fs.readdirSync(folderPath);
  • const stats = await fs.stat(filePath); with const stats = fs.statSync(filePath);

now i am getting my desire output

Aakash
  • 225
  • 13
  • 1
    Your code is now blocking - statSync will not yield to the event loop and nothing will run concurrently. – Benjamin Gruenbaum May 27 '21 at 16:05
  • so do i need to change anything @BenjaminGruenbaum ? if any changes required can you help me – Aakash May 27 '21 at 16:08
  • @BenjaminGruenbaum sorry first i used to await them but now i dont used await check my qstn code and ans code they are different :) – Aakash May 27 '21 at 17:14