adpa-enterprise-framework-automation
Version:
Modular, standards-compliant Node.js/TypeScript automation framework for enterprise requirements, project, and data management. Provides CLI and API for BABOK v3, PMBOK 7th Edition, and DMBOK 2.0 (in progress). Production-ready Express.js API with TypeSpe
142 lines • 3.96 kB
JavaScript
/**
* Common Command Utilities
* Shared utility functions for CLI commands
*/
import { join } from 'path';
import { createRequire } from 'module';
// 2. Third-party dependencies (none in this file)
// 3. Internal modules (none in this file)
// 4. Constants and configuration
const require = createRequire(import.meta.url);
/**
* Get package version from package.json
*/
export function getPackageVersion() {
try {
const packagePath = join(process.cwd(), 'package.json');
const { version } = require(packagePath);
return version;
}
catch {
return 'unknown';
}
}
/**
* Format duration in milliseconds to human readable
*/
export function formatDuration(ms) {
if (ms < 1000) {
return `${ms}ms`;
}
const seconds = Math.floor(ms / 1000);
if (seconds < 60) {
return `${seconds}s`;
}
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
if (minutes < 60) {
return remainingSeconds > 0
? `${minutes}m ${remainingSeconds}s`
: `${minutes}m`;
}
const hours = Math.floor(minutes / 60);
const remainingMinutes = minutes % 60;
return `${hours}h ${remainingMinutes}m`;
}
/**
* Create progress indicator
*/
export function createProgressIndicator(current, total, description) {
const percentage = Math.round((current / total) * 100);
const progressBar = '█'.repeat(Math.floor(percentage / 5)) +
'░'.repeat(20 - Math.floor(percentage / 5));
const baseProgress = `[${progressBar}] ${percentage}% (${current}/${total})`;
return description
? `${baseProgress} - ${description}`
: baseProgress;
}
/**
* Safely execute async operation with error handling
*/
export async function safeExecute(operation, errorMessage = 'Operation failed') {
try {
return await operation();
}
catch (error) {
console.error(`❌ ${errorMessage}:`, error instanceof Error ? error.message : String(error));
return null;
}
}
/**
* Check if running in quiet mode from CLI args
*/
export function isQuietMode() {
return process.argv.includes('--quiet') || process.argv.includes('-q');
}
/**
* Check if running in verbose mode from CLI args
*/
export function isVerboseMode() {
return process.argv.includes('--verbose') || process.argv.includes('-v');
}
/**
* Log with timestamp and level
*/
export function logWithLevel(level, message, ...args) {
const timestamp = new Date().toISOString();
const prefix = {
info: '🔵 INFO',
warn: '🟡 WARN',
error: '🔴 ERROR'
}[level];
console.log(`[${timestamp}] ${prefix}: ${message}`, ...args);
}
/**
* Create a simple spinner for async operations
*/
export class SimpleSpinner {
timer;
frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
index = 0;
text;
constructor(text = 'Loading...') {
this.text = text;
}
start() {
if (isQuietMode())
return;
this.timer = setInterval(() => {
process.stdout.write(`\r${this.frames[this.index]} ${this.text}`);
this.index = (this.index + 1) % this.frames.length;
}, 100);
}
stop(finalText) {
if (this.timer) {
clearInterval(this.timer);
this.timer = undefined;
}
if (!isQuietMode()) {
process.stdout.write(`\r${finalText || '✅ Complete!'}\n`);
}
}
updateText(text) {
this.text = text;
}
}
/**
* Debounce function for rate limiting
*/
export function debounce(func, delay) {
let timeoutId;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => func(...args), delay);
};
}
/**
* Sleep utility for delays
*/
export function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
//# sourceMappingURL=common.js.map