accs-cli
Version:
ACCS CLI — Full-featured developer tool for scaffolding, running, building, and managing multi-language projects
187 lines (160 loc) • 4.53 kB
JavaScript
/**
* System requirements checker
*/
import { execa } from 'execa';
import semver from 'semver';
import { logger } from './logger.js';
export class SystemCheck {
/**
* Check if a command exists in the system
*/
static async commandExists(command) {
try {
await execa('which', [command]);
return true;
} catch {
try {
await execa('where', [command]);
return true;
} catch {
return false;
}
}
}
/**
* Get version of a command
*/
static async getCommandVersion(command, args = ['--version']) {
try {
const { stdout } = await execa(command, args);
return stdout.trim();
} catch {
return null;
}
}
/**
* Check Node.js version
*/
static async checkNode() {
const version = process.version;
const minVersion = '18.0.0';
return {
installed: true,
version: version.replace('v', ''),
valid: semver.gte(version, minVersion),
minVersion
};
}
/**
* Check npm version
*/
static async checkNpm() {
const exists = await this.commandExists('npm');
if (!exists) {
return { installed: false, version: null, valid: false };
}
const version = await this.getCommandVersion('npm');
return {
installed: true,
version: version?.split(' ')[0] || 'unknown',
valid: true
};
}
/**
* Check Python version
*/
static async checkPython() {
let exists = await this.commandExists('python3');
let command = 'python3';
if (!exists) {
exists = await this.commandExists('python');
command = 'python';
}
if (!exists) {
return { installed: false, version: null, valid: false, command: null };
}
const version = await this.getCommandVersion(command);
const versionMatch = version?.match(/Python (\d+\.\d+\.\d+)/);
const versionNumber = versionMatch ? versionMatch[1] : 'unknown';
return {
installed: true,
version: versionNumber,
valid: versionNumber !== 'unknown',
command
};
}
/**
* Check PHP version
*/
static async checkPhp() {
const exists = await this.commandExists('php');
if (!exists) {
return { installed: false, version: null, valid: false };
}
const version = await this.getCommandVersion('php');
const versionMatch = version?.match(/PHP (\d+\.\d+\.\d+)/);
const versionNumber = versionMatch ? versionMatch[1] : 'unknown';
return {
installed: true,
version: versionNumber,
valid: versionNumber !== 'unknown'
};
}
/**
* Check Git version
*/
static async checkGit() {
const exists = await this.commandExists('git');
if (!exists) {
return { installed: false, version: null, valid: false };
}
const version = await this.getCommandVersion('git');
const versionMatch = version?.match(/git version (\d+\.\d+\.\d+)/);
const versionNumber = versionMatch ? versionMatch[1] : 'unknown';
return {
installed: true,
version: versionNumber,
valid: versionNumber !== 'unknown'
};
}
/**
* Run comprehensive system check
*/
static async runFullCheck() {
logger.info('Running system diagnostics...');
logger.separator();
const checks = {
node: await this.checkNode(),
npm: await this.checkNpm(),
python: await this.checkPython(),
php: await this.checkPhp(),
git: await this.checkGit()
};
return checks;
}
/**
* Display system check results
*/
static displayResults(checks) {
logger.section('System Requirements Check');
Object.entries(checks).forEach(([tool, result]) => {
const toolName = tool.charAt(0).toUpperCase() + tool.slice(1);
if (result.installed && result.valid) {
logger.success(`${toolName} ${result.version}`, '(✓ Ready)');
} else if (result.installed && !result.valid) {
logger.warn(`${toolName} ${result.version}`, '(⚠ Version may be incompatible)');
} else {
logger.error(`${toolName}`, '(✖ Not installed)');
}
});
logger.separator();
// Summary
const installed = Object.values(checks).filter(c => c.installed && c.valid).length;
const total = Object.keys(checks).length;
if (installed === total) {
logger.success(`All systems ready! (${installed}/${total})`);
} else {
logger.warn(`${installed}/${total} tools are ready. Some features may be limited.`);
}
}
}