2021-07-02 11:41:57 +00:00
import { readFileSync , writeFileSync , copyFileSync } from "fs" ;
import { dirname , join } from 'path' ;
import { fileURLToPath } from 'url' ;
import glob from 'glob' ;
const _ _dirname = dirname ( fileURLToPath ( import . meta . url ) ) ;
const genAuthors = ( a ) => {
let authors = [ ] ;
if ( typeof a === "string" ) {
authors = a . split ( ', ' ) ;
} else if ( Array . isArray ( a ) ) {
authors = a ;
} ;
return authors . map ( ( x , i ) => {
if ( typeof x === 'object' ) { // User object
const pfp = ` <img loading=lazy style="display: inline; border-radius: 50%; margin-right: 5px; vertical-align: bottom;" src="https://cdn.discordapp.com/avatars/ ${ x . i } / ${ x . a } .png?size=32"> ` ;
const name = ` <span class="author" style="line-height: 32px;"> ${ x . n } </span> ` ; //<span class="description-3_Ncsb">#${result.discriminator}</span></span>`;
return i > 1 ? pfp : pfp + name ;
}
let idMatch = x . match ( /(.*) \(([0-9]{17,18})\)/ ) ; // "<name> (<id>)"
if ( idMatch === null ) return ` <span class="author"> ${ x } </span> ` ; // "<name>"
return ` <span class="author"> ${ idMatch [ 1 ] } </span> ` ;
} ) . join ( '<span class="description-3_Ncsb">,</span> ' ) ;
} ;
const makeCard = ( m ) => {
const authors = genAuthors ( m . authors ) ;
return ` <div class="gm-store-card ${ m . tags . join ( ' ' ) } " data-last-updated= ${ m . lastUpdated } data-stars= ${ m . github . stars } >
< div style = "background-image: url('${m.images?.length ? m.images[0] : ''}');" > < / d i v >
< div class = "title-31JmR4 ${!authors.includes('avatar') ? 'no-pfp' : ''}" > $ { authors } < / d i v >
< div class = "title-31JmR4" > $ { m . name } < / d i v >
< div class = "colorStandard-2KCXvj size14-e6ZScH default-3nhoK- formText-3fs7AJ description-3_Ncsb formText-3fs7AJ modeDefault-3a2Ph1" > $ { m . description } < / d i v >
< div >
< div class = "colorStandard-2KCXvj size14-e6ZScH default-3nhoK- formText-3fs7AJ description-3_Ncsb formText-3fs7AJ modeDefault-3a2Ph1" >
< span > $ { m . github . stars } < / s p a n >
< svg width = "16" height = "16" viewBox = "0 0 24 24" fill = "currentColor" > < path d = "M12 .587l3.668 7.568 8.332 1.151-6.064 5.828 1.48 8.279-7.416-3.967-7.417 3.967 1.481-8.279-6.064-5.828 8.332-1.151z" > < / p a t h > < / s v g >
< / d i v >
< div class = "colorStandard-2KCXvj size14-e6ZScH default-3nhoK- formText-3fs7AJ description-3_Ncsb formText-3fs7AJ modeDefault-3a2Ph1" > $ { m . version === '0' || m . version . toLowerCase ( ) . includes ( 'auto' ) ? '' : ` v ${ m . version } ` } < / d i v >
< / d i v >
< / d i v > ` ;
2021-07-02 18:22:57 +00:00
} ;
const makeRepo = ( r ) => {
return ` <a href=" ${ r . filename } " class="repo">
< div >
< div class = "title-31JmR4" > $ { r . name } < / d i v >
< div class = "colorStandard-2KCXvj size14-e6ZScH default-3nhoK- formText-3fs7AJ description-3_Ncsb formText-3fs7AJ modeDefault-3a2Ph1" > $ { r . description } < / d i v >
< / d i v >
< div class = "colorStandard-2KCXvj size14-e6ZScH default-3nhoK- formText-3fs7AJ description-3_Ncsb formText-3fs7AJ modeDefault-3a2Ph1" >
< div > < strong > $ { r . themes } < / s t r o n g > t h e m e s < / d i v >
< div > < strong > $ { r . plugins } < / s t r o n g > p l u g i n s < / d i v >
< div > < strong > $ { r . developers } < / s t r o n g > d e v e l o p e r s < / d i v >
< / d i v >
< / a > ` ;
} ;
2021-07-02 11:41:57 +00:00
export default ( ) => {
let template = readFileSync ( join ( _ _dirname , 'template.html' ) , 'utf8' ) ;
const repos = glob . sync ( join ( global . distDir , '*.json' ) ) ;
let cards = [ ] ;
2021-07-02 18:22:57 +00:00
let metas = [ ] ;
2021-07-02 11:41:57 +00:00
2021-07-02 16:18:08 +00:00
let name = 'GooseMod Store' ;
2021-07-02 17:06:45 +00:00
let description = '' ;
2021-07-02 16:18:08 +00:00
2021-07-02 11:41:57 +00:00
for ( const repo of repos ) {
2021-07-02 16:18:08 +00:00
console . log ( repo ) ;
2021-07-02 11:41:57 +00:00
const json = JSON . parse ( readFileSync ( repo , 'utf8' ) ) ;
2021-07-02 16:18:08 +00:00
if ( repos . length === 1 ) { // 1 repo likely means a custom repo, so use that name and description
name = json . meta . name ;
2021-07-02 17:06:45 +00:00
description = ` ${ json . meta . description } ` ;
2021-07-02 16:18:08 +00:00
}
2021-07-02 11:41:57 +00:00
cards = cards . concat ( json . modules ) ;
2021-07-02 18:22:57 +00:00
metas . push ( {
... json . meta ,
themes : json . modules . filter ( ( x ) => x . tags . includes ( 'theme' ) ) . length ,
plugins : json . modules . filter ( ( x ) => ! x . tags . includes ( 'theme' ) ) . length ,
developers : Object . keys ( json . modules . reduce ( ( acc , x ) => { ( ! Array . isArray ( x . authors ) ? [ x . authors ] : x . authors ) . forEach ( ( a ) => acc [ typeof a === 'object' ? a . i : a ] = true ) ; return acc ; } , { } ) ) . length ,
filename : repo . split ( '/' ) . pop ( )
} ) ;
2021-07-02 11:41:57 +00:00
}
2021-07-02 17:06:45 +00:00
description += ` Browse ${ cards . filter ( ( x ) => x . tags . includes ( 'theme' ) ) . length } themes and ${ cards . filter ( ( x ) => ! x . tags . includes ( 'theme' ) ) . length } plugins from ${ Object . keys ( cards . reduce ( ( acc , x ) => { ( ! Array . isArray ( x . authors ) ? [ x . authors ] : x . authors ) . forEach ( ( a ) => acc [ typeof a === 'object' ? a . i : a ] = true ) ; return acc ; } , {})).length} developers. ` ;
2021-07-02 11:41:57 +00:00
cards = cards . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) . map ( ( x ) => makeCard ( x ) ) ;
2021-07-02 16:18:08 +00:00
template = template
. replace ( 'ALL_CARDS' , cards . join ( '\n' ) )
2021-07-02 18:22:57 +00:00
. replace ( 'REPOS' , metas . map ( ( x ) => makeRepo ( x ) ) . join ( '\n' ) )
2021-07-02 16:18:08 +00:00
. replaceAll ( 'NAME' , name )
. replaceAll ( 'DESCRIPTION' , description ) ;
2021-07-02 11:41:57 +00:00
writeFileSync ( join ( global . distDir , 'index.html' ) , template ) ;
copyFileSync ( join ( _ _dirname , 'NotoSans-Medium.ttf' ) , join ( global . distDir , 'NotoSans-Medium.ttf' ) ) ;
} ;