myaidev-method
Version:
Comprehensive development framework with SPARC methodology for AI-assisted software development, multi-platform publishing (WordPress, PayloadCMS, Astro, Docusaurus, Mintlify), and Coolify deployment
176 lines (145 loc) • 4.28 kB
JavaScript
#!/usr/bin/env node
/**
* WordPress Health Check Script
* Scriptable health check with JSON output for agent processing
*
* Usage:
* npx myaidev-method wordpress:health-check [options]
* node src/scripts/wordpress-health-check.js [options]
*/
import { WordPressAdminUtils } from '../lib/wordpress-admin-utils.js';
import { writeFileSync } from 'fs';
import { resolve } from 'path';
const args = process.argv.slice(2);
const options = {
format: 'json', // json or text
output: null, // file path for output
verbose: false
};
// Parse arguments
for (let i = 0; i < args.length; i++) {
switch (args[i]) {
case '--format':
options.format = args[++i] || 'json';
break;
case '--output':
case '-o':
options.output = args[++i];
break;
case '--verbose':
case '-v':
options.verbose = true;
break;
case '--help':
case '-h':
printHelp();
process.exit(0);
}
}
function printHelp() {
console.log(`
WordPress Health Check Script
Usage:
npx myaidev-method wordpress:health-check [options]
Options:
--format <type> Output format: json or text (default: json)
--output <file> Write output to file
-o <file> Alias for --output
--verbose Show detailed progress information
-v Alias for --verbose
--help Show this help message
-h Alias for --help
Environment Variables (from .env):
WORDPRESS_URL WordPress site URL
WORDPRESS_USERNAME Admin username
WORDPRESS_APP_PASSWORD Application password
Examples:
# Run health check with JSON output
npx myaidev-method wordpress:health-check
# Save JSON report to file
npx myaidev-method wordpress:health-check --output health-report.json
# Display human-readable text report
npx myaidev-method wordpress:health-check --format text
# Verbose mode with detailed progress
npx myaidev-method wordpress:health-check --verbose
Output Structure (JSON):
{
"success": true,
"timestamp": "2025-10-01T12:00:00.000Z",
"site": { "name": "...", "url": "..." },
"overall_health": {
"score": 85,
"grade": "B",
"status": "healthy"
},
"checks": [...],
"summary": { "total_checks": 5, "passed": 4, "warnings": 1 },
"recommendations": [...]
}
`);
}
async function runHealthCheck() {
try {
if (options.verbose) {
console.error('Initializing WordPress connection...');
}
const wpUtils = new WordPressAdminUtils();
if (options.verbose) {
console.error('Running health checks...');
}
const healthData = await wpUtils.runHealthCheck();
if (options.verbose) {
console.error('Health check completed.');
}
// Format output
let output;
if (options.format === 'text') {
output = wpUtils.formatHealthReport(healthData);
} else {
output = JSON.stringify(healthData, null, 2);
}
// Write to file or stdout
if (options.output) {
const outputPath = resolve(options.output);
writeFileSync(outputPath, output, 'utf8');
if (options.verbose) {
console.error(`Report written to: ${outputPath}`);
}
// Also output summary to stdout for piping
console.log(JSON.stringify({
success: true,
output_file: outputPath,
health_score: healthData.overall_health?.score,
health_grade: healthData.overall_health?.grade
}));
} else {
console.log(output);
}
// Exit with appropriate code
if (!healthData.success) {
process.exit(1);
}
const criticalIssues = healthData.checks?.filter(c => c.status === 'critical').length || 0;
if (criticalIssues > 0) {
process.exit(2); // Critical issues found
}
process.exit(0);
} catch (error) {
const errorOutput = {
success: false,
error: error.message,
timestamp: new Date().toISOString()
};
if (options.format === 'json') {
console.log(JSON.stringify(errorOutput, null, 2));
} else {
console.error(`ERROR: ${error.message}`);
}
process.exit(1);
}
}
// Run if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
runHealthCheck();
}
export { runHealthCheck };