UNPKG

trading-platform-template

Version:

CLI tool to create a new Trading Platform project template

129 lines (108 loc) 4.54 kB
#!/usr/bin/env node import { program } from 'commander'; import chalk from 'chalk'; import ora from 'ora'; import prompts from 'prompts'; import { fileURLToPath } from 'url'; import { dirname, resolve } from 'path'; import { execSync } from 'child_process'; import fs from 'fs'; import path from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); // Get the template directory (bundled with the package) const templateDir = path.resolve(__dirname, '../template'); // Function to copy directory recursively const copyDir = (src, dest) => { fs.mkdirSync(dest, { recursive: true }); const entries = fs.readdirSync(src, { withFileTypes: true }); console.log(chalk.blue('\nCopying files from template:')); for (const entry of entries) { const srcPath = path.join(src, entry.name); const destPath = path.join(dest, entry.name); if (entry.isDirectory()) { console.log(chalk.green(`Creating directory: ${entry.name}`)); copyDir(srcPath, destPath); } else { console.log(chalk.green(`Copying file: ${entry.name}`)); fs.copyFileSync(srcPath, destPath); } } }; program .name('create-trading-platform') .description('Create a new Trading Platform project from template') .argument('[project-directory]', 'Directory to create the project in') .action(async (projectDirectory) => { try { if (!projectDirectory) { const response = await prompts({ type: 'text', name: 'projectDirectory', message: 'What is your project named?', initial: 'my-trading-platform' }); projectDirectory = response.projectDirectory; } if (!projectDirectory) { console.log(chalk.red('Please specify the project directory')); process.exit(1); } const targetPath = resolve(process.cwd(), projectDirectory); if (fs.existsSync(targetPath)) { const { overwrite } = await prompts({ type: 'confirm', name: 'overwrite', message: 'Directory already exists. Do you want to overwrite it?', initial: false }); if (!overwrite) { console.log(chalk.yellow('Operation cancelled')); process.exit(0); } fs.rmSync(targetPath, { recursive: true, force: true }); } const spinner = ora('Creating your project...').start(); try { fs.mkdirSync(targetPath, { recursive: true }); copyDir(templateDir, targetPath); // Update package.json const packageJsonPath = path.join(targetPath, 'package.json'); if (fs.existsSync(packageJsonPath)) { const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); packageJson.name = projectDirectory.toLowerCase().replace(/\s+/g, '-'); packageJson.version = '0.1.0'; packageJson.private = true; delete packageJson.repository; delete packageJson.bugs; delete packageJson.homepage; fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); } spinner.succeed(chalk.green('Project files created successfully!')); } catch (error) { spinner.fail(chalk.red('Failed to create project files')); console.error(chalk.red('Error details:'), error.message); process.exit(1); } // Install dependencies spinner.start('Installing dependencies...'); try { execSync('npm install', { cwd: targetPath, stdio: 'inherit' }); spinner.succeed(chalk.green('Dependencies installed successfully!')); } catch (error) { spinner.fail(chalk.red('Failed to install dependencies')); console.error(chalk.red('Error details:'), error.message); process.exit(1); } console.log('\n' + chalk.green('🎉 Project created successfully!')); console.log('\n' + chalk.cyan('Next steps:')); console.log(chalk.white(` 1. cd ${projectDirectory}`)); console.log(chalk.white(' 2. npm run dev')); console.log('\n' + chalk.cyan('To start development server:')); console.log(chalk.white(' http://localhost:3000')); } catch (error) { console.error(chalk.red('Error creating project:'), error); process.exit(1); } }); program.parse();