refactoring to ts
This commit is contained in:
Executable → Regular
+24
-12
@@ -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;
|
||||
@@ -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;
|
||||
@@ -0,0 +1,7 @@
|
||||
export default async function delay(time: number = 500) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(true);
|
||||
}, time);
|
||||
});
|
||||
}
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user