Skip to content

Commit

Permalink
break: update color and name
Browse files Browse the repository at this point in the history
  • Loading branch information
vKxni committed Feb 11, 2024
1 parent 5413213 commit e214456
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 37 deletions.
12 changes: 12 additions & 0 deletions dist/functions/envguard.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type KeyOrKeys = string | string[];
type KeywordCheckOptions = {
strict: KeyOrKeys;
};
/**
* Check if specific keywords exist in the .env file and have non-empty values
* @param {string | string[] | KeywordCheckOptions} keywords - Keywords to check, or options object
* @param {boolean} onlyWarnings - If true, log warnings only and do not throw errors
* @throws {Error} - If the .env file is not found or empty (unless onlyWarnings is true)
*/
export declare function envguard(keywords: KeyOrKeys | KeywordCheckOptions, onlyWarnings?: boolean): void;
export {};
77 changes: 77 additions & 0 deletions dist/functions/envguard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.envguard = void 0;
const fs_1 = __importDefault(require("fs"));
const dotenv_1 = __importDefault(require("dotenv"));
const colors_1 = __importDefault(require("colors"));
/**
* Type guard to check if an object is of type KeywordCheckOptions
* @param {any} obj - Object to be checked
* @returns {boolean} - True if the object is of type KeywordCheckOptions
*/
function isKeywordCheckOptions(obj) {
return typeof obj === 'object' && obj !== null && 'strict' in obj;
}
/**
* Check if specific keywords exist in the .env file and have non-empty values
* @param {string | string[] | KeywordCheckOptions} keywords - Keywords to check, or options object
* @param {boolean} onlyWarnings - If true, log warnings only and do not throw errors
* @throws {Error} - If the .env file is not found or empty (unless onlyWarnings is true)
*/
function envguard(keywords, onlyWarnings = false) {
// Check if the .env file exists
if (!fs_1.default.existsSync('.env')) {
throw new Error(colors_1.default.red('.env file not found'));
}
// Check if the .env file is not empty
const fileContent = fs_1.default.readFileSync('.env', 'utf-8');
if (fileContent.trim() === '') {
throw new Error(colors_1.default.red('.env file is empty'));
}
// Parse the .env file using dotenv
const envConfig = dotenv_1.default.parse(fileContent);
// Extract keys to check and strict mode flag
const { keysToCheck, strictMode } = extractKeysToCheck(keywords);
// Check if keywords exist and have non-empty values
const missingKeys = [];
keysToCheck.forEach((key) => {
if (!envConfig[key]) {
missingKeys.push(key);
}
});
if (missingKeys.length > 0) {
if (strictMode && !onlyWarnings) {
const errorMessage = Array.isArray(keywords)
? colors_1.default.red(`Required keys missing or empty in .env file: ${missingKeys.join(', ')}`)
: colors_1.default.red(`Required key missing or empty in .env file: ${missingKeys.join(', ')}`);
throw new Error(errorMessage);
}
else {
const warningMessage = Array.isArray(keywords)
? colors_1.default.magenta(`Warning: The following keys are missing or empty in .env file: ${missingKeys.join(', ')}`)
: colors_1.default.magenta(`Warning: The key is missing or empty in .env file: ${missingKeys.join(', ')}`);
console.log(warningMessage);
}
}
else {
console.log(colors_1.default.green('Checks passed: All required keys exist and have non-empty values'));
}
}
exports.envguard = envguard;
// Extract keys to check and strict mode flag
function extractKeysToCheck(keywords) {
if (typeof keywords === 'string') {
return { keysToCheck: [keywords], strictMode: false };
}
if (Array.isArray(keywords)) {
return { keysToCheck: keywords, strictMode: false };
}
if (isKeywordCheckOptions(keywords)) {
const keysToCheck = Array.isArray(keywords.strict) ? keywords.strict : [keywords.strict];
return { keysToCheck, strictMode: true };
}
throw new Error(colors_1.default.red('Invalid argument for keywordCheck. Expected string, string[], or KeywordCheckOptions object.\n\nExample: keywordCheck(\'API_KEY\') or keywordCheck([\'API_KEY\', \'API_SECRET\']) or keywordCheck({ strict: [\'API_KEY\', \'API_SECRET\'] })'));
}
16 changes: 1 addition & 15 deletions dist/functions/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class L {
logToConsole(level, formattedMessage) {
switch (level) {
case LogLevel.SOFT:
console.log(colors_1.default.magenta(formattedMessage));
console.log(colors_1.default.yellow(formattedMessage));
break;
case LogLevel.ERROR:
console.log(colors_1.default.red(formattedMessage));
Expand Down Expand Up @@ -105,17 +105,3 @@ class L {
}
}
exports.L = L;
// import { L, LogLevel } from 'x';
// export * from './functions/env';
// export * from './functions/keyword';
// export * from './functions/logger';
// const logger = new L({
// logLevel: LogLevel.SOFT,
// enableTimestamp: true,
// timeZone: 'Europe/Berlin',
// dateFormat: 'en-US',
// showLogLevel: true,
// enableConsole: true,
// writeToFile: true,
// filePath: 'AUTO',
// });
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './functions/env';
export * from './functions/keyword';
export * from './functions/envguard';
export * from './functions/logger';
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./functions/env"), exports);
__exportStar(require("./functions/keyword"), exports);
__exportStar(require("./functions/envguard"), exports);
__exportStar(require("./functions/logger"), exports);
2 changes: 1 addition & 1 deletion src/functions/keyword.ts → src/functions/envguard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function isKeywordCheckOptions(obj: unknown): obj is KeywordCheckOptions {
* @param {boolean} onlyWarnings - If true, log warnings only and do not throw errors
* @throws {Error} - If the .env file is not found or empty (unless onlyWarnings is true)
*/
export function k(keywords: KeyOrKeys | KeywordCheckOptions, onlyWarnings: boolean = false): void {
export function envguard(keywords: KeyOrKeys | KeywordCheckOptions, onlyWarnings: boolean = false): void {
// Check if the .env file exists
if (!fs.existsSync('.env')) {
throw new Error(colors.red('.env file not found'));
Expand Down
19 changes: 1 addition & 18 deletions src/functions/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class L {
private logToConsole(level: LogLevel, formattedMessage: string): void {
switch (level) {
case LogLevel.SOFT:
console.log(colors.magenta(formattedMessage));
console.log(colors.yellow(formattedMessage));
break;
case LogLevel.ERROR:
console.log(colors.red(formattedMessage));
Expand Down Expand Up @@ -125,20 +125,3 @@ export class L {
this.logMessage(this.options.logLevel, message);
}
}

// import { L, LogLevel } from 'x';

// export * from './functions/env';
// export * from './functions/keyword';
// export * from './functions/logger';

// const logger = new L({
// logLevel: LogLevel.SOFT,
// enableTimestamp: true,
// timeZone: 'Europe/Berlin',
// dateFormat: 'en-US',
// showLogLevel: true,
// enableConsole: true,
// writeToFile: true,
// filePath: 'AUTO',
// });
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './functions/env';
export * from './functions/keyword';
export * from './functions/envguard';
export * from './functions/logger';

0 comments on commit e214456

Please sign in to comment.