Skip to content

Commit

Permalink
chore(webpack):added webpack modules
Browse files Browse the repository at this point in the history
  • Loading branch information
VigneshKna committed Sep 29, 2024
1 parent 43e8a05 commit 014795b
Showing 11 changed files with 80 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/js/config.ini
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions src/js/how2validate/handler/validator_handler.ts
Original file line number Diff line number Diff line change
@@ -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 = (
25 changes: 5 additions & 20 deletions src/js/how2validate/index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<void> {
// 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<void> {
/**
* 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}`));
2 changes: 1 addition & 1 deletion src/js/how2validate/utility/log_utility.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
import {
getActiveSecretStatus,
getInactiveSecretStatus,
} from "./config_utility.js";
} from "./config_utility";

/**
* Generates a formatted message about the status of a secret.
45 changes: 22 additions & 23 deletions src/js/how2validate/utility/tool_utility.ts
Original file line number Diff line number Diff line change
@@ -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
// }
4 changes: 2 additions & 2 deletions src/js/how2validate/validators/npm/npm_access_token.ts
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions src/js/how2validate/validators/snyk/snyk_auth_key.ts
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions src/js/how2validate/validators/sonarcloud/sonarcloud_token.ts
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion src/js/jsr.json
Original file line number Diff line number Diff line change
@@ -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": {
15 changes: 10 additions & 5 deletions src/js/package.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
31 changes: 31 additions & 0 deletions src/js/webpack.config.cjs
Original file line number Diff line number Diff line change
@@ -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()
],
};

0 comments on commit 014795b

Please sign in to comment.