Skip to content

Commit a46ad2b

Browse files
committed
Generating files for authors too plus some format refactoring
1 parent 7be8ba1 commit a46ad2b

File tree

6 files changed

+221
-130
lines changed

6 files changed

+221
-130
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,5 @@ dist
128128
.yarn/build-state.yml
129129
.yarn/install-state.gz
130130
.pnp.*
131+
132+
lib/

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"type": "module",
77
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "eslint scripts/**/*.ts src/**/*.ts && tsc",
9+
"build": "rm -rf dist && ts-node-esm scripts/build.ts"
910
},
1011
"repository": {
1112
"type": "git",
@@ -27,7 +28,8 @@
2728
"eslint-plugin-n": "^16.0.1",
2829
"eslint-plugin-promise": "^6.1.1",
2930
"mkdirp": "^3.0.1",
31+
"slugify": "^1.6.6",
3032
"ts-node": "^10.9.1",
3133
"typescript": "^5.1.6"
3234
}
33-
}
35+
}

scripts/build.ts

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@ import { writeFile } from 'node:fs/promises'
22
import { dirname, join } from 'node:path'
33
import { fileURLToPath } from 'node:url'
44
import { mkdirp } from 'mkdirp'
5-
import quotes, { type Quote, type QuoteWithIdAndLink } from '../src/quotes.js'
5+
import slugify from 'slugify'
6+
import quotes, { type Quote, type Author, type AuthorDescription, type RawQuote, type AuthorWithQuotes } from '../src/quotes.js'
7+
8+
const GH_PAGES_URL = 'https://fullStackbulletin.github.io/tech-quotes'
9+
const baseUrl = process.env.BASE_URL ?? GH_PAGES_URL
610

711
const currentDir = dirname(fileURLToPath(import.meta.url))
812
const destPath: string = join(currentDir, '..', 'dist')
13+
const quotesPath: string = join(currentDir, '..', 'dist', 'quotes')
14+
const authorsPath: string = join(currentDir, '..', 'dist', 'authors')
915

10-
// Creates the `api` folder if it does not exist
11-
await mkdirp(destPath)
16+
// Creates the `dest` and `dest/quotes` folders if they don't exist
17+
await Promise.all([
18+
mkdirp(destPath),
19+
mkdirp(quotesPath),
20+
mkdirp(authorsPath)
21+
])
1222

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

2434
const stats = {
2535
total: quotes.length,
26-
all: 'https://fullStackbulletin.github.io/tech-quotes/all.json',
27-
first: 'https://fullStackbulletin.github.io/tech-quotes/0.json',
28-
last: `https://fullStackbulletin.github.io/tech-quotes/${quotes.length - 1}.json`,
29-
urlPrefix: 'https://fullStackbulletin.github.io/tech-quotes/'
36+
all: `${baseUrl}/quotes/all.json`,
37+
first: `${baseUrl}/quotes/0.json`,
38+
last: `${baseUrl}/quotes/${quotes.length - 1}.json`,
39+
urlPrefix: baseUrl
3040
}
3141

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

3545
// Creates a JSON file for each quote and an all.json file with all the quotes
36-
function mapQuote (id: string, quote: Quote): QuoteWithIdAndLink {
46+
function mapQuote (id: string, quote: RawQuote): Quote {
3747
const idAsNumber = Number(id)
3848
return {
3949
id: idAsNumber,
40-
quote,
41-
url: `https://fullStackbulletin.github.io/tech-quotes/${idAsNumber}.json`
50+
text: quote.text,
51+
author: makeAuthor(quote.authorName, quote.authorDescription, quote.authorWiki),
52+
url: `${baseUrl}/quotes/${idAsNumber}.json`
53+
}
54+
}
55+
56+
function makeAuthor (name: string, description: AuthorDescription, wiki?: `https://en.wikipedia.org/wiki/${string}`): Author {
57+
const slug = slugify.default(name, { lower: true, strict: true })
58+
59+
return {
60+
id: slug,
61+
name,
62+
description,
63+
wiki,
64+
url: `${baseUrl}/authors/${slug}.json`
4265
}
4366
}
4467

@@ -48,14 +71,48 @@ const all = {
4871
first: 0,
4972
last: quotes.length - 1
5073
},
51-
quotes: Object.entries(quotes).map(([id, fact]) => mapQuote(id, fact))
74+
quotes: Object.entries(quotes).map(([id, quote]) => mapQuote(id, quote))
5275
}
5376

54-
await writeFile(`${destPath}/all.json`, JSON.stringify(all, null, 2))
55-
console.log(`Written ${destPath}/all.json`)
77+
await writeFile(`${quotesPath}/all.json`, JSON.stringify(all, null, 2))
78+
console.log(`Written ${quotesPath}/all.json`)
79+
80+
// As it goes through the various quotes starts to accumulate authors and quotes
81+
const authorsWithQuotes = new Map<string, AuthorWithQuotes>()
5682

5783
for (const quote of all.quotes) {
58-
const dest = join(destPath, `${String(quote.id)}.json`)
84+
const dest = join(quotesPath, `${String(quote.id)}.json`)
5985
await writeFile(dest, JSON.stringify(quote, null, 2))
86+
87+
const authorEntry = authorsWithQuotes.get(quote.author.id)
88+
if (typeof authorEntry !== 'undefined') {
89+
authorEntry.quotes.push(quote)
90+
} else {
91+
authorsWithQuotes.set(quote.author.id, {
92+
...quote.author,
93+
quotes: [quote]
94+
})
95+
}
96+
6097
console.log(`Written ${String(dest)}`)
6198
}
99+
100+
// persists authors
101+
let totalAuthors = 0
102+
for (const author of authorsWithQuotes.values()) {
103+
const dest = join(authorsPath, `${author.id}.json`)
104+
await writeFile(dest, JSON.stringify(author, null, 2))
105+
console.log(`Written ${String(dest)}`)
106+
totalAuthors++
107+
}
108+
109+
// Create all.json for authors
110+
const allAuthors = {
111+
metadata: {
112+
total: totalAuthors
113+
},
114+
authors: [...authorsWithQuotes.keys()]
115+
}
116+
117+
await writeFile(`${authorsPath}/all.json`, JSON.stringify(allAuthors, null, 2))
118+
console.log(`Written ${authorsPath}/all.json`)

0 commit comments

Comments
 (0)