File size: 2,433 Bytes
c13f797
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import * as fs from 'fs';
import * as path from 'path';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { ar } from '../i18n';
import { GCC_LOCATIONS } from '../config';

const envPath = path.resolve(process.cwd(), '.env');

async function runOnboarding() {
  console.log(chalk.bold.cyan(ar.onboardingStart));

  const answers = await inquirer.prompt([
    {
      type: 'input',
      name: 'userName',
      message: ar.askName,
      default: process.env.SANE_USER_NAME || '',
    },
    {
      type: 'list',
      name: 'location',
      message: ar.askLocation,
      choices: Object.entries(GCC_LOCATIONS).map(([key, value]) => ({
        name: `${value.nameAr} (${value.name})`,
        value: key,
      })),
      default: process.env.SANE_LOCATION || 'kuwait_city',
    },
    {
      type: 'list',
      name: 'language',
      message: ar.askLanguage,
      choices: [
        { name: 'العربية', value: 'ar' },
        { name: 'English', value: 'en' },
      ],
      default: process.env.SANE_LANGUAGE || 'ar',
    },
  ]);

  // Create or update the .env file
  let envContent = '';
  if (fs.existsSync(envPath)) {
    envContent = fs.readFileSync(envPath, 'utf-8');
  }

  const updates = {
    SANE_USER_NAME: answers.userName,
    SANE_LOCATION: answers.location,
    SANE_LANGUAGE: answers.language,
    SANE_TIMEZONE: GCC_LOCATIONS[answers.location as keyof typeof GCC_LOCATIONS].timezone,
  };

  for (const [key, value] of Object.entries(updates)) {
    if (envContent.includes(`${key}=`)) {
      envContent = envContent.replace(new RegExp(`${key}=.*`), `${key}=${value}`);
    } else {
      envContent += `\n${key}=${value}`;
    }
  }

  // Ensure required keys from .env.example are present
  const exampleEnv = fs.readFileSync(path.resolve(process.cwd(), '.env.example'), 'utf-8');
  const exampleLines = exampleEnv.split('\n');
  for (const line of exampleLines) {
    if (line.startsWith('#') || !line.includes('=')) continue;
    const key = line.split('=')[0];
    if (!envContent.includes(`${key}=`)) {
      envContent += `\n${line}`;
    }
  }

  fs.writeFileSync(envPath, envContent.trim());

  console.log(chalk.bold.green(`\n${ar.setupComplete}`));
  console.log(chalk.gray(`تم حفظ الإعدادات في ملف .env`));
}

runOnboarding().catch(error => {
  console.error(chalk.red('❌ حدث خطأ أثناء الإعداد:'), error);
  process.exit(1);
});