@codecot/pw-checker
Version:
A comprehensive CLI tool to audit passwords locally using HIBP and import from Chrome/Bitwarden
104 lines ⢠3.92 kB
JavaScript
// healthCheck.ts - Validate pw-checker installation and configuration
import dotenv from "dotenv";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import chalk from "chalk";
import { getDatabase } from "./database.js";
// Load environment variables from .env file
dotenv.config();
// ESM replacement for __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function runHealthCheck() {
console.log(chalk.blue.bold("š„ pw-checker Health Check\n"));
let allGood = true;
// Check 1: Verify required directories exist
console.log("š Checking directories...");
const requiredDirs = ["data", "db"];
for (const dir of requiredDirs) {
if (fs.existsSync(dir)) {
console.log(chalk.green(`ā
${dir}/ directory exists`));
}
else {
console.log(chalk.red(`ā ${dir}/ directory missing`));
allGood = false;
}
}
// Check 2: Verify database connection
console.log("\nš¾ Checking database...");
try {
const db = await getDatabase();
const result = await db.get("SELECT COUNT(*) as count FROM pw_entries");
console.log(chalk.green(`ā
Database connection successful`));
console.log(chalk.blue(`ā¹ļø Found ${result.count} entries in database`));
await db.close();
}
catch (error) {
console.log(chalk.red(`ā Database connection failed: ${error}`));
allGood = false;
}
// Check 3: Verify template files
console.log("\nš Checking template files...");
const templateFiles = [
"data/passwords.csv.template",
"data/chrome-passwords.csv.template",
".env.template",
];
for (const file of templateFiles) {
if (fs.existsSync(file)) {
console.log(chalk.green(`ā
${file} exists`));
}
else {
console.log(chalk.yellow(`ā ļø ${file} missing (optional)`));
}
}
// Check 4: Environment variables
console.log("\nš Checking environment variables...");
const hibpKey = process.env.HIBP_API_KEY;
if (hibpKey) {
console.log(chalk.green(`ā
HIBP_API_KEY is set`));
}
else {
console.log(chalk.yellow(`ā ļø HIBP_API_KEY not set (required for breach checking)`));
console.log(chalk.blue(`ā¹ļø Get an API key from https://haveibeenpwned.com/API/Key`));
}
// Check 5: TypeScript configuration
console.log("\nāļø Checking configuration...");
if (fs.existsSync("tsconfig.json")) {
console.log(chalk.green(`ā
TypeScript configuration found`));
}
else {
console.log(chalk.red(`ā tsconfig.json missing`));
allGood = false;
}
if (fs.existsSync("package.json")) {
console.log(chalk.green(`ā
Package configuration found`));
}
else {
console.log(chalk.red(`ā package.json missing`));
allGood = false;
}
// Final result
console.log("\n" + "=".repeat(50));
if (allGood) {
console.log(chalk.green.bold("ā
All critical checks passed!"));
console.log(chalk.blue("š pw-checker is ready to use"));
console.log("\nNext steps:");
console.log(" npm start - Import CSV and check passwords");
console.log(" npm run view - View imported entries");
console.log(" npm run help - Show all available commands");
}
else {
console.log(chalk.red.bold("ā Some critical checks failed"));
console.log(chalk.yellow("š§ Please fix the issues above before using pw-checker"));
process.exit(1);
}
}
// Run the health check
runHealthCheck().catch((error) => {
console.error(chalk.red("ā Health check failed:"), error);
process.exit(1);
});
//# sourceMappingURL=healthCheck.js.map