From 014795bdc646881f17fd0042009fdeb7008db0ed Mon Sep 17 00:00:00 2001 From: VigneshKna Date: Sun, 29 Sep 2024 22:09:50 +0530 Subject: [PATCH] chore(webpack):added webpack modules --- src/js/config.ini | 2 +- .../how2validate/handler/validator_handler.ts | 6 +-- src/js/how2validate/index.ts | 25 +++-------- src/js/how2validate/utility/log_utility.ts | 2 +- src/js/how2validate/utility/tool_utility.ts | 45 +++++++++---------- .../validators/npm/npm_access_token.ts | 4 +- .../validators/snyk/snyk_auth_key.ts | 4 +- .../validators/sonarcloud/sonarcloud_token.ts | 4 +- src/js/jsr.json | 2 +- src/js/package.json | 15 ++++--- src/js/webpack.config.cjs | 31 +++++++++++++ 11 files changed, 80 insertions(+), 60 deletions(-) create mode 100644 src/js/webpack.config.cjs diff --git a/src/js/config.ini b/src/js/config.ini index 6fa1fc3..b131ba0 100644 --- a/src/js/config.ini +++ b/src/js/config.ini @@ -1,6 +1,6 @@ [DEFAULT] package_name = @how2validate/how2validate -version = 0.0.1-beta.17 +version = 0.0.1-beta.20 [SECRET] secret_active = Active diff --git a/src/js/how2validate/handler/validator_handler.ts b/src/js/how2validate/handler/validator_handler.ts index bbe14df..0b3630a 100644 --- a/src/js/how2validate/handler/validator_handler.ts +++ b/src/js/how2validate/handler/validator_handler.ts @@ -1,6 +1,6 @@ -import { validateSnykAuthKey } from "../validators/snyk/snyk_auth_key.js"; // Import the Snyk authentication key validator -import { validateSonarcloudToken } from "../validators/sonarcloud/sonarcloud_token.js"; // Import the Sonarcloud token validator -import { validateNpmAccessToken } from "../validators/npm/npm_access_token.js"; // Import the NPM access token validator +import { validateSnykAuthKey } from "../validators/snyk/snyk_auth_key"; // Import the Snyk authentication key validator +import { validateSonarcloudToken } from "../validators/sonarcloud/sonarcloud_token"; // Import the Sonarcloud token validator +import { validateNpmAccessToken } from "../validators/npm/npm_access_token"; // Import the NPM access token validator // Define a type for the validator function signature type ValidatorFunction = ( diff --git a/src/js/how2validate/index.ts b/src/js/how2validate/index.ts index 8fbd61d..23d70c1 100755 --- a/src/js/how2validate/index.ts +++ b/src/js/how2validate/index.ts @@ -1,31 +1,17 @@ -// index.ts - -/** - * @module How2Validate Tool - * @description - * A Command-Line Interface (CLI) tool designed to validate various types of secrets across different services. - * It leverages multiple secret providers and services to ensure the authenticity and status of secrets. - * - * @requires commander - * @requires ./utility/config_utility.js - * @requires ./utility/tool_utility.js - * @requires ./handler/validator_handler.js - */ - import { Command } from "commander"; // Importing Commander for building CLI applications import { getActiveSecretStatus, getInactiveSecretStatus, getVersion, -} from "./utility/config_utility.js"; // Importing configuration utility functions +} from "./utility/config_utility"; // Importing configuration utility functions import { formatString, getSecretProviders, getSecretscope, getSecretServices, validateChoice, -} from "./utility/tool_utility.js"; // Importing utility functions for secret validation -import { validatorHandleService } from "./handler/validator_handler.js"; // Importing the validation handler +} from "./utility/tool_utility"; // Importing utility functions for secret validation +import { validatorHandleService } from "./handler/validator_handler"; // Importing the validation handler import { fileURLToPath } from "url"; /** @@ -165,7 +151,7 @@ async function main(): Promise { getSecretscope(); return; // Exit after processing } catch (error) { - console.error(`Error fetching Scoped secret services: ${error}`); // Log any errors + console.error(`Error fetching Scoped secret services: ${error}`); return; } } @@ -186,7 +172,7 @@ async function main(): Promise { // Validate required arguments if (!options.provider || !options.service || !options.secret) { console.error("Missing required arguments: -provider, -service, -secret"); - console.error("Use '-h' or '--help' for tool usage information."); // Provide help info + console.error("Use '-h' or '--help' for tool usage information."); return; } @@ -212,7 +198,6 @@ async function main(): Promise { /** * Execute the main function only if the script is run directly from the command line. */ -// ES module check for direct execution const isMain = process.argv[1] === fileURLToPath(import.meta.url); if (isMain) { main().catch((error) => console.error(`Unexpected error: ${error}`)); diff --git a/src/js/how2validate/utility/log_utility.ts b/src/js/how2validate/utility/log_utility.ts index 5501f6e..462ad92 100644 --- a/src/js/how2validate/utility/log_utility.ts +++ b/src/js/how2validate/utility/log_utility.ts @@ -2,7 +2,7 @@ import { getActiveSecretStatus, getInactiveSecretStatus, -} from "./config_utility.js"; +} from "./config_utility"; /** * Generates a formatted message about the status of a secret. diff --git a/src/js/how2validate/utility/tool_utility.ts b/src/js/how2validate/utility/tool_utility.ts index 5a2e1f8..6e84bfd 100644 --- a/src/js/how2validate/utility/tool_utility.ts +++ b/src/js/how2validate/utility/tool_utility.ts @@ -1,11 +1,10 @@ import * as fs from "fs"; // Importing the 'fs' module for file system operations import * as path from "path"; // Importing the 'path' module for handling file and directory paths import { fileURLToPath } from 'url'; // Importing fileURLToPath to convert URL to path -import * as logging from "loglevel"; // Importing loglevel for logging messages +// import * as logging from "loglevel"; // Importing loglevel for logging messages import Table from 'cli-table3'; // Importing cli-table3 for formatted table display -// import { execSync } from "child_process"; // Importing execSync to run shell commands synchronously -import { getPackageName } from "./config_utility.js"; // Importing a function to get the package name from configuration +// import { getPackageName } from "./config_utility.js"; // Importing a function to get the package name from configuration // Convert import.meta.url to __filename and __dirname const __filename = fileURLToPath(import.meta.url); @@ -226,9 +225,9 @@ export function redactSecret(secret: string): string { return secret.slice(0, 5) + "*".repeat(secret.length - 5); } -/** - * Update the tool to the latest version using the appropriate package manager. - */ +// /** +// * Update the tool to the latest version using the appropriate package manager. +// */ // export function updateTool(): void { // logging.info("Updating the tool..."); // Log the update initiation @@ -259,20 +258,20 @@ export function redactSecret(secret: string): string { // } // } -/** - * Detect the package manager being used in the project. - * @returns {string} The name of the detected package manager. - */ -function detectPackageManager(): string { - const hasNpm = fs.existsSync(path.join(__dirname, "..", "node_modules")); // Check for npm - const hasYarn = fs.existsSync(path.join(__dirname, "..", "yarn.lock")); // Check for yarn - const hasPnpm = fs.existsSync(path.join(__dirname, "..", "pnpm-lock.yaml")); // Check for pnpm - const hasBun = fs.existsSync(path.join(__dirname, "..", "bun.lockb")); // Check for bun - - if (hasBun) return "bun"; // Return bun if found - if (hasPnpm) return "pnpm"; // Return pnpm if found - if (hasYarn) return "yarn"; // Return yarn if found - if (hasNpm) return "npm"; // Return npm if found - - return "unknown"; // Return unknown if none found -} +// /** +// * Detect the package manager being used in the project. +// * @returns {string} The name of the detected package manager. +// */ +// function detectPackageManager(): string { +// const hasNpm = fs.existsSync(path.join(__dirname, "..", "node_modules")); // Check for npm +// const hasYarn = fs.existsSync(path.join(__dirname, "..", "yarn.lock")); // Check for yarn +// const hasPnpm = fs.existsSync(path.join(__dirname, "..", "pnpm-lock.yaml")); // Check for pnpm +// const hasBun = fs.existsSync(path.join(__dirname, "..", "bun.lockb")); // Check for bun + +// if (hasBun) return "bun"; // Return bun if found +// if (hasPnpm) return "pnpm"; // Return pnpm if found +// if (hasYarn) return "yarn"; // Return yarn if found +// if (hasNpm) return "npm"; // Return npm if found + +// return "unknown"; // Return unknown if none found +// } diff --git a/src/js/how2validate/validators/npm/npm_access_token.ts b/src/js/how2validate/validators/npm/npm_access_token.ts index d114460..f9ad216 100644 --- a/src/js/how2validate/validators/npm/npm_access_token.ts +++ b/src/js/how2validate/validators/npm/npm_access_token.ts @@ -13,8 +13,8 @@ import axios, { AxiosError } from "axios"; // Import Axios for making HTTP reque import { getActiveSecretStatus, getInactiveSecretStatus, -} from "../../utility/config_utility.js"; // Import functions to retrieve active and inactive secret statuses -import { getSecretStatusMessage } from "../../utility/log_utility.js"; // Import function to format status messages +} from "../../utility/config_utility"; // Import functions to retrieve active and inactive secret statuses +import { getSecretStatusMessage } from "../../utility/log_utility"; // Import function to format status messages /** * Validate an NPM access token by making an API request to check its validity. diff --git a/src/js/how2validate/validators/snyk/snyk_auth_key.ts b/src/js/how2validate/validators/snyk/snyk_auth_key.ts index a7b98a4..b5b39f8 100644 --- a/src/js/how2validate/validators/snyk/snyk_auth_key.ts +++ b/src/js/how2validate/validators/snyk/snyk_auth_key.ts @@ -13,8 +13,8 @@ import axios, { AxiosError } from "axios"; // Import Axios for making HTTP reque import { getActiveSecretStatus, getInactiveSecretStatus, -} from "../../utility/config_utility.js"; // Import functions to get secret statuses -import { getSecretStatusMessage } from "../../utility/log_utility.js"; // Import function to format status messages +} from "../../utility/config_utility"; // Import functions to get secret statuses +import { getSecretStatusMessage } from "../../utility/log_utility"; // Import function to format status messages /** * Validate a Snyk auth key by making an API request to check its validity. diff --git a/src/js/how2validate/validators/sonarcloud/sonarcloud_token.ts b/src/js/how2validate/validators/sonarcloud/sonarcloud_token.ts index ec499dc..c0eeec6 100644 --- a/src/js/how2validate/validators/sonarcloud/sonarcloud_token.ts +++ b/src/js/how2validate/validators/sonarcloud/sonarcloud_token.ts @@ -13,8 +13,8 @@ import axios, { AxiosError } from "axios"; // Import Axios for making HTTP reque import { getActiveSecretStatus, getInactiveSecretStatus, -} from "../../utility/config_utility.js"; // Import functions to get secret statuses -import { getSecretStatusMessage } from "../../utility/log_utility.js"; // Import function to format status messages +} from "../../utility/config_utility"; // Import functions to get secret statuses +import { getSecretStatusMessage } from "../../utility/log_utility"; // Import function to format status messages /** * Validate a SonarCloud token by making an API request to check its validity. diff --git a/src/js/jsr.json b/src/js/jsr.json index f6292e3..212cdc8 100644 --- a/src/js/jsr.json +++ b/src/js/jsr.json @@ -1,6 +1,6 @@ { "name": "@how2validate/how2validate", - "version": "0.0.2-beta.19", + "version": "0.0.2-beta.22", "license": "MIT", "exports": "./how2validate/index.ts", "publish": { diff --git a/src/js/package.json b/src/js/package.json index 92794b2..acd0e37 100644 --- a/src/js/package.json +++ b/src/js/package.json @@ -1,11 +1,11 @@ { "name": "how2validate", - "version": "0.0.1-beta.17", + "version": "0.0.1-beta.20", "description": "A CLI tool to validate secrets for different services.", - "main": "how2validate/index.ts", + "main": "dist/index.js", "type": "module", "scripts": { - "build": "npm link && npx tsc && npx cpx './config.ini' dist/ && npx cpx './tokenManager.json' dist/", + "build": "npm link && npx tsc && npx webpack --config webpack.config.cjs && npx cpx './config.ini' dist/ && npx cpx './tokenManager.json' dist/", "start": "node dist/index.js", "publishnpm": "npm publish", "publishjsr": "npx jsr publish --allow-dirty" @@ -42,12 +42,17 @@ "@types/ini": "^4.1.1", "@types/node": "^22.5.5", "cpx": "^1.2.1", + "node-polyfill-webpack-plugin": "^4.0.0", "semantic-release": "^24.1.1", - "typescript": "^5.7.0-dev.20240928" + "ts-loader": "^9.5.1", + "typescript": "^5.7.0-dev.20240928", + "webpack": "^5.95.0", + "webpack-cli": "^5.1.4", + "webpack-node-externals": "^3.0.0" }, "files": [ "how2validate/*", "./config.ini", "./tokenManager.json" ] -} +} \ No newline at end of file diff --git a/src/js/webpack.config.cjs b/src/js/webpack.config.cjs new file mode 100644 index 0000000..4ede2ba --- /dev/null +++ b/src/js/webpack.config.cjs @@ -0,0 +1,31 @@ +const path = require('path'); +const NodePolyfillPlugin = require('node-polyfill-webpack-plugin'); + +module.exports = { + mode: 'production', + entry: './how2validate/index.ts', + target: 'node', + module: { + rules: [ + { + test: /\.ts$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], + }, + resolve: { + extensions: ['.ts', '.js'], + }, + output: { + filename: 'index.js', + path: path.resolve(__dirname, 'dist'), + }, + externals: { + child_process: 'commonjs child_process', + // Add other core modules as needed + }, + plugins: [ + new NodePolyfillPlugin() + ], +}; \ No newline at end of file