From 30336bede1f2735a1496f9e81ec59730c9319b6c Mon Sep 17 00:00:00 2001 From: praliptarajoo Date: Thu, 18 Jan 2024 10:36:54 +0530 Subject: [PATCH] changes to update json with api data --- .github/workflows/ci.yml | 5 ++ package.json | 5 +- updateProject.js | 122 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 updateProject.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d71addb5..6cfff105 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,11 @@ jobs: - name: 'Build application' run: npm run build + + - name: 'Restore changes in json' + run: | + git restore src/app/projects/assets/upcomingProjects.json + git restore src/app/projects/assets/projects.json - name: 'Fetch branches' run: git fetch origin diff --git a/package.json b/package.json index 58bd4245..f60aef8f 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "dev": "next dev", - "build": "next build && touch ./build/.nojekyll", + "build": "npm run update-projects && next build && touch ./build/.nojekyll", "start": "next start", "lint": "next lint", "start:build": "serve ./build", @@ -13,7 +13,8 @@ "check-format": "npx prettier --check .", "format": "npx prettier --write .", "check-lint": "eslint . --ext ts --ext tsx --ext js", - "pre-commit": "npm run check-format && npm run check-lint && npm run check-types" + "pre-commit": "npm run check-format && npm run check-lint && npm run check-types", + "update-projects": "node updateProject.js" }, "dependencies": { "clsx": "^2.0.0", diff --git a/updateProject.js b/updateProject.js new file mode 100644 index 00000000..c739dc14 --- /dev/null +++ b/updateProject.js @@ -0,0 +1,122 @@ +// updateProjects.js +const fs = require("fs"); +const path = require("path"); + +async function getCurrentProjects() { + const query = `query getCurrentProjects { + foss_projects(filter: {project_type: { _eq: "current" }}) { + id, + title, + short_description, + github_repository_link, + documentation_link, + project_type, + date_created, + date_updated, + status, + } + }`; + + try { + process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; + + const response = await fetch("https://directus.ourgoalplan.co.in/graphql", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + query: query, + }), + }); + + if (response.ok) { + const { data } = await response.json(); + data.foss_projects.forEach((entry) => { + entry.id = parseInt(entry.id); + entry.shortDescription = entry.short_description; + entry.githubUrl = entry.github_repository_link; + entry.documentationUrl = entry.documentation_link; + + delete entry.short_description; + delete entry.github_repository_link; + delete entry.documentation_link; + }); + + const projectsJsonPath = path.join( + __dirname, + "src/app/projects/assets/projects.json" + ); + + fs.writeFileSync( + projectsJsonPath, + JSON.stringify(data.foss_projects, null, 2) + ); + + console.log("Projects updated successfully."); + } else { + console.log("API is not available. Using existing JSON."); + } + } catch (error) { + console.log(error); + } +} + +async function getUpcomingProjects() { + const query = `query getUpcomingProjects { + foss_projects(filter: {project_type: { _eq: "upcoming" }}) { + id, + title, + short_description, + github_repository_link, + documentation_link, + project_type, + date_created, + date_updated, + status, + } + }`; + + try { + process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; + + const response = await fetch("https://directus.ourgoalplan.co.in/graphql", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + query: query, + }), + }); + + if (response.ok) { + const { data } = await response.json(); + data.foss_projects.forEach((entry) => { + entry.id = parseInt(entry.id); + entry.shortDescription = entry.short_description; + entry.githubUrl = entry.github_repository_link; + entry.documentationUrl = entry.documentation_link; + + delete entry.short_description; + delete entry.github_repository_link; + delete entry.documentation_link; + }); + + const projectsJsonPath = path.join( + __dirname, + "src/app/projects/assets/upcomingProjects.json" + ); + + fs.writeFileSync( + projectsJsonPath, + JSON.stringify(data.foss_projects, null, 2) + ); + + console.log("Upcoming projects updated successfully."); + } else { + console.log("API is not available. Using existing JSON."); + } + } catch (error) { + console.log(error); + } +} + +getCurrentProjects(); +getUpcomingProjects();