Skip to content

Commit

Permalink
ci(demo): automatically generate routesFile.txt before prerender
Browse files Browse the repository at this point in the history
…command (#245)
  • Loading branch information
nsbarsukov authored Mar 31, 2023
1 parent 1fcd17e commit 4c1033f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ Thumbs.db

.ssl
RELEASE_BODY.md
projects/demo/routesFile.txt
15 changes: 14 additions & 1 deletion projects/demo/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@
},
"defaultConfiguration": "development"
},
"generate-routes-file": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"command": "ts-node ./scripts/generate-demo-routes-file.ts"
}
},
"prerender": {
"executor": "@nguniversal/builders:prerender",
"options": {
Expand All @@ -210,7 +216,14 @@
"github": {
"browserTarget": "demo:build:github"
}
}
},
"dependsOn": [
{
"target": "generate-routes-file",
"projects": "self",
"params": "ignore"
}
]
}
}
}
28 changes: 0 additions & 28 deletions projects/demo/routesFile.txt

This file was deleted.

39 changes: 39 additions & 0 deletions scripts/generate-demo-routes-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {readFileSync, writeFileSync} from 'fs';
import {join} from 'path';

import {infoLog, SMALL_TAB_SYMBOL, titleLog} from './helpers/colored-log';

const EXCEPTIONS = ['/', 'angular/Setup'];

/**
* This script is required for correct of `nx prerender demo` command.
* `@nguniversal/builders:prerender` (version 12) can't extract routes for lazy-loading modules.
* See: https://github.com/angular/universal/issues/1769
* ___
* TODO: after update to the newer version of Angular and `@nguniversal/builders`, check if this script is still required.
*/
(function main(): void {
const demoPathEnumContent = readFileSync(
join(process.cwd(), `projects`, `demo`, `src`, `app`, `demo-path.ts`),
`utf-8`,
);
const routes =
demoPathEnumContent
.match(/['"`](.*)['"`]/g)
?.map(route => route.replace(/['"`]/g, '')) || [];

routes.forEach(route => {
if (route.startsWith('kit')) {
routes.push(`${route}/API`);
}
});
routes.push(...EXCEPTIONS);

titleLog('Generated routes:');
infoLog(`${SMALL_TAB_SYMBOL}* ${routes?.join(`\n${SMALL_TAB_SYMBOL}* `)}`);

writeFileSync(
join(process.cwd(), `projects`, `demo`, `routesFile.txt`),
routes?.join('\n') || '',
);
})();
2 changes: 2 additions & 0 deletions scripts/helpers/colored-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export function infoLog(message: string): void {
export function titleLog(message: string): void {
console.info('\x1b[35m', message);
}

export const SMALL_TAB_SYMBOL = ` `; // @note: if you use \t then we have big gaps

0 comments on commit 4c1033f

Please sign in to comment.