Compare commits

..
20 Commits
Author SHA1 Message Date
tben 9eae95e391 Bugfix: fix race conditions 2026-08-10 11:07:39 +01:00
tben 44c5c19050 Updates 2025-12-25 06:04:03 +01:00
tben a784e7f520 Updates 2025-11-20 08:03:10 +01:00
tben fde40e8ece Updates 2025-11-20 08:02:54 +01:00
Benjamin Toby 822778d43b Updates 2025-07-21 13:51:59 +01:00
Benjamin Toby 5de4d1dc73 Updates 2025-07-15 19:49:01 +01:00
Benjamin Toby 8a7c4530da Updates 2025-07-15 10:03:31 +01:00
Benjamin Toby 4177497e48 Updates 2025-07-15 10:02:55 +01:00
Benjamin Toby a71614cb0f Updates 2025-07-15 10:02:20 +01:00
Benjamin Toby 8865292893 Updates 2025-07-13 07:52:50 +01:00
Benjamin Toby 37a314273a Updates 2025-07-11 06:49:06 +01:00
Benjamin Toby 68c1074de1 Updates 2025-05-29 11:29:29 +01:00
Benjamin Toby accf486151 Updates 2025-02-04 13:48:47 +01:00
Benjamin Toby efcee1bb11 Updates 2025-02-04 13:42:30 +01:00
Benjamin Toby 8a1294a348 Updates 2025-01-20 06:43:09 +01:00
Benjamin Toby 6447abd3fb Updates 2025-01-16 07:17:06 +01:00
Benjamin Toby de429f4d2d Updates 2025-01-16 07:16:31 +01:00
Benjamin Toby 219db3d88e Updates 2025-01-16 07:12:45 +01:00
Benjamin Toby 222d1a4372 refactoring to ts 2025-01-15 05:43:18 +01:00
Benjamin Toby 4c45eba321 Remove bin folder 2024-12-25 20:45:13 +01:00
68 changed files with 2064 additions and 535 deletions
+1
View File
@@ -177,3 +177,4 @@ out
/test /test
/lib-node /lib-node
/dump /dump
/bin
+1 -1
View File
@@ -1,2 +1,2 @@
@moduletrace:registry=https://git.tben.me/api/packages/moduletrace/npm/ @moduletrace:registry=https://git.tben.me/api/packages/Moduletrace/npm/
//git.tben.me/api/packages/Moduletrace/npm/:_authToken=${GITBEN_NPM_TOKEN} //git.tben.me/api/packages/Moduletrace/npm/:_authToken=${GITBEN_NPM_TOKEN}
+33
View File
@@ -84,3 +84,36 @@ You can also use environment variables in the config file. Example:
} }
] ]
``` ```
## System Processes
Run Turbosync as a system process to keep it running in the background.
### Systemd Config
Use this template to create a systemd service for turbosync.
```ini
[Unit]
Description=Service Name
After=network.target
[Service]
ExecStart=/home/user/.bun/bin/turbosync
Restart=always
RestartSec=5
Environment="PATH=/usr/bin:/home/user/.bun/bin:/home/user/.nvm/versions/node/v20.18.1/bin:${PATH}"
User=user
WorkingDirectory=/home/user/services/turbosync/service-name
[Install]
WantedBy=multi-user.target
```
After this you can run:
```bash
sudo systemctl daemon-reload
sudo systemctl enable turbosync-service-name
sudo systemctl start turbosync-service-name
```
BIN
View File
Binary file not shown.
+16
View File
@@ -0,0 +1,16 @@
{
"lockfileVersion": 1,
"workspaces": {
"": {
"name": "@moduletrace/turbosync",
"devDependencies": {
"@types/node": "^22.10.2",
},
},
},
"packages": {
"@types/node": ["@types/node@22.10.2", "", { "dependencies": { "undici-types": "~6.20.0" } }, "sha512-Xxr6BBRCAOQixvonOye19wnzyDiUtTeqldOOmj3CkeblonbccA12PFwlufvRdrpjXxqnmUaeiU5EOA+7s5diUQ=="],
"undici-types": ["undici-types@6.20.0", "", {}, "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg=="],
}
}
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env node
declare global {
var CONFIG_DIR: string;
}
export {};
+69
View File
@@ -0,0 +1,69 @@
#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const child_process_1 = require("child_process");
const env_1 = __importDefault(require("./utils/env"));
const confFileProvidedPath = process.argv[process.argv.length - 1];
global.CONFIG_DIR = process.cwd();
if (confFileProvidedPath === "--version" || confFileProvidedPath === "-v") {
try {
const packageJson = fs_1.default.readFileSync(path_1.default.resolve(__dirname, "../package.json"), "utf-8");
console.log(`Turbo Sync Version: ${JSON.parse(packageJson).version}`);
}
catch (error) {
console.log(`Turbo Sync Version fetch failed! ${error.message} \nNo Worries, Turbo Sync is still installed properly`);
}
process.exit(6);
}
console.log("Running Folder Sync ...");
const defaultConfigFilePath = path_1.default.resolve(process.cwd(), "turbosync.config.json");
const confFileComputedPath = typeof confFileProvidedPath == "string" &&
confFileProvidedPath.endsWith(".json")
? path_1.default.resolve(process.cwd(), confFileProvidedPath)
: null;
if (!fs_1.default.existsSync(defaultConfigFilePath) && !confFileComputedPath) {
console.log("Please Provide the path to a config file or add a config file named `turbosync.config.json` to the path you're running this program");
process.exit();
}
if (!defaultConfigFilePath &&
confFileComputedPath &&
!fs_1.default.existsSync(confFileComputedPath)) {
console.log("Config File does not exist");
process.exit();
}
try {
const configFinalPath = fs_1.default.existsSync(defaultConfigFilePath)
? defaultConfigFilePath
: confFileComputedPath && fs_1.default.existsSync(confFileComputedPath)
? confFileComputedPath
: null;
const configJSON = configFinalPath
? fs_1.default.readFileSync(configFinalPath, "utf8")
: null;
if (!configJSON)
throw new Error("Config JSON could not be resolved. Please check your files.");
const parsedConfigJSON = (0, env_1.default)({ json: configJSON });
const configArray = JSON.parse(parsedConfigJSON);
for (let i = 0; i < configArray.length; i++) {
const config = configArray[i];
const childProcess = (0, child_process_1.spawn)("node", [
path_1.default.resolve(__dirname, "./lib/sync.js"),
`${JSON.stringify(config)}`,
], {
stdio: "inherit",
detached: false,
});
}
}
catch (error) {
console.log(`Process Error =>`, error.message);
process.exit();
}
setInterval(() => {
console.log(`Turbo Sync Running for ${process.uptime().toLocaleString()}s ...`);
}, 60000);
+1
View File
@@ -0,0 +1 @@
export {};
+86
View File
@@ -0,0 +1,86 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const files_1 = __importDefault(require("./watch/files"));
const folders_1 = __importDefault(require("./watch/folders"));
const get_last_edited_src_1 = __importDefault(require("../utils/get-last-edited-src"));
const grab_folders_files_string_paths_1 = __importStar(require("../utils/grab-folders-files-string-paths"));
const confFileProvidedJSON = process.argv[process.argv.length - 1];
global.CONFIG_DIR = process.cwd();
try {
const configFileObject = JSON.parse(confFileProvidedJSON);
const lastUpdated = (0, get_last_edited_src_1.default)({
dirs: (0, grab_folders_files_string_paths_1.default)(configFileObject.folders),
files: (0, grab_folders_files_string_paths_1.default)(configFileObject.files),
config: configFileObject,
});
console.log(`Running '${configFileObject.title}' ...`);
console.log(`Last Updated Path => '${lastUpdated || "N/A"}' ...`);
if (Array.isArray(configFileObject.files) &&
Array.isArray(configFileObject.folders)) {
throw new Error("Choose wither `files` or `folders`. Not both");
}
const files = configFileObject === null || configFileObject === void 0 ? void 0 : configFileObject.files;
const firstFile = files === null || files === void 0 ? void 0 : files[0];
const folders = configFileObject === null || configFileObject === void 0 ? void 0 : configFileObject.folders;
const firstFolder = folders === null || folders === void 0 ? void 0 : folders[0];
const options = configFileObject.options;
const sortedFoldersByLastUpdated = (folders === null || folders === void 0 ? void 0 : folders[0]) && lastUpdated
? [
lastUpdated,
...((folders === null || folders === void 0 ? void 0 : folders.filter((fl) => (0, grab_folders_files_string_paths_1.fldFileToStr)(fl) !== lastUpdated)) || []),
]
: folders;
const sortedFilesByLastUpdated = (files === null || files === void 0 ? void 0 : files[0]) && lastUpdated
? [
lastUpdated,
...((files === null || files === void 0 ? void 0 : files.filter((fl) => (0, grab_folders_files_string_paths_1.fldFileToStr)(fl) !== lastUpdated)) ||
[]),
]
: files;
if (firstFile && (sortedFilesByLastUpdated === null || sortedFilesByLastUpdated === void 0 ? void 0 : sortedFilesByLastUpdated[0])) {
(0, files_1.default)({ files: sortedFilesByLastUpdated, options });
}
else if (firstFolder && (sortedFoldersByLastUpdated === null || sortedFoldersByLastUpdated === void 0 ? void 0 : sortedFoldersByLastUpdated[0])) {
(0, folders_1.default)({ folders: sortedFoldersByLastUpdated, options });
}
}
catch (error) {
console.log(error);
process.exit();
}
+2
View File
@@ -0,0 +1,2 @@
import { SyncFilesFnParams } from "../../types";
export default function watchFiles({ files, options, }: SyncFilesFnParams): Promise<void>;
+87
View File
@@ -0,0 +1,87 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = watchFiles;
const fs_1 = __importDefault(require("fs"));
const delay_1 = __importDefault(require("../../utils/delay"));
const sync_1 = __importDefault(require("../../utils/sync"));
const sync_scheduler_1 = __importDefault(require("../../utils/sync-scheduler"));
function watchFiles(_a) {
return __awaiter(this, arguments, void 0, function* ({ files, options, }) {
const UPDATE_TIMEOUT = 1000;
try {
const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval) ? options.interval : UPDATE_TIMEOUT;
const scheduler = new sync_scheduler_1.default((filePath, firstRun) => (0, sync_1.default)({
options,
dirPath: filePath,
dirs: files,
isFiles: true,
firstRun,
}), INTERVAL);
for (let i = 0; i < files.length; i++) {
const file = files[i];
const filePath = typeof file == "string" ? file : (file === null || file === void 0 ? void 0 : file.path) ? file.path : null;
const interval = typeof file == "object" ? file.interval : null;
if (!filePath)
continue;
if (typeof file == "string" && !fs_1.default.existsSync(filePath)) {
try {
const existingFilePath = files.find((fl) => {
if (typeof fl == "string")
return fs_1.default.existsSync(fl);
if (!fl.host)
return fs_1.default.existsSync(fl.path); // TODO handle remote
});
if (!existingFilePath) {
throw new Error("No existing Files for reference");
}
const fileDirPath = typeof existingFilePath == "string"
? existingFilePath
: existingFilePath.path;
if (!fs_1.default.existsSync(fileDirPath)) {
fs_1.default.mkdirSync(fileDirPath, { recursive: true });
}
fs_1.default.writeFileSync(filePath, "");
}
catch (error) {
throw new Error(`File Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`);
}
}
if (typeof file == "string" && !fs_1.default.statSync(filePath).isFile()) {
throw new Error(`'${filePath}' is not a File!`);
}
if (typeof file == "object" && file.host) {
// TODO Handle SSH
}
else if (typeof file == "string") {
yield (0, delay_1.default)();
fs_1.default.watchFile(filePath, {
interval: interval || 200,
}, (curr, prev) => {
scheduler.schedule(filePath);
});
}
}
const lastUpdatedFile = files[0];
const lastUpdatedFilePath = typeof lastUpdatedFile == "string"
? lastUpdatedFile
: lastUpdatedFile.path;
scheduler.enqueue(lastUpdatedFilePath, true);
}
catch (error) {
console.log("ERROR:", error.message);
process.exit(0);
}
});
}
+2
View File
@@ -0,0 +1,2 @@
import { SyncFoldersFnParams } from "../../types";
export default function watchFolders({ folders, options, }: SyncFoldersFnParams): Promise<void>;
+85
View File
@@ -0,0 +1,85 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = watchFolders;
const fs_1 = __importDefault(require("fs"));
const sync_1 = __importDefault(require("../../utils/sync"));
const sync_scheduler_1 = __importDefault(require("../../utils/sync-scheduler"));
function watchFolders(_a) {
return __awaiter(this, arguments, void 0, function* ({ folders, options, }) {
const UPDATE_TIMEOUT = 1000;
try {
const dirs = folders;
console.log(`Now handling ${dirs.length} Directories`);
/**
* # Watch Directories
*/
const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval) ? options.interval : UPDATE_TIMEOUT;
const scheduler = new sync_scheduler_1.default((dirPath, firstRun) => (0, sync_1.default)({ dirPath, dirs, options, firstRun }), INTERVAL);
for (let i = 0; i < dirs.length; i++) {
const dir = dirs[i];
if (!dir) {
console.log(`Dir: ${dir} doesn't exist`);
continue;
}
const dirPath = typeof dir == "string" ? dir : dir.path;
if ((typeof dir == "string" && !fs_1.default.existsSync(dirPath)) ||
(typeof dir == "object" &&
dir.path &&
!dir.host &&
!fs_1.default.existsSync(dir.path))) {
console.log(`Dir ${dirPath} does not exist. Creating ...`);
try {
const existingDirPath = dirs.find((dr) => {
if (typeof dr == "string")
return fs_1.default.existsSync(dr);
if (!dr.host)
return fs_1.default.existsSync(dr.path); // TODO handle remote
return false;
});
console.log(`Existing Dir to clone: ${existingDirPath}`);
if (!existingDirPath) {
throw new Error("No existing Directories for reference");
}
fs_1.default.mkdirSync(dirPath, {
recursive: true,
});
}
catch (error) {
console.log("Error:", error.message);
throw new Error(`Folder Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`);
}
}
if (typeof dir == "string") {
fs_1.default.watch(dirPath, { recursive: true }, (evt, fileName) => {
console.log("Folder Changed", evt, fileName);
scheduler.schedule(dirPath);
});
}
}
/**
* # Sync Last Updated
*/
const lastUpdatedDir = dirs[0];
const lastUpdatedDirPath = typeof lastUpdatedDir == "string"
? lastUpdatedDir
: lastUpdatedDir.path;
scheduler.enqueue(lastUpdatedDirPath, true);
}
catch (error) {
console.log("ERROR:", error.message);
process.exit(0);
}
});
}
+49
View File
@@ -0,0 +1,49 @@
export type TurboSyncConfigArray = TurboSyncConfigObject[];
export interface TurboSyncConfigObject {
title?: string;
files?: (string | TurboSyncFileObject)[];
folders?: (string | TurboSyncFileObject)[];
options?: TurboSyncOptions;
}
export interface TurboSyncFileObject {
path: string;
host?: string;
user?: string;
ssh_key?: string;
interval?: number;
}
export interface TurboSyncOptions {
delete?: boolean;
exclude?: string[];
include?: string[];
interval?: number;
bootstrapLastEdited?: boolean;
}
export interface SyncFilesFnParams {
files: (string | TurboSyncFileObject)[];
options: TurboSyncOptions | undefined;
}
export interface SyncFilesSyncFnParams {
files: (string | TurboSyncFileObject)[];
options: TurboSyncOptions | undefined;
filePath: string;
}
export interface SyncFoldersFnParams {
folders: (string | TurboSyncFileObject)[];
options: TurboSyncOptions | undefined;
}
export interface SyncFoldersSyncFnParams {
dirs: (string | TurboSyncFileObject)[];
options: TurboSyncOptions | undefined;
dirPath: string;
firstRun?: boolean;
isFiles?: boolean;
}
export interface HandleEnvVarsFnParams {
json: string;
}
export declare const TurboSyncStatuses: readonly ["syncing", "error", "done"];
export type SyncFileConfig = {
status?: (typeof TurboSyncStatuses)[number];
lastSyncedPath?: string;
};
+4
View File
@@ -0,0 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TurboSyncStatuses = void 0;
exports.TurboSyncStatuses = ["syncing", "error", "done"];
+3
View File
@@ -0,0 +1,3 @@
declare const colorsArr: readonly ["red", "bright", "dim", "underscore", "blink", "reverse", "hidden", "black", "green", "yellow", "blue", "magenta", "cyan", "white", "gray"];
export default function colors(text: string, type: (typeof colorsArr)[number], bg: boolean): string;
export {};
+22 -18
View File
@@ -1,5 +1,23 @@
// @ts-check "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = colors;
const colorsArr = [
"red",
"bright",
"dim",
"underscore",
"blink",
"reverse",
"hidden",
"black",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
"gray",
];
const colorCodes = { const colorCodes = {
Reset: "\x1b[0m", Reset: "\x1b[0m",
Bright: "\x1b[1m", Bright: "\x1b[1m",
@@ -8,7 +26,6 @@ const colorCodes = {
Blink: "\x1b[5m", Blink: "\x1b[5m",
Reverse: "\x1b[7m", Reverse: "\x1b[7m",
Hidden: "\x1b[8m", Hidden: "\x1b[8m",
FgBlack: "\x1b[30m", FgBlack: "\x1b[30m",
FgRed: "\x1b[31m", FgRed: "\x1b[31m",
FgGreen: "\x1b[32m", FgGreen: "\x1b[32m",
@@ -18,7 +35,6 @@ const colorCodes = {
FgCyan: "\x1b[36m", FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m", FgWhite: "\x1b[37m",
FgGray: "\x1b[90m", FgGray: "\x1b[90m",
BgBlack: "\x1b[40m", BgBlack: "\x1b[40m",
BgRed: "\x1b[41m", BgRed: "\x1b[41m",
BgGreen: "\x1b[42m", BgGreen: "\x1b[42m",
@@ -29,35 +45,23 @@ const colorCodes = {
BgWhite: "\x1b[47m", BgWhite: "\x1b[47m",
BgGray: "\x1b[100m", BgGray: "\x1b[100m",
}; };
/**
* @param {string} text
* @param {"red" | "bright"| "dim"| "underscore" | "blink" | "reverse" | "hidden" | "black" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray"} [type]
* @param {boolean} [bg]
* @returns {string}
*/
function colors(text, type, bg) { function colors(text, type, bg) {
let finalText = ``; let finalText = ``;
switch (type) { switch (type) {
case "red": case "red":
finalText += bg ? colorCodes.BgRed : colorCodes.FgRed; finalText += bg ? colorCodes.BgRed : colorCodes.FgRed;
break; break;
case "green": case "green":
finalText += bg ? colorCodes.BgGreen : colorCodes.FgGrBgGreen; finalText += bg ? colorCodes.BgGreen : colorCodes.FgGreen;
break; break;
case "blue": case "blue":
finalText += bg ? colorCodes.BgBlue : colorCodes.FgGrBgBlue; finalText += bg ? colorCodes.BgBlue : colorCodes.FgBlue;
break; break;
default: default:
finalText += colorCodes.Bright; finalText += colorCodes.Bright;
break; break;
} }
finalText += `${text}${colorCodes.Reset}`; finalText += `${text}${colorCodes.Reset}`;
console.log("finalText", finalText); console.log("finalText", finalText);
return finalText; return finalText;
} }
module.exports = colors;
+1
View File
@@ -0,0 +1 @@
export default function delay(time?: number): Promise<unknown>;
+21
View File
@@ -0,0 +1,21 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = delay;
function delay() {
return __awaiter(this, arguments, void 0, function* (time = 200) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, time);
});
});
}
+2
View File
@@ -0,0 +1,2 @@
import { HandleEnvVarsFnParams } from "../types";
export default function handleEnvVars({ json }: HandleEnvVarsFnParams): string;
+42
View File
@@ -0,0 +1,42 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = handleEnvVars;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function handleEnvVars({ json }) {
let newJson = json;
try {
let envVars = Object.assign({}, process.env);
const localEnvFilePath = path_1.default.resolve(process.cwd(), "./.env");
if (fs_1.default.existsSync(localEnvFilePath)) {
const localEnvText = fs_1.default.readFileSync(localEnvFilePath, "utf8");
const localEnvKeyPairArray = localEnvText
.split("\n")
.filter((keyPair) => keyPair &&
keyPair.match(/.{3,}/) &&
!keyPair.match(/^\#/))
.map((keyPair) => keyPair.trim());
localEnvKeyPairArray.forEach((keyPair) => {
let keyPairArray = keyPair.split("=");
const key = keyPairArray.shift();
const value = keyPairArray.join("=");
if (!key)
return;
const newEnvObject = {};
newEnvObject[key] = value;
envVars = Object.assign(Object.assign({}, envVars), newEnvObject);
});
}
for (let key in envVars) {
newJson = newJson.replaceAll(`$${key}`, String(envVars[key]));
}
}
catch (error) {
console.log(`Error replacing Environment variables`, error.message);
return json;
}
return newJson;
}
+2
View File
@@ -0,0 +1,2 @@
import { SyncFoldersFnParams } from "../../types";
export default function watchFolders({ folders, options, }: SyncFoldersFnParams): Promise<void>;
+167
View File
@@ -0,0 +1,167 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = watchFolders;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const util_1 = __importDefault(require("util"));
const child_process_1 = require("child_process");
const delay_1 = __importDefault(require("../../utils/delay"));
const write_sync_config_1 = __importDefault(require("../../utils/write-sync-config"));
const execPromise = util_1.default.promisify(child_process_1.exec);
function watchFolders(_a) {
return __awaiter(this, arguments, void 0, function* ({ folders, options, }) {
let timeout;
const UPDATE_TIMEOUT = 1000;
try {
const dirs = folders;
console.log(`Now handling ${dirs.length} Directories`);
/**
* # Sync Last Updated
*/
const lastUpdatedDir = dirs[0];
const lastUpdatedDirPath = typeof lastUpdatedDir == "string"
? lastUpdatedDir
: lastUpdatedDir.path;
yield sync({ dirPath: lastUpdatedDirPath, dirs, options });
/**
* # Watch Directories
*/
const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval) ? options.interval : UPDATE_TIMEOUT;
for (let i = 0; i < dirs.length; i++) {
const dir = dirs[i];
if (!dir) {
console.log(`Dir: ${dir} doesn't exist`);
continue;
}
const dirPath = typeof dir == "string" ? dir : dir.path;
console.log("global.SYNCING", global.SYNCING);
if ((typeof dir == "string" && !fs_1.default.existsSync(dirPath)) ||
(typeof dir == "object" &&
dir.path &&
!dir.host &&
!fs_1.default.existsSync(dir.path))) {
console.log(`Dir ${dirPath} does not exist. Creating ...`);
try {
const existingDirPath = dirs.find((dr) => {
if (typeof dr == "string")
return fs_1.default.existsSync(dr);
if (!dr.host)
return fs_1.default.existsSync(dr.path); // TODO handle remote
return false;
});
console.log(`Existing Dir to clone: ${existingDirPath}`);
if (!existingDirPath) {
throw new Error("No existing Directories for reference");
}
fs_1.default.mkdirSync(dirPath, {
recursive: true,
});
}
catch (error) {
console.log("Error:", error.message);
throw new Error(`Folder Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`);
}
}
if (typeof dir == "string") {
yield (0, delay_1.default)();
fs_1.default.watch(dirPath, { recursive: true }, (evt, fileName) => {
console.log("Folder Changed", evt, fileName);
if (global.SYNCING) {
console.log("Existing Sync found. Returning ...");
return;
}
clearTimeout(timeout);
timeout = setTimeout(() => {
console.log("Folder Syncing in progress ...");
global.SYNCING = true;
(0, write_sync_config_1.default)({
status: "syncing",
lastSyncedPath: dirPath,
});
sync({ dirPath, dirs, options }).finally(() => {
setTimeout(() => {
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
}, INTERVAL);
});
}, INTERVAL);
});
}
}
}
catch (error) {
console.log("ERROR:", error.message);
process.exit(0);
}
});
}
function sync(_a) {
return __awaiter(this, arguments, void 0, function* ({ options, dirs, dirPath, init }) {
var _b, _c;
const dstDirs = dirs.filter((dr) => {
if (typeof dr == "string")
return dr !== dirPath;
if (dr === null || dr === void 0 ? void 0 : dr.path)
return dr.path !== dirPath;
return false;
});
const allCommandsArr = [];
for (let j = 0; j < dstDirs.length; j++) {
const dstDr = dstDirs[j];
let cmdArray = ["rsync", "-azu", "--inplace"];
if (options === null || options === void 0 ? void 0 : options.delete) {
cmdArray.push("--delete");
}
if ((_b = options === null || options === void 0 ? void 0 : options.include) === null || _b === void 0 ? void 0 : _b[0]) {
options.include.forEach((incl) => {
cmdArray.push(`--include='${incl}'`);
});
}
if ((_c = options === null || options === void 0 ? void 0 : options.exclude) === null || _c === void 0 ? void 0 : _c[0]) {
options.exclude.forEach((excl) => {
cmdArray.push(`--exclude='${excl}'`);
});
}
if (typeof dstDr == "string") {
if (!fs_1.default.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_1.default.normalize(dirPath) + "/", path_1.default.normalize(dstDr) + "/");
}
else if (dstDr.path) {
if (!dstDr.host && !fs_1.default.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_1.default.normalize(dirPath) + "/", `${dstDr.user}@${dstDr.host}:${dstDr.path}/`);
}
else {
cmdArray.push(path_1.default.normalize(dirPath), path_1.default.normalize(dstDr.path));
}
}
allCommandsArr.push(cmdArray);
}
yield Promise.all(allCommandsArr.map((cmdArr) => {
return execPromise(cmdArr.join(" "));
}));
console.log(`${dirPath} Folder Sync Complete. Exiting ...`);
});
}
+8
View File
@@ -0,0 +1,8 @@
import { TurboSyncConfigObject } from "../types";
type Params = {
dirs?: string[];
files?: string[];
config: TurboSyncConfigObject;
};
export default function getLatestSource({ dirs, files, config, }: Params): string | undefined;
export {};
+65
View File
@@ -0,0 +1,65 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getLatestSource;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function getLatestSource({ dirs, files, config, }) {
let latestDir = undefined;
let latestMtime = 0;
const isFiles = files === null || files === void 0 ? void 0 : files[0];
const finalPaths = isFiles ? files : dirs;
if (!finalPaths)
return undefined;
for (const pth of finalPaths) {
try {
const stats = fs_1.default.statSync(pth);
const pathMtime = stats.isDirectory()
? getLatestDirMtime(pth)
: stats.mtimeMs;
if (pathMtime > latestMtime) {
latestMtime = pathMtime;
latestDir = pth;
}
}
catch (error) {
console.error(`Error accessing ${pth}: ${error.message}`);
}
}
if (latestDir) {
if (isDirEmptySync(latestDir))
return undefined;
}
return latestDir;
}
function getLatestDirMtime(dir) {
let latestMtime = 0;
try {
const stats = fs_1.default.statSync(dir);
if (stats.isDirectory()) {
latestMtime = stats.mtimeMs;
const entries = fs_1.default.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const entryPath = path_1.default.join(dir, entry.name);
if (entry.isDirectory()) {
const subMtime = getLatestDirMtime(entryPath);
latestMtime = Math.max(latestMtime, subMtime);
}
else if (entry.isFile()) {
const fileStats = fs_1.default.statSync(entryPath);
latestMtime = Math.max(latestMtime, fileStats.mtimeMs);
}
}
}
}
catch (error) {
console.error(`Error accessing ${dir}: ${error.message}`);
}
return latestMtime;
}
function isDirEmptySync(path) {
const files = fs_1.default.readdirSync(path);
return files.length === 0;
}
+2
View File
@@ -0,0 +1,2 @@
import { SyncFileConfig } from "../types";
export default function getSyncConfig(): SyncFileConfig;
+22
View File
@@ -0,0 +1,22 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getSyncConfig;
const fs_1 = __importDefault(require("fs"));
const grab_dir_names_1 = __importDefault(require("./grab-dir-names"));
function getSyncConfig() {
try {
const { syncConfigFilePath } = (0, grab_dir_names_1.default)();
if (!fs_1.default.existsSync(syncConfigFilePath)) {
fs_1.default.writeFileSync(syncConfigFilePath, JSON.stringify({}), "utf-8");
return {};
}
const syncConfigJSON = fs_1.default.readFileSync(syncConfigFilePath, "utf-8");
return JSON.parse(syncConfigJSON);
}
catch (error) {
return { status: "error" };
}
}
+2
View File
@@ -0,0 +1,2 @@
import { SyncFileConfig } from "../types";
export default function getSyncConfig(): SyncFileConfig;
+22
View File
@@ -0,0 +1,22 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getSyncConfig;
const fs_1 = __importDefault(require("fs"));
const grab_dir_names_1 = __importDefault(require("./grab-dir-names"));
function getSyncConfig() {
try {
const { syncConfigFilePath } = (0, grab_dir_names_1.default)();
if (!fs_1.default.existsSync(syncConfigFilePath)) {
fs_1.default.writeFileSync(syncConfigFilePath, JSON.stringify({}), "utf-8");
return {};
}
const syncConfigJSON = fs_1.default.readFileSync(syncConfigFilePath, "utf-8");
return JSON.parse(syncConfigJSON);
}
catch (error) {
return { status: "error" };
}
}
+5
View File
@@ -0,0 +1,5 @@
export default function grabDirNames(): {
rootDir: string;
configFileName: string;
configFilePath: string;
};
+13
View File
@@ -0,0 +1,13 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = grabDirNames;
const path_1 = __importDefault(require("path"));
function grabDirNames() {
const rootDir = process.cwd();
const configFileName = "turbosync.config.json";
const configFilePath = path_1.default.join(rootDir, configFileName);
return { rootDir, configFileName, configFilePath };
}
+6
View File
@@ -0,0 +1,6 @@
export default function grabDirNames(): {
rootDir: string;
syncConfigFileName: string;
syncConfigFilePath: string;
ignoreFileName: string;
};
+14
View File
@@ -0,0 +1,14 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = grabDirNames;
const path_1 = __importDefault(require("path"));
function grabDirNames() {
const rootDir = process.cwd();
const syncConfigFileName = "__trsyc.json";
const syncConfigFilePath = path_1.default.join(rootDir, syncConfigFileName);
const ignoreFileName = "turbosync.ignore";
return { rootDir, syncConfigFileName, syncConfigFilePath, ignoreFileName };
}
+3
View File
@@ -0,0 +1,3 @@
import { TurboSyncFileObject } from "../types";
export default function fldFileToStrArr(srces?: (string | TurboSyncFileObject)[]): string[] | undefined;
export declare function fldFileToStr(src?: string | TurboSyncFileObject): string | undefined;
+28
View File
@@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = fldFileToStrArr;
exports.fldFileToStr = fldFileToStr;
function fldFileToStrArr(srces) {
if (!srces)
return undefined;
let arr = [];
for (let i = 0; i < srces.length; i++) {
const src = srces[i];
const srcStr = fldFileToStr(src);
if (srcStr) {
arr.push(srcStr);
}
}
return arr;
}
function fldFileToStr(src) {
if (!src)
return undefined;
if (typeof src == "string") {
return src;
}
else if (typeof src == "object" && src.path) {
return src.path;
}
return undefined;
}
+5
View File
@@ -0,0 +1,5 @@
export default function grabDirNames(): {
rootDir: string;
configFileName: string;
configFilePath: string;
};
+13
View File
@@ -0,0 +1,13 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = grabDirNames;
const path_1 = __importDefault(require("path"));
function grabDirNames() {
const rootDir = process.cwd();
const configFileName = "turbosync.config.json";
const configFilePath = path_1.default.join(rootDir, configFileName);
return { rootDir, configFileName, configFilePath };
}
+13
View File
@@ -0,0 +1,13 @@
type SyncTask = (dirPath: string, firstRun?: boolean) => Promise<void>;
export default class SyncScheduler {
private readonly task;
private readonly debounceMs;
private pending;
private timers;
private flushing;
constructor(task: SyncTask, debounceMs: number);
schedule(dirPath: string): void;
enqueue(dirPath: string, firstRun?: boolean): void;
private flush;
}
export {};
+68
View File
@@ -0,0 +1,68 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
class SyncScheduler {
constructor(task, debounceMs) {
this.task = task;
this.debounceMs = debounceMs;
this.pending = new Map();
this.timers = new Map();
this.flushing = false;
}
schedule(dirPath) {
const existing = this.timers.get(dirPath);
if (existing)
clearTimeout(existing);
const timer = setTimeout(() => {
this.timers.delete(dirPath);
this.pending.set(dirPath, false);
void this.flush();
}, this.debounceMs);
this.timers.set(dirPath, timer);
}
enqueue(dirPath, firstRun) {
const existing = this.timers.get(dirPath);
if (existing) {
clearTimeout(existing);
this.timers.delete(dirPath);
}
this.pending.set(dirPath, !!firstRun);
void this.flush();
}
flush() {
return __awaiter(this, void 0, void 0, function* () {
if (this.flushing)
return;
this.flushing = true;
try {
while (this.pending.size > 0) {
const batch = [...this.pending.entries()];
this.pending.clear();
for (const [dirPath, firstRun] of batch) {
try {
yield this.task(dirPath, firstRun);
}
catch (error) {
console.log("ERROR:", error.message);
}
}
}
}
finally {
this.flushing = false;
if (this.pending.size > 0) {
void this.flush();
}
}
});
}
}
exports.default = SyncScheduler;
+2
View File
@@ -0,0 +1,2 @@
import { SyncFoldersSyncFnParams } from "../types";
export default function sync({ options, dirs, dirPath, isFiles, firstRun, }: SyncFoldersSyncFnParams): Promise<void>;
+152
View File
@@ -0,0 +1,152 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = sync;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
const util_1 = __importDefault(require("util"));
const crypto_1 = __importDefault(require("crypto"));
const child_process_1 = require("child_process");
const grab_dir_names_1 = __importDefault(require("./grab-dir-names"));
const grab_folders_files_string_paths_1 = require("./grab-folders-files-string-paths");
const delay_1 = __importDefault(require("./delay"));
const execPromise = util_1.default.promisify(child_process_1.exec);
const LOCK_STALE_MS = 5 * 60 * 1000;
const LOCK_RETRY_MS = 100;
function lockPathFor(dirPath) {
const hash = crypto_1.default
.createHash("sha1")
.update(path_1.default.resolve(dirPath))
.digest("hex")
.slice(0, 16);
return path_1.default.join(os_1.default.tmpdir(), `turbosync-${hash}.lock`);
}
function acquireLock(lockPath) {
return __awaiter(this, void 0, void 0, function* () {
while (true) {
try {
const fd = fs_1.default.openSync(lockPath, "wx");
fs_1.default.writeFileSync(fd, String(process.pid));
fs_1.default.closeSync(fd);
return;
}
catch (error) {
if (error.code !== "EEXIST")
throw error;
try {
const { mtimeMs } = fs_1.default.statSync(lockPath);
if (Date.now() - mtimeMs > LOCK_STALE_MS) {
fs_1.default.unlinkSync(lockPath);
continue;
}
}
catch (_a) {
continue;
}
yield (0, delay_1.default)(LOCK_RETRY_MS);
}
}
});
}
function acquireLocks(lockPaths) {
return __awaiter(this, void 0, void 0, function* () {
for (const lockPath of lockPaths) {
yield acquireLock(lockPath);
}
});
}
function releaseLocks(lockPaths) {
for (const lockPath of lockPaths) {
try {
fs_1.default.unlinkSync(lockPath);
}
catch (_a) { }
}
}
function sync(_a) {
return __awaiter(this, arguments, void 0, function* ({ options, dirs, dirPath, isFiles, firstRun, }) {
var _b, _c;
const dstDirs = dirs.filter((dr) => {
if (typeof dr == "string")
return dr !== dirPath;
if (dr === null || dr === void 0 ? void 0 : dr.path)
return dr.path !== dirPath;
return false;
});
const { ignoreFileName } = (0, grab_dir_names_1.default)();
const rsyncIgnoreFile = path_1.default.join(dirPath, ignoreFileName);
const rsyncTrailingSlash = isFiles ? "" : "/";
const allCommandsArr = [];
for (let j = 0; j < dstDirs.length; j++) {
const dstDr = dstDirs[j];
let cmdArray = ["rsync", firstRun ? "-az" : "-azu", "--inplace"];
if (options === null || options === void 0 ? void 0 : options.delete) {
cmdArray.push("--delete");
}
if ((_b = options === null || options === void 0 ? void 0 : options.include) === null || _b === void 0 ? void 0 : _b[0]) {
options.include.forEach((incl) => {
cmdArray.push(`--include='${incl}'`);
});
}
if (fs_1.default.existsSync(rsyncIgnoreFile)) {
cmdArray.push(`--exclude-from=${rsyncIgnoreFile}`);
}
if ((_c = options === null || options === void 0 ? void 0 : options.exclude) === null || _c === void 0 ? void 0 : _c[0]) {
options.exclude.forEach((excl) => {
cmdArray.push(`--exclude='${excl}'`);
});
}
if (typeof dstDr == "string") {
if (!fs_1.default.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_1.default.normalize(dirPath) + rsyncTrailingSlash, path_1.default.normalize(dstDr) + rsyncTrailingSlash);
}
else if (dstDr.path) {
if (!dstDr.host && !fs_1.default.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_1.default.normalize(dirPath) + rsyncTrailingSlash, `${dstDr.user}@${dstDr.host}:${dstDr.path}${rsyncTrailingSlash}`);
}
else {
cmdArray.push(path_1.default.normalize(dirPath), path_1.default.normalize(dstDr.path));
}
}
allCommandsArr.push(cmdArray);
}
const lockPaths = [dirPath, ...dstDirs.map((dr) => (0, grab_folders_files_string_paths_1.fldFileToStr)(dr))]
.filter((pth) => Boolean(pth))
.map((pth) => lockPathFor(pth))
.sort();
yield acquireLocks(lockPaths);
try {
yield Promise.all(allCommandsArr.map((cmdArr) => {
return execPromise(cmdArr.join(" "));
}));
}
finally {
releaseLocks(lockPaths);
}
console.log(`${dirPath} Folder Sync Complete. Exiting ...`);
});
}
+2
View File
@@ -0,0 +1,2 @@
import { SyncFileConfig } from "../types";
export default function writeSyncConfig(config: SyncFileConfig): boolean;
+18
View File
@@ -0,0 +1,18 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = writeSyncConfig;
const fs_1 = __importDefault(require("fs"));
const grab_dir_names_1 = __importDefault(require("./grab-dir-names"));
function writeSyncConfig(config) {
try {
const { syncConfigFilePath } = (0, grab_dir_names_1.default)();
fs_1.default.writeFileSync(syncConfigFilePath, JSON.stringify(config), "utf-8");
return true;
}
catch (error) {
return false;
}
}
+18 -14
View File
@@ -1,23 +1,29 @@
#! /usr/bin/env node #!/usr/bin/env node
// @ts-check
const fs = require("fs"); import fs from "fs";
const path = require("path"); import path from "path";
const { spawn } = require("child_process"); import { spawn } from "child_process";
const handleEnvVars = require("./utils/env"); import handleEnvVars from "./utils/env";
import { TurboSyncConfigArray } from "./types";
declare global {
var CONFIG_DIR: string;
}
const confFileProvidedPath = process.argv[process.argv.length - 1]; const confFileProvidedPath = process.argv[process.argv.length - 1];
global.CONFIG_DIR = process.cwd();
if (confFileProvidedPath === "--version" || confFileProvidedPath === "-v") { if (confFileProvidedPath === "--version" || confFileProvidedPath === "-v") {
try { try {
const packageJson = fs.readFileSync( const packageJson = fs.readFileSync(
path.resolve(__dirname, "package.json"), path.resolve(__dirname, "../package.json"),
"utf8" "utf-8"
); );
console.log(`Turbo Sync Version: ${JSON.parse(packageJson).version}`); console.log(`Turbo Sync Version: ${JSON.parse(packageJson).version}`);
} catch (error) { } catch (error: any) {
console.log( console.log(
"Turbo Sync Version fetch failed! \nNo Worries, Turbo Sync is still installed properly" `Turbo Sync Version fetch failed! ${error.message} \nNo Worries, Turbo Sync is still installed properly`
); );
} }
@@ -72,12 +78,10 @@ try {
const parsedConfigJSON = handleEnvVars({ json: configJSON }); const parsedConfigJSON = handleEnvVars({ json: configJSON });
/** @type {import(".").TurboSyncConfigArray} */ const configArray = JSON.parse(parsedConfigJSON) as TurboSyncConfigArray;
const configArray = JSON.parse(parsedConfigJSON);
for (let i = 0; i < configArray.length; i++) { for (let i = 0; i < configArray.length; i++) {
const config = configArray[i]; const config = configArray[i];
console.log(`Syncing \`${config.title} ...\``);
const childProcess = spawn( const childProcess = spawn(
"node", "node",
@@ -91,7 +95,7 @@ try {
} }
); );
} }
} catch (error) { } catch (error: any) {
console.log(`Process Error =>`, error.message); console.log(`Process Error =>`, error.message);
process.exit(); process.exit();
} }
-53
View File
@@ -1,53 +0,0 @@
#! /usr/bin/env node
// @ts-check
const { spawn } = require("child_process");
const watchFiles = require("./watch/files");
const watchFolders = require("./watch/folders");
const confFileProvidedJSON = process.argv[process.argv.length - 1];
try {
/** @type {import("..").TurboSyncConfigObject} */
const configFileObject = JSON.parse(confFileProvidedJSON);
console.log(`Running '${configFileObject.title}' ...`);
if (
Array.isArray(configFileObject.files) &&
Array.isArray(configFileObject.folders)
) {
throw new Error("Choose wither `files` or `folders`. Not both");
}
const files = configFileObject?.files;
const firstFile = files?.[0];
const folders = configFileObject?.folders;
const firstFolder = folders?.[0];
const options = configFileObject.options;
if (firstFile && files?.[0]) {
watchFiles({ files, options });
} else if (firstFolder && folders?.[0]) {
watchFolders({ folders, options });
}
} catch (error) {
console.log(error);
process.exit();
}
process.on("exit", (code) => {
if (code == 1) {
const args = process.argv;
const cmd = args.shift();
if (cmd) {
spawn(cmd, args, {
stdio: "inherit",
});
}
} else {
process.exit(0);
}
});
+67
View File
@@ -0,0 +1,67 @@
import watchFiles from "./watch/files";
import watchFolders from "./watch/folders";
import { TurboSyncConfigObject } from "../types";
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.CONFIG_DIR = process.cwd();
try {
const configFileObject: TurboSyncConfigObject =
JSON.parse(confFileProvidedJSON);
const lastUpdated = getLatestSource({
dirs: fldFileToStrArr(configFileObject.folders),
files: fldFileToStrArr(configFileObject.files),
config: configFileObject,
});
console.log(`Running '${configFileObject.title}' ...`);
console.log(`Last Updated Path => '${lastUpdated || "N/A"}' ...`);
if (
Array.isArray(configFileObject.files) &&
Array.isArray(configFileObject.folders)
) {
throw new Error("Choose wither `files` or `folders`. Not both");
}
const files = configFileObject?.files;
const firstFile = files?.[0];
const folders = configFileObject?.folders;
const firstFolder = folders?.[0];
const options = configFileObject.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);
process.exit();
}
-176
View File
@@ -1,176 +0,0 @@
// @ts-check
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const delay = require("../../utils/delay");
/** @type {any} */
let timeout;
const UPDATE_TIMEOUT = 2000;
/**
*
* @param {SyncFilesFnParams} param0
*/
async function watchFiles({ files, options }) {
try {
for (let i = 0; i < files.length; i++) {
const file = files[i];
const filePath =
typeof file == "string" ? file : file?.path ? file.path : null;
const interval = typeof file == "object" ? file.interval : null;
if (!filePath) continue;
if (typeof file == "string" && !fs.existsSync(filePath)) {
try {
const existingFilePath = files.find((fl) => {
if (typeof fl == "string") return fs.existsSync(fl);
if (!fl.host) return fs.existsSync(fl.path); // TODO handle remote
});
if (!existingFilePath) {
throw new Error("No existing Files for reference");
}
const fileDirPath =
typeof existingFilePath == "string"
? existingFilePath
: existingFilePath.path;
if (!fs.existsSync(fileDirPath)) {
fs.mkdirSync(fileDirPath, { recursive: true });
}
fs.writeFileSync(filePath, "");
if (typeof existingFilePath == "string") {
sync({ filePath: existingFilePath, files, options });
} else {
sync({
filePath: existingFilePath.path,
files,
options,
});
}
} catch (error) {
throw new Error(
`File Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`
);
}
}
if (typeof file == "string" && !fs.statSync(filePath).isFile()) {
throw new Error(`'${filePath}' is not a File!`);
}
if (typeof file == "object" && file.host) {
// TODO Handle SSH
} else if (typeof file == "string") {
sync({ options, filePath, files });
await delay();
fs.watchFile(
filePath,
{
interval: interval || 500,
},
(curr, prev) => {
const INTERVAL = options?.interval
? options.interval
: UPDATE_TIMEOUT;
clearTimeout(timeout);
timeout = setTimeout(() => {
sync({ options, filePath, files });
process.exit(1);
}, INTERVAL);
}
);
}
}
} catch (error) {
console.log("ERROR:", error.message);
process.exit(0);
}
}
/**
*
* @param {SyncFilesSyncFnParams} param0
*/
function sync({ options, filePath, files }) {
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",
});
}
}
}
}
module.exports = watchFiles;
+95
View File
@@ -0,0 +1,95 @@
import fs from "fs";
import delay from "../../utils/delay";
import { SyncFilesFnParams } from "../../types";
import sync from "../../utils/sync";
import SyncScheduler from "../../utils/sync-scheduler";
export default async function watchFiles({
files,
options,
}: SyncFilesFnParams) {
const UPDATE_TIMEOUT = 1000;
try {
const INTERVAL = options?.interval ? options.interval : UPDATE_TIMEOUT;
const scheduler = new SyncScheduler(
(filePath, firstRun) =>
sync({
options,
dirPath: filePath,
dirs: files,
isFiles: true,
firstRun,
}),
INTERVAL
);
for (let i = 0; i < files.length; i++) {
const file = files[i];
const filePath =
typeof file == "string" ? file : file?.path ? file.path : null;
const interval = typeof file == "object" ? file.interval : null;
if (!filePath) continue;
if (typeof file == "string" && !fs.existsSync(filePath)) {
try {
const existingFilePath = files.find((fl) => {
if (typeof fl == "string") return fs.existsSync(fl);
if (!fl.host) return fs.existsSync(fl.path); // TODO handle remote
});
if (!existingFilePath) {
throw new Error("No existing Files for reference");
}
const fileDirPath =
typeof existingFilePath == "string"
? existingFilePath
: existingFilePath.path;
if (!fs.existsSync(fileDirPath)) {
fs.mkdirSync(fileDirPath, { recursive: true });
}
fs.writeFileSync(filePath, "");
} catch (error: any) {
throw new Error(
`File Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`
);
}
}
if (typeof file == "string" && !fs.statSync(filePath).isFile()) {
throw new Error(`'${filePath}' is not a File!`);
}
if (typeof file == "object" && file.host) {
// TODO Handle SSH
} else if (typeof file == "string") {
await delay();
fs.watchFile(
filePath,
{
interval: interval || 200,
},
(curr, prev) => {
scheduler.schedule(filePath);
}
);
}
}
const lastUpdatedFile = files[0];
const lastUpdatedFilePath =
typeof lastUpdatedFile == "string"
? lastUpdatedFile
: lastUpdatedFile.path;
scheduler.enqueue(lastUpdatedFilePath, true);
} catch (error: any) {
console.log("ERROR:", error.message);
process.exit(0);
}
}
-183
View File
@@ -1,183 +0,0 @@
// @ts-check
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const delay = require("../../utils/delay");
/** @type {any} */
let timeout;
const UPDATE_TIMEOUT = 2000;
/**
*
* @param {SyncFoldersFnParams} param0
*/
async function watchFolders({ folders, options }) {
try {
const dirs = folders;
console.log(`Now handling ${dirs.length} Directories`);
const INTERVAL = options?.interval ? options.interval : UPDATE_TIMEOUT;
for (let i = 0; i < dirs.length; i++) {
const dir = dirs[i];
if (!dir) {
console.log(`Dir: ${dir} doesn't exist`);
continue;
}
const dirPath = typeof dir == "string" ? dir : dir.path;
if (
(typeof dir == "string" && !fs.existsSync(dirPath)) ||
(typeof dir == "object" &&
dir.path &&
!dir.host &&
!fs.existsSync(dir.path))
) {
console.log(`Dir ${dirPath} does not exist. Creating ...`);
try {
const existingDirPath = dirs.find((dr) => {
if (typeof dr == "string") return fs.existsSync(dr);
if (!dr.host) return fs.existsSync(dr.path); // TODO handle remote
return false;
});
console.log(`Existing Dir to clone: ${existingDirPath}`);
if (!existingDirPath) {
throw new Error(
"No existing Directories for reference"
);
}
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) {
console.log("Error:", error.message);
throw new Error(
`Folder Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`
);
}
}
if (typeof dir == "string") {
sync({ dirPath, dirs, options });
await delay();
fs.watch(dirPath, { recursive: true }, (evt, fileName) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
sync({ dirPath, dirs, options });
process.exit(1);
}, INTERVAL);
});
}
}
} catch (error) {
console.log("ERROR:", error.message);
process.exit(0);
}
}
/**
*
* @param {SyncFoldersSyncFnParams} param0
*/
function sync({ options, dirs, dirPath, init }) {
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?.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",
});
}
}
}
}
module.exports = watchFolders;
+96
View File
@@ -0,0 +1,96 @@
import fs from "fs";
import { SyncFoldersFnParams } from "../../types";
import sync from "../../utils/sync";
import SyncScheduler from "../../utils/sync-scheduler";
export default async function watchFolders({
folders,
options,
}: SyncFoldersFnParams) {
const UPDATE_TIMEOUT = 1000;
try {
const dirs = folders;
console.log(`Now handling ${dirs.length} Directories`);
/**
* # Watch Directories
*/
const INTERVAL = options?.interval ? options.interval : UPDATE_TIMEOUT;
const scheduler = new SyncScheduler(
(dirPath, firstRun) => sync({ dirPath, dirs, options, firstRun }),
INTERVAL
);
for (let i = 0; i < dirs.length; i++) {
const dir = dirs[i];
if (!dir) {
console.log(`Dir: ${dir} doesn't exist`);
continue;
}
const dirPath = typeof dir == "string" ? dir : dir.path;
if (
(typeof dir == "string" && !fs.existsSync(dirPath)) ||
(typeof dir == "object" &&
dir.path &&
!dir.host &&
!fs.existsSync(dir.path))
) {
console.log(`Dir ${dirPath} does not exist. Creating ...`);
try {
const existingDirPath = dirs.find((dr) => {
if (typeof dr == "string") return fs.existsSync(dr);
if (!dr.host) return fs.existsSync(dr.path); // TODO handle remote
return false;
});
console.log(`Existing Dir to clone: ${existingDirPath}`);
if (!existingDirPath) {
throw new Error(
"No existing Directories for reference"
);
}
fs.mkdirSync(dirPath, {
recursive: true,
});
} catch (error: any) {
console.log("Error:", error.message);
throw new Error(
`Folder Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`
);
}
}
if (typeof dir == "string") {
fs.watch(dirPath, { recursive: true }, (evt, fileName) => {
console.log("Folder Changed", evt, fileName);
scheduler.schedule(dirPath);
});
}
}
/**
* # Sync Last Updated
*/
const lastUpdatedDir = dirs[0];
const lastUpdatedDirPath =
typeof lastUpdatedDir == "string"
? lastUpdatedDir
: lastUpdatedDir.path;
scheduler.enqueue(lastUpdatedDirPath, true);
} catch (error: any) {
console.log("ERROR:", error.message);
process.exit(0);
}
}
+10 -6
View File
@@ -1,17 +1,21 @@
{ {
"name": "@moduletrace/turbosync", "name": "@moduletrace/turbosync",
"version": "1.0.0", "version": "1.2.6",
"module": "index.js", "module": "dist/index.js",
"scripts": { "scripts": {
"start": "node index.ts", "start": "node dist/index.js",
"build": "tsc", "build": "tsc",
"compile": "bun build index.js --compile --outfile bin/turbosync", "compile": "bun build index.ts --compile --outfile bin/turbosync",
"dev": "node index.js --watch" "dev": "tsc --watch"
}, },
"bin": { "bin": {
"turbosync": "./index.js" "turbosync": "./dist/index.js"
}, },
"description": "Sync files and directories with ease", "description": "Sync files and directories with ease",
"repository": {
"type": "git",
"url": "https://git.tben.me/Moduletrace/turbo-sync.git"
},
"main": "index.js", "main": "index.js",
"author": "Benjamin Toby", "author": "Benjamin Toby",
"license": "ISC", "license": "ISC",
+3 -1
View File
@@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
tsc
if [ -z "$1" ]; then if [ -z "$1" ]; then
msg="Updates" msg="Updates"
else else
@@ -9,4 +11,4 @@ fi
git add . git add .
git commit -m "$msg" git commit -m "$msg"
git push git push
npm publish bun publish
+11
View File
@@ -0,0 +1,11 @@
#!/bin/bash
if [ -z "$1" ]; then
msg="Updates"
else
msg="$1"
fi
git add .
git commit -m "$msg"
git push
+32
View File
@@ -0,0 +1,32 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"title": {
"type": "string"
},
"options": {
"type": "object",
"properties": {
"delete": {
"type": "boolean"
}
},
"required": ["delete"],
"additionalProperties": false
},
"folders": {
"type": "array",
"items": {
"type": "string",
"format": "uri-reference"
},
"minItems": 1
}
},
"required": ["title", "options", "folders"],
"additionalProperties": false
}
}
+10
View File
@@ -0,0 +1,10 @@
[
{
"title": "Sync Title",
"options": {
"delete": true
},
"folders": ["/home/user/folder-1", "/home/user/folder-2"],
"files": ["/home/user/file-1.txt", "/home/user/file-2.txt"]
}
]
+21
View File
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "ES2015",
"module": "commonjs",
"maxNodeModuleJsDepth": 10,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"incremental": true,
"resolveJsonModule": true,
"jsx": "preserve",
"moduleResolution": "node",
"declaration": true,
"outDir": "dist"
},
"include": ["**/*.ts"],
"exclude": ["node_modules", "dist", "test"]
}
+17 -9
View File
@@ -1,11 +1,9 @@
// @ts-check
export type TurboSyncConfigArray = TurboSyncConfigObject[]; export type TurboSyncConfigArray = TurboSyncConfigObject[];
export interface TurboSyncConfigObject { export interface TurboSyncConfigObject {
title?: string; title?: string;
files?: string[] | TurboSyncFileObject[]; files?: (string | TurboSyncFileObject)[];
folders?: string[] | TurboSyncFileObject[]; folders?: (string | TurboSyncFileObject)[];
options?: TurboSyncOptions; options?: TurboSyncOptions;
} }
@@ -20,32 +18,42 @@ export interface TurboSyncFileObject {
export interface TurboSyncOptions { export interface TurboSyncOptions {
delete?: boolean; delete?: boolean;
exclude?: string[]; exclude?: string[];
include?: string[];
interval?: number; interval?: number;
bootstrapLastEdited?: boolean;
} }
export interface SyncFilesFnParams { export interface SyncFilesFnParams {
files: string[] | TurboSyncFileObject[]; files: (string | TurboSyncFileObject)[];
options: TurboSyncOptions | undefined; options: TurboSyncOptions | undefined;
} }
export interface SyncFilesSyncFnParams { export interface SyncFilesSyncFnParams {
files: string[] | TurboSyncFileObject[]; files: (string | TurboSyncFileObject)[];
options: TurboSyncOptions | undefined; options: TurboSyncOptions | undefined;
filePath: string; filePath: string;
} }
export interface SyncFoldersFnParams { export interface SyncFoldersFnParams {
folders: string[] | TurboSyncFileObject[]; folders: (string | TurboSyncFileObject)[];
options: TurboSyncOptions | undefined; options: TurboSyncOptions | undefined;
} }
export interface SyncFoldersSyncFnParams { export interface SyncFoldersSyncFnParams {
dirs: string[] | TurboSyncFileObject[]; dirs: (string | TurboSyncFileObject)[];
options: TurboSyncOptions | undefined; options: TurboSyncOptions | undefined;
dirPath: string; dirPath: string;
init?: boolean; firstRun?: boolean;
isFiles?: boolean;
} }
export interface HandleEnvVarsFnParams { export interface HandleEnvVarsFnParams {
json: string; json: string;
} }
export const TurboSyncStatuses = ["syncing", "error", "done"] as const;
export type SyncFileConfig = {
status?: (typeof TurboSyncStatuses)[number];
lastSyncedPath?: string;
};
+75
View File
@@ -0,0 +1,75 @@
const colorsArr = [
"red",
"bright",
"dim",
"underscore",
"blink",
"reverse",
"hidden",
"black",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
"gray",
] as const;
const colorCodes = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
FgGray: "\x1b[90m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",
BgGray: "\x1b[100m",
};
export default function colors(
text: string,
type: (typeof colorsArr)[number],
bg: boolean
): string {
let finalText = ``;
switch (type) {
case "red":
finalText += bg ? colorCodes.BgRed : colorCodes.FgRed;
break;
case "green":
finalText += bg ? colorCodes.BgGreen : colorCodes.FgGreen;
break;
case "blue":
finalText += bg ? colorCodes.BgBlue : colorCodes.FgBlue;
break;
default:
finalText += colorCodes.Bright;
break;
}
finalText += `${text}${colorCodes.Reset}`;
console.log("finalText", finalText);
return finalText;
}
-16
View File
@@ -1,16 +0,0 @@
// @ts-check
/**
*
* @param {number} [time]
* @returns
*/
async function delay(time = 500) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, time);
});
}
module.exports = delay;
+7
View File
@@ -0,0 +1,7 @@
export default async function delay(time: number = 200) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, time);
});
}
+8 -13
View File
@@ -1,14 +1,9 @@
// @ts-check import { HandleEnvVarsFnParams } from "../types";
const fs = require("fs"); import fs from "fs";
const path = require("path"); import path from "path";
/** export default function handleEnvVars({ json }: HandleEnvVarsFnParams): string {
*
* @param {HandleEnvVarsFnParams} param0
* @returns {string}
*/
function handleEnvVars({ json }) {
let newJson = json; let newJson = json;
try { try {
let envVars = { ...process.env }; let envVars = { ...process.env };
@@ -31,7 +26,9 @@ function handleEnvVars({ json }) {
const key = keyPairArray.shift(); const key = keyPairArray.shift();
const value = keyPairArray.join("="); const value = keyPairArray.join("=");
const newEnvObject = {}; if (!key) return;
const newEnvObject: { [k: string]: any } = {};
newEnvObject[key] = value; newEnvObject[key] = value;
envVars = { ...envVars, ...newEnvObject }; envVars = { ...envVars, ...newEnvObject };
@@ -41,12 +38,10 @@ function handleEnvVars({ json }) {
for (let key in envVars) { for (let key in envVars) {
newJson = newJson.replaceAll(`$${key}`, String(envVars[key])); newJson = newJson.replaceAll(`$${key}`, String(envVars[key]));
} }
} catch (error) { } catch (error: any) {
console.log(`Error replacing Environment variables`, error.message); console.log(`Error replacing Environment variables`, error.message);
return json; return json;
} }
return newJson; return newJson;
} }
module.exports = handleEnvVars;
+79
View File
@@ -0,0 +1,79 @@
import fs from "fs";
import path from "path";
import { TurboSyncConfigObject } from "../types";
type Params = {
dirs?: string[];
files?: string[];
config: TurboSyncConfigObject;
};
export default function getLatestSource({
dirs,
files,
config,
}: Params): string | undefined {
let latestDir = undefined;
let latestMtime = 0;
const isFiles = files?.[0];
const finalPaths = isFiles ? files : dirs;
if (!finalPaths) return undefined;
for (const pth of finalPaths) {
try {
const stats = fs.statSync(pth);
const pathMtime = stats.isDirectory()
? getLatestDirMtime(pth)
: stats.mtimeMs;
if (pathMtime > latestMtime) {
latestMtime = pathMtime;
latestDir = pth;
}
} catch (error: any) {
console.error(`Error accessing ${pth}: ${error.message}`);
}
}
if (latestDir) {
if (isDirEmptySync(latestDir)) return undefined;
}
return latestDir;
}
function getLatestDirMtime(dir: string) {
let latestMtime = 0;
try {
const stats = fs.statSync(dir);
if (stats.isDirectory()) {
latestMtime = stats.mtimeMs;
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const entryPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
const subMtime = getLatestDirMtime(entryPath);
latestMtime = Math.max(latestMtime, subMtime);
} else if (entry.isFile()) {
const fileStats = fs.statSync(entryPath);
latestMtime = Math.max(latestMtime, fileStats.mtimeMs);
}
}
}
} catch (error: any) {
console.error(`Error accessing ${dir}: ${error.message}`);
}
return latestMtime;
}
function isDirEmptySync(path: string) {
const files = fs.readdirSync(path);
return files.length === 0;
}
+18
View File
@@ -0,0 +1,18 @@
import fs from "fs";
import grabDirNames from "./grab-dir-names";
import { SyncFileConfig } from "../types";
export default function getSyncConfig(): SyncFileConfig {
try {
const { syncConfigFilePath } = grabDirNames();
if (!fs.existsSync(syncConfigFilePath)) {
fs.writeFileSync(syncConfigFilePath, JSON.stringify({}), "utf-8");
return {};
}
const syncConfigJSON = fs.readFileSync(syncConfigFilePath, "utf-8");
return JSON.parse(syncConfigJSON);
} catch (error) {
return { status: "error" };
}
}
+10
View File
@@ -0,0 +1,10 @@
import path from "path";
export default function grabDirNames() {
const rootDir = process.cwd();
const syncConfigFileName = "__trsyc.json";
const syncConfigFilePath = path.join(rootDir, syncConfigFileName);
const ignoreFileName = "turbosync.ignore";
return { rootDir, syncConfigFileName, syncConfigFilePath, ignoreFileName };
}
+31
View File
@@ -0,0 +1,31 @@
import { TurboSyncFileObject } from "../types";
export default function fldFileToStrArr(
srces?: (string | TurboSyncFileObject)[]
) {
if (!srces) return undefined;
let arr: string[] = [];
for (let i = 0; i < srces.length; i++) {
const src = srces[i];
const srcStr = fldFileToStr(src);
if (srcStr) {
arr.push(srcStr);
}
}
return arr;
}
export function fldFileToStr(src?: string | TurboSyncFileObject) {
if (!src) return undefined;
if (typeof src == "string") {
return src;
} else if (typeof src == "object" && src.path) {
return src.path;
}
return undefined;
}
+63
View File
@@ -0,0 +1,63 @@
type SyncTask = (dirPath: string, firstRun?: boolean) => Promise<void>;
export default class SyncScheduler {
private pending = new Map<string, boolean>();
private timers = new Map<string, NodeJS.Timeout>();
private flushing = false;
constructor(
private readonly task: SyncTask,
private readonly debounceMs: number
) {}
schedule(dirPath: string) {
const existing = this.timers.get(dirPath);
if (existing) clearTimeout(existing);
const timer = setTimeout(() => {
this.timers.delete(dirPath);
this.pending.set(dirPath, false);
void this.flush();
}, this.debounceMs);
this.timers.set(dirPath, timer);
}
enqueue(dirPath: string, firstRun?: boolean) {
const existing = this.timers.get(dirPath);
if (existing) {
clearTimeout(existing);
this.timers.delete(dirPath);
}
this.pending.set(dirPath, !!firstRun);
void this.flush();
}
private async flush() {
if (this.flushing) return;
this.flushing = true;
try {
while (this.pending.size > 0) {
const batch = [...this.pending.entries()];
this.pending.clear();
for (const [dirPath, firstRun] of batch) {
try {
await this.task(dirPath, firstRun);
} catch (error: any) {
console.log("ERROR:", error.message);
}
}
}
} finally {
this.flushing = false;
if (this.pending.size > 0) {
void this.flush();
}
}
}
}
+171
View File
@@ -0,0 +1,171 @@
import fs from "fs";
import path from "path";
import os from "os";
import util from "util";
import crypto from "crypto";
import { exec } from "child_process";
import { SyncFoldersSyncFnParams } from "../types";
import grabDirNames from "./grab-dir-names";
import { fldFileToStr } from "./grab-folders-files-string-paths";
import delay from "./delay";
const execPromise = util.promisify(exec);
const LOCK_STALE_MS = 5 * 60 * 1000;
const LOCK_RETRY_MS = 100;
function lockPathFor(dirPath: string) {
const hash = crypto
.createHash("sha1")
.update(path.resolve(dirPath))
.digest("hex")
.slice(0, 16);
return path.join(os.tmpdir(), `turbosync-${hash}.lock`);
}
async function acquireLock(lockPath: string) {
while (true) {
try {
const fd = fs.openSync(lockPath, "wx");
fs.writeFileSync(fd, String(process.pid));
fs.closeSync(fd);
return;
} catch (error: any) {
if (error.code !== "EEXIST") throw error;
try {
const { mtimeMs } = fs.statSync(lockPath);
if (Date.now() - mtimeMs > LOCK_STALE_MS) {
fs.unlinkSync(lockPath);
continue;
}
} catch {
continue;
}
await delay(LOCK_RETRY_MS);
}
}
}
async function acquireLocks(lockPaths: string[]) {
for (const lockPath of lockPaths) {
await acquireLock(lockPath);
}
}
function releaseLocks(lockPaths: string[]) {
for (const lockPath of lockPaths) {
try {
fs.unlinkSync(lockPath);
} catch {}
}
}
export default async function sync({
options,
dirs,
dirPath,
isFiles,
firstRun,
}: SyncFoldersSyncFnParams) {
const dstDirs = dirs.filter((dr) => {
if (typeof dr == "string") return dr !== dirPath;
if (dr?.path) return dr.path !== dirPath;
return false;
});
const { ignoreFileName } = grabDirNames();
const rsyncIgnoreFile = path.join(dirPath, ignoreFileName);
const rsyncTrailingSlash = isFiles ? "" : "/";
const allCommandsArr: string[][] = [];
for (let j = 0; j < dstDirs.length; j++) {
const dstDr = dstDirs[j];
let cmdArray = ["rsync", firstRun ? "-az" : "-azu", "--inplace"];
if (options?.delete) {
cmdArray.push("--delete");
}
if (options?.include?.[0]) {
options.include.forEach((incl) => {
cmdArray.push(`--include='${incl}'`);
});
}
if (fs.existsSync(rsyncIgnoreFile)) {
cmdArray.push(`--exclude-from=${rsyncIgnoreFile}`);
}
if (options?.exclude?.[0]) {
options.exclude.forEach((excl) => {
cmdArray.push(`--exclude='${excl}'`);
});
}
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) + rsyncTrailingSlash,
path.normalize(dstDr) + rsyncTrailingSlash
);
} 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) + rsyncTrailingSlash,
`${dstDr.user}@${dstDr.host}:${dstDr.path}${rsyncTrailingSlash}`
);
} else {
cmdArray.push(
path.normalize(dirPath),
path.normalize(dstDr.path)
);
}
}
allCommandsArr.push(cmdArray);
}
const lockPaths = [dirPath, ...dstDirs.map((dr) => fldFileToStr(dr))]
.filter((pth): pth is string => Boolean(pth))
.map((pth) => lockPathFor(pth))
.sort();
await acquireLocks(lockPaths);
try {
await Promise.all(
allCommandsArr.map((cmdArr) => {
return execPromise(cmdArr.join(" "));
})
);
} finally {
releaseLocks(lockPaths);
}
console.log(`${dirPath} Folder Sync Complete. Exiting ...`);
}
+13
View File
@@ -0,0 +1,13 @@
import fs from "fs";
import grabDirNames from "./grab-dir-names";
import { SyncFileConfig } from "../types";
export default function writeSyncConfig(config: SyncFileConfig): boolean {
try {
const { syncConfigFilePath } = grabDirNames();
fs.writeFileSync(syncConfigFilePath, JSON.stringify(config), "utf-8");
return true;
} catch (error) {
return false;
}
}