-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatchVelocityProjectFolder.js
More file actions
219 lines (176 loc) · 6.84 KB
/
watchVelocityProjectFolder.js
File metadata and controls
219 lines (176 loc) · 6.84 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const chokidar = require('chokidar');
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const clc = require('cli-color');
let colors = {};
colors.lightPurple = clc.xterm(200);
colors.coral = clc.xterm(202);
colors.lightGreen = clc.xterm(83);
colors.lightCyan = clc.xterm(81);
colors.perrywinkle = clc.xterm(105);
const watchFolder = 'C:/Users/Hot Nickels/AppData/Local/Wavelink'; // Update this to the new folder you want to watch
const destinationFolder = 'C:/Users/Hot Nickels/OneDrive - Barcoding, Inc/ACTUAL STUFF/Project Files/Velocity Files'; // Update this to the destination local folder
var lastRunTime = new Date(); // Store the last run time
// LOG DATING
const logPath = 'C:/Users/Hot Nickels/OneDrive - Barcoding, Inc/nodeLogs/'; // Update this to the destination local folder
let month = () => lastRunTime.getMonth()+1 < 10 ? '0'+(lastRunTime.getMonth()+1) : ''+(lastRunTime.getMonth()+1);
let date = () => lastRunTime.getDate() < 10 ? '0'+lastRunTime.getDate() : ''+lastRunTime.getDate();
var logDate = ''+lastRunTime.getFullYear() + month() + date();
var logName = 'VelocityTransferLog-' + logDate + '.txt';
var logFile = logPath+logName;
var logCheck = false;
var createdDirectories = {}; // Define the createdDirectories object
function copyToDestination(item) {
const sourcePath = item;
const destinationPath = path.join(destinationFolder, path.relative(watchFolder, sourcePath));
var fileArr = item.split('\\');
fileArr.splice(0, fileArr.length-3);
var fileAbbr = fileArr.join('/');
fs.stat(sourcePath, (err, stats) => {
if (err) {
console.error(`${colors.coral('╡')} Failed to get source item stats: ${fileAbbr}`);
return;
}
// Check if the item has been modified since the last run
if (stats.mtime > lastRunTime) {
if (stats.isDirectory()) {
fs.copy(sourcePath, destinationPath, (copyErr) => {
if (copyErr) {
console.error(`${clc.red('╪═')} Failed to copy directory: ${fileAbbr}`);
console.error(copyErr);
} else {
logTransfer(fileAbbr);
}
});
} else if (stats.isFile()) {
fs.copyFile(sourcePath, destinationPath, (copyErr) => {
if (copyErr) {
console.error(`${clc.red('╪')} Failed to copy file: ${fileAbbr}`);
console.error(copyErr);
} else {
logTransfer(fileAbbr);
}
});
}
}
});
}
function logTransfer(fileAbbr) {
var dateTime = new Date;
var hours = dateTime.getHours();
if (hours < 10) hours = '0'+hours;
var minutes = dateTime.getMinutes();
if (minutes < 10) minutes = '0'+minutes;
var seconds = dateTime.getSeconds();
if (seconds < 10) seconds = '0'+seconds;
var timeDisplay = `${hours}:${minutes}:${seconds}`;
var output = `${colors.lightGreen('╪')} ${timeDisplay} Copied file: ${colors.lightCyan(fileAbbr)}`;
console.log(output);
writeToLog(output);
}
const watcher = chokidar.watch(watchFolder, {
ignored: /[\/\\]\./, // Ignore dotfiles
persistent: true,
recursive: true, // Watch all subfolders
});
watcher
.on('add', copyToDestination)
.on('change', copyToDestination)
.on('addDir', (item) => {
const destinationPath = path.join(destinationFolder, path.relative(watchFolder, item));
fs.promises.access(destinationPath, fs.constants.F_OK)
.then(() => {
// console.log(`Directory already exists: ${destinationPath}`);
})
.catch(() => {
return fs.promises.mkdir(destinationPath, { recursive: true })
.then(() => {
console.log(`${colors.lightGreen('╪═')} Created directory: ${colors.lightPurple(destinationPath)}`);
writeToLog(`+ Created directory: ${destinationPath}`);
})
.catch((err) => {
console.error(`${clc.red('╪═')} Failed to create directory: ${destinationPath}`);
console.error(err);
writeToLog(err);
});
});
})
.on('unlink', async (item) => {
const destinationPath = path.join(destinationFolder, path.relative(watchFolder, item));
try {
await fs.promises.access(destinationPath, fs.constants.F_OK);
await fs.promises.rm(destinationPath);
console.log(`${clc.yellow('╪')} Removed item: ${destinationPath}`);
writeToLog(`- Removed item: ${destinationPath}`);
} catch (err) {
if (err.code !== 'ENOENT') {
console.error(`${clc.red('╪')} Failed to remove item: ${destinationPath}`);
console.error(err);
writeToLog(err);
}
}
})
.on('unlinkDir', async (item) => {
const destinationPath = path.join(destinationFolder, path.relative(watchFolder, item));
try {
await fs.promises.access(destinationPath, fs.constants.F_OK);
await fs.promises.rm(destinationPath, { recursive: true });
console.log(`${clc.yellow('╪═')} Removed directory: ${destinationPath}`);
writeToLog(`- Removed directory: ${destinationPath}`);
} catch (err) {
if (err.code !== 'ENOENT') {
console.error(`${clc.red('╪═')} Failed to remove directory: ${destinationPath}`);
console.error(err);
writeToLog(err);
}
}
});
function writeToLog(log){
log = log.replaceAll('[92m╪[39m', '');
log = log.replaceAll('[96m', '');
log = log.replaceAll('[39m', '');
fs.stat(logFile, (err, stats) => {
if (err) {
if (err.code === 'ENOENT') {
if (logCheck === false) {
console.log(`${colors.lightGreen('╪')} File ${logName} does not exist. Creating.`);
// Handle the case where the file does not exist.
logCheck = true;
}
// Write to the file
fs.writeFile(logFile, log+'\n', (err) => {
if (err) {
console.error(`${clc.red('╪')} Error writing to file:`, err);
} else {
if (logCheck === false) {
console.log(`${colors.lightGreen('╪')} Data has been written to`, logName);
}
}
});
} else {
console.error(`${clc.red('╪')} Error checking file existence:`, err);
// Handle other errors that may occur during the file check.
}
} else {
if (logCheck === false) {
console.log(`${clc.yellow('╪')} File ${logName} exists. Appending.`);
// You can perform further operations here, like reading or deleting the file.
logCheck = true;
}
// Write to the file
fs.appendFile(logFile, log+'\n', (err) => {
if (err) {
console.error(`${clc.red('╪')} Error writing to file:`, err);
} else {
if (logCheck === false) {
console.log(`${colors.lightGreen('╪')} Data has been appended to`, logName);
}
}
});
}
});
}
console.log(`Watching folder and subfolders: ${watchFolder}`);
console.log(`Copying to: ${destinationFolder}`);
console.log(`Writing to: ${logFile}`);