aboutsummaryrefslogblamecommitdiffstats
path: root/RPCTests/modules/utils.js
blob: 0d0f2b02f79c3bb1c3be685e988f13d47254d690 (plain) (tree)
1
2
3
4
5
6
7
8
9
                         
                 






                                                          


                                                       
 




                                                               




                                      















                                                          






                                      

                                                                            



                                                             

   
                                                        





                                                

                                                                



                                  














                                                
  
 
           
const fs = require('fs');
var debug = true;

module.exports = {

 sleep: function sleep(ms) {
   return new Promise(resolve => setTimeout(resolve, ms));
 },

 setDebug: function setDebug(value) { debug = value; },

 getDebug: function getDebug() { return debug; },

 cLog: function cLog(value) { if (debug) console.log(value); },

 testLog: function testLog(value) { console.log(value); },

 mkdir: function mkdir(path) {
  try {
    fs.mkdirSync(path);
  } catch(e) {
    if ( e.code != 'EEXIST' ) throw e;
  }
 },

 rmdir: function rmdir(path) {
  if( fs.existsSync(path) ) {
    fs.readdirSync(path).forEach(function(file,index){
      var curPath = path + "/" + file;
      if(fs.lstatSync(curPath).isDirectory()) { // recurse
        rmdir(curPath);
      } else { // delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(path);
  }
 },

 fileExists: function fileExists(path)
 {
    if(fs.existsSync(path))
    return true;
    return false;
 },

 readFile: function readFile(path, callback, cb) {
    fs.readFile(path, 'utf8',  (err, data) => { callback (err, data, cb) });
 },

 writeFile: function writeFile(path, data) {
   fs.writeFile(path, data, (err) => { if (err) throw err;});
 },

 listFiles: function listFiles(dir, recursive = false) {

    var results = [];
    fs.readdirSync(dir).forEach(function(file) {
        file = dir+'/'+file;
        var stat = fs.statSync(file);

        if (stat && stat.isDirectory() && recursive) {
            results = results.concat(listFiles(file, recursive))
        } else results.push(file);
    });

    return results;
 },

 listFolders: function listFolders(dir) {

    var results = [];
    fs.readdirSync(dir).forEach(function(file) {
        file = dir+'/'+file;
        var stat = fs.statSync(file);

        if (stat && stat.isDirectory()) {
            results.push(file);
        }
    });

    return results;
 }

} //exports