Skip to content

Commit f07ba67

Browse files
authored
fix(extrroutes): fix bug wehre extraroutes fails (#224)
When using the extraroutes option to add an empty route to render `/` scully errors out closes #223
1 parent 99c0253 commit f07ba67

File tree

3 files changed

+49
-45
lines changed

3 files changed

+49
-45
lines changed

scully.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ exports.config = {
99
/** outDir is where the static distribution files end up */
1010
outDir: './dist/static',
1111
// hostName: '0.0.0.0',
12+
extraRoutes: [''],
1213
routes: {
1314
'/demo/:id': {
1415
type: 'extra',
Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {parseAngularRoutes} from 'guess-parser';
2-
import {scullyConfig} from '../utils/config';
3-
import {logError, logWarn, log, green, yellow} from '../utils/log';
42
import * as yargs from 'yargs';
3+
import {scullyConfig} from '../utils/config';
4+
import {green, logError, logWarn, yellow} from '../utils/log';
55

66
const {sge} = yargs
77
.boolean('sge')
@@ -47,53 +47,56 @@ ${green(`By adding '' to the extraRoutes array in the scully.config option, you
4747
};
4848

4949
export async function addExtraRoutes(): Promise<string[]> {
50-
const extraRoutes: any[] = scullyConfig.extraRoutes;
50+
const isPromise = (x: any) => x && x.then !== undefined && typeof x.then === 'function';
51+
const extraRoutes: string | (string | Promise<string | string[]>)[] | Promise<string | string[]> =
52+
scullyConfig.extraRoutes;
5153
if (!extraRoutes) {
52-
return Promise.resolve([]);
54+
return [];
5355
}
54-
55-
if (!Array.isArray(extraRoutes)) {
56-
logWarn(`ExtraRoutes must be provided as an array. Current type: ${typeof extraRoutes}`);
57-
return Promise.resolve([]);
58-
} else {
59-
log(`Adding all extra routes (${extraRoutes.length})`);
60-
const extraRoutePromises = extraRoutes.map(extraRoute => {
61-
if (!extraRoute) {
62-
return Promise.resolve();
63-
}
64-
// It is a promise already
65-
if (extraRoute.then && {}.toString.call(extraRoute.then) === '[object Function]') {
66-
return extraRoute;
67-
}
68-
69-
// Turn into promise<string>
70-
if (typeof extraRoute === 'string') {
71-
return Promise.resolve(extraRoute);
56+
if (typeof extraRoutes === 'string') {
57+
/** convert a simple string to an array */
58+
return [extraRoutes];
59+
}
60+
const workList: (string | Promise<string | string[]>)[] = [];
61+
if (isPromise(extraRoutes)) {
62+
const outerResult = await extraRoutes;
63+
if (typeof outerResult === 'string') {
64+
/** ok, we got a promise<string> return the result */
65+
return [outerResult];
66+
}
67+
workList.concat(outerResult);
68+
} else if (Array.isArray(extraRoutes)) {
69+
extraRoutes.forEach(r => {
70+
if (workList.includes(r)) {
71+
/** don't add duplicates */
72+
return;
7273
}
73-
74-
logWarn(
75-
`The extraRoute ${JSON.stringify(
76-
extraRoute
77-
)} needs to be either a string or a Promise<string|string[]>. Excluding for now. `
78-
);
79-
// Turn into promise<undefined>
80-
return Promise.resolve();
74+
workList.push(r);
8175
});
82-
const extraRouteValues = await Promise.all(extraRoutePromises);
83-
extraRouteValues.reduce((acc, val) => {
84-
// Remove empties and just return acc
85-
if (!val) {
86-
return acc;
87-
}
76+
} else {
77+
logWarn(`ExtraRoutes must be provided as an string array. Current type: ${typeof extraRoutes}`);
78+
return [];
79+
}
8880

89-
// Spread acc and arrays together
90-
if (Array.isArray(val)) {
91-
return [...acc, ...val];
81+
const result: string[] = [];
82+
for (const route of workList) {
83+
/** note, this ignores non-string/non-promise things in array */
84+
if (typeof route === 'string') {
85+
result.push(route);
86+
}
87+
if (isPromise(route)) {
88+
const x = await route;
89+
if (typeof x === 'string') {
90+
result.push(x);
9291
}
93-
94-
// Append values into acc
95-
return [...acc, val];
96-
}, []);
97-
return extraRouteValues;
92+
if (Array.isArray(x)) {
93+
x.forEach(s => {
94+
if (typeof s === 'string') {
95+
result.push(s);
96+
}
97+
});
98+
}
99+
}
98100
}
101+
return result;
99102
}

scully/utils/interfacesandenums.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface ScullyConfig {
2020
/** routes that needs additional processing have their configuration in here */
2121
routes: RouteConfig;
2222
/** routes that are in the application but have no route in the router */
23-
extraRoutes?: string[];
23+
extraRoutes?: (string | Promise<string[] | string>)[];
2424
/** Port-number where the original application is served */
2525
appPort: number;
2626
/** port-number where the Scully generated files are available */

0 commit comments

Comments
 (0)