@xonovex/core
v0.1.12
Published
Core library for Xonovex TypeScript scripts (Node.js)
Readme
@xonovex/tool-lib
Core library functions for Xonovex TypeScript scripts running with Node.js.
Overview
This package provides common utilities used across all TypeScript scripts in the Xonovex platform:
- Colors: ANSI color codes for terminal output
- Logging: Structured logging with color-coded levels
- Platform Detection: OS detection and platform-specific commands
- Error Handling: Graceful error handling and validation
- Path Utilities: File system navigation and platform root detection
Installation
This package is designed to be imported directly by other script packages in the monorepo workspace.
Usage
import {
die,
getPlatformRoot,
logDebug,
logError,
logInfo,
logSuccess,
logWarning,
printSection,
requireCommand,
} from "@xonovex/tool-lib";
// Logging
logInfo("Starting operation...");
logSuccess("Operation completed!");
logWarning("This is a warning");
logError("An error occurred");
logDebug("Debug information"); // Only shown if DEBUG env var is set
// Sections
printSection("Configuration", "Loading platform configuration...");
// Platform detection
const root = await getPlatformRoot();
logInfo("Platform root:", root);
// Error handling
await requireCommand("git", "git");
die("Fatal error occurred", 1);API Reference
Logging Functions
logInfo(...args)- Log info message (blue)logSuccess(...args)- Log success message (green)logWarning(...args)- Log warning message (yellow)logError(...args)- Log error message (red)logDebug(...args)- Log debug message (purple, only if DEBUG is set)printSection(title, content?)- Print formatted section headerprintSubsection(title)- Print subsection headercheckResult(name, status, details?)- Print check result with color
Platform Detection
isMacOS()- Check if running on macOSisLinux()- Check if running on LinuxisWindows()- Check if running on WindowsgetPlatformCommand(macCmd, linuxCmd, winCmd?)- Get OS-specific commandgetOS()- Get current OS name
Error Handling
die(message, exitCode?)- Exit with error messagerequireCommand(cmd, package?)- Verify command existsrequireFile(path, description?)- Verify file existsrequireDirectory(path, description?)- Verify directory existsvalidateInArray(value, array)- Check if value is in arrayvalidateBoolean(value, varName)- Validate boolean valuevalidateRepository(repo, platformRoot?)- Validate git repository
Path Utilities
getScriptDir(importMeta)- Get script directory pathgetPlatformRoot(startDir?)- Find platform root directorygetGitRoot()- Get git repository rootfileExists(path)- Check if file existsdirExists(path)- Check if directory existsgetFileMtime(path)- Get file modification timeformatTimestamp(date)- Format date for displayfindClusterDirectory(platformRoot?)- Find cluster directoryfindInfrastructureDirectory(platformRoot?)- Find infrastructure directorydetectAvailableEnvironments(infraDir?)- List available environments
Colors
import {Colors} from "@xonovex/tool-lib";
console.log(`${Colors.GREEN}Success!${Colors.NC}`);
console.log(`${Colors.RED}Error!${Colors.NC}`);Available colors:
RED,GREEN,YELLOW,BLUE,CYAN,PURPLE,NC(no color)
Environment Variables
DEBUG- Enable debug loggingPLATFORM_ROOT- Override platform root detection
Examples
Simple Script
#!/usr/bin/env node
import {getPlatformRoot, logInfo, logSuccess} from "@xonovex/tool-lib";
async function main() {
const root = await getPlatformRoot();
logInfo("Working in:", root);
// Do something...
logSuccess("Done!");
}
main();Script with Error Handling
#!/usr/bin/env node
import {
die,
logError,
logInfo,
requireCommand,
requireDirectory,
} from "@xonovex/tool-lib";
async function main() {
try {
await requireCommand("git");
await requireDirectory("./cluster", "cluster directory");
logInfo("All checks passed!");
} catch (error) {
logError("Validation failed:", error);
die("Cannot continue", 1);
}
}
main();Development
# Build
npm run build
# Type check
npm run check
# Format
npm run fmt
# Lint
npm run lint
# Test
npm test