整合資料庫、完成登入註冊忘記密碼功能
This commit is contained in:
137
scripts/setup.js
Normal file
137
scripts/setup.js
Normal file
@@ -0,0 +1,137 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
// =====================================================
|
||||
// 專案快速設置腳本
|
||||
// =====================================================
|
||||
|
||||
const { execSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
console.log('🚀 開始設置 AI 展示平台...\n');
|
||||
|
||||
// 檢查 Node.js 版本
|
||||
function checkNodeVersion() {
|
||||
console.log('🔍 檢查 Node.js 版本...');
|
||||
const nodeVersion = process.version;
|
||||
const majorVersion = parseInt(nodeVersion.slice(1).split('.')[0]);
|
||||
|
||||
if (majorVersion < 18) {
|
||||
console.error('❌ Node.js 版本過低,需要 18.0.0 或更高版本');
|
||||
console.error(` 當前版本: ${nodeVersion}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`✅ Node.js 版本: ${nodeVersion}`);
|
||||
}
|
||||
|
||||
// 檢查 pnpm
|
||||
function checkPnpm() {
|
||||
console.log('🔍 檢查 pnpm...');
|
||||
try {
|
||||
execSync('pnpm --version', { stdio: 'pipe' });
|
||||
console.log('✅ pnpm 已安裝');
|
||||
} catch (error) {
|
||||
console.log('⚠️ pnpm 未安裝,正在安裝...');
|
||||
try {
|
||||
execSync('npm install -g pnpm', { stdio: 'inherit' });
|
||||
console.log('✅ pnpm 安裝成功');
|
||||
} catch (installError) {
|
||||
console.error('❌ pnpm 安裝失敗,請手動安裝');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 安裝依賴
|
||||
function installDependencies() {
|
||||
console.log('📦 安裝專案依賴...');
|
||||
try {
|
||||
execSync('pnpm install', { stdio: 'inherit' });
|
||||
console.log('✅ 依賴安裝完成');
|
||||
} catch (error) {
|
||||
console.error('❌ 依賴安裝失敗');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// 檢查環境變數文件
|
||||
function checkEnvFile() {
|
||||
console.log('🔍 檢查環境變數文件...');
|
||||
const envFile = path.join(process.cwd(), '.env.local');
|
||||
const envExampleFile = path.join(process.cwd(), 'env.example');
|
||||
|
||||
if (!fs.existsSync(envFile)) {
|
||||
if (fs.existsSync(envExampleFile)) {
|
||||
console.log('📝 創建環境變數文件...');
|
||||
fs.copyFileSync(envExampleFile, envFile);
|
||||
console.log('✅ 環境變數文件已創建 (.env.local)');
|
||||
console.log('⚠️ 請編輯 .env.local 文件並填入正確的配置');
|
||||
} else {
|
||||
console.log('⚠️ 未找到 env.example 文件');
|
||||
}
|
||||
} else {
|
||||
console.log('✅ 環境變數文件已存在');
|
||||
}
|
||||
}
|
||||
|
||||
// 測試資料庫連接
|
||||
function testDatabaseConnection() {
|
||||
console.log('🔍 測試資料庫連接...');
|
||||
try {
|
||||
execSync('pnpm run test:db', { stdio: 'inherit' });
|
||||
console.log('✅ 資料庫連接測試成功');
|
||||
} catch (error) {
|
||||
console.log('⚠️ 資料庫連接測試失敗,請檢查配置');
|
||||
console.log(' 您可以稍後運行: pnpm run test:db');
|
||||
}
|
||||
}
|
||||
|
||||
// 執行資料庫遷移
|
||||
function runMigration() {
|
||||
console.log('🗄️ 執行資料庫遷移...');
|
||||
try {
|
||||
execSync('pnpm run migrate', { stdio: 'inherit' });
|
||||
console.log('✅ 資料庫遷移完成');
|
||||
} catch (error) {
|
||||
console.log('⚠️ 資料庫遷移失敗,請檢查資料庫配置');
|
||||
console.log(' 您可以稍後運行: pnpm run migrate');
|
||||
}
|
||||
}
|
||||
|
||||
// 顯示完成信息
|
||||
function showCompletionMessage() {
|
||||
console.log('\n🎉 設置完成!');
|
||||
console.log('\n📋 下一步操作:');
|
||||
console.log('1. 編輯 .env.local 文件,填入正確的資料庫配置');
|
||||
console.log('2. 運行資料庫遷移: pnpm run migrate');
|
||||
console.log('3. 測試資料庫連接: pnpm run test:db');
|
||||
console.log('4. 啟動開發服務器: pnpm run dev');
|
||||
console.log('\n📚 更多信息請查看:');
|
||||
console.log('- README-DATABASE.md (資料庫文檔)');
|
||||
console.log('- PROJECT_ANALYSIS.md (專案解析)');
|
||||
console.log('- SOFTWARE_SPECIFICATION.md (軟體規格)');
|
||||
}
|
||||
|
||||
// 主函數
|
||||
async function main() {
|
||||
try {
|
||||
checkNodeVersion();
|
||||
checkPnpm();
|
||||
installDependencies();
|
||||
checkEnvFile();
|
||||
testDatabaseConnection();
|
||||
runMigration();
|
||||
showCompletionMessage();
|
||||
} catch (error) {
|
||||
console.error('❌ 設置過程中發生錯誤:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// 執行設置
|
||||
if (require.main === module) {
|
||||
main().catch(console.error);
|
||||
}
|
||||
|
||||
module.exports = { main };
|
Reference in New Issue
Block a user