Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): add extraRoutes to scully.config #139

Merged
merged 2 commits into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 66 additions & 12 deletions scully/utils/defaultAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@ export const generateAll = async (config?: Partial<ScullyConfig>) => {
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);
Expand All @@ -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<string[]> {
let extraRoutes = config.extraRoutes;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SanderElias please read this now. I changed it to allow you to pass a hodge-podge array of string and promises, and it will take care of resolving everything, removing empty values, and flattening nested arrays of unhandled routes.

[ '/user/:id', Promise<string>, Promise<string[]>, undefined]

gets turned into

['/user/:id', '/foo/:id', '/bar/:id', '/baz/:id']

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awsome, that is even more as I would have wanted ;)

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<string>
if (typeof extraRoute === 'string') {
return Promise.resolve(extraRoute);
}

logWarn(
`The extraRoute ${JSON.stringify(
extraRoute
)} needs to be either a string or a Promise<string|string[]>. Excluding for now. `
);
// Turn into promise<undefined>
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;
}
}
1 change: 1 addition & 0 deletions scully/utils/interfacesandenums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ScullyConfig {
outFolder?: string;
distFolder?: string;
routes: RouteConfig;
extraRoutes?: string[];
appPort: number;
staticport: number;
}
Expand Down