quotes-nextjs

My favourite quotes. (nextjs)
git clone http://git.hanabi.in/repos/quotes-nextjs.git
Log | Files | Refs | LICENSE

commit 4b0d648fff82dcde59358522914de7a0c8b79c98
parent 3131c8ab9512c1f5b5b0759953d82a42ca56fb12
Author: Agastya Chandrakant <me@hanabi.in>
Date:   Thu, 15 Apr 2021 10:45:27 +0530

minimise number of read-writes

Diffstat:
AgenerateRandom.js | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MhelperFns.js | 2+-
Mpackage.json | 5+++--
Mpages/random.js | 20+-------------------
4 files changed, 82 insertions(+), 22 deletions(-)

diff --git a/generateRandom.js b/generateRandom.js @@ -0,0 +1,77 @@ +const { readdir, readFile } = require("fs/promises"); +const { join } = require("path"); +const { createWriteStream } = require("fs"); + +async function main() { + try { + const quotesANDauthors = []; + const authors = await readdir("quotes"); + for (const author of authors) { + const allFiles = await readdir(join("quotes", author)); + const { details, quotes } = getDetailsANDQuotes(allFiles); + + if (!details) throw new Error(`Error finding details`); + if (quotes.length < 1) throw new Error(`Not enough quotes`); + + for (const quote of quotes) { + const { data } = await readFrontMatter(author, quote); + + const { draft } = data; + if (draft) continue; + + const quoteObj = createQuoteObj(author, data, quote); + quotesANDauthors.push(quoteObj); + } + } + + const writeStream = createWriteStream("./random-quotes.js"); + writeStream.write(`module.exports = [`); + for (const obj of quotesANDauthors) writeStream.write(JSON.stringify(obj) + ","); + writeStream.write("]"); + } catch (err) { + errHandler(err); + } +} + +main(); + +const errHandler = (err) => { + console.error(err); +}; + +async function readFrontMatter(author, quote) { + const matter = require("gray-matter"); + + try { + const fileBuffer = await readFile(join("quotes", author, quote)); + const fileStr = fileBuffer.toString(); + const frontMatter = matter(fileStr); + return frontMatter; + } catch (err) { + errHandler(err); + } +} + +function makeUri(quote) { + return quote.replace(/\.md$/, ""); +} + +function getDetailsANDQuotes(allFiles) { + const details = allFiles.filter((_) => _ == "data.md")[0]; + const quotes = allFiles.filter((_) => _ != "data.md"); + return { details, quotes }; +} + +function createQuoteObj(author, data, quote) { + const { attributed, date, misattributed, title, unverified } = data; + const uri = makeUri(quote); + return { + attributed, + author, + date, + misattributed, + title, + unverified, + uri, + }; +} diff --git a/helperFns.js b/helperFns.js @@ -1,4 +1,4 @@ -export const join = require("path").join; +const join = require("path").join; export const errHandler = (err) => { console.error(err); diff --git a/package.json b/package.json @@ -4,8 +4,9 @@ "private": true, "scripts": { "dev": "next dev", - "build": "next build", - "start": "next start" + "build": "next build && node generateRandom.js", + "start": "next start", + "prestart": "node generateRandom.js" }, "dependencies": { "gray-matter": "^4.0.2", diff --git a/pages/random.js b/pages/random.js @@ -43,26 +43,8 @@ export async function getServerSideProps() { shuffleAndGiveRandom, } = require("../helperFns.js"); - const quotesANDauthors = []; try { - const authors = await readdir("quotes"); - for (const author of authors) { - const allFiles = await readdir(join("quotes", author)); - const { details, quotes } = getDetailsANDQuotes(allFiles); - - if (!details) throw new Error(`Error finding details`); - if (quotes.length < 1) throw new Error(`Not enough quotes`); - - for (const quote of quotes) { - const { data } = await readFrontMatter(author, quote); - - const { draft } = data; - if (draft) continue; - - const quoteObj = createQuoteObj(author, data, quote); - quotesANDauthors.push(quoteObj); - } - } + const quotesANDauthors = require("../random-quotes.js"); const randomQuote = shuffleAndGiveRandom(quotesANDauthors); const { author, uri } = randomQuote;