Skip to content

Commit

Permalink
Merge pull request #25 from plezanje-net/prebuildGrades
Browse files Browse the repository at this point in the history
Add a prebuild script to fetch all gradingsystems
  • Loading branch information
demshy authored Oct 22, 2023
2 parents 33a8b6a + 928880e commit 91ccc94
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1,799 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev -p 3003",
"dev": "npm run gengradsys && next dev -p 3003",
"prebuild": "npm run gengradsys",
"build": "next build",
"start": "next start -p 3003",
"lint": "next lint",
"codegen": "graphql-codegen"
"codegen": "graphql-codegen",
"gengradsys": "node ./src/scripts/generate-grading-systems.mjs"
},
"dependencies": {
"@headlessui/react": "^1.7.15",
Expand Down
55 changes: 55 additions & 0 deletions src/scripts/generate-grading-systems.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pkg from "@next/env";
const { loadEnvConfig } = pkg;

import fs from "fs";
import path from "path";

console.log("Fetching all grading systems from server...");

loadEnvConfig(process.cwd());

const fullFilePath = path.join(
process.cwd(),
"src/utils",
"grading-systems.ts"
);

const url = process.env.NEXT_PUBLIC_API_URL;

const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `{
gradingSystems {
name
id
grades {
difficulty
name
id
__typename
}
routeTypes {
id
name
__typename
}
__typename
}
}`,
}),
});

const data = await response.json();

fs.writeFileSync(
fullFilePath,
"/* This is an auto-generated file. */\n" +
"export const gradingSystems = " +
JSON.stringify(data.data.gradingSystems)
);

console.log("All grading systems stored into " + fullFilePath);
Loading

0 comments on commit 91ccc94

Please sign in to comment.