diff --git a/scully.config.js b/scully.config.js index d9e2fa098..e6d43f958 100644 --- a/scully.config.js +++ b/scully.config.js @@ -9,6 +9,7 @@ exports.config = { /** outDir is where the static distribution files end up */ outDir: './dist/static', // hostName: '0.0.0.0', + extraRoutes: [''], routes: { '/demo/:id': { type: 'extra', diff --git a/scully/routerPlugins/traverseAppRoutesPlugin.ts b/scully/routerPlugins/traverseAppRoutesPlugin.ts index 43b320a49..7c5b20a45 100644 --- a/scully/routerPlugins/traverseAppRoutesPlugin.ts +++ b/scully/routerPlugins/traverseAppRoutesPlugin.ts @@ -1,7 +1,7 @@ import {parseAngularRoutes} from 'guess-parser'; -import {scullyConfig} from '../utils/config'; -import {logError, logWarn, log, green, yellow} from '../utils/log'; import * as yargs from 'yargs'; +import {scullyConfig} from '../utils/config'; +import {green, logError, logWarn, yellow} from '../utils/log'; const {sge} = yargs .boolean('sge') @@ -47,53 +47,56 @@ ${green(`By adding '' to the extraRoutes array in the scully.config option, you }; export async function addExtraRoutes(): Promise { - const extraRoutes: any[] = scullyConfig.extraRoutes; + const isPromise = (x: any) => x && x.then !== undefined && typeof x.then === 'function'; + const extraRoutes: string | (string | Promise)[] | Promise = + scullyConfig.extraRoutes; if (!extraRoutes) { - return Promise.resolve([]); + return []; } - - 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 (${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); + if (typeof extraRoutes === 'string') { + /** convert a simple string to an array */ + return [extraRoutes]; + } + const workList: (string | Promise)[] = []; + if (isPromise(extraRoutes)) { + const outerResult = await extraRoutes; + if (typeof outerResult === 'string') { + /** ok, we got a promise return the result */ + return [outerResult]; + } + workList.concat(outerResult); + } else if (Array.isArray(extraRoutes)) { + extraRoutes.forEach(r => { + if (workList.includes(r)) { + /** don't add duplicates */ + return; } - - logWarn( - `The extraRoute ${JSON.stringify( - extraRoute - )} needs to be either a string or a Promise. Excluding for now. ` - ); - // Turn into promise - return Promise.resolve(); + workList.push(r); }); - const extraRouteValues = await Promise.all(extraRoutePromises); - extraRouteValues.reduce((acc, val) => { - // Remove empties and just return acc - if (!val) { - return acc; - } + } else { + logWarn(`ExtraRoutes must be provided as an string array. Current type: ${typeof extraRoutes}`); + return []; + } - // Spread acc and arrays together - if (Array.isArray(val)) { - return [...acc, ...val]; + const result: string[] = []; + for (const route of workList) { + /** note, this ignores non-string/non-promise things in array */ + if (typeof route === 'string') { + result.push(route); + } + if (isPromise(route)) { + const x = await route; + if (typeof x === 'string') { + result.push(x); } - - // Append values into acc - return [...acc, val]; - }, []); - return extraRouteValues; + if (Array.isArray(x)) { + x.forEach(s => { + if (typeof s === 'string') { + result.push(s); + } + }); + } + } } + return result; } diff --git a/scully/utils/interfacesandenums.ts b/scully/utils/interfacesandenums.ts index 593f32d66..f232417d7 100644 --- a/scully/utils/interfacesandenums.ts +++ b/scully/utils/interfacesandenums.ts @@ -20,7 +20,7 @@ export interface ScullyConfig { /** routes that needs additional processing have their configuration in here */ routes: RouteConfig; /** routes that are in the application but have no route in the router */ - extraRoutes?: string[]; + extraRoutes?: (string | Promise)[]; /** Port-number where the original application is served */ appPort: number; /** port-number where the Scully generated files are available */