This commit is contained in:
Benjamin Toby 2025-02-04 13:42:30 +01:00
parent 8a1294a348
commit efcee1bb11
24 changed files with 100 additions and 23 deletions

3
dist/index.d.ts vendored
View File

@ -1,2 +1,5 @@
#!/usr/bin/env node #!/usr/bin/env node
declare global {
var SYNCING: boolean;
}
export {}; export {};

2
dist/index.js vendored
View File

@ -8,6 +8,7 @@ const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path")); const path_1 = __importDefault(require("path"));
const child_process_1 = require("child_process"); const child_process_1 = require("child_process");
const env_1 = __importDefault(require("./utils/env")); const env_1 = __importDefault(require("./utils/env"));
global.SYNCING = false;
const confFileProvidedPath = process.argv[process.argv.length - 1]; const confFileProvidedPath = process.argv[process.argv.length - 1];
if (confFileProvidedPath === "--version" || confFileProvidedPath === "-v") { if (confFileProvidedPath === "--version" || confFileProvidedPath === "-v") {
try { try {
@ -47,7 +48,6 @@ try {
if (!configJSON) if (!configJSON)
throw new Error("Config JSON could not be resolved. Please check your files."); throw new Error("Config JSON could not be resolved. Please check your files.");
const parsedConfigJSON = (0, env_1.default)({ json: configJSON }); const parsedConfigJSON = (0, env_1.default)({ json: configJSON });
/** @type {import(".").TurboSyncConfigArray} */
const configArray = JSON.parse(parsedConfigJSON); 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];

1
dist/lib/sync.js vendored
View File

@ -7,6 +7,7 @@ const child_process_1 = require("child_process");
const files_1 = __importDefault(require("./watch/files")); const files_1 = __importDefault(require("./watch/files"));
const folders_1 = __importDefault(require("./watch/folders")); const folders_1 = __importDefault(require("./watch/folders"));
const confFileProvidedJSON = process.argv[process.argv.length - 1]; const confFileProvidedJSON = process.argv[process.argv.length - 1];
global.SYNCING = false;
try { try {
const configFileObject = JSON.parse(confFileProvidedJSON); const configFileObject = JSON.parse(confFileProvidedJSON);
console.log(`Running '${configFileObject.title}' ...`); console.log(`Running '${configFileObject.title}' ...`);

View File

@ -70,13 +70,16 @@ function watchFiles(_a) {
sync({ options, filePath, files }); sync({ options, filePath, files });
yield (0, delay_1.default)(); yield (0, delay_1.default)();
fs_1.default.watchFile(filePath, { fs_1.default.watchFile(filePath, {
interval: interval || 500, interval: interval || 200,
}, (curr, prev) => { }, (curr, prev) => {
if (global.SYNCING)
return;
const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval) const INTERVAL = (options === null || options === void 0 ? void 0 : options.interval)
? options.interval ? options.interval
: UPDATE_TIMEOUT; : UPDATE_TIMEOUT;
clearTimeout(timeout); clearTimeout(timeout);
timeout = setTimeout(() => { timeout = setTimeout(() => {
global.SYNCING = true;
sync({ options, filePath, files }); sync({ options, filePath, files });
process.exit(1); process.exit(1);
}, INTERVAL); }, INTERVAL);

View File

@ -18,7 +18,7 @@ const path_1 = __importDefault(require("path"));
const child_process_1 = require("child_process"); const child_process_1 = require("child_process");
const delay_1 = __importDefault(require("../../utils/delay")); const delay_1 = __importDefault(require("../../utils/delay"));
let timeout; let timeout;
const UPDATE_TIMEOUT = 2000; const UPDATE_TIMEOUT = 200;
function watchFolders(_a) { function watchFolders(_a) {
return __awaiter(this, arguments, void 0, function* ({ folders, options, }) { return __awaiter(this, arguments, void 0, function* ({ folders, options, }) {
try { try {
@ -79,8 +79,11 @@ function watchFolders(_a) {
sync({ dirPath, dirs, options }); sync({ dirPath, dirs, options });
yield (0, delay_1.default)(); yield (0, delay_1.default)();
fs_1.default.watch(dirPath, { recursive: true }, (evt, fileName) => { fs_1.default.watch(dirPath, { recursive: true }, (evt, fileName) => {
if (global.SYNCING)
return;
clearTimeout(timeout); clearTimeout(timeout);
timeout = setTimeout(() => { timeout = setTimeout(() => {
global.SYNCING = true;
sync({ dirPath, dirs, options }); sync({ dirPath, dirs, options });
process.exit(1); process.exit(1);
}, INTERVAL); }, INTERVAL);

View File

@ -0,0 +1 @@
export {};

View File

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

1
dist/test/test-1/folder-1/test.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export {};

2
dist/test/test-1/folder-1/test.js vendored Normal file
View File

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View File

@ -0,0 +1 @@
export {};

View File

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

1
dist/test/test-1/folder-2/test.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export {};

2
dist/test/test-1/folder-2/test.js vendored Normal file
View File

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

12
dist/types/index.d.ts vendored
View File

@ -1,8 +1,8 @@
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;
} }
export interface TurboSyncFileObject { export interface TurboSyncFileObject {
@ -18,20 +18,20 @@ export interface TurboSyncOptions {
interval?: number; interval?: number;
} }
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; init?: boolean;

2
dist/utils/delay.js vendored
View File

@ -11,7 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.default = delay; exports.default = delay;
function 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) => { return new Promise((resolve) => {
setTimeout(() => { setTimeout(() => {
resolve(true); resolve(true);

View File

@ -4,6 +4,13 @@ import fs from "fs";
import path from "path"; import path from "path";
import { spawn } from "child_process"; import { spawn } from "child_process";
import handleEnvVars from "./utils/env"; 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]; const confFileProvidedPath = process.argv[process.argv.length - 1];
@ -71,8 +78,7 @@ 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];

View File

@ -4,6 +4,8 @@ import watchFolders from "./watch/folders";
const confFileProvidedJSON = process.argv[process.argv.length - 1]; const confFileProvidedJSON = process.argv[process.argv.length - 1];
global.SYNCING = false;
try { try {
const configFileObject = JSON.parse(confFileProvidedJSON); const configFileObject = JSON.parse(confFileProvidedJSON);

View File

@ -69,9 +69,11 @@ export default async function watchFiles({
fs.watchFile( fs.watchFile(
filePath, filePath,
{ {
interval: interval || 500, interval: interval || 200,
}, },
(curr, prev) => { (curr, prev) => {
if (global.SYNCING) return;
const INTERVAL = options?.interval const INTERVAL = options?.interval
? options.interval ? options.interval
: UPDATE_TIMEOUT; : UPDATE_TIMEOUT;
@ -79,6 +81,7 @@ export default async function watchFiles({
clearTimeout(timeout); clearTimeout(timeout);
timeout = setTimeout(() => { timeout = setTimeout(() => {
global.SYNCING = true;
sync({ options, filePath, files }); sync({ options, filePath, files });
process.exit(1); process.exit(1);
}, INTERVAL); }, INTERVAL);

View File

@ -5,7 +5,7 @@ import delay from "../../utils/delay";
import { SyncFoldersFnParams, SyncFoldersSyncFnParams } from "../../types"; import { SyncFoldersFnParams, SyncFoldersSyncFnParams } from "../../types";
let timeout: any; let timeout: any;
const UPDATE_TIMEOUT = 2000; const UPDATE_TIMEOUT = 200;
export default async function watchFolders({ export default async function watchFolders({
folders, folders,
@ -85,9 +85,11 @@ export default async function watchFolders({
await delay(); await delay();
fs.watch(dirPath, { recursive: true }, (evt, fileName) => { fs.watch(dirPath, { recursive: true }, (evt, fileName) => {
if (global.SYNCING) return;
clearTimeout(timeout); clearTimeout(timeout);
timeout = setTimeout(() => { timeout = setTimeout(() => {
global.SYNCING = true;
sync({ dirPath, dirs, options }); sync({ dirPath, dirs, options });
process.exit(1); process.exit(1);
}, INTERVAL); }, INTERVAL);

View File

@ -1,12 +1,12 @@
{ {
"name": "@moduletrace/turbosync", "name": "@moduletrace/turbosync",
"version": "1.0.2", "version": "1.0.4",
"module": "dist/index.js", "module": "dist/index.js",
"scripts": { "scripts": {
"start": "node dist/index.js", "start": "node dist/index.js",
"build": "tsc", "build": "tsc",
"compile": "bun build index.ts --compile --outfile bin/turbosync", "compile": "bun build index.ts --compile --outfile bin/turbosync",
"dev": "node index.js --watch" "dev": "tsc --watch"
}, },
"bin": { "bin": {
"turbosync": "./dist/index.js" "turbosync": "./dist/index.js"

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

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"]
}
]

View File

@ -2,8 +2,8 @@ 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;
} }
@ -22,23 +22,23 @@ export interface TurboSyncOptions {
} }
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; init?: boolean;

View File

@ -1,4 +1,4 @@
export default async function delay(time: number = 500) { export default async function delay(time: number = 200) {
return new Promise((resolve) => { return new Promise((resolve) => {
setTimeout(() => { setTimeout(() => {
resolve(true); resolve(true);