@@ -2,18 +2,28 @@ import { writeFile } from 'node:fs/promises'
22import { dirname , join } from 'node:path'
33import { fileURLToPath } from 'node:url'
44import { 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
711const currentDir = dirname ( fileURLToPath ( import . meta. url ) )
812const 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
1424const 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
2434const 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
3242await writeFile ( `${ destPath } /stats.json` , JSON . stringify ( stats , null , 2 ) )
3343console . 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
5783for ( 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