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

16
bun.lock Normal file
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=="],
}
}

4
dist/index.d.ts vendored
View File

@ -1,5 +1,7 @@
#!/usr/bin/env node
declare global {
var SYNCING: boolean;
var SYNC_SUCCESS_EXIT_CODE: number;
var CONFIG_DIR: string;
var SYNCING_FILE: string;
}
export {};

6
dist/index.js vendored
View File

@ -8,8 +8,12 @@ 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"));
global.SYNCING = false;
const confFileProvidedPath = process.argv[process.argv.length - 1];
global.CONFIG_DIR = process.cwd();
global.SYNCING_FILE = path_1.default.join(global.CONFIG_DIR, "syncing.txt");
if (fs_1.default.existsSync(global.SYNCING_FILE)) {
fs_1.default.unlinkSync(global.SYNCING_FILE);
}
if (confFileProvidedPath === "--version" || confFileProvidedPath === "-v") {
try {
const packageJson = fs_1.default.readFileSync(path_1.default.resolve(__dirname, "../package.json"), "utf-8");

12
dist/lib/sync.js vendored
View File

@ -3,12 +3,19 @@ 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 child_process_1 = require("child_process");
const files_1 = __importDefault(require("./watch/files"));
const folders_1 = __importDefault(require("./watch/folders"));
const path_1 = __importDefault(require("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_1.default.join(global.CONFIG_DIR, "syncing.txt");
try {
if (fs_1.default.existsSync(global.SYNCING_FILE)) {
process.exit(0);
}
const configFileObject = JSON.parse(confFileProvidedJSON);
console.log(`Running '${configFileObject.title}' ...`);
if (Array.isArray(configFileObject.files) &&
@ -32,7 +39,8 @@ catch (error) {
process.exit();
}
process.on("exit", (code) => {
if (code == 1) {
if (code == global.SYNC_SUCCESS_EXIT_CODE) {
fs_1.default.unlinkSync(global.SYNCING_FILE);
const args = process.argv;
const cmd = args.shift();
if (cmd) {

View File

@ -72,16 +72,16 @@ function watchFiles(_a) {
fs_1.default.watchFile(filePath, {
interval: interval || 200,
}, (curr, prev) => {
if (global.SYNCING)
if (fs_1.default.existsSync(global.SYNCING_FILE))
return;
const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval)
? options.interval
: UPDATE_TIMEOUT;
clearTimeout(timeout);
timeout = setTimeout(() => {
global.SYNCING = true;
fs_1.default.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);
});
}

View File

@ -79,13 +79,18 @@ function watchFolders(_a) {
sync({ dirPath, dirs, options });
yield (0, delay_1.default)();
fs_1.default.watch(dirPath, { recursive: true }, (evt, fileName) => {
if (global.SYNCING)
console.log("Folder Changed", evt, fileName);
if (fs_1.default.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_1.default.writeFileSync(global.SYNCING_FILE, `SYNCING Folder: evt:${evt} :: fileName:${fileName}`);
sync({ dirPath, dirs, options });
process.exit(1);
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
}, INTERVAL);
});
}
@ -159,4 +164,5 @@ function sync({ options, dirs, dirPath, init }) {
}
}
}
console.log("Folder Sync Complete. Exiting ...");
}

View File

@ -0,0 +1 @@
declare var l: string;

View File

@ -0,0 +1,2 @@
"use strict";
var l = "Hi There #8";

View File

@ -1,3 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testVar = 35;
const testVar = 37;

View File

@ -1,3 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testVar = 5;

View File

@ -0,0 +1 @@
declare var l: string;

View File

@ -0,0 +1,2 @@
"use strict";
var l = "Hi There #7";

View File

@ -1,3 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testVar = 35;
const testVar = 37;

View File

@ -7,15 +7,20 @@ import handleEnvVars from "./utils/env";
import { TurboSyncConfigArray } from "./types";
declare global {
var SYNCING: boolean;
var SYNC_SUCCESS_EXIT_CODE: number;
var CONFIG_DIR: string;
var SYNCING_FILE: string;
}
global.SYNCING = false;
global.SYNC_SUCCESS_EXIT_CODE = 32;
const confFileProvidedPath = process.argv[process.argv.length - 1];
global.CONFIG_DIR = process.cwd();
global.SYNCING_FILE = path.join(global.CONFIG_DIR, "syncing.txt");
if (fs.existsSync(global.SYNCING_FILE)) {
fs.unlinkSync(global.SYNCING_FILE);
}
if (confFileProvidedPath === "--version" || confFileProvidedPath === "-v") {
try {
const packageJson = fs.readFileSync(

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) {

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);
}
);

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 ...");
}

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/turbosync",
"version": "1.0.8",
"version": "1.0.9",
"module": "dist/index.js",
"scripts": {
"start": "node dist/index.js",