| 15 | * @returns {object} - { success: boolean, message: string, data?: any } |
| 16 | */ |
| 17 | export async function uploadFileToGitHub(category, componentName, fileContent) { |
| 18 | const path = `src/uploads/components/${category}/${componentName}.jsx`; |
| 19 | const url = `https://api.github.com/repos/${OWNER}/${REPO}/contents/${path}`; |
| 20 | |
| 21 | // encode file content to base64 |
| 22 | const encodedContent = Buffer.from(fileContent).toString("base64"); |
| 23 | |
| 24 | try { |
| 25 | const response = await axios.put( |
| 26 | url, |
| 27 | { |
| 28 | message: `Added/Updated ${componentName} in ${category}`, |
| 29 | content: encodedContent, |
| 30 | }, |
| 31 | { |
| 32 | headers: { |
| 33 | Authorization: `token ${GITHUB_TOKEN}`, |
| 34 | "Content-Type": "application/json", |
| 35 | }, |
| 36 | } |
| 37 | ); |
| 38 | |
| 39 | console.log(`✅ File ${componentName}.jsx uploaded to GitHub at ${path}`); |
| 40 | |
| 41 | return { |
| 42 | success: true, |
| 43 | message: `File ${componentName}.jsx uploaded successfully`, |
| 44 | data: response.data, |
| 45 | }; |
| 46 | } catch (error) { |
| 47 | console.error("❌ Upload failed:", error.response?.data || error.message); |
| 48 | |
| 49 | return { |
| 50 | success: false, |
| 51 | message: error.response?.data?.message || "Failed to upload file to GitHub", |
| 52 | data: error.response?.data || error.message, |
| 53 | }; |
| 54 | } |
| 55 | } |