From 92a777cde7f2ebb2f3dcc65a4ccc60e0323fcc18 Mon Sep 17 00:00:00 2001 From: Brandon Stirnaman Date: Mon, 5 Jun 2017 14:11:48 -0400 Subject: [PATCH] Remove support for export default, require->import --- lib/sky-pages-module-generator.js | 3 ++- lib/sky-pages-route-generator.js | 34 +++++++++++++------------- test/sky-pages-route-generator.spec.js | 33 +++---------------------- 3 files changed, 23 insertions(+), 47 deletions(-) diff --git a/lib/sky-pages-module-generator.js b/lib/sky-pages-module-generator.js index 74c8dc91..15a1a3fe 100644 --- a/lib/sky-pages-module-generator.js +++ b/lib/sky-pages-module-generator.js @@ -124,6 +124,7 @@ import { AppExtrasModule } from '${skyAppConfig.runtime.skyPagesOutAlias}/src/app/app-extras.module'; import { ${runtimeImports.join(', ')} } from '${skyAppConfig.runtime.runtimeAlias}'; +${routes.imports.join('\n')} export function SkyAppConfigFactory(windowRef: SkyAppWindowRef): any { const config: any = ${skyAppConfigAsString}; @@ -141,7 +142,7 @@ ${components.imports} ${routes.definitions} // Routes need to be defined after their corresponding components -const appRoutingProviders: any[] = ${routes.providers}; +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 46b59682..c5d1231e 100644 --- a/lib/sky-pages-route-generator.js +++ b/lib/sky-pages-route-generator.js @@ -59,12 +59,9 @@ function parseFileIntoEntity(skyAppConfig, file, index) { // 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`); - let guardName; - + let guard; if (fs.existsSync(guardPath)) { - guardName = extractGuardName(guardPath); - } else { - guardPath = undefined; + guard = extractGuard(guardPath); } // Removes srcPath + filename @@ -99,8 +96,7 @@ function parseFileIntoEntity(skyAppConfig, file, index) { componentDefinition: componentDefinition, routePath: routePath.join('/'), routeParams: routeParams, - guardPath: guardPath, - guardName: guardName + guard: guard }; } @@ -130,7 +126,7 @@ function generateDeclarations(routes) { const p = indent(1); const declarations = routes .map(r => { - let guard = r.guardPath && r.guardName ? `require('${r.guardPath}').${r.guardName}` : ''; + let guard = r.guard ? r.guard.name : ''; let declaration = `${p}{ path: '${r.routePath}', @@ -145,12 +141,16 @@ function generateDeclarations(routes) { return `[\n${declarations}\n]`; } -function generateProviders(routes) { - const providers = routes - .map(r => r.guardPath ? `require('${r.guardPath}').${r.guardName}` : undefined) - .filter(p => p); +function generateRuntimeImports(routes) { + return routes + .filter(r => r.guard) + .map(r => `import { ${r.guard.name} } from '${r.guard.path.replace(/\.ts$/, '')}';`); +} - return `[\n${providers}\n]`; +function generateProviders(routes) { + return routes + .filter(r => r.guard) + .map(r => r.guard.name); } function generateNames(routes) { @@ -171,14 +171,15 @@ function getRoutes(skyAppConfig) { return { declarations: generateDeclarations(routes), definitions: generateDefinitions(routes), + imports: generateRuntimeImports(routes), providers: generateProviders(routes), names: generateNames(routes), routesForConfig: getRoutesForConfig(routes) }; } -function extractGuardName(file) { - const matchRegexp = /@Injectable\s*\(\s*\)\s*export\s*(default)*\s*class\s(\w+)/g; +function extractGuard(file) { + const matchRegexp = /@Injectable\s*\(\s*\)\s*export\s*class\s(\w+)/g; const content = fs.readFileSync(file, { encoding: 'utf8' }); let result; @@ -188,8 +189,7 @@ function extractGuardName(file) { throw new Error(`As a best practice, only export one guard per file in ${file}`); } - // If its a default export, use that value instead of guard component name - result = match[1] || match[2]; + result = { path: file, name: match[1] }; } return result; diff --git a/test/sky-pages-route-generator.spec.js b/test/sky-pages-route-generator.spec.js index 04cd07cb..dd0adc3a 100644 --- a/test/sky-pages-route-generator.spec.js +++ b/test/sky-pages-route-generator.spec.js @@ -117,40 +117,15 @@ describe('SKY UX Builder route generator', () => { }); expect(routes.declarations).toContain( - `canActivate: [require(\'my-custom-src/my-custom-route/index.guard.ts\').Guard]` + `canActivate: [Guard]` ); expect(routes.declarations).toContain( - `canDeactivate: [require(\'my-custom-src/my-custom-route/index.guard.ts\').Guard]` + `canDeactivate: [Guard]` ); expect(routes.providers).toContain( - `require(\'my-custom-src/my-custom-route/index.guard.ts\').Guard` - ); - }); - - it('should support default export guards', () => { - spyOn(glob, 'sync').and.callFake(() => ['my-custom-src/my-custom-route/index.html']); - spyOn(fs, 'readFileSync').and.returnValue('@Injectable() export default class Guard {}'); - 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` + `Guard` ); }); @@ -158,7 +133,7 @@ describe('SKY UX Builder route generator', () => { spyOn(glob, 'sync').and.callFake(() => ['my-custom-src/my-custom-route/index.html']); spyOn(fs, 'existsSync').and.returnValue(true); spyOn(fs, 'readFileSync').and.returnValue(` - @Injectable() export default class Guard {} + @Injectable() export class Guard {} @Injectable() export class Guard2 {} `);