Listing all files in my application

3

I want to back up my application and send it to my bucket on S3. But for this, I need to first list the directories (along with the files), then upload them to S3.

I found this tutorial but he did not it does, because you have to put each path to list the files, which makes the code unnecessarily larger than it should be.

NOTE: My application is made in Node, along with Express and Angular.

Could someone give me a light? : D

    
asked by anonymous 07.10.2014 / 20:51

1 answer

1

Here is a feature we use on the new MooTools website.

var path = require('path');
var fs = require('fs');
function getFiles(dir, files_, fileType){

    var regex = fileType ? new RegExp('\' + fileType + '$') : '';

    return fs.readdirSync(dir).reduce(function(allFiles, file){
        var name = path.join(dir, file);
        if (fs.statSync(name).isDirectory()){
            getFiles(name, allFiles, fileType);
        } else if (file.match(regex)){
            allFiles.push(name);
        }
        return allFiles;
    }, files_ || []);

}

The function is synchronous and accepts 3 arguments :

  • the board
  • an array of files already in memory (this argument is also used by the function when calling itself)
  • file / extension type

It returns an array with all files within the directory and subdirectories.

    
30.10.2014 / 23:27