-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (72 loc) · 2.97 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const fs = require('fs');
const chalk = require('chalk');
const ms = require('ms');
const manager = require('./functions.js');
const {
SOURCE_FILE_PATH,
SOURCE_FOLDER_PATH,
DATA_FOLDER_NAME,
DATA_FOLDER_SEPERATOR,
DESTINATION_PATH,
INTERVALTIME,
} = require('./config.json');
// Making sure the necessary properties have been provided and they are valid
if (!SOURCE_FILE_PATH && !SOURCE_FOLDER_PATH)
throw new TypeError(chalk.red.bold('Neither a source file or a source folder have been provided!'));
if (SOURCE_FILE_PATH && !fs.existsSync(SOURCE_FILE_PATH))
throw new TypeError(chalk.red.bold(`The source file path ${chalk.yellow(SOURCE_FILE_PATH)} is not a valid path!`));
if (SOURCE_FOLDER_PATH && !fs.existsSync(SOURCE_FOLDER_PATH))
throw new TypeError(
chalk.red.bold(`The source folder path ${chalk.yellow(SOURCE_FOLDER_PATH)} is not a valid path!`)
);
if (!DATA_FOLDER_NAME) throw new TypeError(chalk.red.bold('No name for the data folders has been provided!'));
if (!DATA_FOLDER_SEPERATOR) throw new TypeError(chalk.red.bold('No seperator for the data folders has been provided!'));
if (!DESTINATION_PATH) {
throw new TypeError(chalk.red.bold('No destination path has been provided!'));
} else if (!fs.existsSync(DESTINATION_PATH.replace(/[^/]*$/, ''))) {
throw new TypeError(
chalk.red.bold(`The destiantion path ${chalk.yellow(DESTINATION_PATH.replace(/[^/]*$/, ''))} is not a valid path!`)
);
}
if (!INTERVALTIME) {
throw new TypeError(chalk.red.bold('No interval time has been provided!'));
} else if (!ms(INTERVALTIME) || /^\d+$/.test(INTERVALTIME)) {
throw new TypeError(
chalk.red.bold(
`The interval time is not a valid number!\nYou can use ${chalk.yellow('d')} for days, ${chalk.yellow(
'h'
)} for hours, ${chalk.yellow('m')} for minutes, ${chalk.yellow('s')} for seconds and ${chalk.yellow(
'ms'
)} for milliseconds!`
)
);
}
// Make a directory for the data if it does not exist yet
manager.mkdir(DESTINATION_PATH);
// Do the process once at startup
manager.COPY();
// Set an interval for copying the file and/or the folder after a specific amount of time
setInterval(() => {
// Making sure the necessary paths are are still valid
if (SOURCE_FILE_PATH && !fs.existsSync(SOURCE_FILE_PATH))
throw new TypeError(
chalk.red.bold(`The source file path ${chalk.yellow(SOURCE_FILE_PATH)} is not a valid path anymore!`)
);
if (SOURCE_FOLDER_PATH && !fs.existsSync(SOURCE_FOLDER_PATH))
throw new TypeError(
chalk.red.bold(`The source folder path ${chalk.yellow(SOURCE_FOLDER_PATH)} is not a valid path anymore!`)
);
if (!fs.existsSync(DESTINATION_PATH.replace(/[^/]*$/, '')))
throw new TypeError(
chalk.red.bold(
`The destiantion path ${chalk.yellow(DESTINATION_PATH.replace(/[^/]*$/, ''))} is not a valid path anymore!`
)
);
// Copy
manager.COPY();
}, ms(INTERVALTIME)); // Interval time
// Handling errors
process.on('unhandledRejection', error => {
if (!error) return process.exit(1);
return console.error('Unhandled promise rejection:', error);
});