-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
99 lines (89 loc) · 2.87 KB
/
functions.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const fs = require('fs');
const { ncp } = require('ncp');
const chalk = require('chalk');
const {
SOURCE_FILE_PATH,
SOURCE_FOLDER_PATH,
DATA_FOLDER_NAME,
DATA_FOLDER_SEPERATOR,
DESTINATION_PATH,
} = require('./config.json');
// Make a directory and if it already exists do nothing
const mkdir = dir => {
try {
return fs.mkdirSync(dir);
} catch (error) {
if (error.code !== 'EEXIST') {
throw new Error(error);
} else {
return undefined;
}
}
};
exports.mkdir = mkdir;
// Copy a file from the source to the destination
const copy = (src, dest) => {
return fs.copyFile(src, dest, error => {
if (error) {
throw console.error(error);
} else {
return;
}
});
};
exports.copy = copy;
// Copy a directory === Copy all files from the source, then make a new directory with the same name in the destination and paste all the files in the new directory
const copyDir = (src, dest) => {
return ncp(src, dest, error => {
if (error) {
throw console.error(error);
} else {
return;
}
});
};
exports.copyDir = copyDir;
// If needed, add 0s to the parts of a date
const dateTimePad = (value, digits) => {
let number = value;
while (number.toString().length < digits) number = `0${number}`;
return number;
};
exports.dateTimePad = dateTimePad;
// Main function
const COPY = async () => {
let i = 1; // This number gets used for making numbered data folders
while (fs.existsSync(`${DESTINATION_PATH}/${DATA_FOLDER_NAME}${DATA_FOLDER_SEPERATOR}${i}`)) i++; // As long as a Data folder with with the number exists, increase the number by 1
mkdir(`${DESTINATION_PATH}/${DATA_FOLDER_NAME}${DATA_FOLDER_SEPERATOR}${i}`); // Make a numbered data folder
// Copy the source file, if one was provided
if (SOURCE_FILE_PATH)
copy(
SOURCE_FILE_PATH,
`${DESTINATION_PATH}/${DATA_FOLDER_NAME}${DATA_FOLDER_SEPERATOR}${i}/${SOURCE_FILE_PATH.split('/').pop()}`
);
// Copy the source folder, if one was provided
if (SOURCE_FOLDER_PATH)
copyDir(
SOURCE_FOLDER_PATH,
`${DESTINATION_PATH}/${DATA_FOLDER_NAME}${DATA_FOLDER_SEPERATOR}${i}/${SOURCE_FOLDER_PATH.split('/').pop()}`
);
// Logging
const date = new Date(Date.now());
console.log(
chalk.green.bold(
`The ${SOURCE_FILE_PATH ? `${chalk.yellow.underline(SOURCE_FILE_PATH.split('/').pop())} file` : ''} ${
SOURCE_FOLDER_PATH
? `${SOURCE_FILE_PATH ? 'and the ' : ''}${chalk.yellow.underline(
SOURCE_FOLDER_PATH.split('/').pop()
)} folder ${SOURCE_FILE_PATH ? 'were' : 'was'}`
: 'was'
} successfully copied to the ${chalk.yellow.underline(
`${DESTINATION_PATH}/${DATA_FOLDER_NAME}${DATA_FOLDER_SEPERATOR}${i}`
)} directory! | #${i} | ${`${date.getFullYear()}-${dateTimePad(date.getMonth() + 1, 2)}-${dateTimePad(
date.getDate(),
2
)} ${dateTimePad(date.getHours(), 2)}:${dateTimePad(date.getMinutes(), 2)}:${dateTimePad(date.getSeconds(), 2)}`}`
)
);
};
exports.COPY = COPY;