@unimatrix-01/create-borg-ui
Version:
Borg UI - A modern React component library
70 lines (69 loc) • 2.96 kB
JavaScript
import { execSync } from 'child_process';
import fs from 'fs-extra';
import path from 'path';
import { promisify } from 'util';
import { exec } from 'child_process';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const execAsync = promisify(exec);
export async function createApp(options) {
const { appName, template, appTitle, icon, footerContent, noGit } = options;
// Create app directory
const appDir = path.resolve(process.cwd(), appName);
await fs.ensureDir(appDir);
// Copy template files
const templateDir = path.resolve(__dirname, 'templates', template);
await fs.copy(templateDir, appDir);
// Update package.json
const packageJsonPath = path.join(appDir, 'package.json');
const packageJson = await fs.readJson(packageJsonPath);
packageJson.name = appName;
await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 });
// Update index.html
const indexHtmlPath = path.join(appDir, 'index.html');
let indexHtml = await fs.readFile(indexHtmlPath, 'utf-8');
indexHtml = indexHtml.replace('{{appTitle}}', appTitle);
await fs.writeFile(indexHtmlPath, indexHtml);
// Update App.tsx
const appTsxPath = path.join(appDir, 'src', 'App.tsx');
let appTsx = await fs.readFile(appTsxPath, 'utf-8');
appTsx = appTsx.replace('{{appTitle}}', appTitle);
appTsx = appTsx.replace('{{icon}}', icon);
appTsx = appTsx.replace('{{footerContent}}', footerContent);
await fs.writeFile(appTsxPath, appTsx);
// Update config.ts
const configPath = path.join(appDir, 'src', 'config.ts');
let config = await fs.readFile(configPath, 'utf-8');
config = config.replace('"Borg UI App"', `"${appTitle}"`);
config = config.replace('"icon"', `"${icon}"`);
config = config.replace('"A modern React application built with Borg UI components"', `"${footerContent}"`);
await fs.writeFile(configPath, config);
// Install dependencies
console.log('Installing dependencies...');
try {
// Install all dependencies from package.json
await execAsync('npm install', { cwd: appDir });
}
catch (error) {
console.error('Error installing dependencies:', error);
throw error;
}
// Initialize git repository if not disabled
if (!noGit) {
try {
execSync('git init', { cwd: appDir });
execSync('git add .', { cwd: appDir });
execSync('git commit -m "Initial commit"', { cwd: appDir });
}
catch (error) {
console.warn('Warning: Failed to initialize git repository:', error);
}
}
// Change into the project directory
process.chdir(appDir);
console.log(`\nProject created successfully! You are now in the ${appName} directory.`);
console.log('\nTo start developing, run:');
console.log(' npm run dev\n');
return appDir;
}