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

fix(extrroutes): fix bug wehre extraroutes fails #224

Merged
merged 1 commit into from
Jan 23, 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
1 change: 1 addition & 0 deletions scully.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
91 changes: 47 additions & 44 deletions scully/routerPlugins/traverseAppRoutesPlugin.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -47,53 +47,56 @@ ${green(`By adding '' to the extraRoutes array in the scully.config option, you
};

export async function addExtraRoutes(): Promise<string[]> {
const extraRoutes: any[] = scullyConfig.extraRoutes;
const isPromise = (x: any) => x && x.then !== undefined && typeof x.then === 'function';
const extraRoutes: string | (string | Promise<string | string[]>)[] | Promise<string | string[]> =
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<string>
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<string | string[]>)[] = [];
if (isPromise(extraRoutes)) {
const outerResult = await extraRoutes;
if (typeof outerResult === 'string') {
/** ok, we got a promise<string> 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<string|string[]>. Excluding for now. `
);
// Turn into promise<undefined>
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;
}
2 changes: 1 addition & 1 deletion scully/utils/interfacesandenums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[] | string>)[];
/** Port-number where the original application is served */
appPort: number;
/** port-number where the Scully generated files are available */
Expand Down