aboutsummaryrefslogtreecommitdiffstats
path: root/RPCTests/modules/utils.js
blob: 0d0f2b02f79c3bb1c3be685e988f13d47254d690 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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