Updates
This commit is contained in:
+7
-5
@@ -3,14 +3,13 @@ import fs from "fs";
|
||||
import delay from "../../utils/delay";
|
||||
import { SyncFilesFnParams, SyncFilesSyncFnParams } from "../../types";
|
||||
|
||||
|
||||
export default async function watchFiles({
|
||||
files,
|
||||
options,
|
||||
}: SyncFilesFnParams) {
|
||||
let timeout: any;
|
||||
const UPDATE_TIMEOUT = 2000;
|
||||
|
||||
|
||||
try {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
@@ -73,7 +72,7 @@ export default async function watchFiles({
|
||||
interval: interval || 200,
|
||||
},
|
||||
(curr, prev) => {
|
||||
if (global.SYNCING) return;
|
||||
if (fs.existsSync(global.SYNCING_FILE)) return;
|
||||
|
||||
const INTERVAL = options?.interval
|
||||
? options.interval
|
||||
@@ -82,9 +81,12 @@ export default async function watchFiles({
|
||||
clearTimeout(timeout);
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
global.SYNCING = true;
|
||||
fs.writeFileSync(
|
||||
global.SYNCING_FILE,
|
||||
`SYNCING FILE: curr:${curr} :: prev:${prev}`
|
||||
);
|
||||
sync({ options, filePath, files });
|
||||
process.exit(1);
|
||||
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
|
||||
}, INTERVAL);
|
||||
}
|
||||
);
|
||||
|
||||
+17
-10
@@ -11,10 +11,6 @@ export default async function watchFolders({
|
||||
let timeout: any;
|
||||
const UPDATE_TIMEOUT = 200;
|
||||
|
||||
if (global.SYNCING) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const dirs = folders;
|
||||
|
||||
@@ -89,17 +85,26 @@ export default async function watchFolders({
|
||||
await delay();
|
||||
|
||||
fs.watch(dirPath, { recursive: true }, (evt, fileName) => {
|
||||
if (global.SYNCING) return;
|
||||
console.log("Folder Changed", evt, fileName);
|
||||
|
||||
if (fs.existsSync(global.SYNCING_FILE)) {
|
||||
console.log("Existing Sync found. Returning ...");
|
||||
return;
|
||||
}
|
||||
|
||||
clearTimeout(timeout);
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
global.SYNCING = true;
|
||||
console.log("Folder Syncing in progress ...");
|
||||
console.log(
|
||||
`Writing Sync File =>${global.SYNCING_FILE}`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
global.SYNCING_FILE,
|
||||
`SYNCING Folder: evt:${evt} :: fileName:${fileName}`
|
||||
);
|
||||
sync({ dirPath, dirs, options });
|
||||
setTimeout(() => {
|
||||
global.SYNCING = false;
|
||||
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
|
||||
}, 500);
|
||||
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
|
||||
}, INTERVAL);
|
||||
});
|
||||
}
|
||||
@@ -188,4 +193,6 @@ function sync({ options, dirs, dirPath, init }: SyncFoldersSyncFnParams) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Folder Sync Complete. Exiting ...");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user