-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
55 lines (49 loc) · 1.43 KB
/
main.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
const fs = require("fs-extra");
const path = require("path");
const chokidar = require("chokidar");
/**
* Create directory at path if path is not directory
* @param {String} path
*/
function createIfNotExist(path) {
if (!fs.existsSync(path)) {
console.log("Creating directory : ", path);
fs.mkdirSync(path);
}
}
/**
*
* @param {String} fromPath - full path of old file
* @param {String} toPath - full path of new file
*/
async function moveFile(fromPath, toPath) {
await fs.rename(fromPath, toPath);
}
module.exports = function(config) {
config.forEach(module => {
const modulePath = module.directory;
if (modulePath) {
console.log("Monitoring " + modulePath);
module.files.forEach(type => {
createIfNotExist(path.join(module.directory + type.directory));
});
chokidar.watch(module.directory).on("add", filePath => {
const fileExtension = path.extname(filePath).substr(1);
const fileName = path.basename(filePath);
module.files.forEach(async type => {
if (type.fileTypes.includes(fileExtension)) {
try {
moveFile(
filePath,
path.join(modulePath, type.directory, fileName)
);
} catch (error) {
console.log("error: ", error);
}
console.log(`Moved ${fileName} to ${type.directory} directory`);
}
});
});
}
});
};