leshi-ui
Version:
Modern CLI for building and managing React Native UI components with copy-paste simplicity, custom theming, and open source design system support
112 lines (111 loc) • 4.74 kB
JavaScript
import { FileUtils } from '../utils/file-utils.js';
import { GitHubService } from './github-service.js';
import { Logger } from '../utils/logger.js';
export class GitHubProjectService {
static async getProjectConfig(cwd) {
return {
framework: 'rn',
componentsDir: FileUtils.join(cwd, 'components', 'ui'),
stylesDir: FileUtils.join(cwd, 'styles'),
libDir: FileUtils.join(cwd, 'lib'),
};
}
static async isReactNativeProject(cwd) {
const packageJsonPath = FileUtils.join(cwd, 'package.json');
if (!(await FileUtils.exists(packageJsonPath))) {
return false;
}
try {
const packageJson = await FileUtils.readJson(packageJsonPath);
const dependencies = {
...packageJson.dependencies,
...packageJson.devDependencies,
};
return !!(dependencies['react-native'] ||
dependencies['expo'] ||
dependencies['@expo/cli']);
}
catch {
return false;
}
}
static async downloadComponent(framework, componentName, targetPath, overwrite = false) {
try {
if (await FileUtils.exists(targetPath) && !overwrite) {
Logger.warning(`Component '${componentName}' already exists. Use --overwrite to replace it.`);
return false;
}
const componentContent = await GitHubService.downloadComponent(framework, componentName);
await FileUtils.ensureDir(FileUtils.dirname(targetPath));
await FileUtils.writeFile(targetPath, componentContent);
return true;
}
catch (error) {
Logger.error(`Failed to download component '${componentName}': ${error instanceof Error ? error.message : 'Unknown error'}`);
return false;
}
}
static async downloadUtility(framework, utilityPath, targetPath, overwrite = false) {
try {
if (await FileUtils.exists(targetPath) && !overwrite) {
return false;
}
const utilityContent = await GitHubService.downloadUtility(framework, utilityPath);
await FileUtils.ensureDir(FileUtils.dirname(targetPath));
await FileUtils.writeFile(targetPath, utilityContent);
return true;
}
catch (error) {
Logger.error(`Failed to download utility '${utilityPath}': ${error instanceof Error ? error.message : 'Unknown error'}`);
return false;
}
}
static async downloadTheme(framework, themeName, targetPath, overwrite = false) {
try {
if (await FileUtils.exists(targetPath) && !overwrite) {
Logger.warning(`Theme '${themeName}' already exists. Use --overwrite to replace it.`);
return false;
}
const themeContent = await GitHubService.downloadTheme(framework, themeName);
await FileUtils.ensureDir(FileUtils.dirname(targetPath));
await FileUtils.writeFile(targetPath, themeContent);
return true;
}
catch (error) {
Logger.error(`Failed to download theme '${themeName}': ${error instanceof Error ? error.message : 'Unknown error'}`);
return false;
}
}
static async downloadStyleFiles(framework, stylesDir, overwrite = false) {
const downloadedFiles = [];
try {
const styleFiles = await GitHubService.downloadStyleFiles(framework);
for (const [filename, content] of Object.entries(styleFiles)) {
const targetPath = FileUtils.join(stylesDir, filename);
if (await FileUtils.exists(targetPath) && !overwrite) {
continue;
}
await FileUtils.ensureDir(FileUtils.dirname(targetPath));
await FileUtils.writeFile(targetPath, content);
downloadedFiles.push(filename);
}
return { success: true, files: downloadedFiles };
}
catch (error) {
Logger.error(`Failed to download style files: ${error instanceof Error ? error.message : 'Unknown error'}`);
return { success: false, files: downloadedFiles };
}
}
static async getAvailableThemes(framework) {
try {
return await GitHubService.getAvailableThemes(framework);
}
catch (error) {
Logger.error('Failed to fetch available themes from GitHub');
return [];
}
}
static async checkGitHubConnection() {
return await GitHubService.isRepositoryAccessible();
}
}