|
| 1 | +import { spawn } from 'node:child_process'; |
| 2 | +import fs from 'node:fs'; |
| 3 | + |
| 4 | +const envPath = '.env'; |
| 5 | +const envExamplePath = '.env.example'; |
| 6 | + |
| 7 | +const spawnWithLog = async (command, args, options) => { |
| 8 | + if (!options.silent) { |
| 9 | + console.log(`Executing: ${command} ${args.join(' ')}`); |
| 10 | + } |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + let output = ''; |
| 13 | + const childProcess = spawn(command, args, { |
| 14 | + ...options, |
| 15 | + env: { |
| 16 | + ...process.env, |
| 17 | + FORCE_COLOR: process.env.FORCE_COLOR || '1', |
| 18 | + }, |
| 19 | + stdio: ['inherit', 'pipe', 'pipe'] |
| 20 | + }); |
| 21 | + |
| 22 | + childProcess.stdout.on('data', (data) => { |
| 23 | + const str = data.toString(); |
| 24 | + if (!options.silent) { |
| 25 | + process.stdout.write(str); |
| 26 | + } |
| 27 | + output += str; |
| 28 | + }); |
| 29 | + |
| 30 | + if (!options.silent) { |
| 31 | + childProcess.stderr.on('data', (data) => { |
| 32 | + const str = data.toString(); |
| 33 | + process.stderr.write(str); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + childProcess.on('close', (code) => { |
| 38 | + if (code === 0) { |
| 39 | + resolve(output); |
| 40 | + } else { |
| 41 | + reject(new Error(`Process exited with code ${code}`)); |
| 42 | + } |
| 43 | + }); |
| 44 | + }); |
| 45 | +}; |
| 46 | + |
| 47 | +const waitDb = async () => { |
| 48 | + console.log('Waiting for database...'); |
| 49 | + while (true) { |
| 50 | + try { |
| 51 | + const db = await spawnWithLog('php', ['artisan', 'db:monitor'], { shell: true, encoding: 'utf8', silent: true }); |
| 52 | + if (db.includes('OK')) { |
| 53 | + break; |
| 54 | + } |
| 55 | + } catch (e) { |
| 56 | + } |
| 57 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +let envExists = true; |
| 62 | +if (!fs.existsSync(envPath)) { |
| 63 | + envExists = false; |
| 64 | + fs.copyFileSync(envExamplePath, envPath); |
| 65 | + console.log('Created .env file from .env.example'); |
| 66 | +} |
| 67 | + |
| 68 | +await spawnWithLog('composer', ['install'], { shell: true, encoding: 'utf8' }); |
| 69 | + |
| 70 | +if (fs.readFileSync(envPath, 'utf8').match(/^APP_KEY=$/m)) { |
| 71 | + await spawnWithLog('php', ['artisan', 'key:generate', '-n'], { shell: true, encoding: 'utf8' }); |
| 72 | +} |
| 73 | + |
| 74 | +await waitDb(); |
| 75 | +await spawnWithLog('php', ['artisan', 'migrate', '-n'], { shell: true, encoding: 'utf8' }); |
| 76 | +await spawnWithLog('php', ['artisan', 'db:seed', '-n'], { shell: true, encoding: 'utf8' }); |
| 77 | + |
| 78 | +if (!envExists) { |
| 79 | + // .envがないのにDB上にはある場合、強制的に再生成した方が恐らく楽 |
| 80 | + const passportOutput = await spawnWithLog('php', ['artisan', 'passport:install', '-n', '--force', '--no-ansi'], { shell: true, encoding: 'utf8' }); |
| 81 | + |
| 82 | + const personalAccessMatch = passportOutput.match(/Personal access client.*?Client ID.*?(\d+).*?Client secret.*?([A-Za-z0-9]+)/s); |
| 83 | + const passwordGrantMatch = passportOutput.match(/Password grant client.*?Client ID.*?(\d+).*?Client secret.*?([A-Za-z0-9]+)/s); |
| 84 | + if (personalAccessMatch && passwordGrantMatch) { |
| 85 | + const personalAccessClientId = personalAccessMatch[1]; |
| 86 | + const personalAccessClientSecret = personalAccessMatch[2]; |
| 87 | + |
| 88 | + const envContent = fs.readFileSync(envPath, 'utf8'); |
| 89 | + const newEnvContent = envContent + |
| 90 | + `\nPASSPORT_PERSONAL_ACCESS_CLIENT_ID=${personalAccessClientId}` + |
| 91 | + `\nPASSPORT_PERSONAL_ACCESS_CLIENT_SECRET=${personalAccessClientSecret}`; |
| 92 | + |
| 93 | + fs.writeFileSync(envPath, newEnvContent); |
| 94 | + console.log('Added Passport configuration to .env file'); |
| 95 | + } else { |
| 96 | + throw new Error('Failed to install Passport'); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +await spawnWithLog('chown', ['-R', 'www-data', '/var/www/html/storage'], { shell: true, encoding: 'utf8' }); |
| 101 | + |
| 102 | +console.log('Setup completed successfully!'); |
| 103 | +console.log("You can access the application at http://localhost:4545 ."); |
0 commit comments