justice-ctl
Version:
CLI tool for managing the Justice control demo project of Luka
114 lines (98 loc) • 3.95 kB
JavaScript
import { program } from 'commander';
import chalk from 'chalk';
import { exec } from 'child_process';
import path from 'path';
import fs from 'fs';
// Helper function to wrap exec into a promise
function execPromise(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve({ stdout, stderr });
}
});
});
}
// Command để clone project và thông báo copy file .env thủ công
program
.command('install')
.description('Clone and set up a new Justice project')
.option('--name <projectName>', 'Name of the project folder', 'justice-app')
.action(async ({ name }) => {
const projectDir = path.resolve(process.cwd(), name);
// Clone project vào thư mục người dùng chỉ định
console.log(chalk.green(`📦 Cloning into ${projectDir}...`));
await execPromise(
`git clone https://github.com/chuhong19/justice.git ${projectDir}`
);
// Thông báo cho người dùng copy file .env vào đúng vị trí
console.log(
chalk.green(
`📂 Please copy your .env file into the backend directory of ${projectDir}.`
)
);
console.log(
chalk.green(
'🚀 You can now run "justice-ctl start --name <project-name>" to start the project.'
)
);
});
// Command để khởi động project sau khi đã setup xong
program
.command('start')
.description('Start the Justice project using Docker Compose')
.option('--name <projectName>', 'Name of the project folder', 'justice-app') // Thêm tham số --name vào lệnh start
.action(async ({ name }) => {
const projectDir = path.resolve(process.cwd(), name); // Lấy tên dự án từ tham số --name
const envFilePath = path.resolve(projectDir, 'backend', '.env'); // Đảm bảo đúng đường dẫn tới file .env
if (!fs.existsSync(envFilePath)) {
console.log(
chalk.red(
`❌ .env file is missing! Please make sure to copy it into ${path.resolve(
projectDir,
'backend'
)}.`
)
);
return;
}
console.log(chalk.green('🚀 Starting the Justice App...'));
// Log ngay khi bắt đầu chạy docker-compose (với -d để chạy trong nền)
console.log(chalk.yellow('🔧 Running docker-compose up --build -d...'));
// Chạy docker-compose lên và kiểm tra kết quả
try {
const { stdout, stderr } = await execPromise(
`cd ${projectDir} && docker-compose up --build -d`
);
console.log(chalk.green('🚀 docker-compose completed'));
if (stderr) {
console.error(chalk.yellow(`⚠️ stderr: ${stderr}`));
}
console.log(chalk.blue(`✅ Docker Compose output: \n${stdout}`));
// Thêm một chút delay để đảm bảo các container đã có thời gian khởi động
console.log(chalk.yellow('⏳ Waiting for containers to start...'));
await new Promise((resolve) => setTimeout(resolve, 5000)); // 5 giây delay
// Kiểm tra xem container có chạy hay không
console.log(chalk.yellow('🔧 Running docker ps to check containers...'));
const { stdout: psOutput, stderr: psStderr } = await execPromise(
'docker ps'
);
if (psStderr) {
console.error(chalk.yellow(`⚠️ stderr: ${psStderr}`));
}
console.log(chalk.green('🚀 Docker containers are running:'));
console.log(chalk.cyan(`${psOutput}`)); // In ra output của docker ps để kiểm tra
} catch (error) {
console.error(chalk.red(`❌ Error: ${error.message}`));
}
});
// Đặt tên và mô tả cho chương trình CLI
program
.name('justice-ctl')
.description('CLI tool for Justice project')
.version('1.0.5');
// Parse các lệnh đã nhập từ người dùng
program.parse(process.argv);