cvm-cli
Version:
A unified CLI tool for managing PHP, Node.js, and Python versions with virtual environment and dependency management support.
162 lines (140 loc) ⢠5.5 kB
JavaScript
const { program } = require('commander');
const chalk = require('chalk');
const VersionManagerFactory = require('../lib/versionManagerFactory');
const CVMUtils = require('../lib/utils');
program
.name('cvm')
.version('1.0.0')
.description('Cross-platform Version Manager');
program
.command('install <language> <version>')
.description('Install a specific version of the given language')
.action(async (language, version) => {
try {
if (!VersionManagerFactory.isSupported(language)) {
console.log(chalk.red(`ā Language '${language}' is not supported`));
console.log(chalk.yellow(`Supported languages: ${VersionManagerFactory.getSupportedLanguages().join(', ')}`));
return;
}
const manager = VersionManagerFactory.create(language);
await manager.install(version);
} catch (error) {
console.error(chalk.red('Installation failed:'), error.message);
}
});
program
.command('use <language> <version>')
.description('Switch to a specific version of the given language')
.action(async (language, version) => {
try {
if (!VersionManagerFactory.isSupported(language)) {
console.log(chalk.red(`ā Language '${language}' is not supported`));
return;
}
const manager = VersionManagerFactory.create(language);
await manager.use(version);
} catch (error) {
console.error(chalk.red('Switch failed:'), error.message);
}
});
program
.command('list <language>')
.description('List the installed versions of the given language')
.action(async (language) => {
try {
if (!VersionManagerFactory.isSupported(language)) {
console.log(chalk.red(`ā Language '${language}' is not supported`));
return;
}
const manager = VersionManagerFactory.create(language);
const versions = await manager.getInstalledVersions();
const current = await manager.getCurrentVersion();
if (versions.length === 0) {
console.log(chalk.yellow(`No ${language} versions installed`));
return;
}
console.log(chalk.blue(`\nš Installed ${language} versions:\n`));
versions.forEach(v => {
const marker = v === current ? chalk.green('* ') : ' ';
const color = v === current ? chalk.green : chalk.white;
console.log(`${marker}${color(v)}${v === current ? chalk.gray(' (current)') : ''}`);
});
console.log();
} catch (error) {
console.error(chalk.red('List failed:'), error.message);
}
});
program
.command('available <language>')
.description('List available versions for download')
.option('-l, --limit <number>', 'Limit number of versions to show', '20')
.action(async (language, options) => {
try {
if (!VersionManagerFactory.isSupported(language)) {
console.log(chalk.red(`ā Language '${language}' is not supported`));
return;
}
const manager = VersionManagerFactory.create(language);
console.log(chalk.blue(`Fetching available ${language} versions...`));
const versions = await manager.getAvailableVersions();
const limit = parseInt(options.limit);
const limitedVersions = versions.slice(0, limit);
if (limitedVersions.length === 0) {
console.log(chalk.yellow(`No ${language} versions available`));
return;
}
console.log(chalk.blue(`\nš Available ${language} versions (showing ${limitedVersions.length}${versions.length > limit ? ` of ${versions.length}` : ''}):\n`));
limitedVersions.forEach(v => {
console.log(` ${chalk.green(v)}`);
});
if (versions.length > limit) {
console.log(chalk.gray(`\n ... and ${versions.length - limit} more versions`));
console.log(chalk.cyan(` Use --limit to show more versions`));
}
console.log();
} catch (error) {
console.error(chalk.red('Failed to fetch available versions:'), error.message);
}
});
program
.command('uninstall <language> <version>')
.description('Remove a specific version of the given language')
.action(async (language, version) => {
try {
if (!VersionManagerFactory.isSupported(language)) {
console.log(chalk.red(`ā Language '${language}' is not supported`));
return;
}
const manager = VersionManagerFactory.create(language);
await manager.uninstall(version);
} catch (error) {
console.error(chalk.red('Uninstall failed:'), error.message);
}
});
program
.command('env <command> [name]')
.description('Manage virtual environments')
.action((command, name) => {
switch (command) {
case 'create':
console.log(chalk.green(`Creating environment ${name}...`));
// Add environment creation logic here
break;
case 'activate':
console.log(chalk.green(`Activating environment ${name}...`));
// Add environment activation logic here
break;
case 'deactivate':
console.log(chalk.green('Deactivating current environment...'));
// Add environment deactivation logic here
break;
case 'list':
console.log(chalk.green('Listing all environments...'));
// Add environment listing logic here
break;
default:
console.log(chalk.red('Unknown environment command:', command));
}
});
program.parse(process.argv);