|
| 1 | +import { exec } from "child_process"; |
| 2 | +import { DATA_DIR, TODAY } from "./config.js"; |
| 3 | +import { handleFetchFollowers } from "./fetchFollowers.js"; |
| 4 | +import { handleFetchBlogs } from "./fetchLatestBlogPosts.js"; |
| 5 | +import { writeFileSync, mkdirSync, renameSync, rmSync, existsSync } from "fs"; |
| 6 | +import { updateWorkflowNumber } from "./updateWorkflow.js"; |
| 7 | + |
| 8 | +async function main() { |
| 9 | + console.log("Starting data fetch process..."); |
| 10 | + |
| 11 | + try { |
| 12 | + console.log("Fetching followers and blogs..."); |
| 13 | + const [followers, blogs] = await Promise.all([ |
| 14 | + handleFetchFollowers(), |
| 15 | + handleFetchBlogs(), |
| 16 | + ]); |
| 17 | + await updateWorkflowNumber(); |
| 18 | + console.log( |
| 19 | + `Successfully fetched ${followers.length} followers and ${blogs.length} blogs, respectively.`, |
| 20 | + ); |
| 21 | + |
| 22 | + const TODAY_DIR_NAME = `${TODAY.getDate()}-${TODAY.getMonth() + 1}-${TODAY.getFullYear()}`; |
| 23 | + const targetDir = `${DATA_DIR}/${TODAY_DIR_NAME}`; |
| 24 | + |
| 25 | + console.log(`Creating directory: ${DATA_DIR}`); |
| 26 | + mkdirSync(`${DATA_DIR}`, { recursive: true }); |
| 27 | + |
| 28 | + console.log(`Creating temporary directory: ${TODAY_DIR_NAME}`); |
| 29 | + mkdirSync(`${TODAY_DIR_NAME}`, { recursive: true }); |
| 30 | + |
| 31 | + console.log("Writing followers data..."); |
| 32 | + writeFileSync( |
| 33 | + `${TODAY_DIR_NAME}/followers.json`, |
| 34 | + JSON.stringify(followers, null, 2), |
| 35 | + ); |
| 36 | + |
| 37 | + console.log("Writing blogs data..."); |
| 38 | + writeFileSync( |
| 39 | + `${TODAY_DIR_NAME}/blogs.json`, |
| 40 | + JSON.stringify(blogs, null, 2), |
| 41 | + ); |
| 42 | + |
| 43 | + console.log("Running formatter..."); |
| 44 | + exec("npm run format", (err, stdout, stderr) => { |
| 45 | + if (err) { |
| 46 | + console.error("Formatting failed:", err); |
| 47 | + return; |
| 48 | + } |
| 49 | + console.log("Formatting completed:", stdout); |
| 50 | + }); |
| 51 | + |
| 52 | + if (existsSync(targetDir)) { |
| 53 | + rmSync(targetDir, { recursive: true }); |
| 54 | + } |
| 55 | + renameSync(TODAY_DIR_NAME, targetDir); |
| 56 | + console.log(`Renamed ${TODAY_DIR_NAME} to ${targetDir}`); |
| 57 | + |
| 58 | + console.log("Process completed successfully!"); |
| 59 | + } catch (error) { |
| 60 | + console.error("An error occurred:", error); |
| 61 | + process.exit(1); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +main(); |
0 commit comments