From efcee1bb11143bd1136a0fbb91b9fb03a4777c96 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Tue, 4 Feb 2025 13:42:30 +0100 Subject: [PATCH] Updates --- dist/index.d.ts | 3 ++ dist/index.js | 2 +- dist/lib/sync.js | 1 + dist/lib/watch/files.js | 5 +++- dist/lib/watch/folders.js | 5 +++- dist/test/test-1/folder-1/test-file-2.d.ts | 1 + dist/test/test-1/folder-1/test-file-2.js | 2 ++ dist/test/test-1/folder-1/test.d.ts | 1 + dist/test/test-1/folder-1/test.js | 2 ++ dist/test/test-1/folder-2/test-file-2.d.ts | 1 + dist/test/test-1/folder-2/test-file-2.js | 2 ++ dist/test/test-1/folder-2/test.d.ts | 1 + dist/test/test-1/folder-2/test.js | 2 ++ dist/types/index.d.ts | 12 ++++---- dist/utils/delay.js | 2 +- index.ts | 10 +++++-- lib/sync.ts | 2 ++ lib/watch/files.ts | 5 +++- lib/watch/folders.ts | 4 ++- package.json | 4 +-- schema/turbosync.config.schema.json | 32 ++++++++++++++++++++++ schema/turbosync.config.test.json | 10 +++++++ types/index.ts | 12 ++++---- utils/delay.ts | 2 +- 24 files changed, 100 insertions(+), 23 deletions(-) create mode 100644 dist/test/test-1/folder-1/test-file-2.d.ts create mode 100644 dist/test/test-1/folder-1/test-file-2.js create mode 100644 dist/test/test-1/folder-1/test.d.ts create mode 100644 dist/test/test-1/folder-1/test.js create mode 100644 dist/test/test-1/folder-2/test-file-2.d.ts create mode 100644 dist/test/test-1/folder-2/test-file-2.js create mode 100644 dist/test/test-1/folder-2/test.d.ts create mode 100644 dist/test/test-1/folder-2/test.js create mode 100644 schema/turbosync.config.schema.json create mode 100644 schema/turbosync.config.test.json diff --git a/dist/index.d.ts b/dist/index.d.ts index b798801..ddbb6e3 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,2 +1,5 @@ #!/usr/bin/env node +declare global { + var SYNCING: boolean; +} export {}; diff --git a/dist/index.js b/dist/index.js index 3e19113..aaf368b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8,6 +8,7 @@ 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]; if (confFileProvidedPath === "--version" || confFileProvidedPath === "-v") { try { @@ -47,7 +48,6 @@ try { if (!configJSON) throw new Error("Config JSON could not be resolved. Please check your files."); const parsedConfigJSON = (0, env_1.default)({ json: configJSON }); - /** @type {import(".").TurboSyncConfigArray} */ const configArray = JSON.parse(parsedConfigJSON); for (let i = 0; i < configArray.length; i++) { const config = configArray[i]; diff --git a/dist/lib/sync.js b/dist/lib/sync.js index 93e4795..52bf369 100644 --- a/dist/lib/sync.js +++ b/dist/lib/sync.js @@ -7,6 +7,7 @@ const child_process_1 = require("child_process"); const files_1 = __importDefault(require("./watch/files")); const folders_1 = __importDefault(require("./watch/folders")); const confFileProvidedJSON = process.argv[process.argv.length - 1]; +global.SYNCING = false; try { const configFileObject = JSON.parse(confFileProvidedJSON); console.log(`Running '${configFileObject.title}' ...`); diff --git a/dist/lib/watch/files.js b/dist/lib/watch/files.js index 0a9415b..6c6f3b7 100644 --- a/dist/lib/watch/files.js +++ b/dist/lib/watch/files.js @@ -70,13 +70,16 @@ function watchFiles(_a) { sync({ options, filePath, files }); yield (0, delay_1.default)(); fs_1.default.watchFile(filePath, { - interval: interval || 500, + interval: interval || 200, }, (curr, prev) => { + if (global.SYNCING) + return; const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval) ? options.interval : UPDATE_TIMEOUT; clearTimeout(timeout); timeout = setTimeout(() => { + global.SYNCING = true; sync({ options, filePath, files }); process.exit(1); }, INTERVAL); diff --git a/dist/lib/watch/folders.js b/dist/lib/watch/folders.js index 498c071..30019f6 100644 --- a/dist/lib/watch/folders.js +++ b/dist/lib/watch/folders.js @@ -18,7 +18,7 @@ const path_1 = __importDefault(require("path")); const child_process_1 = require("child_process"); const delay_1 = __importDefault(require("../../utils/delay")); let timeout; -const UPDATE_TIMEOUT = 2000; +const UPDATE_TIMEOUT = 200; function watchFolders(_a) { return __awaiter(this, arguments, void 0, function* ({ folders, options, }) { try { @@ -79,8 +79,11 @@ function watchFolders(_a) { sync({ dirPath, dirs, options }); yield (0, delay_1.default)(); fs_1.default.watch(dirPath, { recursive: true }, (evt, fileName) => { + if (global.SYNCING) + return; clearTimeout(timeout); timeout = setTimeout(() => { + global.SYNCING = true; sync({ dirPath, dirs, options }); process.exit(1); }, INTERVAL); diff --git a/dist/test/test-1/folder-1/test-file-2.d.ts b/dist/test/test-1/folder-1/test-file-2.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/test/test-1/folder-1/test-file-2.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/test/test-1/folder-1/test-file-2.js b/dist/test/test-1/folder-1/test-file-2.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/dist/test/test-1/folder-1/test-file-2.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/dist/test/test-1/folder-1/test.d.ts b/dist/test/test-1/folder-1/test.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/test/test-1/folder-1/test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/test/test-1/folder-1/test.js b/dist/test/test-1/folder-1/test.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/dist/test/test-1/folder-1/test.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/dist/test/test-1/folder-2/test-file-2.d.ts b/dist/test/test-1/folder-2/test-file-2.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/test/test-1/folder-2/test-file-2.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/test/test-1/folder-2/test-file-2.js b/dist/test/test-1/folder-2/test-file-2.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/dist/test/test-1/folder-2/test-file-2.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/dist/test/test-1/folder-2/test.d.ts b/dist/test/test-1/folder-2/test.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/test/test-1/folder-2/test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/test/test-1/folder-2/test.js b/dist/test/test-1/folder-2/test.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/dist/test/test-1/folder-2/test.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index 8b82408..662277a 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -1,8 +1,8 @@ export type TurboSyncConfigArray = TurboSyncConfigObject[]; export interface TurboSyncConfigObject { title?: string; - files?: string[] | TurboSyncFileObject[]; - folders?: string[] | TurboSyncFileObject[]; + files?: (string | TurboSyncFileObject)[]; + folders?: (string | TurboSyncFileObject)[]; options?: TurboSyncOptions; } export interface TurboSyncFileObject { @@ -18,20 +18,20 @@ export interface TurboSyncOptions { interval?: number; } export interface SyncFilesFnParams { - files: string[] | TurboSyncFileObject[]; + files: (string | TurboSyncFileObject)[]; options: TurboSyncOptions | undefined; } export interface SyncFilesSyncFnParams { - files: string[] | TurboSyncFileObject[]; + files: (string | TurboSyncFileObject)[]; options: TurboSyncOptions | undefined; filePath: string; } export interface SyncFoldersFnParams { - folders: string[] | TurboSyncFileObject[]; + folders: (string | TurboSyncFileObject)[]; options: TurboSyncOptions | undefined; } export interface SyncFoldersSyncFnParams { - dirs: string[] | TurboSyncFileObject[]; + dirs: (string | TurboSyncFileObject)[]; options: TurboSyncOptions | undefined; dirPath: string; init?: boolean; diff --git a/dist/utils/delay.js b/dist/utils/delay.js index 603e107..fc41bba 100644 --- a/dist/utils/delay.js +++ b/dist/utils/delay.js @@ -11,7 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge Object.defineProperty(exports, "__esModule", { value: true }); exports.default = delay; function delay() { - return __awaiter(this, arguments, void 0, function* (time = 500) { + return __awaiter(this, arguments, void 0, function* (time = 200) { return new Promise((resolve) => { setTimeout(() => { resolve(true); diff --git a/index.ts b/index.ts index 40e85ce..f27aed3 100644 --- a/index.ts +++ b/index.ts @@ -4,6 +4,13 @@ import fs from "fs"; import path from "path"; import { spawn } from "child_process"; import handleEnvVars from "./utils/env"; +import { TurboSyncConfigArray } from "./types"; + +declare global { + var SYNCING: boolean; +} + +global.SYNCING = false; const confFileProvidedPath = process.argv[process.argv.length - 1]; @@ -71,8 +78,7 @@ try { const parsedConfigJSON = handleEnvVars({ json: configJSON }); - /** @type {import(".").TurboSyncConfigArray} */ - const configArray = JSON.parse(parsedConfigJSON); + const configArray = JSON.parse(parsedConfigJSON) as TurboSyncConfigArray; for (let i = 0; i < configArray.length; i++) { const config = configArray[i]; diff --git a/lib/sync.ts b/lib/sync.ts index 4d8912e..dc4ce92 100644 --- a/lib/sync.ts +++ b/lib/sync.ts @@ -4,6 +4,8 @@ import watchFolders from "./watch/folders"; const confFileProvidedJSON = process.argv[process.argv.length - 1]; +global.SYNCING = false; + try { const configFileObject = JSON.parse(confFileProvidedJSON); diff --git a/lib/watch/files.ts b/lib/watch/files.ts index f9fe9dc..1d1e1bc 100644 --- a/lib/watch/files.ts +++ b/lib/watch/files.ts @@ -69,9 +69,11 @@ export default async function watchFiles({ fs.watchFile( filePath, { - interval: interval || 500, + interval: interval || 200, }, (curr, prev) => { + if (global.SYNCING) return; + const INTERVAL = options?.interval ? options.interval : UPDATE_TIMEOUT; @@ -79,6 +81,7 @@ export default async function watchFiles({ clearTimeout(timeout); timeout = setTimeout(() => { + global.SYNCING = true; sync({ options, filePath, files }); process.exit(1); }, INTERVAL); diff --git a/lib/watch/folders.ts b/lib/watch/folders.ts index 84e9094..b14e765 100644 --- a/lib/watch/folders.ts +++ b/lib/watch/folders.ts @@ -5,7 +5,7 @@ import delay from "../../utils/delay"; import { SyncFoldersFnParams, SyncFoldersSyncFnParams } from "../../types"; let timeout: any; -const UPDATE_TIMEOUT = 2000; +const UPDATE_TIMEOUT = 200; export default async function watchFolders({ folders, @@ -85,9 +85,11 @@ export default async function watchFolders({ await delay(); fs.watch(dirPath, { recursive: true }, (evt, fileName) => { + if (global.SYNCING) return; clearTimeout(timeout); timeout = setTimeout(() => { + global.SYNCING = true; sync({ dirPath, dirs, options }); process.exit(1); }, INTERVAL); diff --git a/package.json b/package.json index d999f1f..29e3c2b 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "@moduletrace/turbosync", - "version": "1.0.2", + "version": "1.0.4", "module": "dist/index.js", "scripts": { "start": "node dist/index.js", "build": "tsc", "compile": "bun build index.ts --compile --outfile bin/turbosync", - "dev": "node index.js --watch" + "dev": "tsc --watch" }, "bin": { "turbosync": "./dist/index.js" diff --git a/schema/turbosync.config.schema.json b/schema/turbosync.config.schema.json new file mode 100644 index 0000000..15a8a9d --- /dev/null +++ b/schema/turbosync.config.schema.json @@ -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 + } +} diff --git a/schema/turbosync.config.test.json b/schema/turbosync.config.test.json new file mode 100644 index 0000000..0b7a426 --- /dev/null +++ b/schema/turbosync.config.test.json @@ -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"] + } +] diff --git a/types/index.ts b/types/index.ts index 153d002..a757c59 100644 --- a/types/index.ts +++ b/types/index.ts @@ -2,8 +2,8 @@ export type TurboSyncConfigArray = TurboSyncConfigObject[]; export interface TurboSyncConfigObject { title?: string; - files?: string[] | TurboSyncFileObject[]; - folders?: string[] | TurboSyncFileObject[]; + files?: (string | TurboSyncFileObject)[]; + folders?: (string | TurboSyncFileObject)[]; options?: TurboSyncOptions; } @@ -22,23 +22,23 @@ export interface TurboSyncOptions { } export interface SyncFilesFnParams { - files: string[] | TurboSyncFileObject[]; + files: (string | TurboSyncFileObject)[]; options: TurboSyncOptions | undefined; } export interface SyncFilesSyncFnParams { - files: string[] | TurboSyncFileObject[]; + files: (string | TurboSyncFileObject)[]; options: TurboSyncOptions | undefined; filePath: string; } export interface SyncFoldersFnParams { - folders: string[] | TurboSyncFileObject[]; + folders: (string | TurboSyncFileObject)[]; options: TurboSyncOptions | undefined; } export interface SyncFoldersSyncFnParams { - dirs: string[] | TurboSyncFileObject[]; + dirs: (string | TurboSyncFileObject)[]; options: TurboSyncOptions | undefined; dirPath: string; init?: boolean; diff --git a/utils/delay.ts b/utils/delay.ts index b049c08..1277025 100644 --- a/utils/delay.ts +++ b/utils/delay.ts @@ -1,4 +1,4 @@ -export default async function delay(time: number = 500) { +export default async function delay(time: number = 200) { return new Promise((resolve) => { setTimeout(() => { resolve(true);