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

Commit

Permalink
Add support for route guards
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbaud-brandonstirnaman committed Jun 4, 2017
1 parent 81b5af1 commit a39df36
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/sky-pages-module-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ ${components.imports}
${routes.definitions}
// Routes need to be defined after their corresponding components
const appRoutingProviders: any[] = [];
const appRoutingProviders: any[] = ${routes.providers};
const routes: Routes = ${routes.declarations};
const routing = RouterModule.forRoot(routes);
Expand Down
31 changes: 29 additions & 2 deletions lib/sky-pages-route-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const glob = require('glob');
const path = require('path');
const fs = require('fs');

function indent(count) {
return ' '.repeat(count);
Expand Down Expand Up @@ -53,6 +54,11 @@ function parseFileIntoEntity(skyAppConfig, file, index) {

let routePath = [];
let routeParams = [];
let parsedPath = path.parse(file);

// Make no assumptions on extension used to create route, just remove
// it and append .guard.ts (ex: index.html -> index.guard.ts)
let guardPath = path.join(parsedPath.dir, `${parsedPath.name}.guard.ts`);

// Removes srcPath + filename
// glob always uses '/' for path separator!
Expand Down Expand Up @@ -85,7 +91,8 @@ function parseFileIntoEntity(skyAppConfig, file, index) {
componentName: componentName,
componentDefinition: componentDefinition,
routePath: routePath.join('/'),
routeParams: routeParams
routeParams: routeParams,
guardPath: fs.existsSync(guardPath) ? guardPath : undefined
};
}

Expand Down Expand Up @@ -114,11 +121,30 @@ function generateDefinitions(routes) {
function generateDeclarations(routes) {
const p = indent(1);
const declarations = routes
.map(r => `${p}{ path: '${r.routePath}', component: ${r.componentName} }`)
.map(r => {
let guard = r.guardPath ? `require('${r.guardPath}').default` : '';
let declaration =
`${p}{
path: '${r.routePath}',
component: ${r.componentName},
canActivate: [${guard}],
canDeactivate: [${guard}]
}`;

return declaration;
})
.join(',\n');
return `[\n${declarations}\n]`;
}

function generateProviders(routes) {
const providers = routes
.map(r => r.guardPath ? `require('${r.guardPath}').default` : undefined)
.filter(p => p);

return `[\n${providers}\n]`;
}

function generateNames(routes) {
return routes.map(route => route.componentName);
}
Expand All @@ -137,6 +163,7 @@ function getRoutes(skyAppConfig) {
return {
declarations: generateDeclarations(routes),
definitions: generateDefinitions(routes),
providers: generateProviders(routes),
names: generateNames(routes),
routesForConfig: getRoutesForConfig(routes)
};
Expand Down
27 changes: 27 additions & 0 deletions test/sky-pages-route-generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,31 @@ describe('SKY UX Builder route generator', () => {
expect(suppliedPattern).toEqual('my-custom-src/my-custom-pattern');
});

it('should support guards with custom routesPattern', () => {
let suppliedPattern;
spyOn(glob, 'sync').and.callFake((p) => {
suppliedPattern = p;
return ['my-custom-src/my-custom-route/index.html'];
});
spyOn(fs, 'existsSync').and.returnValue(true);

let routes = generator.getRoutes({
runtime: {
srcPath: 'my-custom-src/',
routesPattern: 'my-custom-pattern',
}
});

expect(routes.declarations).toContain(
`canActivate: [require(\'my-custom-src/my-custom-route/index.guard.ts\').default]`
);

expect(routes.declarations).toContain(
`canDeactivate: [require(\'my-custom-src/my-custom-route/index.guard.ts\').default]`
);

expect(routes.providers).toContain(
`require(\'my-custom-src/my-custom-route/index.guard.ts\').default`
);
});
});

0 comments on commit a39df36

Please sign in to comment.