skip to Main Content

I have this code in my node script

const fs = require('fs-extra');

const backupPath = path.join( __dirname , 'wp-backup');
const excludedResources = ['.DS_Store', 'dep.js', 'node_modules'];
const wpFolders = [];
const wpFiles = [];

fs.readdir( __dirname, { withFileTypes: true }, (err, files) => {
    if( err ) {
        console.log(err);
    }
    files.forEach( (file, i) => {
      const srcPath = path.join( __dirname, file.name);
      const destPath = path.join( backupPath, file.name );
         //
         if( file.isDirectory() ) {
            console.log(file.name);
            if( !fs.existsSync( destPath ) ) {
              fs.mkdir( destPath, (err) => {
                wpFolders.push( srcPath );
              });
            }
          } else {
            console.log(file.name);
            wpFiles.push( srcPath );
          }
    });
});

After I’ve readed the folder contents where the script will be placed, I want to check for the file names to check and skip the files that are into the excludedResources array to be pushed into the designed array. What is the best javascript function I can use to achive this?

2

Answers


  1. I think you did good, you know the basic concept of how it should work.

    What I would change, is adding return to the ìf (err) handling, so that we don’t start iterating the files if we have an error.

    Another improvement is that you could move the console.log(file.name) above of the if-else condition so it wouldn’t have to be defined twice.

    Login or Signup to reply.
  2. What you want is to check if the file name exists in the excludedResources array. This is easy using the indexOf function for arrays:

    const fs = require('fs-extra');
    
    const backupPath = path.join( __dirname , 'wp-backup');
    const excludedResources = ['.DS_Store', 'dep.js', 'node_modules'];
    const wpFolders = [];
    const wpFiles = [];
    
    fs.readdir( __dirname, { withFileTypes: true }, (err, files) => {
        if( err ) {
            console.log(err);
        }
        for (let file of files) {
          // check if file name is in excluded files array
          if (excludedResources.indexOf(file.name) >= 0) {
              // yes, we skip this file
              continue;
          }
    
          const srcPath = path.join( __dirname, file.name);
          const destPath = path.join( backupPath, file.name );
             //
             if( file.isDirectory() ) {
                console.log(file.name);
                if( !fs.existsSync( destPath ) ) {
                  fs.mkdir( destPath, (err) => {
                    wpFolders.push( srcPath );
                  });
                }
              } else {
                console.log(file.name);
                wpFiles.push( srcPath );
              }
        });
    });
    

    The indexOf function returns the index where the string was found in the array or -1 if not found. So if it exists the result will be index 0 or greater.

    Notice that I rewrote
    files.forEach( (file, i) => {
    as
    for (let file of files) {

    It’s just a modern way and makes it clear how to break or continue in loops.

    If you want to use your
    files.forEach( (file, i) => {

    change continue to return but it does not make your code clear.

    Happy coding.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search