|
1 | 1 | import {parseAngularRoutes} from 'guess-parser';
|
2 |
| -import {scullyConfig} from '../utils/config'; |
3 |
| -import {logError, logWarn, log, green, yellow} from '../utils/log'; |
4 | 2 | import * as yargs from 'yargs';
|
| 3 | +import {scullyConfig} from '../utils/config'; |
| 4 | +import {green, logError, logWarn, yellow} from '../utils/log'; |
5 | 5 |
|
6 | 6 | const {sge} = yargs
|
7 | 7 | .boolean('sge')
|
@@ -47,53 +47,56 @@ ${green(`By adding '' to the extraRoutes array in the scully.config option, you
|
47 | 47 | };
|
48 | 48 |
|
49 | 49 | 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; |
51 | 53 | if (!extraRoutes) {
|
52 |
| - return Promise.resolve([]); |
| 54 | + return []; |
53 | 55 | }
|
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; |
72 | 73 | }
|
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); |
81 | 75 | });
|
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 | + } |
88 | 80 |
|
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); |
92 | 91 | }
|
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 | + } |
98 | 100 | }
|
| 101 | + return result; |
99 | 102 | }
|
0 commit comments