feat: Add interactive setup script
Added an interactive Node.js script (setup.js) to guide users through the initial environment setup. This script prompts for necessary database credentials and generates JWT secrets, then creates/updates the .env file. A 'setup' script has been added to api.hyungi.net/package.json for easy execution. This improves the first-time setup experience by streamlining the .env file creation process.
This commit is contained in:
82
setup.js
Normal file
82
setup.js
Normal file
@@ -0,0 +1,82 @@
|
||||
// setup.js
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const readline = require('readline');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const envExamplePath = path.join(__dirname, '.env.example');
|
||||
const envPath = path.join(__dirname, '.env');
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
function question(query) {
|
||||
return new Promise(resolve => {
|
||||
rl.question(query, resolve);
|
||||
});
|
||||
}
|
||||
|
||||
function generateSecret() {
|
||||
return crypto.randomBytes(32).toString('hex');
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log('🚀 TK-FB-Project 환경 설정 스크립트를 시작합니다.');
|
||||
console.log('--------------------------------------------------');
|
||||
|
||||
if (fs.existsSync(envPath)) {
|
||||
const answer = await question('⚠️ `.env` 파일이 이미 존재합니다. 덮어쓰시겠습니까? (y/N): ');
|
||||
if (answer.toLowerCase() !== 'y') {
|
||||
console.log('🚫 설정을 중단합니다. 기존 .env 파일을 사용합니다.');
|
||||
rl.close();
|
||||
return;
|
||||
}
|
||||
console.log('기존 `.env` 파일을 덮어씁니다.');
|
||||
}
|
||||
|
||||
let envExampleContent;
|
||||
try {
|
||||
envExampleContent = fs.readFileSync(envExamplePath, 'utf8');
|
||||
} catch (error) {
|
||||
console.error(`❌ '.env.example' 파일을 읽을 수 없습니다. 설정 템플릿이 필요합니다.`);
|
||||
rl.close();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('\n데이터베이스 설정을 시작합니다. 안전한 비밀번호를 입력해주세요.');
|
||||
|
||||
const rootPassword = await question('🔑 MariaDB/MySQL ROOT 비밀번호를 입력하세요: ');
|
||||
const userPassword = await question('🔑 데이터베이스 사용자 비밀번호를 입력하세요: ');
|
||||
|
||||
if (!rootPassword || !userPassword) {
|
||||
console.error('❌ 비밀번호는 비워둘 수 없습니다. 스크립트를 다시 실행해주세요.');
|
||||
rl.close();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('\n🔐 JWT 시크릿 키를 자동으로 생성합니다...');
|
||||
const jwtSecret = generateSecret();
|
||||
const jwtRefreshSecret = generateSecret();
|
||||
console.log('✅ JWT 시크릿 키 생성이 완료되었습니다.');
|
||||
|
||||
let newEnvContent = envExampleContent;
|
||||
newEnvContent = newEnvContent.replace('change_this_root_password_min_12_chars', rootPassword);
|
||||
newEnvContent = newEnvContent.replace(/change_this_user_password_min_12_chars/g, userPassword);
|
||||
newEnvContent = newEnvContent.replace('change_this_to_random_string_min_32_chars', jwtSecret);
|
||||
newEnvContent = newEnvContent.replace('change_this_to_another_random_string_min_32_chars', jwtRefreshSecret);
|
||||
|
||||
try {
|
||||
fs.writeFileSync(envPath, newEnvContent, 'utf8');
|
||||
console.log('\n✅ `.env` 파일 생성이 완료되었습니다!');
|
||||
console.log('이제 다음 명령어를 실행하여 프로젝트를 시작할 수 있습니다:');
|
||||
console.log('\x1b[36m%s\x1b[0m', '\ndocker-compose up -d --build\n');
|
||||
} catch (error) {
|
||||
console.error('❌ `.env` 파일 저장 중 오류가 발생했습니다:', error);
|
||||
}
|
||||
|
||||
rl.close();
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user