blob: 3234d3edbe6aa7641005db82912a023a1408a023 (
plain)
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
|
'use strict';
const Fs = require('fs');
const Path = require('path');
// Module containing utility functions.
const Util = {
// Parses a 16 bit number buffer.
parse16BitBuffer: function (buffer) {
return buffer[0] * 256 + buffer[1];
},
// Picks a random element from an array.
pickRandom: function (array) {
return array[Math.floor(Math.random() * array.length)];
},
// For a given path, requires all of the files and returns an array
// with the results. If the directory contains any non-requireable file,
// it will fail.
loadFiles: function (path) {
return new Promise(function (resolve, reject) {
Fs.readdir(path, function (err, files) {
if (err) {
return reject(err);
}
let loadedFiles = [];
for (let file of files) {
let filePath = Path.join(path, file);
let loadedFile;
try {
loadedFile = require(filePath);
} catch (err) {
return reject(err);
}
loadedFiles.push(loadedFile);
}
resolve(loadedFiles);
});
});
}
};
module.exports = Util;
|