Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

feat(@schematics/angular): Add guard schematic #30

Merged
merged 1 commit into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/schematics/angular/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"description": "Create an enumeration.",
"schema": "./enum/schema.json"
},
"guard": {
"factory": "./guard",
"description": "Create an guard.",
"schema": "./guard/schema.json"
},
"interface": {
"factory": "./interface",
"description": "Create an interface.",
Expand Down
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();
}));
});
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;
}
}
83 changes: 83 additions & 0 deletions packages/schematics/angular/guard/index.ts
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';
Copy link
Contributor

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?


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),
])),
]);
}
36 changes: 36 additions & 0 deletions packages/schematics/angular/guard/schema.json
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"
]
}