Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit 24811b2

Browse files
Bobby EarlBlackbaud-PaulCrowder
Bobby Earl
authored andcommitted
Allowing for redirects to be declared. (#217)
* Allowing for redirects to be declared. * Fixed linting error * Fixed linting error x2
1 parent a8fae4c commit 24811b2

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

lib/sky-pages-route-generator.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ function parseFileIntoEntity(skyAppConfig, file, index) {
100100
};
101101
}
102102

103+
function generateRedirects(skyAppConfig) {
104+
let redirects = [];
105+
if (skyAppConfig.skyux && skyAppConfig.skyux.redirects) {
106+
redirects = Object.keys(skyAppConfig.skyux.redirects).map(key =>
107+
`{
108+
path: '${key}',
109+
redirectTo: '${skyAppConfig.skyux.redirects[key]}'
110+
}`);
111+
}
112+
113+
return redirects;
114+
}
115+
103116
function generateRoutes(skyAppConfig) {
104117
let counter = 0;
105118
let entities = glob
@@ -231,7 +244,7 @@ function mergeRoutes(routes) {
231244
return uniqueRoutes;
232245
}
233246

234-
function generateDeclarations(routes) {
247+
function generateDeclarations(skyAppConfig, routes) {
235248
let mappedRoutes = mergeRoutes(routes.map(r => parseRoute(r)));
236249

237250
// nest all routes under a top-level route to allow for app-wide guard
@@ -253,8 +266,10 @@ function generateDeclarations(routes) {
253266

254267
// reset root route path to ''
255268
rootRoute.routePath = '';
256-
const declarations = baseRoutes
257-
.map(r => generateRouteDeclaration(r))
269+
270+
// Redirects need to be before '**' catch all
271+
let declarations = generateRedirects(skyAppConfig)
272+
.concat(baseRoutes.map(r => generateRouteDeclaration(r)))
258273
.join(',\n');
259274

260275
return `[\n${declarations}\n]`;
@@ -289,7 +304,7 @@ function getRoutesForConfig(routes) {
289304
function getRoutes(skyAppConfig) {
290305
const routes = generateRoutes(skyAppConfig);
291306
return {
292-
declarations: generateDeclarations(routes),
307+
declarations: generateDeclarations(skyAppConfig, routes),
293308
definitions: generateDefinitions(routes),
294309
imports: generateRuntimeImports(skyAppConfig, routes),
295310
providers: generateProviders(routes),

test/sky-pages-route-generator.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,29 @@ describe('SKY UX Builder route generator', () => {
297297
});
298298
expect(routes.definitions).toContain('public config: SkyAppConfig');
299299
});
300+
301+
it('should prepend any redirects to the route declarations', () => {
302+
spyOn(glob, 'sync').and.returnValue(['custom/nested/index.html']);
303+
spyOn(path, 'join').and.returnValue('');
304+
spyOn(fs, 'readFileSync').and.returnValue('');
305+
306+
const routes = generator.getRoutes({
307+
runtime: {
308+
srcPath: ''
309+
},
310+
skyux: {
311+
redirects: {
312+
'old': 'new'
313+
}
314+
}
315+
});
316+
317+
const rootIndex = routes.declarations.indexOf(`path: ''`);
318+
const redirectIndex = routes.declarations.indexOf(`{
319+
path: 'old',
320+
redirectTo: 'new'
321+
}`);
322+
323+
expect(redirectIndex).toBeLessThan(rootIndex);
324+
});
300325
});

0 commit comments

Comments
 (0)