| import { exec } from 'child_process'; |
| import { promisify } from 'util'; |
| import { unlink } from 'fs/promises'; |
| import path from 'path'; |
| import { fileURLToPath } from 'url'; |
|
|
| const execAsync = promisify(exec); |
|
|
| const zipUrl = process.env.ZIP_URL; |
| const downloadPath = '/tmp/app.zip'; |
| const extractPath = '/app'; |
|
|
| async function runCommand(command) { |
| try { |
| const { stdout, stderr } = await execAsync(command); |
| console.log(`Command executed successfully: ${command}`); |
| if (stdout) console.log(stdout); |
| if (stderr) console.error(stderr); |
| return stdout; |
| } catch (error) { |
| console.error(`Error executing command: ${command}`); |
| console.error(error.stderr || error.message); |
| throw error; |
| } |
| } |
|
|
| async function main() { |
| if (!zipUrl) { |
| console.error("Error: ZIP_URL environment variable not set."); |
| process.exit(1); |
| } |
|
|
| try { |
| console.log(`Downloading ZIP file from: ${zipUrl} to ${downloadPath}`); |
| await runCommand(`wget "${zipUrl}" -O ${downloadPath}`); |
|
|
| console.log(`Extracting ZIP file to: ${extractPath}`); |
| await runCommand(`unzip -o ${downloadPath} -d ${extractPath}`); |
|
|
| console.log("Removing downloaded ZIP file..."); |
| await unlink(downloadPath); |
|
|
| console.log("Starting the application..."); |
| await runCommand('npm start'); |
|
|
| } catch (error) { |
| console.error("An error occurred:", error); |
| process.exit(1); |
| } |
| } |
|
|
| main(); |
|
|