diff --git a/scully/utils/defaultAction.ts b/scully/utils/defaultAction.ts index 48dcdf69c..f0187615c 100644 --- a/scully/utils/defaultAction.ts +++ b/scully/utils/defaultAction.ts @@ -18,22 +18,26 @@ export const generateAll = async (config?: Partial) => { await loadConfig; try { log('Finding all routes in application.'); - const appRouteArray = await traverseAppRoutes(); - if (appRouteArray.length<1) { - logWarn('No routes found in application, are you sure you installed the router? Terminating.') + const appUnhandledRoutes = await traverseAppRoutes(); + const extraUnhandledRoutes = await addExtraRoutes(config); + const unhandledRoutes = [...appUnhandledRoutes, ...extraUnhandledRoutes]; + + 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); @@ -54,11 +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([])); } + +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; + } +} 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; }