codec-ai
Version:
Codec AI - 智能代码生成和编程助手,支持多语言代码生成、项目脚手架、代码转换和优化
122 lines • 3.42 kB
JavaScript
import chalk from 'chalk';
/**
* 日志工具类
*/
export class Logger {
static info(message) {
console.log(chalk.blue('ℹ️'), message);
}
static success(message) {
console.log(chalk.green('✅'), message);
}
static warning(message) {
console.log(chalk.yellow('⚠️'), message);
}
static error(message) {
console.log(chalk.red('❌'), message);
}
static debug(message) {
if (process.env.DEBUG) {
console.log(chalk.gray('🔍'), message);
}
}
}
/**
* 文件工具类
*/
export class FileUtils {
static async ensureFileExists(filePath) {
try {
const fs = await import('fs-extra');
return await fs.pathExists(filePath);
}
catch (error) {
return false;
}
}
static async readFileSafe(filePath) {
try {
const fs = await import('fs-extra');
return await fs.readFile(filePath, 'utf8');
}
catch (error) {
return null;
}
}
}
/**
* 验证工具类
*/
export class Validator {
static isValidLanguage(language) {
const validLanguages = ['javascript', 'typescript', 'python', 'java', 'go', 'rust'];
return validLanguages.includes(language.toLowerCase());
}
static isValidCodeType(type) {
const validTypes = ['function', 'class', 'component', 'api', 'test', 'utility'];
return validTypes.includes(type.toLowerCase());
}
static isValidProjectType(type) {
const validTypes = ['web', 'api', 'cli', 'library', 'mobile', 'desktop'];
return validTypes.includes(type.toLowerCase());
}
}
/**
* 字符串工具类
*/
export class StringUtils {
static camelCase(str) {
return str
.replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '')
.replace(/^(.)/, (_, c) => c.toLowerCase());
}
static pascalCase(str) {
return str
.replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '')
.replace(/^(.)/, (_, c) => c.toUpperCase());
}
static kebabCase(str) {
return str
.replace(/[A-Z]/g, match => '-' + match.toLowerCase())
.replace(/^-/, '')
.toLowerCase();
}
static snakeCase(str) {
return str
.replace(/[A-Z]/g, match => '_' + match.toLowerCase())
.replace(/^_/, '')
.toLowerCase();
}
}
/**
* 进度指示器
*/
export class ProgressIndicator {
static spinners = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
static currentFrame = 0;
static show(message) {
const frame = this.spinners[this.currentFrame];
this.currentFrame = (this.currentFrame + 1) % this.spinners.length;
process.stdout.write(`\r${chalk.cyan(frame)} ${message}`);
}
static hide() {
process.stdout.write('\r');
}
static async withProgress(message, action) {
const interval = setInterval(() => {
this.show(message);
}, 100);
try {
const result = await action();
clearInterval(interval);
this.hide();
return result;
}
catch (error) {
clearInterval(interval);
this.hide();
throw error;
}
}
}
//# sourceMappingURL=index.js.map