Skip to content

Commit 28dab59

Browse files
aaronfrostSanderElias
authored andcommitted
feat(config): add extraRoutes to scully.config (#139)
* 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. * improvement(scully): promisify the extraRoutes process I converted this process to return a `Promise<unhandledRoutes[]>` 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.
1 parent 8415355 commit 28dab59

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

scully/utils/defaultAction.ts

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,26 @@ export const generateAll = async (config?: Partial<ScullyConfig>) => {
1818
await loadConfig;
1919
try {
2020
log('Finding all routes in application.');
21-
const appRouteArray = await traverseAppRoutes();
22-
if (appRouteArray.length<1) {
23-
logWarn('No routes found in application, are you sure you installed the router? Terminating.')
21+
const appUnhandledRoutes = await traverseAppRoutes();
22+
const extraUnhandledRoutes = await addExtraRoutes(config);
23+
const unhandledRoutes = [...appUnhandledRoutes, ...extraUnhandledRoutes];
24+
25+
if (unhandledRoutes.length < 1) {
26+
logWarn('No routes found in application, are you sure you installed the router? Terminating.');
2427
process.exit(15);
2528
}
29+
2630
log('Pull in data to create additional routes.');
27-
const dataRoutes = await addOptionalRoutes(appRouteArray);
28-
await storeRoutes(dataRoutes);
31+
const handledRoutes = await addOptionalRoutes(unhandledRoutes);
32+
await storeRoutes(handledRoutes);
2933
/** launch the browser, its shared among renderers */
3034
const browser = await launchedBrowser();
3135
/** start handling each route, works in chunked parallel mode */
32-
// await doChunks(dataRoutes);
33-
await renderParallel(dataRoutes);
36+
// await doChunks(handledRoutes);
37+
await renderParallel(handledRoutes);
3438
/** save router to static json thingie */
35-
await storeRoutes(dataRoutes);
36-
return dataRoutes;
39+
await storeRoutes(handledRoutes);
40+
return handledRoutes;
3741
} catch (e) {
3842
// TODO: add better error handling
3943
log(e);
@@ -54,11 +58,61 @@ async function doChunks(dataRoutes) {
5458
const x = await acc;
5559
const activeChunk = await Promise.all(
5660
part.map(async (handledRoute: HandledRoute) =>
57-
routeContentRenderer(handledRoute).then((html: string) =>
58-
writeToFs(handledRoute.route, html)
59-
)
61+
routeContentRenderer(handledRoute).then((html: string) => writeToFs(handledRoute.route, html))
6062
)
6163
);
6264
return x.concat(activeChunk);
6365
}, Promise.resolve([]));
6466
}
67+
68+
async function addExtraRoutes(config): Promise<string[]> {
69+
let extraRoutes = config.extraRoutes;
70+
if (!extraRoutes) {
71+
return Promise.resolve([]);
72+
}
73+
74+
if (!Array.isArray(extraRoutes)) {
75+
logWarn(`ExtraRoutes must be provided as an array. Current type: ${typeof extraRoutes}`);
76+
return Promise.resolve([]);
77+
} else {
78+
log(`Adding all extra routes (${config.extraRoutes.length})`);
79+
const extraRoutePromises = extraRoutes.map(extraRoute => {
80+
if (!extraRoute) {
81+
return Promise.resolve();
82+
}
83+
// It is a promise already
84+
if (extraRoute.then && {}.toString.call(extraRoute.then) === '[object Function]') {
85+
return extraRoute;
86+
}
87+
88+
// Turn into promise<string>
89+
if (typeof extraRoute === 'string') {
90+
return Promise.resolve(extraRoute);
91+
}
92+
93+
logWarn(
94+
`The extraRoute ${JSON.stringify(
95+
extraRoute
96+
)} needs to be either a string or a Promise<string|string[]>. Excluding for now. `
97+
);
98+
// Turn into promise<undefined>
99+
return Promise.resolve();
100+
});
101+
const extraRouteValues = await Promise.all(extraRoutePromises);
102+
extraRouteValues.reduce((acc, val) => {
103+
// Remove empties and just return acc
104+
if (!val) {
105+
return acc;
106+
}
107+
108+
// Spread acc and arrays together
109+
if (Array.isArray(val)) {
110+
return [...acc, ...val];
111+
}
112+
113+
// Append values into acc
114+
return [...acc, val];
115+
}, []);
116+
return extraRouteValues;
117+
}
118+
}

scully/utils/interfacesandenums.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface ScullyConfig {
1010
outFolder?: string;
1111
distFolder?: string;
1212
routes: RouteConfig;
13+
extraRoutes?: string[];
1314
appPort: number;
1415
staticport: number;
1516
}

0 commit comments

Comments
 (0)