@tshifhiwa/ohrm-ui-automation-framework
Version:
Playwright and TypeScript–based test automation framework for validating core UI features and workflows of the OrangeHRM demo application.
85 lines (83 loc) • 3.33 kB
JavaScript
import { Command } from "commander";
import chalk from "chalk";
import ora from "ora";
import figlet from "figlet";
import boxen from "boxen";
import { execa } from "execa";
const program = new Command();
console.log(boxen(chalk.cyan(figlet.textSync("Playwright AE", { horizontalLayout: "default" })), {
padding: 1,
margin: 1,
borderColor: "cyan",
align: "center",
}));
console.log(chalk.gray.bold("🔐 Key Lifecycle Management and Encryption\n"));
const ALLOWED_ENVIRONMENTS = ["dev", "qa", "uat", "preprod"];
const log = (msg) => console.log(chalk.gray(`[${new Date().toISOString()}]`), msg);
function validateEnv(env) {
if (env && !ALLOWED_ENVIRONMENTS.includes(env)) {
console.error(chalk.red(`Invalid environment "${env}". Allowed: ${ALLOWED_ENVIRONMENTS.join(", ")}`));
process.exit(1);
}
}
async function initialize(tag, env) {
const spinner = ora(`Running ${tag} for ${env ?? "all environments"}...`).start();
try {
const envVars = {
SKIP_BROWSER_INIT: "true",
PLAYWRIGHT_GREP: `@${tag}`,
...(env ? { ENV: env } : {}),
};
await execa("npm", ["run", "test:cryptography"], {
stdio: "inherit",
env: { ...process.env, ...envVars },
});
spinner.succeed(chalk.green(`✔ ${tag} completed successfully`));
log(chalk.green(`Operation completed for ${env ?? "all environments"}`));
}
catch (err) {
spinner.fail(chalk.red(`✖ ${tag} failed`));
console.error(chalk.red(err.message));
log(chalk.red(`Operation failed: ${err.message}`));
process.exit(1);
}
}
function registerEnvCommand(name, desc, tag) {
program
.command(name)
.description(desc)
.requiredOption("--env <env>", `Target environment (${ALLOWED_ENVIRONMENTS.join("|")})`)
.action(async (opts) => {
validateEnv(opts.env);
await initialize(tag, opts.env);
});
}
program
.name("pae")
.description("Playwright CLI for key lifecycle management and encryption")
.version("1.0.0")
.option("--config <path>", "Path to custom config file (optional)");
registerEnvCommand("generate-key", "Generate a new encryption key for a specific environment", "generate-key");
registerEnvCommand("encrypt-env", "Encrypt environment variables for a specific environment", "env-encryption");
registerEnvCommand("rotate-key", "Rotate the encryption key for a specific environment", "key-rotation");
registerEnvCommand("audit", "Run key audit checks for a specific environment", "key-audit");
program
.command("batch-rotation")
.description("Rotate all environments' keys at once")
.action(() => initialize("batch-rotation"));
program.addHelpText("after", `
${chalk.bold("Examples:")}
${chalk.cyan("$ pae generate-key --env dev")}
${chalk.cyan("$ pae encrypt-env --env qa")}
${chalk.cyan("$ pae rotate-key --env preprod")}
${chalk.cyan("$ pae audit --env uat")}
${chalk.cyan("$ pae batch-rotation")}
${chalk.bold("Environment Options:")}
${ALLOWED_ENVIRONMENTS.map((env) => chalk.yellow(env)).join(", ")}
`);
process.on("SIGINT", () => {
console.log(chalk.yellow("\n⚠️ Operation cancelled by user."));
process.exit(0);
});
program.parse(process.argv);