refactoring to ts

This commit is contained in:
Benjamin Toby 2025-01-15 05:43:18 +01:00
parent 4c45eba321
commit 222d1a4372
11 changed files with 90 additions and 87 deletions

View File

@ -1,10 +1,10 @@
#! /usr/bin/env node #! /usr/bin/env node
// @ts-check // @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";
const confFileProvidedPath = process.argv[process.argv.length - 1]; const confFileProvidedPath = process.argv[process.argv.length - 1];
@ -91,7 +91,7 @@ try {
} }
); );
} }
} catch (error) { } catch (error: any) {
console.log(`Process Error =>`, error.message); console.log(`Process Error =>`, error.message);
process.exit(); process.exit();
} }

View File

@ -9,7 +9,6 @@ const watchFolders = require("./watch/folders");
const confFileProvidedJSON = process.argv[process.argv.length - 1]; const confFileProvidedJSON = process.argv[process.argv.length - 1];
try { try {
/** @type {import("..").TurboSyncConfigObject} */
const configFileObject = JSON.parse(confFileProvidedJSON); const configFileObject = JSON.parse(confFileProvidedJSON);
console.log(`Running '${configFileObject.title}' ...`); console.log(`Running '${configFileObject.title}' ...`);

View File

@ -1,19 +1,17 @@
// @ts-check // @ts-check
const { execSync } = require("child_process"); import { execSync } from "child_process";
const fs = require("fs"); import fs from "fs";
const path = require("path"); import delay from "../../utils/delay";
const delay = require("../../utils/delay"); import { SyncFilesFnParams, SyncFilesSyncFnParams } from "../../types";
/** @type {any} */ let timeout: any;
let timeout;
const UPDATE_TIMEOUT = 2000; const UPDATE_TIMEOUT = 2000;
/** export default async function watchFiles({
* files,
* @param {SyncFilesFnParams} param0 options,
*/ }: SyncFilesFnParams) {
async function watchFiles({ files, options }) {
try { try {
for (let i = 0; i < files.length; i++) { for (let i = 0; i < files.length; i++) {
const file = files[i]; const file = files[i];
@ -53,7 +51,7 @@ async function watchFiles({ files, options }) {
options, options,
}); });
} }
} catch (error) { } catch (error: any) {
throw new Error( throw new Error(
`File Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}` `File Doesn't exist and couldn't be created. Please check if Directory exists.\nERROR => ${error.message}`
); );
@ -90,17 +88,13 @@ async function watchFiles({ files, options }) {
); );
} }
} }
} catch (error) { } catch (error: any) {
console.log("ERROR:", error.message); console.log("ERROR:", error.message);
process.exit(0); process.exit(0);
} }
} }
/** function sync({ options, filePath, files }: SyncFilesSyncFnParams) {
*
* @param {SyncFilesSyncFnParams} param0
*/
function sync({ options, filePath, files }) {
const destFiles = files.filter((fl) => { const destFiles = files.filter((fl) => {
if (typeof fl == "string") return fl !== filePath; if (typeof fl == "string") return fl !== filePath;
if (fl?.path) return fl.path !== filePath; if (fl?.path) return fl.path !== filePath;
@ -172,5 +166,3 @@ function sync({ options, filePath, files }) {
} }
} }
} }
module.exports = watchFiles;

View File

@ -1,19 +1,18 @@
// @ts-check // @ts-check
const fs = require("fs"); import fs from "fs";
const path = require("path"); import path from "path";
const { execSync } = require("child_process"); import { execSync } from "child_process";
const delay = require("../../utils/delay"); import delay from "../../utils/delay";
import { SyncFoldersFnParams, SyncFoldersSyncFnParams } from "../../types";
/** @type {any} */ let timeout: any;
let timeout;
const UPDATE_TIMEOUT = 2000; const UPDATE_TIMEOUT = 2000;
/** export default async function watchFolders({
* folders,
* @param {SyncFoldersFnParams} param0 options,
*/ }: SyncFoldersFnParams) {
async function watchFolders({ folders, options }) {
try { try {
const dirs = folders; const dirs = folders;
@ -73,7 +72,7 @@ async function watchFolders({ folders, options }) {
init: true, init: true,
}); });
} }
} catch (error) { } catch (error: any) {
console.log("Error:", error.message); console.log("Error:", error.message);
throw new Error( throw new Error(
@ -97,17 +96,13 @@ async function watchFolders({ folders, options }) {
}); });
} }
} }
} catch (error) { } catch (error: any) {
console.log("ERROR:", error.message); console.log("ERROR:", error.message);
process.exit(0); process.exit(0);
} }
} }
/** function sync({ options, dirs, dirPath, init }: SyncFoldersSyncFnParams) {
*
* @param {SyncFoldersSyncFnParams} param0
*/
function sync({ options, dirs, dirPath, init }) {
const dstDirs = dirs.filter((dr) => { const dstDirs = dirs.filter((dr) => {
if (typeof dr == "string") return dr !== dirPath; if (typeof dr == "string") return dr !== dirPath;
if (dr?.path) return dr.path !== dirPath; if (dr?.path) return dr.path !== dirPath;
@ -179,5 +174,3 @@ function sync({ options, dirs, dirPath, init }) {
} }
} }
} }
module.exports = watchFolders;

0
publish.sh Executable file → Normal file
View File

21
tsconfig.json Normal file
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"]
}

36
utils/console-colors.js → utils/console-colors.ts Executable file → Normal file
View File

@ -1,4 +1,20 @@
// @ts-check const colorsArr = [
"red",
"bright",
"dim",
"underscore",
"blink",
"reverse",
"hidden",
"black",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
"gray",
] as const;
const colorCodes = { const colorCodes = {
Reset: "\x1b[0m", Reset: "\x1b[0m",
@ -30,13 +46,11 @@ const colorCodes = {
BgGray: "\x1b[100m", BgGray: "\x1b[100m",
}; };
/** export default function colors(
* @param {string} text text: string,
* @param {"red" | "bright"| "dim"| "underscore" | "blink" | "reverse" | "hidden" | "black" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray"} [type] type: (typeof colorsArr)[number],
* @param {boolean} [bg] bg: boolean
* @returns {string} ): string {
*/
function colors(text, type, bg) {
let finalText = ``; let finalText = ``;
switch (type) { switch (type) {
@ -44,10 +58,10 @@ function colors(text, type, bg) {
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:
@ -59,5 +73,3 @@ function colors(text, type, bg) {
return finalText; return finalText;
} }
module.exports = colors;

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
utils/delay.ts Normal file
View File

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

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;