From fe90c93ed16a8ea876c986f3c93ccea9b65c19d7 Mon Sep 17 00:00:00 2001 From: frosty Date: Sat, 4 Jan 2020 03:00:51 -0700 Subject: [PATCH 1/2] feat(config): add extraRoutes to scully.config With the additional of extraRoutes to the scully.config, users can now provide additional routes that they want Scully to discover. These routes can plug into the router plugins as well, and can produce multitudes of browseable routes each. --- scully/utils/defaultAction.ts | 16 ++++++++++++++++ scully/utils/interfacesandenums.ts | 1 + 2 files changed, 17 insertions(+) diff --git a/scully/utils/defaultAction.ts b/scully/utils/defaultAction.ts index 48dcdf69c..ecb30fed9 100644 --- a/scully/utils/defaultAction.ts +++ b/scully/utils/defaultAction.ts @@ -19,10 +19,13 @@ export const generateAll = async (config?: Partial) => { try { log('Finding all routes in application.'); const appRouteArray = await traverseAppRoutes(); + addExtraRoutes(appRouteArray, config); + if (appRouteArray.length<1) { logWarn('No routes found in application, are you sure you installed the router? Terminating.') process.exit(15); } + log('Pull in data to create additional routes.'); const dataRoutes = await addOptionalRoutes(appRouteArray); await storeRoutes(dataRoutes); @@ -62,3 +65,16 @@ async function doChunks(dataRoutes) { return x.concat(activeChunk); }, Promise.resolve([])); } + +function addExtraRoutes(appRoutes, config){ + if(config.extraRoutes) { + let extraRoutes = config.extraRoutes; + if(!Array.isArray([])) { + logWarn(`ExtraRoutes must be provided as an array. Current type: ${typeof extraRoutes}`); + } else { + log(`Adding all extra routes (${config.extraRoutes.length})`); + extraRoutes = config.extraRoutes || []; + extraRoutes.forEach(extraRoute => appRoutes.push(extraRoute)); + } + } +} diff --git a/scully/utils/interfacesandenums.ts b/scully/utils/interfacesandenums.ts index 6d241902e..bffd92c5a 100644 --- a/scully/utils/interfacesandenums.ts +++ b/scully/utils/interfacesandenums.ts @@ -10,6 +10,7 @@ export interface ScullyConfig { outFolder?: string; distFolder?: string; routes: RouteConfig; + extraRoutes?: string[]; appPort: number; staticport: number; } From 7f3e222222fd7483d0f39b75faea56325bec470c Mon Sep 17 00:00:00 2001 From: frosty Date: Sat, 4 Jan 2020 04:57:56 -0700 Subject: [PATCH 2/2] improvement(scully): promisify the extraRoutes process I converted this process to return a `Promise` which is what we needed it to be in order to allow for async fetching of extraRoutes at build time. This is actually a very wonderful and robust method. --- scully/utils/defaultAction.ts | 84 +++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 23 deletions(-) diff --git a/scully/utils/defaultAction.ts b/scully/utils/defaultAction.ts index ecb30fed9..f0187615c 100644 --- a/scully/utils/defaultAction.ts +++ b/scully/utils/defaultAction.ts @@ -18,25 +18,26 @@ export const generateAll = async (config?: Partial) => { await loadConfig; try { log('Finding all routes in application.'); - const appRouteArray = await traverseAppRoutes(); - addExtraRoutes(appRouteArray, config); + const appUnhandledRoutes = await traverseAppRoutes(); + const extraUnhandledRoutes = await addExtraRoutes(config); + const unhandledRoutes = [...appUnhandledRoutes, ...extraUnhandledRoutes]; - if (appRouteArray.length<1) { - logWarn('No routes found in application, are you sure you installed the router? Terminating.') + if (unhandledRoutes.length < 1) { + logWarn('No routes found in application, are you sure you installed the router? Terminating.'); process.exit(15); } log('Pull in data to create additional routes.'); - const dataRoutes = await addOptionalRoutes(appRouteArray); - await storeRoutes(dataRoutes); + const handledRoutes = await addOptionalRoutes(unhandledRoutes); + await storeRoutes(handledRoutes); /** launch the browser, its shared among renderers */ const browser = await launchedBrowser(); /** start handling each route, works in chunked parallel mode */ - // await doChunks(dataRoutes); - await renderParallel(dataRoutes); + // await doChunks(handledRoutes); + await renderParallel(handledRoutes); /** save router to static json thingie */ - await storeRoutes(dataRoutes); - return dataRoutes; + await storeRoutes(handledRoutes); + return handledRoutes; } catch (e) { // TODO: add better error handling log(e); @@ -57,24 +58,61 @@ async function doChunks(dataRoutes) { const x = await acc; const activeChunk = await Promise.all( part.map(async (handledRoute: HandledRoute) => - routeContentRenderer(handledRoute).then((html: string) => - writeToFs(handledRoute.route, html) - ) + routeContentRenderer(handledRoute).then((html: string) => writeToFs(handledRoute.route, html)) ) ); return x.concat(activeChunk); }, Promise.resolve([])); } -function addExtraRoutes(appRoutes, config){ - if(config.extraRoutes) { - let extraRoutes = config.extraRoutes; - if(!Array.isArray([])) { - logWarn(`ExtraRoutes must be provided as an array. Current type: ${typeof extraRoutes}`); - } else { - log(`Adding all extra routes (${config.extraRoutes.length})`); - extraRoutes = config.extraRoutes || []; - extraRoutes.forEach(extraRoute => appRoutes.push(extraRoute)); - } +async function addExtraRoutes(config): Promise { + let extraRoutes = config.extraRoutes; + if (!extraRoutes) { + return Promise.resolve([]); + } + + if (!Array.isArray(extraRoutes)) { + logWarn(`ExtraRoutes must be provided as an array. Current type: ${typeof extraRoutes}`); + return Promise.resolve([]); + } else { + log(`Adding all extra routes (${config.extraRoutes.length})`); + const extraRoutePromises = extraRoutes.map(extraRoute => { + if (!extraRoute) { + return Promise.resolve(); + } + // It is a promise already + if (extraRoute.then && {}.toString.call(extraRoute.then) === '[object Function]') { + return extraRoute; + } + + // Turn into promise + if (typeof extraRoute === 'string') { + return Promise.resolve(extraRoute); + } + + logWarn( + `The extraRoute ${JSON.stringify( + extraRoute + )} needs to be either a string or a Promise. Excluding for now. ` + ); + // Turn into promise + return Promise.resolve(); + }); + const extraRouteValues = await Promise.all(extraRoutePromises); + extraRouteValues.reduce((acc, val) => { + // Remove empties and just return acc + if (!val) { + return acc; + } + + // Spread acc and arrays together + if (Array.isArray(val)) { + return [...acc, ...val]; + } + + // Append values into acc + return [...acc, val]; + }, []); + return extraRouteValues; } }