This repository was archived by the owner on Apr 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
feat(@schematics/angular): Add guard schematic #30
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/schematics/angular/guard/files/__path__/__name@dasherize__.guard.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { TestBed, async, inject } from '@angular/core/testing'; | ||
|
||
import { <%= classify(name) %>Guard } from './<%= dasherize(name) %>.guard'; | ||
|
||
describe('<%= classify(name) %>Guard', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [<%= classify(name) %>Guard] | ||
}); | ||
}); | ||
|
||
it('should ...', inject([<%= classify(name) %>Guard], (guard: <%= classify(name) %>Guard) => { | ||
expect(guard).toBeTruthy(); | ||
})); | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/schematics/angular/guard/files/__path__/__name@dasherize__.guard.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
@Injectable() | ||
export class <%= classify(name) %>Guard implements CanActivate { | ||
canActivate( | ||
next: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { | ||
return true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
// TODO: replace `options: any` with an actual type generated from the schema. | ||
// tslint:disable:no-any | ||
import {addDeclarationToModule} from '../utility/ast-utils'; | ||
import {InsertChange} from '../utility/change'; | ||
|
||
import { | ||
Rule, | ||
Tree, | ||
apply, | ||
branchAndMerge, | ||
chain, | ||
filter, | ||
mergeWith, | ||
move, | ||
noop, | ||
template, | ||
url, | ||
} from '@angular-devkit/schematics'; | ||
import * as stringUtils from '../strings'; | ||
|
||
import 'rxjs/add/operator/merge'; | ||
import * as ts from 'typescript'; | ||
import {buildRelativePath} from '../utility/find-module'; | ||
|
||
|
||
function addDeclarationToNgModule(options: any): Rule { | ||
return (host: Tree) => { | ||
if (!options.module) { | ||
return host; | ||
} | ||
|
||
if (!host.exists(options.module)) { | ||
throw new Error(`Module specified (${options.module}) does not exist.`); | ||
} | ||
const modulePath = options.module; | ||
|
||
const sourceText = host.read(modulePath) !.toString('utf-8'); | ||
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true); | ||
|
||
const componentPath = `/${options.sourceDir}/${options.path}/` | ||
+ (options.flat ? '' : stringUtils.dasherize(options.name) + '/') | ||
+ stringUtils.dasherize(options.name) | ||
+ '.guard'; | ||
const relativePath = buildRelativePath(modulePath, componentPath); | ||
const changes = addDeclarationToModule(source, modulePath, | ||
stringUtils.classify(`${options.name}Component`), | ||
relativePath); | ||
const recorder = host.beginUpdate(modulePath); | ||
for (const change of changes) { | ||
if (change instanceof InsertChange) { | ||
recorder.insertLeft(change.pos, change.toAdd); | ||
} | ||
} | ||
host.commitUpdate(recorder); | ||
|
||
return host; | ||
}; | ||
} | ||
|
||
export default function (options: any): Rule { | ||
const templateSource = apply(url('./files'), [ | ||
options.spec ? noop() : filter(path => !path.endsWith('.spec.ts')), | ||
template({ | ||
...stringUtils, | ||
...options, | ||
}), | ||
move(options.sourceDir), | ||
]); | ||
|
||
return chain([ | ||
branchAndMerge(chain([ | ||
addDeclarationToNgModule(options), | ||
mergeWith(templateSource), | ||
])), | ||
]); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"id": "SchematicsAngularGuard", | ||
"title": "Angular Guard Options Schema", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"spec": { | ||
"type": "boolean", | ||
"default": true | ||
}, | ||
"flat": { | ||
"type": "boolean", | ||
"default": false | ||
}, | ||
"module": { | ||
"type": "string", | ||
"description": "Allows specification of the declaring module.", | ||
"alias": "m", | ||
"subtype": "filepath" | ||
}, | ||
"path": { | ||
"type": "string", | ||
"default": "app" | ||
}, | ||
"sourceDir": { | ||
"type": "string", | ||
"default": "src" | ||
} | ||
}, | ||
"required": [ | ||
"name" | ||
] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you merge those groups of imports?