This commit is contained in:
Benjamin Toby
2025-07-15 10:02:20 +01:00
parent 8865292893
commit a71614cb0f
18 changed files with 99 additions and 34 deletions
+11 -1
View File
@@ -1,13 +1,21 @@
import fs from "fs";
import { spawn } from "child_process";
import watchFiles from "./watch/files";
import watchFolders from "./watch/folders";
import { TurboSyncConfigObject } from "../types";
import path from "path";
const confFileProvidedJSON = process.argv[process.argv.length - 1];
global.SYNCING = false;
global.SYNC_SUCCESS_EXIT_CODE = 32;
global.CONFIG_DIR = process.cwd();
global.SYNCING_FILE = path.join(global.CONFIG_DIR, "syncing.txt");
try {
if (fs.existsSync(global.SYNCING_FILE)) {
process.exit(0);
}
const configFileObject: TurboSyncConfigObject =
JSON.parse(confFileProvidedJSON);
@@ -39,6 +47,8 @@ try {
process.on("exit", (code) => {
if (code == global.SYNC_SUCCESS_EXIT_CODE) {
fs.unlinkSync(global.SYNCING_FILE);
const args = process.argv;
const cmd = args.shift();
if (cmd) {
+7 -5
View File
@@ -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
View File
@@ -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 ...");
}