kawkab-frontend
Version:
Kawkab frontend is a frontend library for the Kawkab framework
68 lines (64 loc) ⢠2.77 kB
JavaScript
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
export function envCommand(program) {
program
.command('env')
.description('Create a working .env file, using .env.example as a template if it exists.')
.action(() => {
const projectRoot = process.cwd();
const exampleEnvPath = path.join(projectRoot, '.env.example');
const mainEnvPath = path.join(projectRoot, '.env');
let envContent;
let templateSource;
console.log(chalk.blue('š Setting up environment file...'));
// --- Step 1: Determine the source of the content ---
if (fs.existsSync(exampleEnvPath)) {
// If .env.example exists, use it as the source
console.log(chalk.cyan(' - Found existing template: .env.example'));
envContent = fs.readFileSync(exampleEnvPath, 'utf8');
templateSource = '.env.example';
}
else {
// If .env.example does not exist, use the default content and create it
console.log(chalk.cyan(' - No .env.example found, creating a default one.'));
envContent = `# Kawkab Application Environment Variables
# This is a template. Copy this to .env for your local setup.
# --- Application Settings ---
KAWKAB_APP_NAME="Kawkab App"
KAWKAB_APP_PORT=3000
# --- API and Services ---
# API_BASE_URL=http://localhost:3333/api
# SOCKET_URL=ws://localhost:3333
# --- Storage Settings ---
# STORAGE_TYPE=localStorage
`.trim();
try {
fs.writeFileSync(exampleEnvPath, envContent);
console.log(chalk.green(` ā Created template: .env.example`));
templateSource = 'default template';
}
catch (error) {
console.error(chalk.red('ā Failed to create .env.example:'), error);
process.exit(1);
}
}
// --- Step 2: Create .env if it doesn't exist ---
if (!fs.existsSync(mainEnvPath)) {
try {
fs.writeFileSync(mainEnvPath, envContent);
console.log(chalk.green(` ā Created working file .env from ${templateSource}.`));
console.log(chalk.bold.green('\nš Environment setup complete.'));
console.log(chalk.cyan('Next step: Edit the .env file with your specific configuration.'));
}
catch (error) {
console.error(chalk.red('ā Failed to create .env:'), error);
process.exit(1);
}
}
else {
console.log(chalk.yellow(' - Skipping, .env file already exists.'));
console.log(chalk.bold.green('\nš Environment setup complete.'));
}
});
}