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(could not connect to server): explain the reasons #216

Merged
merged 1 commit into from
Jan 22, 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
3,822 changes: 1,395 additions & 2,427 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"express": "^4.17.1",
"front-matter": "^3.0.2",
"fs-extra": "^8.1.0",
"guess-parser": "^0.4.12",
"guess-parser": "^0.4.13",
"husky": "^4.0.0-beta.5",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~4.2.1",
Expand Down
1 change: 1 addition & 0 deletions projects/sampleBlog/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const routes: Routes = [
path: '**',
loadChildren: () => import('./pagenotfound/pagenotfound.module').then(m => m.PagenotfoundModule),
},
{path: '', redirectTo: 'home', pathMatch: 'full'},
];

@NgModule({
Expand Down
2 changes: 1 addition & 1 deletion scully.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ exports.config = {
property: 'id',
},
},
'/user/:userId/:friendCode': {
'/nouser/:userId/:friendCode': {
// Type is mandatory
type: 'json',
/**
Expand Down
2 changes: 1 addition & 1 deletion scully/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"express": "^4.17.1",
"front-matter": "^3.0.2",
"fs-extra": "^8.1.0",
"guess-parser": "^0.4.11",
"guess-parser": "^0.4.13",
"jsonc": "2.0.0",
"marked": "^0.7.0",
"puppeteer": "^2.0.0",
Expand Down
92 changes: 85 additions & 7 deletions scully/routerPlugins/traverseAppRoutesPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,99 @@
import {parseAngularRoutes} from 'guess-parser';
import {scullyConfig} from '../utils/config';
import {logError} from '../utils/log';
import {logError, logWarn, log, green, yellow} from '../utils/log';
import * as yargs from 'yargs';

const {sge} = yargs
.boolean('sge')
.alias('sge', 'showGuessError')
.describe('sb', 'dumps the error from guess to the console').argv;
export const traverseAppRoutes = async (appRootFolder = scullyConfig.projectRoot) => {
const extraRoutes = await addExtraRoutes();
let routes = [];
try {
const routes = parseAngularRoutes(appRootFolder).map(r => r.path);

return routes;
routes = parseAngularRoutes(appRootFolder).map(r => r.path);
} catch (e) {
// console.log('e', e);
throw new Error('Scully could not traverse routes');
if (sge) {
console.error(e);
}
logError(`
We encountered a problem while reading the routes from your applications source.
This might happen when there are lazy-loaded routes, that are not loaded,
Or when there are paths we can not resolve statically.
Check the routes in your app, rebuild and retry.
${yellow('(You can inspect the error by passing the --showGuessError flag')}

${green('When there are extraRoutes in your config, we will still try to render those.')}

`);
}
// process.exit(15);
const allRoutes = [...routes, ...extraRoutes];
if (allRoutes.findIndex(r => r === '') === -1) {
logWarn(`

We did not find an empty route ({path:'', component:rootComponent}) in your app.
This means that the root of your application will be you normal angular app, and
is not rendered by Scully
In some circumstances this can be cause because a redirect like:
({path: '', redirectTo: 'home', pathMatch: 'full'})
is not picked up by our scanner.

${green(`By adding '' to the extraRoutes array in the scully.config option, you can bypass this issue`)}

`);
process.exit(15);
}
return allRoutes;
};

export async function addExtraRoutes(): Promise<string[]> {
const extraRoutes: any[] = scullyConfig.extraRoutes;
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 (${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;
}
}
57 changes: 3 additions & 54 deletions scully/utils/defaultAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {storeRoutes} from '../systemPlugins/storeRoutes';
import {writeToFs} from '../systemPlugins/writeToFs.plugin';
import {asyncPool} from './asyncPool';
import {chunk} from './chunk';
import {loadConfig, scullyConfig, updateScullyConfig} from './config';
import {loadConfig, updateScullyConfig} from './config';
import {ScullyConfig} from './interfacesandenums';
import {log, logWarn} from './log';

Expand All @@ -18,7 +18,8 @@ export const generateAll = async (config?: Partial<ScullyConfig>) => {
await loadConfig;
try {
log('Finding all routes in application.');
const unhandledRoutes = [...(await traverseAppRoutes()), ...(await addExtraRoutes())];
const unhandledRoutes = await traverseAppRoutes();
console.log('hr', unhandledRoutes);

if (unhandledRoutes.length < 1) {
logWarn('No routes found in application, are you sure you installed the router? Terminating.');
Expand Down Expand Up @@ -62,55 +63,3 @@ async function doChunks(dataRoutes) {
return x.concat(activeChunk);
}, Promise.resolve([]));
}

async function addExtraRoutes(): Promise<string[]> {
const extraRoutes: any[] = scullyConfig.extraRoutes;
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 (${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;
}
}
2 changes: 1 addition & 1 deletion scully/utils/waitForServerToBeAvailable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const waitForServerToBeAvailable = () =>
if (res && res.res) {
if (res.homeFolder !== scullyConfig.homeFolder) {
logWarn(
'`scully serve` is running in a different project. you can kill it by running `scully killServer`'
'`scully serve` is running in a different project. you can kill it by running `npx scully killServer`'
);
process.exit(15);
}
Expand Down