@ably/cli
Version:
Ably CLI for Pub/Sub, Chat and Spaces
48 lines (47 loc) • 1.38 kB
JavaScript
/**
* Common utilities for version-related functionality
*/
import colorJson from 'color-json';
// Import package.json directly - TypeScript will resolve this at compile time
import packageJson from '../../package.json' with { type: 'json' };
/**
* Get the CLI version from package.json
* @returns The CLI version string
*/
export function getCliVersion() {
return packageJson.version;
}
/**
* Get standardized version information object
*/
export function getVersionInfo(config) {
return {
version: config.version,
name: config.name,
arch: config.arch,
nodeVersion: process.version,
platform: process.platform,
};
}
/**
* Format version info as a standard string
*/
export function formatVersionString(config) {
return `${config.name}/${config.version} ${process.platform}-${config.arch} ${process.version}`;
}
/**
* Format version info as JSON based on flag preferences
*/
export function formatVersionJson(versionInfo, isPretty) {
try {
if (isPretty) {
return colorJson(versionInfo);
}
return JSON.stringify(versionInfo);
}
catch (error) {
// Fallback to regular JSON.stringify if colorJson fails
console.error('Error formatting version as JSON:', error);
return JSON.stringify(versionInfo, undefined, isPretty ? 2 : undefined);
}
}