2021-10-17 00:05:22 +03:00
const { promises : fs } = require ( "fs" ) ;
const path = require ( "path" ) ;
const { html : beautifyHtml } = require ( 'js-beautify' ) ;
const buildConfig = require ( "./buildconfig" ) ;
// source: https://stackoverflow.com/a/41407246
const escapeCodes = {
reset : "\x1b[0m" ,
bright : "\x1b[1m" ,
dim : "\x1b[2m" ,
underscore : "\x1b[4m" ,
blink : "\x1b[5m" ,
reverse : "\x1b[7m" ,
hidden : "\x1b[8m" ,
fgBlack : "\x1b[30m" ,
fgRed : "\x1b[31m" ,
fgGreen : "\x1b[32m" ,
fgYellow : "\x1b[33m" ,
fgBlue : "\x1b[34m" ,
fgMagenta : "\x1b[35m" ,
fgCyan : "\x1b[36m" ,
fgWhite : "\x1b[37m" ,
bgBlack : "\x1b[40m" ,
bgRed : "\x1b[41m" ,
bgGreen : "\x1b[42m" ,
bgYellow : "\x1b[43m" ,
bgBlue : "\x1b[44m" ,
bgMagenta : "\x1b[45m" ,
bgCyan : "\x1b[46m" ,
bgWhite : "\x1b[47m" ,
} ;
if ( ! buildConfig . allowSpecialCharacters ) {
for ( i in escapeCodes ) {
escapeCodes [ i ] = "" ;
}
}
const specialCharacter = ( c ) => {
if ( buildConfig . allowSpecialCharacters ) {
return c ;
}
return "" ;
}
2021-10-18 21:54:41 +03:00
const processPageOutput = async ( out ) => {
if ( typeof out === "function" )
out = await out ( ) ;
if ( typeof out !== "string" )
2021-10-17 00:05:22 +03:00
throw new Error ( "got non-string page output (maybe one of the pages isn't exporting a string?)" ) ;
if ( buildConfig . postProcessing . trimOutput ) {
out = out . trim ( ) ;
}
if ( buildConfig . postProcessing . beautifyOutput ) {
out = beautifyHtml ( out , buildConfig . postProcessing . beautifyOutputOptions ) ;
}
return out ;
} ;
const findAllPages = async ( directory ) =>
( await fs . readdir ( directory ) )
. filter (
f => f . endsWith ( buildConfig . pageExtension )
)
. map ( f => directory + "/" + f ) ; // TODO: hack
2021-10-18 23:25:36 +03:00
const buildPage = async ( pagePath ) => {
const processed = await processPageOutput ( require ( pagePath ) ) ;
console . log ( ` ${ escapeCodes . fgGreen } ${ specialCharacter ( "→" ) } ${ escapeCodes . reset } Built ${ escapeCodes . fgBlue } ${ path . parse ( path . basename ( pagePath , buildConfig . pageExtension ) ) . name } ${ escapeCodes . reset } ` ) ;
return processed
} ;
2021-10-17 00:05:22 +03:00
const buildAllPages = async ( directory ) =>
2021-10-18 21:54:41 +03:00
await Promise . all ( ( await findAllPages ( directory ) )
2021-10-17 00:05:22 +03:00
. map (
2021-10-18 21:54:41 +03:00
async p => [ p , ( await buildPage ( p ) ) ]
) ) ;
2021-10-17 00:05:22 +03:00
const exportAllPages = async ( sourcePath , outputPath ) => {
const pages = await buildAllPages ( sourcePath ) ;
for ( const [ pagePath , pageContent ] of pages ) {
const pageName = path . parse ( path . basename ( pagePath , buildConfig . pageExtension ) ) . name ;
await fs . writeFile ( path . join ( outputPath , pageName + ".html" ) , pageContent ) ;
}
return pages . length ;
} ;
const main = async ( ) => {
const startTime = new Date ( ) ;
const builtPages = await exportAllPages ( buildConfig . sourceDirectory , buildConfig . outputDirectory ) ;
console . log ( ` ${ escapeCodes . fgGreen } ${ specialCharacter ( "✓" ) } Done! ${ escapeCodes . fgBlue } Built ${ builtPages } pages in ${ ( ( new Date ( ) ) - startTime ) } ms. ${ escapeCodes . reset } ` ) ;
} ;
main ( ) ;