DRY up list handling

This commit is contained in:
Sam Carlton 2020-11-27 15:05:37 -06:00
parent 3b0404fbb0
commit 7a68eb5838

View file

@ -6,47 +6,54 @@ import buildAppList from './helpers/build-app-list.js'
import buildGamesList from './helpers/build-game-list.js' import buildGamesList from './helpers/build-game-list.js'
const storeAppList = async function (builder) { const listsOptions = [
// TODO: Make DRY {
buildMethod: buildAppList,
path: '/static/app-list.json',
route: app => '/app/' + app.slug
},
{
buildMethod: buildGamesList,
path: '/static/game-list.json',
route: app => '/game/' + app.slug
}
]
const storeAppLists = async function (builder) {
console.log('Build Lists started') console.log('Build Lists started')
const appListPath = path.join( const savedLists = await Promise.all(listsOptions.map(async list => {
// builder.nuxt.options.buildDir,
builder.nuxt.options.srcDir,
'/static/app-list.json'
)
const gamesListPath = path.join( // Run the build method
// builder.nuxt.options.buildDir, const builtList = await list.buildMethod()
builder.nuxt.options.srcDir,
'/static/game-list.json'
)
// const appList = await buildAppList() // Make the relative path for our new JSON file
// const gamesList = await buildGamesList() const listFullPath = `.${list.path}`
const [ // console.log('listFullPath', listFullPath)
appList,
gamesList
] = await Promise.all([
buildAppList(),
buildGamesList()
])
await Promise.all([ // Write the list to JSON
fs.writeFile(appListPath, JSON.stringify(appList)), await fs.writeFile(listFullPath, JSON.stringify(builtList))
fs.writeFile(gamesListPath, JSON.stringify(gamesList))
])
console.log('Finished building JSON Lists') // Read back the JSON we just wrote to ensure it exists
const savedListJSON = await fs.readFile(listFullPath, 'utf-8')
return // console.log('savedListJSON', savedListJSON)
const savedList = JSON.parse(savedListJSON)
// Import the created JSON File
return savedList
}))
console.log('Build Lists finished')
return savedLists
} }
// console.log('process.env.GAMES_SOURCE', process.env.GAMES_SOURCE)
export default { export default {
target: 'static', target: 'static',
@ -56,10 +63,10 @@ export default {
*/ */
hooks: { hooks: {
build: { build: {
before: storeAppList before: storeAppLists
}, },
generate: { generate: {
before: storeAppList before: storeAppLists
} }
}, },
@ -71,34 +78,45 @@ export default {
] ]
}, },
routes() { routes() {
return Promise.all([ return Promise.all(listsOptions.map(async list => {
import('./static/app-list.json'), const listPath = `.${list.path}`
import('./static/game-list.json')
]) // Read JSON to ensure it exists
.then(([ const savedListJSON = await fs.readFile(listPath, 'utf-8')
importedAppList,
importedGameList // Parse the saved JSON into a variable
]) => { const savedList = JSON.parse(savedListJSON)
const appList = importedAppList.default
const gameList = importedGameList.default // Pass on the variable
return savedList
}))
.then(( lists ) => {
// console.log('appList', appList) // console.log('appList', appList)
const appRoutes = appList.map(app => ({ // const appRoutes = appList.map(app => ({
route: '/app/' + app.slug, // route: '/app/' + app.slug,
// payload: appList // // payload: appList
})) // }))
const gameRoutes = gameList.map(game => ({ // const gameRoutes = gameList.map(game => ({
route: '/game/' + game.slug, // route: '/game/' + game.slug,
// payload: appList // // payload: appList
})) // }))
const sectionList = [] const sectionList = []
appList.forEach(app => { const [
if (sectionList.includes(app.section.slug)) return appRoutes,
gameRoutes
] = lists.map((list, listI) => {
return list.map( app => {
// Find and store all sections
if (sectionList.includes(app.section.slug) == false) {
sectionList.push(app.section.slug)
}
sectionList.push(app.section.slug) return app.endpoint
})
}) })
const sectionRoutes = sectionList.map(slug => ({ const sectionRoutes = sectionList.map(slug => ({