From a39df360d1bc7031ef40a56dc3c74b3ebf021ee4 Mon Sep 17 00:00:00 2001 From: Brandon Stirnaman Date: Fri, 2 Jun 2017 12:49:53 -0500 Subject: [PATCH] Add support for route guards --- lib/sky-pages-module-generator.js | 2 +- lib/sky-pages-route-generator.js | 31 ++++++++++++++++++++++++-- test/sky-pages-route-generator.spec.js | 27 ++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/lib/sky-pages-module-generator.js b/lib/sky-pages-module-generator.js index a34f5979..516c60c5 100644 --- a/lib/sky-pages-module-generator.js +++ b/lib/sky-pages-module-generator.js @@ -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); diff --git a/lib/sky-pages-route-generator.js b/lib/sky-pages-route-generator.js index 619070ea..d0ebdb04 100644 --- a/lib/sky-pages-route-generator.js +++ b/lib/sky-pages-route-generator.js @@ -3,6 +3,7 @@ const glob = require('glob'); const path = require('path'); +const fs = require('fs'); function indent(count) { return ' '.repeat(count); @@ -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! @@ -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 }; } @@ -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); } @@ -137,6 +163,7 @@ function getRoutes(skyAppConfig) { return { declarations: generateDeclarations(routes), definitions: generateDefinitions(routes), + providers: generateProviders(routes), names: generateNames(routes), routesForConfig: getRoutesForConfig(routes) }; diff --git a/test/sky-pages-route-generator.spec.js b/test/sky-pages-route-generator.spec.js index 4d124614..c16e4303 100644 --- a/test/sky-pages-route-generator.spec.js +++ b/test/sky-pages-route-generator.spec.js @@ -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` + ); + }); });