diff --git a/index.js b/index.ts similarity index 94% rename from index.js rename to index.ts index 67463f5..7456005 100644 --- a/index.js +++ b/index.ts @@ -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(); } diff --git a/lib/sync.js b/lib/sync.ts similarity index 95% rename from lib/sync.js rename to lib/sync.ts index 8b22bfd..af7dd58 100644 --- a/lib/sync.js +++ b/lib/sync.ts @@ -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}' ...`); diff --git a/lib/watch/files.js b/lib/watch/files.ts similarity index 91% rename from lib/watch/files.js rename to lib/watch/files.ts index c5420ab..87cac7d 100644 --- a/lib/watch/files.js +++ b/lib/watch/files.ts @@ -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; diff --git a/lib/watch/folders.js b/lib/watch/folders.ts similarity index 91% rename from lib/watch/folders.js rename to lib/watch/folders.ts index 5c4af62..ade0905 100644 --- a/lib/watch/folders.js +++ b/lib/watch/folders.ts @@ -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; diff --git a/publish.sh b/publish.sh old mode 100755 new mode 100644 diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..c9d846c --- /dev/null +++ b/tsconfig.json @@ -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"] +} diff --git a/index.d.ts b/types/index.ts similarity index 100% rename from index.d.ts rename to types/index.ts diff --git a/utils/console-colors.js b/utils/console-colors.ts old mode 100755 new mode 100644 similarity index 69% rename from utils/console-colors.js rename to utils/console-colors.ts index 03697b8..948c2b4 --- a/utils/console-colors.js +++ b/utils/console-colors.ts @@ -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; diff --git a/utils/delay.js b/utils/delay.js deleted file mode 100644 index 183092b..0000000 --- a/utils/delay.js +++ /dev/null @@ -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; diff --git a/utils/delay.ts b/utils/delay.ts new file mode 100644 index 0000000..b049c08 --- /dev/null +++ b/utils/delay.ts @@ -0,0 +1,7 @@ +export default async function delay(time: number = 500) { + return new Promise((resolve) => { + setTimeout(() => { + resolve(true); + }, time); + }); +} diff --git a/utils/env.js b/utils/env.ts similarity index 80% rename from utils/env.js rename to utils/env.ts index 8d80717..d46e4bf 100644 --- a/utils/env.js +++ b/utils/env.ts @@ -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;