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 named export guards
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbaud-brandonstirnaman committed Jun 4, 2017
1 parent a39df36 commit 72e4a34
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
32 changes: 29 additions & 3 deletions lib/sky-pages-route-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ 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;

if (fs.existsSync(guardPath)) {
guardName = extractGuardName(guardPath);
} else {
guardPath = undefined;
}

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

Expand Down Expand Up @@ -122,7 +130,7 @@ function generateDeclarations(routes) {
const p = indent(1);
const declarations = routes
.map(r => {
let guard = r.guardPath ? `require('${r.guardPath}').default` : '';
let guard = r.guardPath && r.guardName ? `require('${r.guardPath}').${r.guardName}` : '';
let declaration =
`${p}{
path: '${r.routePath}',
Expand All @@ -139,7 +147,7 @@ function generateDeclarations(routes) {

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

return `[\n${providers}\n]`;
Expand Down Expand Up @@ -169,6 +177,24 @@ function getRoutes(skyAppConfig) {
};
}

function extractGuardName(file) {
const matchRegexp = /@Injectable\s*\(\s*\)\s*export\s*(default)*\s*class\s(\w+)/g;
const content = fs.readFileSync(file, { encoding: 'utf8' });

let result;
let match;
while ((match = matchRegexp.exec(content))) {
if (result !== undefined) {
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];
}

return result;
}

module.exports = {
getRoutes: getRoutes
};
47 changes: 43 additions & 4 deletions test/sky-pages-route-generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,33 @@ describe('SKY UX Builder route generator', () => {
});

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(glob, 'sync').and.callFake(() => ['my-custom-src/my-custom-route/index.html']);
spyOn(fs, 'readFileSync').and.returnValue('@Injectable() export 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\').Guard]`
);

expect(routes.declarations).toContain(
`canDeactivate: [require(\'my-custom-src/my-custom-route/index.guard.ts\').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({
Expand All @@ -131,4 +153,21 @@ describe('SKY UX Builder route generator', () => {
`require(\'my-custom-src/my-custom-route/index.guard.ts\').default`
);
});

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 Guard2 {}
`);

let file = 'my-custom-src/my-custom-route/index.guard.ts';
expect(() => generator.getRoutes({
runtime: {
srcPath: 'my-custom-src/',
routesPattern: 'my-custom-pattern',
}
})).toThrow(new Error(`As a best practice, only export one guard per file in ${file}`));
});
});

0 comments on commit 72e4a34

Please sign in to comment.