Skip to content

Commit

Permalink
Generating files for authors too plus some format refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Jul 31, 2023
1 parent 7be8ba1 commit a46ad2b
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 130 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

lib/
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "eslint scripts/**/*.ts src/**/*.ts && tsc",
"build": "rm -rf dist && ts-node-esm scripts/build.ts"
},
"repository": {
"type": "git",
Expand All @@ -27,7 +28,8 @@
"eslint-plugin-n": "^16.0.1",
"eslint-plugin-promise": "^6.1.1",
"mkdirp": "^3.0.1",
"slugify": "^1.6.6",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
}
}
}
87 changes: 72 additions & 15 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ import { writeFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { mkdirp } from 'mkdirp'
import quotes, { type Quote, type QuoteWithIdAndLink } from '../src/quotes.js'
import slugify from 'slugify'
import quotes, { type Quote, type Author, type AuthorDescription, type RawQuote, type AuthorWithQuotes } from '../src/quotes.js'

const GH_PAGES_URL = 'https://fullStackbulletin.github.io/tech-quotes'
const baseUrl = process.env.BASE_URL ?? GH_PAGES_URL

const currentDir = dirname(fileURLToPath(import.meta.url))
const destPath: string = join(currentDir, '..', 'dist')
const quotesPath: string = join(currentDir, '..', 'dist', 'quotes')
const authorsPath: string = join(currentDir, '..', 'dist', 'authors')

// Creates the `api` folder if it does not exist
await mkdirp(destPath)
// Creates the `dest` and `dest/quotes` folders if they don't exist
await Promise.all([
mkdirp(destPath),
mkdirp(quotesPath),
mkdirp(authorsPath)
])

// Creates an index.html file that redirects to the GitHub repo
const index = `<html>
<head>
<meta http-equiv="refresh" content="0; url=https://github.com/FullStackBulletin/tech-quotes">
<meta http-equiv="refresh" content="0; url=${GH_PAGES_URL}">
</head>
<body></body>
</html>`
Expand All @@ -23,22 +33,35 @@ console.log(`Written ${destPath}/index.html`)

const stats = {
total: quotes.length,
all: 'https://fullStackbulletin.github.io/tech-quotes/all.json',
first: 'https://fullStackbulletin.github.io/tech-quotes/0.json',
last: `https://fullStackbulletin.github.io/tech-quotes/${quotes.length - 1}.json`,
urlPrefix: 'https://fullStackbulletin.github.io/tech-quotes/'
all: `${baseUrl}/quotes/all.json`,
first: `${baseUrl}/quotes/0.json`,
last: `${baseUrl}/quotes/${quotes.length - 1}.json`,
urlPrefix: baseUrl
}

await writeFile(`${destPath}/stats.json`, JSON.stringify(stats, null, 2))
console.log(`Written ${destPath}/stats.json`)

// Creates a JSON file for each quote and an all.json file with all the quotes
function mapQuote (id: string, quote: Quote): QuoteWithIdAndLink {
function mapQuote (id: string, quote: RawQuote): Quote {
const idAsNumber = Number(id)
return {
id: idAsNumber,
quote,
url: `https://fullStackbulletin.github.io/tech-quotes/${idAsNumber}.json`
text: quote.text,
author: makeAuthor(quote.authorName, quote.authorDescription, quote.authorWiki),
url: `${baseUrl}/quotes/${idAsNumber}.json`
}
}

function makeAuthor (name: string, description: AuthorDescription, wiki?: `https://en.wikipedia.org/wiki/${string}`): Author {
const slug = slugify.default(name, { lower: true, strict: true })

return {
id: slug,
name,
description,
wiki,
url: `${baseUrl}/authors/${slug}.json`
}
}

Expand All @@ -48,14 +71,48 @@ const all = {
first: 0,
last: quotes.length - 1
},
quotes: Object.entries(quotes).map(([id, fact]) => mapQuote(id, fact))
quotes: Object.entries(quotes).map(([id, quote]) => mapQuote(id, quote))
}

await writeFile(`${destPath}/all.json`, JSON.stringify(all, null, 2))
console.log(`Written ${destPath}/all.json`)
await writeFile(`${quotesPath}/all.json`, JSON.stringify(all, null, 2))
console.log(`Written ${quotesPath}/all.json`)

// As it goes through the various quotes starts to accumulate authors and quotes
const authorsWithQuotes = new Map<string, AuthorWithQuotes>()

for (const quote of all.quotes) {
const dest = join(destPath, `${String(quote.id)}.json`)
const dest = join(quotesPath, `${String(quote.id)}.json`)
await writeFile(dest, JSON.stringify(quote, null, 2))

const authorEntry = authorsWithQuotes.get(quote.author.id)
if (typeof authorEntry !== 'undefined') {
authorEntry.quotes.push(quote)
} else {
authorsWithQuotes.set(quote.author.id, {
...quote.author,
quotes: [quote]
})
}

console.log(`Written ${String(dest)}`)
}

// persists authors
let totalAuthors = 0
for (const author of authorsWithQuotes.values()) {
const dest = join(authorsPath, `${author.id}.json`)
await writeFile(dest, JSON.stringify(author, null, 2))
console.log(`Written ${String(dest)}`)
totalAuthors++
}

// Create all.json for authors
const allAuthors = {
metadata: {
total: totalAuthors
},
authors: [...authorsWithQuotes.keys()]
}

await writeFile(`${authorsPath}/all.json`, JSON.stringify(allAuthors, null, 2))
console.log(`Written ${authorsPath}/all.json`)
Loading

0 comments on commit a46ad2b

Please sign in to comment.