-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (58 loc) · 1.56 KB
/
index.js
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
/**
* This file is part of qfiles.
*
* (c) Nicolas Tallefourtane <dev@nicolab.net>
*
* For the full copyright and license information, please view the LICENSE file
* distributed with this source code
* or visit https://github.com/Nicolab/qfiles.js.
*/
'use strict';
const fs = require('fs');
const path = require('path');
/**
* Load all files and populate to `obj`.
*
* @param {object} obj
* @param {array} files
* @param {string} dirname='./' Directory containing the files to load.
*/
function requireToObj(obj, files, dirname = './') {
for (let i = 0, ln = files.length; i < ln; i++) {
if (path.extname(files[i]) === '.js' && files[i] !== 'index.js') {
let filename = path.basename(files[i], '.js');
obj[filename] = require(path.join(dirname, filename));
}
}
}
/**
* Load all files.
*
* @param {array} files
* @param {string} dirname='./' Directory containing the files to load.
*/
function requireFiles(files, dirname = './') {
for (let i = 0, ln = files.length; i < ln; i++) {
if (path.extname(files[i]) === '.js' && files[i] !== 'index.js') {
require(path.join(dirname, path.basename(files[i], '.js')));
}
}
}
/**
* Require all files of `dirname` in `obj`.
*
* @param {string} dirname Directory containing the files to load.
* @param {object} [obj] Object container
*/
function requireAll(dirname, obj) {
if (obj) {
requireToObj(obj, fs.readdirSync(dirname), dirname);
} else {
requireFiles(fs.readdirSync(dirname), dirname);
}
};
module.exports = {
requireToObj,
requireFiles,
requireAll
};