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

Commit

Permalink
Remove support for export default, require->import
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbaud-brandonstirnaman committed Jun 7, 2017
1 parent 3847013 commit 92a777c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 47 deletions.
3 changes: 2 additions & 1 deletion lib/sky-pages-module-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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);
Expand Down
34 changes: 17 additions & 17 deletions lib/sky-pages-route-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,8 +96,7 @@ function parseFileIntoEntity(skyAppConfig, file, index) {
componentDefinition: componentDefinition,
routePath: routePath.join('/'),
routeParams: routeParams,
guardPath: guardPath,
guardName: guardName
guard: guard
};
}

Expand Down Expand Up @@ -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}',
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand Down
33 changes: 4 additions & 29 deletions test/sky-pages-route-generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,48 +117,23 @@ 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`
);
});

it('should throw when a file has multiple guards', () => {
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 {}
`);

Expand Down

0 comments on commit 92a777c

Please sign in to comment.