forked from SlavyanDesu/BocchiBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.js
More file actions
56 lines (52 loc) · 1.31 KB
/
Copy pathloader.js
File metadata and controls
56 lines (52 loc) · 1.31 KB
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
/* eslint-disable no-empty-function */
const fs = require('fs-extra')
const { color } = require('../tools')
/**
* Returns an array of files.
* @param {*} dirPath
* @param {string[]} [arrayOfFiles]
* @returns {string[]}
*/
const getAllDirFiles = (dirPath, arrayOfFiles) => {
const files = fs.readdirSync(dirPath)
arrayOfFiles = arrayOfFiles || []
files.forEach((f) => {
if (fs.statSync(dirPath + '/' + f).isDirectory()) {
arrayOfFiles = getAllDirFiles(dirPath + '/' + f, arrayOfFiles)
} else {
arrayOfFiles.push(f)
}
})
return arrayOfFiles
}
/**
* Uncache a changes.
* @param {*} module
*/
const uncache = (module = '.') => {
return new Promise((resolve, reject) => {
try {
delete require.cache[require.resolve(module)]
resolve()
} catch (err) {
reject(err)
}
})
}
/**
* Delete file cache.
* @param {*} module
* @param {*} call
*/
const nocache = (module, call = () => {}) => {
console.log(color('[WATCH]', 'orange'), color(`=> '${module}'`, 'yellow'), 'file is now being watched by me!')
fs.watchFile(require.resolve(module), async () => {
await uncache(require.resolve(module))
call(module)
})
}
module.exports = {
getAllDirFiles,
uncache,
nocache
}