Updates
This commit is contained in:
+36
-13
@@ -1,25 +1,31 @@
|
||||
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";
|
||||
import getLatestSource from "../utils/get-last-edited-src";
|
||||
import fldFileToStrArr, {
|
||||
fldFileToStr,
|
||||
} from "../utils/grab-folders-files-string-paths";
|
||||
|
||||
const confFileProvidedJSON = process.argv[process.argv.length - 1];
|
||||
|
||||
global.SYNC_SUCCESS_EXIT_CODE = 32;
|
||||
global.CONFIG_DIR = process.cwd();
|
||||
global.SYNCING_FILE = path.join(global.CONFIG_DIR, "syncing.txt");
|
||||
global.SYNCING = false;
|
||||
|
||||
const REWATCH_TIMEOUT = 1000;
|
||||
|
||||
try {
|
||||
if (fs.existsSync(global.SYNCING_FILE)) {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const configFileObject: TurboSyncConfigObject =
|
||||
JSON.parse(confFileProvidedJSON);
|
||||
|
||||
const lastUpdated = getLatestSource({
|
||||
dirs: fldFileToStrArr(configFileObject.folders),
|
||||
files: fldFileToStrArr(configFileObject.files),
|
||||
});
|
||||
|
||||
console.log(`Running '${configFileObject.title}' ...`);
|
||||
console.log(`Last Updated Path => '${lastUpdated || "N/A"}' ...`);
|
||||
|
||||
if (
|
||||
Array.isArray(configFileObject.files) &&
|
||||
@@ -35,10 +41,29 @@ try {
|
||||
|
||||
const options = configFileObject.options;
|
||||
|
||||
if (firstFile && files?.[0]) {
|
||||
watchFiles({ files, options });
|
||||
} else if (firstFolder && folders?.[0]) {
|
||||
watchFolders({ folders, options });
|
||||
const sortedFoldersByLastUpdated =
|
||||
folders?.[0] && lastUpdated
|
||||
? [
|
||||
lastUpdated,
|
||||
...(folders?.filter(
|
||||
(fl) => fldFileToStr(fl) !== lastUpdated
|
||||
) || []),
|
||||
]
|
||||
: folders;
|
||||
|
||||
const sortedFilesByLastUpdated =
|
||||
files?.[0] && lastUpdated
|
||||
? [
|
||||
lastUpdated,
|
||||
...(files?.filter((fl) => fldFileToStr(fl) !== lastUpdated) ||
|
||||
[]),
|
||||
]
|
||||
: files;
|
||||
|
||||
if (firstFile && sortedFilesByLastUpdated?.[0]) {
|
||||
watchFiles({ files: sortedFilesByLastUpdated, options });
|
||||
} else if (firstFolder && sortedFoldersByLastUpdated?.[0]) {
|
||||
watchFolders({ folders: sortedFoldersByLastUpdated, options });
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
@@ -47,8 +72,6 @@ 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) {
|
||||
|
||||
+35
-95
@@ -1,14 +1,15 @@
|
||||
import { execSync } from "child_process";
|
||||
import fs from "fs";
|
||||
import delay from "../../utils/delay";
|
||||
import { SyncFilesFnParams, SyncFilesSyncFnParams } from "../../types";
|
||||
import { SyncFilesFnParams } from "../../types";
|
||||
|
||||
import sync from "../../utils/sync";
|
||||
|
||||
export default async function watchFiles({
|
||||
files,
|
||||
options,
|
||||
}: SyncFilesFnParams) {
|
||||
let timeout: any;
|
||||
const UPDATE_TIMEOUT = 2000;
|
||||
const UPDATE_TIMEOUT = 1000;
|
||||
|
||||
try {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
@@ -39,16 +40,6 @@ export default async function watchFiles({
|
||||
}
|
||||
|
||||
fs.writeFileSync(filePath, "");
|
||||
|
||||
if (typeof existingFilePath == "string") {
|
||||
sync({ filePath: existingFilePath, files, options });
|
||||
} else {
|
||||
sync({
|
||||
filePath: existingFilePath.path,
|
||||
files,
|
||||
options,
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
throw new Error(
|
||||
`File Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`
|
||||
@@ -62,8 +53,6 @@ export default async function watchFiles({
|
||||
if (typeof file == "object" && file.host) {
|
||||
// TODO Handle SSH
|
||||
} else if (typeof file == "string") {
|
||||
sync({ options, filePath, files });
|
||||
|
||||
await delay();
|
||||
|
||||
fs.watchFile(
|
||||
@@ -72,7 +61,7 @@ export default async function watchFiles({
|
||||
interval: interval || 200,
|
||||
},
|
||||
(curr, prev) => {
|
||||
if (fs.existsSync(global.SYNCING_FILE)) return;
|
||||
if (global.SYNCING) return;
|
||||
|
||||
const INTERVAL = options?.interval
|
||||
? options.interval
|
||||
@@ -81,92 +70,43 @@ export default async function watchFiles({
|
||||
clearTimeout(timeout);
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
fs.writeFileSync(
|
||||
global.SYNCING_FILE,
|
||||
`SYNCING FILE: curr:${curr} :: prev:${prev}`
|
||||
);
|
||||
sync({ options, filePath, files });
|
||||
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
|
||||
global.SYNCING = true;
|
||||
|
||||
sync({
|
||||
options,
|
||||
dirPath: filePath,
|
||||
dirs: files,
|
||||
isFiles: true,
|
||||
}).finally(() => {
|
||||
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
|
||||
});
|
||||
}, INTERVAL);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const lastUpdatedFile = files[0];
|
||||
const lastUpdatedFilePath =
|
||||
typeof lastUpdatedFile == "string"
|
||||
? lastUpdatedFile
|
||||
: lastUpdatedFile.path;
|
||||
|
||||
global.SYNCING = true;
|
||||
|
||||
await sync({
|
||||
dirPath: lastUpdatedFilePath,
|
||||
dirs: files,
|
||||
options,
|
||||
isFiles: true,
|
||||
firstRun: true,
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
global.SYNCING = false;
|
||||
}, UPDATE_TIMEOUT);
|
||||
} catch (error: any) {
|
||||
console.log("ERROR:", error.message);
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
function sync({ options, filePath, files }: SyncFilesSyncFnParams) {
|
||||
const destFiles = files.filter((fl) => {
|
||||
if (typeof fl == "string") return fl !== filePath;
|
||||
if (fl?.path) return fl.path !== filePath;
|
||||
return false;
|
||||
});
|
||||
|
||||
for (let j = 0; j < destFiles.length; j++) {
|
||||
let cmdArray = ["rsync", "-avh"];
|
||||
|
||||
if (options?.delete) {
|
||||
cmdArray.push("--delete");
|
||||
}
|
||||
|
||||
if (options?.exclude?.[0]) {
|
||||
options.exclude.forEach((excl) => {
|
||||
cmdArray.push(`--exclude '${excl}'`);
|
||||
});
|
||||
}
|
||||
|
||||
const dstFl = destFiles[j];
|
||||
if (typeof dstFl == "string") {
|
||||
if (!fs.existsSync(dstFl)) continue;
|
||||
|
||||
if (filePath === dstFl) {
|
||||
console.log(
|
||||
`You can't sync the same paths. Please check your configuration and resolve duplicate paths`
|
||||
);
|
||||
process.exit(6);
|
||||
}
|
||||
|
||||
cmdArray.push(filePath, dstFl);
|
||||
const cmd = cmdArray.join(" ");
|
||||
console.log(`Running cmd 1 => ${cmd}`);
|
||||
|
||||
execSync(cmd, {
|
||||
stdio: "inherit",
|
||||
});
|
||||
} else if (dstFl.path) {
|
||||
if (!dstFl.host && !fs.existsSync(dstFl.path)) continue;
|
||||
|
||||
if (filePath === dstFl.path) {
|
||||
console.log(
|
||||
`You can't sync the same paths. Please check your configuration and resolve duplicate paths`
|
||||
);
|
||||
process.exit(6);
|
||||
}
|
||||
|
||||
if (dstFl.host && dstFl.ssh_key && dstFl.user) {
|
||||
cmdArray.push("-e", `'ssh -i ${dstFl.ssh_key}'`);
|
||||
cmdArray.push(
|
||||
filePath,
|
||||
`${dstFl.user}@${dstFl.host}:${dstFl.path}`
|
||||
);
|
||||
const cmd = cmdArray.join(" ");
|
||||
execSync(cmd, {
|
||||
stdio: "inherit",
|
||||
});
|
||||
} else {
|
||||
cmdArray.push(filePath, dstFl.path);
|
||||
|
||||
const cmd = cmdArray.join(" ");
|
||||
|
||||
console.log(`Running cmd 2 => ${cmd}`);
|
||||
|
||||
execSync(cmd, {
|
||||
stdio: "inherit",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+38
-117
@@ -1,22 +1,24 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { execSync } from "child_process";
|
||||
import delay from "../../utils/delay";
|
||||
import { SyncFoldersFnParams, SyncFoldersSyncFnParams } from "../../types";
|
||||
import { SyncFoldersFnParams } from "../../types";
|
||||
import sync from "../../utils/sync";
|
||||
|
||||
export default async function watchFolders({
|
||||
folders,
|
||||
options,
|
||||
}: SyncFoldersFnParams) {
|
||||
let timeout: any;
|
||||
let isSyncing = false;
|
||||
const UPDATE_TIMEOUT = 500;
|
||||
const UPDATE_TIMEOUT = 1000;
|
||||
|
||||
try {
|
||||
const dirs = folders;
|
||||
|
||||
console.log("global.SYNCING", global.SYNCING);
|
||||
console.log(`Now handling ${dirs.length} Directories`);
|
||||
|
||||
/**
|
||||
* # Watch Directories
|
||||
*/
|
||||
const INTERVAL = options?.interval ? options.interval : UPDATE_TIMEOUT;
|
||||
|
||||
for (let i = 0; i < dirs.length; i++) {
|
||||
@@ -28,6 +30,9 @@ export default async function watchFolders({
|
||||
}
|
||||
|
||||
const dirPath = typeof dir == "string" ? dir : dir.path;
|
||||
|
||||
console.log("global.SYNCING", global.SYNCING);
|
||||
|
||||
if (
|
||||
(typeof dir == "string" && !fs.existsSync(dirPath)) ||
|
||||
(typeof dir == "object" &&
|
||||
@@ -55,22 +60,6 @@ export default async function watchFolders({
|
||||
fs.mkdirSync(dirPath, {
|
||||
recursive: true,
|
||||
});
|
||||
|
||||
if (typeof existingDirPath == "string") {
|
||||
sync({
|
||||
dirPath: existingDirPath,
|
||||
dirs,
|
||||
options,
|
||||
init: true,
|
||||
});
|
||||
} else {
|
||||
sync({
|
||||
dirPath: existingDirPath.path,
|
||||
dirs,
|
||||
options,
|
||||
init: true,
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log("Error:", error.message);
|
||||
|
||||
@@ -81,14 +70,12 @@ export default async function watchFolders({
|
||||
}
|
||||
|
||||
if (typeof dir == "string") {
|
||||
sync({ dirPath, dirs, options });
|
||||
|
||||
await delay();
|
||||
|
||||
fs.watch(dirPath, { recursive: true }, (evt, fileName) => {
|
||||
console.log("Folder Changed", evt, fileName);
|
||||
|
||||
if (fs.existsSync(global.SYNCING_FILE) || isSyncing) {
|
||||
if (global.SYNCING) {
|
||||
console.log("Existing Sync found. Returning ...");
|
||||
return;
|
||||
}
|
||||
@@ -97,106 +84,40 @@ export default async function watchFolders({
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
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}`
|
||||
);
|
||||
isSyncing = true;
|
||||
sync({ dirPath, dirs, options });
|
||||
setTimeout(() => {
|
||||
|
||||
global.SYNCING = true;
|
||||
|
||||
sync({ dirPath, dirs, options }).finally(() => {
|
||||
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
|
||||
}, INTERVAL);
|
||||
});
|
||||
}, INTERVAL);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* # Sync Last Updated
|
||||
*/
|
||||
const lastUpdatedDir = dirs[0];
|
||||
const lastUpdatedDirPath =
|
||||
typeof lastUpdatedDir == "string"
|
||||
? lastUpdatedDir
|
||||
: lastUpdatedDir.path;
|
||||
|
||||
global.SYNCING = true;
|
||||
|
||||
await sync({
|
||||
dirPath: lastUpdatedDirPath,
|
||||
dirs,
|
||||
options,
|
||||
firstRun: true,
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
global.SYNCING = false;
|
||||
}, UPDATE_TIMEOUT);
|
||||
} catch (error: any) {
|
||||
console.log("ERROR:", error.message);
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
function sync({ options, dirs, dirPath, init }: SyncFoldersSyncFnParams) {
|
||||
const dstDirs = dirs.filter((dr) => {
|
||||
if (typeof dr == "string") return dr !== dirPath;
|
||||
if (dr?.path) return dr.path !== dirPath;
|
||||
return false;
|
||||
});
|
||||
|
||||
for (let j = 0; j < dstDirs.length; j++) {
|
||||
let cmdArray = ["rsync", "-avh"];
|
||||
|
||||
if (options?.delete) {
|
||||
cmdArray.push("--delete");
|
||||
}
|
||||
|
||||
if (options?.include?.[0]) {
|
||||
options.include.forEach((incl) => {
|
||||
cmdArray.push(`--include='${incl}'`);
|
||||
});
|
||||
}
|
||||
|
||||
if (options?.exclude?.[0]) {
|
||||
options.exclude.forEach((excl) => {
|
||||
cmdArray.push(`--exclude='${excl}'`);
|
||||
});
|
||||
}
|
||||
|
||||
const dstDr = dstDirs[j];
|
||||
if (typeof dstDr == "string") {
|
||||
if (!fs.existsSync(dstDr)) continue;
|
||||
|
||||
if (dirPath === dstDr) {
|
||||
console.log(
|
||||
`You can't sync the same paths. Please check your configuration and resolve duplicate paths`
|
||||
);
|
||||
process.exit(6);
|
||||
}
|
||||
|
||||
cmdArray.push(
|
||||
path.normalize(dirPath) + "/",
|
||||
path.normalize(dstDr) + "/"
|
||||
);
|
||||
const cmd = cmdArray.join(" ");
|
||||
|
||||
execSync(cmd, {
|
||||
stdio: "inherit",
|
||||
});
|
||||
} else if (dstDr.path) {
|
||||
if (!dstDr.host && !fs.existsSync(dstDr.path)) continue;
|
||||
|
||||
if (dirPath === dstDr.path) {
|
||||
console.log(
|
||||
`You can't sync the same paths. Please check your configuration and resolve duplicate paths`
|
||||
);
|
||||
process.exit(6);
|
||||
}
|
||||
|
||||
if (dstDr.host && dstDr.ssh_key && dstDr.user) {
|
||||
cmdArray.push("-e", `'ssh -i ${dstDr.ssh_key}'`);
|
||||
cmdArray.push(
|
||||
path.normalize(dirPath) + "/",
|
||||
`${dstDr.user}@${dstDr.host}:${dstDr.path}/`
|
||||
);
|
||||
const cmd = cmdArray.join(" ");
|
||||
execSync(cmd, {
|
||||
stdio: "inherit",
|
||||
});
|
||||
} else {
|
||||
cmdArray.push(
|
||||
path.normalize(dirPath),
|
||||
path.normalize(dstDr.path)
|
||||
);
|
||||
const cmd = cmdArray.join(" ");
|
||||
execSync(cmd, {
|
||||
stdio: "inherit",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Folder Sync Complete. Exiting ...");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user