diff --git a/bun.lock b/bun.lock new file mode 100644 index 0000000..e5c3c25 --- /dev/null +++ b/bun.lock @@ -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=="], + } +} diff --git a/dist/index.d.ts b/dist/index.d.ts index ddbb6e3..16668e3 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -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 {}; diff --git a/dist/index.js b/dist/index.js index c4ce46b..ffeed3e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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"); diff --git a/dist/lib/sync.js b/dist/lib/sync.js index 52bf369..4f6f744 100644 --- a/dist/lib/sync.js +++ b/dist/lib/sync.js @@ -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) { diff --git a/dist/lib/watch/files.js b/dist/lib/watch/files.js index e57a453..48b11f5 100644 --- a/dist/lib/watch/files.js +++ b/dist/lib/watch/files.js @@ -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); }); } diff --git a/dist/lib/watch/folders.js b/dist/lib/watch/folders.js index f957037..71aeb90 100644 --- a/dist/lib/watch/folders.js +++ b/dist/lib/watch/folders.js @@ -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 ..."); } diff --git a/dist/test/test-1/folder-1/test-folder/test-file.d.ts b/dist/test/test-1/folder-1/test-folder/test-file.d.ts new file mode 100644 index 0000000..817c77f --- /dev/null +++ b/dist/test/test-1/folder-1/test-folder/test-file.d.ts @@ -0,0 +1 @@ +declare var l: string; diff --git a/dist/test/test-1/folder-1/test-folder/test-file.js b/dist/test/test-1/folder-1/test-folder/test-file.js new file mode 100644 index 0000000..5061147 --- /dev/null +++ b/dist/test/test-1/folder-1/test-folder/test-file.js @@ -0,0 +1,2 @@ +"use strict"; +var l = "Hi There #8"; diff --git a/dist/test/test-1/folder-1/test.js b/dist/test/test-1/folder-1/test.js index b9efffe..ce2ca67 100644 --- a/dist/test/test-1/folder-1/test.js +++ b/dist/test/test-1/folder-1/test.js @@ -1,3 +1,3 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -const testVar = 35; +const testVar = 37; diff --git a/dist/test/test-1/folder-2/test-file-2.js b/dist/test/test-1/folder-2/test-file-2.js index 22ef2f1..c8ad2e5 100644 --- a/dist/test/test-1/folder-2/test-file-2.js +++ b/dist/test/test-1/folder-2/test-file-2.js @@ -1,3 +1,2 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -const testVar = 5; diff --git a/dist/test/test-1/folder-2/test-folder/test-file.d.ts b/dist/test/test-1/folder-2/test-folder/test-file.d.ts new file mode 100644 index 0000000..817c77f --- /dev/null +++ b/dist/test/test-1/folder-2/test-folder/test-file.d.ts @@ -0,0 +1 @@ +declare var l: string; diff --git a/dist/test/test-1/folder-2/test-folder/test-file.js b/dist/test/test-1/folder-2/test-folder/test-file.js new file mode 100644 index 0000000..fe5d91b --- /dev/null +++ b/dist/test/test-1/folder-2/test-folder/test-file.js @@ -0,0 +1,2 @@ +"use strict"; +var l = "Hi There #7"; diff --git a/dist/test/test-1/folder-2/test.js b/dist/test/test-1/folder-2/test.js index b9efffe..ce2ca67 100644 --- a/dist/test/test-1/folder-2/test.js +++ b/dist/test/test-1/folder-2/test.js @@ -1,3 +1,3 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -const testVar = 35; +const testVar = 37; diff --git a/index.ts b/index.ts index 03ddf41..1636595 100644 --- a/index.ts +++ b/index.ts @@ -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( diff --git a/lib/sync.ts b/lib/sync.ts index 08efe08..3f8acf4 100644 --- a/lib/sync.ts +++ b/lib/sync.ts @@ -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) { diff --git a/lib/watch/files.ts b/lib/watch/files.ts index 6e462a0..f5e2125 100644 --- a/lib/watch/files.ts +++ b/lib/watch/files.ts @@ -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); } ); diff --git a/lib/watch/folders.ts b/lib/watch/folders.ts index 17ff425..3df9e49 100644 --- a/lib/watch/folders.ts +++ b/lib/watch/folders.ts @@ -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 ..."); } diff --git a/package.json b/package.json index 57e45e7..ba2f758 100644 --- a/package.json +++ b/package.json @@ -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",