|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { spawn } from 'child_process'; |
| 4 | + |
| 5 | +const publicDir = path.join(process.cwd(), 'public'); |
| 6 | +const requiredFiles = [ |
| 7 | + path.join(publicDir, 'config.yaml'), |
| 8 | + path.join(publicDir, 'generated', 'index', 'pieces.json'), |
| 9 | + path.join(publicDir, 'generated', 'index', 'pages.json') |
| 10 | +]; |
| 11 | + |
| 12 | +const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); |
| 13 | + |
| 14 | +async function waitForFiles(timeoutMs: number, intervalMs: number): Promise<void> { |
| 15 | + console.log('[og-image-worker]: waiting for index files...'); |
| 16 | + const start = Date.now(); |
| 17 | + let lastLogTime = 0; |
| 18 | + while (Date.now() - start < timeoutMs) { |
| 19 | + const missingFiles = requiredFiles.filter(filePath => !fs.existsSync(filePath)); |
| 20 | + if (missingFiles.length === 0) { |
| 21 | + console.log('[og-image-worker]: all index files found'); |
| 22 | + return; |
| 23 | + } |
| 24 | + const elapsed = Math.floor((Date.now() - start) / 1000); |
| 25 | + if (elapsed - lastLogTime >= 30) { |
| 26 | + console.log(`[og-image-worker]: still waiting... (${elapsed}s, missing: ${missingFiles.map(f => path.basename(f)).join(', ')})`); |
| 27 | + lastLogTime = elapsed; |
| 28 | + } |
| 29 | + await sleep(intervalMs); |
| 30 | + } |
| 31 | + throw new Error('Timed out waiting for index files'); |
| 32 | +} |
| 33 | + |
| 34 | +async function run(): Promise<void> { |
| 35 | + console.log('[og-image-worker]: starting worker...'); |
| 36 | + await waitForFiles(600000, 2000); |
| 37 | + console.log('[og-image-worker]: running build:og...'); |
| 38 | + await new Promise<void>((resolve, reject) => { |
| 39 | + const command = process.platform === 'win32' ? 'npm.cmd' : 'npm'; |
| 40 | + const child = spawn(command, ['run', 'build:og'], { stdio: 'inherit' }); |
| 41 | + child.on('error', reject); |
| 42 | + child.on('close', code => { |
| 43 | + if (code === 0) { |
| 44 | + console.log('[og-image-worker]: worker complete'); |
| 45 | + resolve(); |
| 46 | + return; |
| 47 | + } |
| 48 | + reject(new Error(`build:og exited with code ${code}`)); |
| 49 | + }); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +run() |
| 54 | + .then(() => { |
| 55 | + process.exit(0); |
| 56 | + }) |
| 57 | + .catch(error => { |
| 58 | + console.error('[og-image-worker]: error running worker:', error); |
| 59 | + process.exit(1); |
| 60 | + }); |
0 commit comments