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
// @ts-check
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
const handleEnvVars = require("./utils/env");
import fs from "fs";
import path from "path";
import { spawn } from "child_process";
import handleEnvVars from "./utils/env";
const confFileProvidedPath = process.argv[process.argv.length - 1];
@ -91,7 +91,7 @@ try {
}
);
}
} catch (error) {
} catch (error: any) {
console.log(`Process Error =>`, error.message);
process.exit();
}

View File

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

View File

@ -1,19 +1,17 @@
// @ts-check
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const delay = require("../../utils/delay");
import { execSync } from "child_process";
import fs from "fs";
import delay from "../../utils/delay";
import { SyncFilesFnParams, SyncFilesSyncFnParams } from "../../types";
/** @type {any} */
let timeout;
let timeout: any;
const UPDATE_TIMEOUT = 2000;
/**
*
* @param {SyncFilesFnParams} param0
*/
async function watchFiles({ files, options }) {
export default async function watchFiles({
files,
options,
}: SyncFilesFnParams) {
try {
for (let i = 0; i < files.length; i++) {
const file = files[i];
@ -53,7 +51,7 @@ async function watchFiles({ files, options }) {
options,
});
}
} catch (error) {
} catch (error: any) {
throw new Error(
`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);
process.exit(0);
}
}
/**
*
* @param {SyncFilesSyncFnParams} param0
*/
function sync({ options, filePath, files }) {
function sync({ options, filePath, files }: SyncFilesSyncFnParams) {
const destFiles = files.filter((fl) => {
if (typeof fl == "string") return fl !== 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
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const delay = require("../../utils/delay");
import fs from "fs";
import path from "path";
import { execSync } from "child_process";
import delay from "../../utils/delay";
import { SyncFoldersFnParams, SyncFoldersSyncFnParams } from "../../types";
/** @type {any} */
let timeout;
let timeout: any;
const UPDATE_TIMEOUT = 2000;
/**
*
* @param {SyncFoldersFnParams} param0
*/
async function watchFolders({ folders, options }) {
export default async function watchFolders({
folders,
options,
}: SyncFoldersFnParams) {
try {
const dirs = folders;
@ -73,7 +72,7 @@ async function watchFolders({ folders, options }) {
init: true,
});
}
} catch (error) {
} catch (error: any) {
console.log("Error:", error.message);
throw new Error(
@ -97,17 +96,13 @@ async function watchFolders({ folders, options }) {
});
}
}
} catch (error) {
} catch (error: any) {
console.log("ERROR:", error.message);
process.exit(0);
}
}
/**
*
* @param {SyncFoldersSyncFnParams} param0
*/
function sync({ options, dirs, dirPath, init }) {
function sync({ options, dirs, dirPath, init }: SyncFoldersSyncFnParams) {
const dstDirs = dirs.filter((dr) => {
if (typeof dr == "string") return dr !== 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 = {
Reset: "\x1b[0m",
@ -30,13 +46,11 @@ const colorCodes = {
BgGray: "\x1b[100m",
};
/**
* @param {string} text
* @param {"red" | "bright"| "dim"| "underscore" | "blink" | "reverse" | "hidden" | "black" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray"} [type]
* @param {boolean} [bg]
* @returns {string}
*/
function colors(text, type, bg) {
export default function colors(
text: string,
type: (typeof colorsArr)[number],
bg: boolean
): string {
let finalText = ``;
switch (type) {
@ -44,10 +58,10 @@ function colors(text, type, bg) {
finalText += bg ? colorCodes.BgRed : colorCodes.FgRed;
break;
case "green":
finalText += bg ? colorCodes.BgGreen : colorCodes.FgGrBgGreen;
finalText += bg ? colorCodes.BgGreen : colorCodes.FgGreen;
break;
case "blue":
finalText += bg ? colorCodes.BgBlue : colorCodes.FgGrBgBlue;
finalText += bg ? colorCodes.BgBlue : colorCodes.FgBlue;
break;
default:
@ -59,5 +73,3 @@ function colors(text, type, bg) {
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");
const path = require("path");
import fs from "fs";
import path from "path";
/**
*
* @param {HandleEnvVarsFnParams} param0
* @returns {string}
*/
function handleEnvVars({ json }) {
export default function handleEnvVars({ json }: HandleEnvVarsFnParams): string {
let newJson = json;
try {
let envVars = { ...process.env };
@ -31,7 +26,9 @@ function handleEnvVars({ json }) {
const key = keyPairArray.shift();
const value = keyPairArray.join("=");
const newEnvObject = {};
if (!key) return;
const newEnvObject: { [k: string]: any } = {};
newEnvObject[key] = value;
envVars = { ...envVars, ...newEnvObject };
@ -41,12 +38,10 @@ function handleEnvVars({ json }) {
for (let key in envVars) {
newJson = newJson.replaceAll(`$${key}`, String(envVars[key]));
}
} catch (error) {
} catch (error: any) {
console.log(`Error replacing Environment variables`, error.message);
return json;
}
return newJson;
}
module.exports = handleEnvVars;