From 586211415a8c58f99242864e8221c0e4c45051b7 Mon Sep 17 00:00:00 2001 From: nishantnz Date: Fri, 3 May 2024 18:38:40 +0530 Subject: [PATCH 1/2] react and vite build issue fixed including minor changes --- build-server/script.js | 145 +++++++++++++++++++++++------------------ 1 file changed, 83 insertions(+), 62 deletions(-) diff --git a/build-server/script.js b/build-server/script.js index 8adb3bf..b93052e 100644 --- a/build-server/script.js +++ b/build-server/script.js @@ -1,74 +1,95 @@ -const { exec } = require('child_process') -const path = require('path') -const fs = require('fs') -const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3') -const mime = require('mime-types') -const Redis = require('ioredis') - - -const publisher = new Redis('') +const { exec } = require("child_process"); +const path = require("path"); +const fs = require("fs"); +const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3"); +const mime = require("mime-types"); +const Redis = require("ioredis"); +const publisher = new Redis(""); const s3Client = new S3Client({ - region: '', - credentials: { - accessKeyId: '', - secretAccessKey: '' - } -}) - -const PROJECT_ID = process.env.PROJECT_ID + region: "", + credentials: { + accessKeyId: "", + secretAccessKey: "", + }, +}); +const PROJECT_ID = process.env.PROJECT_ID; function publishLog(log) { - publisher.publish(`logs:${PROJECT_ID}`, JSON.stringify({ log })) + publisher.publish(`logs:${PROJECT_ID}`, JSON.stringify({ log })); } async function init() { - console.log('Executing script.js') - publishLog('Build Started...') - const outDirPath = path.join(__dirname, 'output') - - const p = exec(`cd ${outDirPath} && npm install && npm run build`) - - p.stdout.on('data', function (data) { - console.log(data.toString()) - publishLog(data.toString()) - }) - - p.stdout.on('error', function (data) { - console.log('Error', data.toString()) - publishLog(`error: ${data.toString()}`) - }) - - p.on('close', async function () { - console.log('Build Complete') - publishLog(`Build Complete`) - const distFolderPath = path.join(__dirname, 'output', 'dist') - const distFolderContents = fs.readdirSync(distFolderPath, { recursive: true }) - - publishLog(`Starting to upload`) - for (const file of distFolderContents) { - const filePath = path.join(distFolderPath, file) - if (fs.lstatSync(filePath).isDirectory()) continue; - - console.log('uploading', filePath) - publishLog(`uploading ${file}`) - - const command = new PutObjectCommand({ - Bucket: 'vercel-clone-outputs', - Key: `__outputs/${PROJECT_ID}/${file}`, - Body: fs.createReadStream(filePath), - ContentType: mime.lookup(filePath) - }) + console.log("Executing script.js"); + publishLog("Build Started..."); + const outDirPath = path.join(__dirname, "output"); + + const p = exec(`cd ${outDirPath} && npm install && npm run build`); + + p.stdout.on("data", function (data) { + console.log(data.toString()); + publishLog(data.toString()); + }); + + p.stdout.on("error", function (data) { + console.log("Error", data.toString()); + publishLog(`error: ${data.toString()}`); + }); + + p.on("close", async function () { + console.log("Build Complete"); + publishLog(`Build Complete`); + + const possibleFolders = ["dist", "build"]; + let distFolderPath = null; + for (const folder of possibleFolders) { + const possibleFolderPath = path.join(__dirname, "output", folder); + if (existsSync(possibleFolderPath)) { + distFolderPath = possibleFolderPath; + break; + } + } + if (!distFolderPath) { + console.error("Neither 'dist' nor 'build' folder found!"); + publishLog("Neither 'dist' nor 'build' folder found!"); + return; + } - await s3Client.send(command) - publishLog(`uploaded ${file}`) - console.log('uploaded', filePath) - } - publishLog(`Done`) - console.log('Done...') - }) + const distFolderContents = fs.readdirSync(distFolderPath, { + recursive: true, + }); + + publishLog(`Starting to upload`); + for (const file of distFolderContents) { + const filePath = path.join(distFolderPath, file); + if (fs.lstatSync(filePath).isDirectory()) continue; + + console.log("Uploading", filePath); + publishLog(`Uploading ${file}`); + + const command = new PutObjectCommand({ + Bucket: "vercel-clone-outputs", + Key: `__outputs/${PROJECT_ID}/${file}`, + Body: fs.createReadStream(filePath), + ContentType: mime.lookup(filePath), + }); + try { + await s3Client.send(command); + publishLog(`Uploaded ${file}`); + console.log("Uploaded file", filename); + } catch (err) { + console.error("Error uploading file:", err); + publishLog(`Error uploading file: ${err}`); + throw err; + } + } + console.log("Uploaded", filePath); + publishLog("Upload complete"); + console.log("Done..."); + publishLog(`Done`); + }); } -init() \ No newline at end of file +init(); From 25c28a92da2e6f27df868fc0a9f31cf44d763c51 Mon Sep 17 00:00:00 2001 From: nishantnz Date: Fri, 3 May 2024 18:44:11 +0530 Subject: [PATCH 2/2] Upload Path Correction --- build-server/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-server/script.js b/build-server/script.js index b93052e..847d784 100644 --- a/build-server/script.js +++ b/build-server/script.js @@ -66,7 +66,7 @@ async function init() { const filePath = path.join(distFolderPath, file); if (fs.lstatSync(filePath).isDirectory()) continue; - console.log("Uploading", filePath); + console.log("Uploading", file); publishLog(`Uploading ${file}`); const command = new PutObjectCommand({