Primeiro commit do projeto Angular
This commit is contained in:
Generated
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { <%= classify(name) %>Guard } from './<%= dasherize(name) %><%= typeSeparator %>guard';
|
||||
|
||||
describe('<%= classify(name) %>Guard', () => {
|
||||
let guard: <%= classify(name) %>Guard;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
guard = TestBed.inject(<%= classify(name) %>Guard);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(guard).toBeTruthy();
|
||||
});
|
||||
});
|
||||
Generated
Vendored
+30
@@ -0,0 +1,30 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { <%= routerImports %> } from '@angular/router';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class <%= classify(name) %>Guard implements <%= implementations %> {
|
||||
<% if (implements.includes('CanActivate')) { %>canActivate(
|
||||
route: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot): MaybeAsync<GuardResult> {
|
||||
return true;
|
||||
}
|
||||
<% } %><% if (implements.includes('CanActivateChild')) { %>canActivateChild(
|
||||
childRoute: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot): MaybeAsync<GuardResult> {
|
||||
return true;
|
||||
}
|
||||
<% } %><% if (implements.includes('CanDeactivate')) { %>canDeactivate(
|
||||
component: unknown,
|
||||
currentRoute: ActivatedRouteSnapshot,
|
||||
currentState: RouterStateSnapshot,
|
||||
nextState?: RouterStateSnapshot): MaybeAsync<GuardResult> {
|
||||
return true;
|
||||
}
|
||||
<% } %><% if (implements.includes('CanMatch')) { %>canMatch(
|
||||
route: Route,
|
||||
segments: UrlSegment[]): MaybeAsync<GuardResult> {
|
||||
return true;
|
||||
}<% } %>
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google LLC 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.dev/license
|
||||
*/
|
||||
import { Rule } from '@angular-devkit/schematics';
|
||||
import { Schema as GuardOptions } from './schema';
|
||||
export default function (options: GuardOptions): Rule;
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
"use strict";
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google LLC 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.dev/license
|
||||
*/
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = default_1;
|
||||
const schematics_1 = require("@angular-devkit/schematics");
|
||||
const generate_from_files_1 = require("../utility/generate-from-files");
|
||||
const schema_1 = require("./schema");
|
||||
function default_1(options) {
|
||||
if (!options.implements) {
|
||||
throw new schematics_1.SchematicsException('Option "implements" is required.');
|
||||
}
|
||||
if (options.implements.length > 1 && options.functional) {
|
||||
throw new schematics_1.SchematicsException('Can only specify one value for implements when generating a functional guard.');
|
||||
}
|
||||
if (options.functional) {
|
||||
const guardType = options.implements[0] + 'Fn';
|
||||
return (0, generate_from_files_1.generateFromFiles)({ ...options, templateFilesDirectory: './type-files' }, { guardType });
|
||||
}
|
||||
else {
|
||||
const implementations = options.implements
|
||||
.map((implement) => (implement === 'CanDeactivate' ? 'CanDeactivate<unknown>' : implement))
|
||||
.join(', ');
|
||||
const commonRouterNameImports = ['ActivatedRouteSnapshot', 'RouterStateSnapshot'];
|
||||
const routerNamedImports = [...options.implements, 'MaybeAsync', 'GuardResult'];
|
||||
if (options.implements.includes(schema_1.Implement.CanMatch)) {
|
||||
routerNamedImports.push('Route', 'subPath');
|
||||
if (options.implements.length > 1) {
|
||||
routerNamedImports.push(...commonRouterNameImports);
|
||||
}
|
||||
}
|
||||
else {
|
||||
routerNamedImports.push(...commonRouterNameImports);
|
||||
}
|
||||
routerNamedImports.sort();
|
||||
const routerImports = routerNamedImports.join(', ');
|
||||
return (0, generate_from_files_1.generateFromFiles)({ ...options, templateFilesDirectory: './implements-files' }, {
|
||||
implementations,
|
||||
routerImports,
|
||||
});
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AAQH,4BA2CC;AAjDD,2DAAuE;AAEvE,wEAAmE;AAEnE,qCAA+E;AAE/E,mBAAyB,OAAqB;IAC5C,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC;QACxB,MAAM,IAAI,gCAAmB,CAAC,kCAAkC,CAAC,CAAC;IACpE,CAAC;IACD,IAAI,OAAO,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;QACxD,MAAM,IAAI,gCAAmB,CAC3B,+EAA+E,CAChF,CAAC;IACJ,CAAC;IAED,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;QACvB,MAAM,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;QAE/C,OAAO,IAAA,uCAAiB,EAAC,EAAE,GAAG,OAAO,EAAE,sBAAsB,EAAE,cAAc,EAAE,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;IAClG,CAAC;SAAM,CAAC;QACN,MAAM,eAAe,GAAG,OAAO,CAAC,UAAU;aACvC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,SAAS,KAAK,eAAe,CAAC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;aAC1F,IAAI,CAAC,IAAI,CAAC,CAAC;QACd,MAAM,uBAAuB,GAAG,CAAC,wBAAwB,EAAE,qBAAqB,CAAC,CAAC;QAClF,MAAM,kBAAkB,GAAa,CAAC,GAAG,OAAO,CAAC,UAAU,EAAE,YAAY,EAAE,aAAa,CAAC,CAAC;QAE1F,IAAI,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,kBAAc,CAAC,QAAQ,CAAC,EAAE,CAAC;YACzD,kBAAkB,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;YAE5C,IAAI,OAAO,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAClC,kBAAkB,CAAC,IAAI,CAAC,GAAG,uBAAuB,CAAC,CAAC;YACtD,CAAC;QACH,CAAC;aAAM,CAAC;YACN,kBAAkB,CAAC,IAAI,CAAC,GAAG,uBAAuB,CAAC,CAAC;QACtD,CAAC;QAED,kBAAkB,CAAC,IAAI,EAAE,CAAC;QAE1B,MAAM,aAAa,GAAG,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAEpD,OAAO,IAAA,uCAAiB,EACtB,EAAE,GAAG,OAAO,EAAE,sBAAsB,EAAE,oBAAoB,EAAE,EAC5D;YACE,eAAe;YACf,aAAa;SACd,CACF,CAAC;IACJ,CAAC;AACH,CAAC"}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Creates a new route guard in your project. Route guards are used to control access to
|
||||
* parts of your application by checking certain conditions before a route is activated.
|
||||
* This schematic generates a new guard with the specified name, type, and options.
|
||||
*/
|
||||
export type Schema = {
|
||||
/**
|
||||
* Creates the new guard files at the top level of the current project. If set to false, a
|
||||
* new folder with the guard's name will be created to contain the files.
|
||||
*/
|
||||
flat?: boolean;
|
||||
/**
|
||||
* Generate the guard as a function instead of a class. Functional guards can be simpler for
|
||||
* basic scenarios.
|
||||
*/
|
||||
functional?: boolean;
|
||||
/**
|
||||
* Specifies the type(s) of guard to create. You can choose one or more of the following:
|
||||
* `CanActivate` (controls access to a route), `CanActivateChild` (controls access to child
|
||||
* routes), `CanDeactivate` (asks for confirmation before leaving a route), `CanMatch`
|
||||
* (determines if a route can be matched).
|
||||
*/
|
||||
implements?: Implement[];
|
||||
/**
|
||||
* The name for the new route guard. This will be used to create the guard's class and spec
|
||||
* files (e.g., `my-guard.guard.ts` and `my-guard.guard.spec.ts`).
|
||||
*/
|
||||
name: string;
|
||||
/**
|
||||
* The path where the guard files should be created, relative to the current workspace. If
|
||||
* not provided, the guard will be created in the current directory.
|
||||
*/
|
||||
path?: string;
|
||||
/**
|
||||
* The name of the project where the guard should be created. If not specified, the CLI will
|
||||
* determine the project from the current directory.
|
||||
*/
|
||||
project: string;
|
||||
/**
|
||||
* Skip the generation of a unit test file `spec.ts` for the new guard.
|
||||
*/
|
||||
skipTests?: boolean;
|
||||
/**
|
||||
* The separator character to use before the type within the generated file's name. For
|
||||
* example, if you set the option to `.`, the file will be named `example.guard.ts`.
|
||||
*/
|
||||
typeSeparator?: TypeSeparator;
|
||||
};
|
||||
export declare enum Implement {
|
||||
CanActivate = "CanActivate",
|
||||
CanActivateChild = "CanActivateChild",
|
||||
CanDeactivate = "CanDeactivate",
|
||||
CanMatch = "CanMatch"
|
||||
}
|
||||
/**
|
||||
* The separator character to use before the type within the generated file's name. For
|
||||
* example, if you set the option to `.`, the file will be named `example.guard.ts`.
|
||||
*/
|
||||
export declare enum TypeSeparator {
|
||||
Empty = "-",
|
||||
TypeSeparator = "."
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
// THIS FILE IS AUTOMATICALLY GENERATED. TO UPDATE THIS FILE YOU NEED TO CHANGE THE
|
||||
// CORRESPONDING JSON SCHEMA FILE, THEN RUN devkit-admin build (or bazel build ...).
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.TypeSeparator = exports.Implement = void 0;
|
||||
var Implement;
|
||||
(function (Implement) {
|
||||
Implement["CanActivate"] = "CanActivate";
|
||||
Implement["CanActivateChild"] = "CanActivateChild";
|
||||
Implement["CanDeactivate"] = "CanDeactivate";
|
||||
Implement["CanMatch"] = "CanMatch";
|
||||
})(Implement || (exports.Implement = Implement = {}));
|
||||
/**
|
||||
* The separator character to use before the type within the generated file's name. For
|
||||
* example, if you set the option to `.`, the file will be named `example.guard.ts`.
|
||||
*/
|
||||
var TypeSeparator;
|
||||
(function (TypeSeparator) {
|
||||
TypeSeparator["Empty"] = "-";
|
||||
TypeSeparator["TypeSeparator"] = ".";
|
||||
})(TypeSeparator || (exports.TypeSeparator = TypeSeparator = {}));
|
||||
//# sourceMappingURL=schema.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"schema.js","sourceRoot":"","sources":["schema.ts"],"names":[],"mappings":";AACA,mFAAmF;AACnF,oFAAoF;;;AAmDpF,IAAY,SAKX;AALD,WAAY,SAAS;IACjB,wCAA2B,CAAA;IAC3B,kDAAqC,CAAA;IACrC,4CAA+B,CAAA;IAC/B,kCAAqB,CAAA;AACzB,CAAC,EALW,SAAS,yBAAT,SAAS,QAKpB;AAED;;;GAGG;AACH,IAAY,aAGX;AAHD,WAAY,aAAa;IACrB,4BAAW,CAAA;IACX,oCAAmB,CAAA;AACvB,CAAC,EAHW,aAAa,6BAAb,aAAa,QAGxB"}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema",
|
||||
"$id": "SchematicsAngularGuard",
|
||||
"title": "Angular Guard Options Schema",
|
||||
"type": "object",
|
||||
"description": "Creates a new route guard in your project. Route guards are used to control access to parts of your application by checking certain conditions before a route is activated. This schematic generates a new guard with the specified name, type, and options.",
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"description": "The name for the new route guard. This will be used to create the guard's class and spec files (e.g., `my-guard.guard.ts` and `my-guard.guard.spec.ts`).",
|
||||
"$default": {
|
||||
"$source": "argv",
|
||||
"index": 0
|
||||
},
|
||||
"x-prompt": "What name would you like to use for the guard?"
|
||||
},
|
||||
"skipTests": {
|
||||
"type": "boolean",
|
||||
"description": "Skip the generation of a unit test file `spec.ts` for the new guard.",
|
||||
"default": false
|
||||
},
|
||||
"flat": {
|
||||
"type": "boolean",
|
||||
"description": "Creates the new guard files at the top level of the current project. If set to false, a new folder with the guard's name will be created to contain the files.",
|
||||
"default": true
|
||||
},
|
||||
"path": {
|
||||
"type": "string",
|
||||
"format": "path",
|
||||
"$default": {
|
||||
"$source": "workingDirectory"
|
||||
},
|
||||
"description": "The path where the guard files should be created, relative to the current workspace. If not provided, the guard will be created in the current directory.",
|
||||
"visible": false
|
||||
},
|
||||
"project": {
|
||||
"type": "string",
|
||||
"description": "The name of the project where the guard should be created. If not specified, the CLI will determine the project from the current directory.",
|
||||
"$default": {
|
||||
"$source": "projectName"
|
||||
}
|
||||
},
|
||||
"functional": {
|
||||
"type": "boolean",
|
||||
"description": "Generate the guard as a function instead of a class. Functional guards can be simpler for basic scenarios.",
|
||||
"default": true
|
||||
},
|
||||
"implements": {
|
||||
"alias": "guardType",
|
||||
"type": "array",
|
||||
"description": "Specifies the type(s) of guard to create. You can choose one or more of the following: `CanActivate` (controls access to a route), `CanActivateChild` (controls access to child routes), `CanDeactivate` (asks for confirmation before leaving a route), `CanMatch` (determines if a route can be matched).",
|
||||
"uniqueItems": true,
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
"enum": ["CanActivate", "CanActivateChild", "CanDeactivate", "CanMatch"],
|
||||
"type": "string"
|
||||
},
|
||||
"default": ["CanActivate"],
|
||||
"x-prompt": "Which type of guard would you like to create?"
|
||||
},
|
||||
"typeSeparator": {
|
||||
"type": "string",
|
||||
"default": "-",
|
||||
"enum": ["-", "."],
|
||||
"description": "The separator character to use before the type within the generated file's name. For example, if you set the option to `.`, the file will be named `example.guard.ts`."
|
||||
}
|
||||
},
|
||||
"required": ["name", "project"]
|
||||
}
|
||||
Generated
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { <%= guardType %> } from '@angular/router';
|
||||
|
||||
import { <%= camelize(name) %>Guard } from './<%= dasherize(name) %><%= typeSeparator %>guard';
|
||||
|
||||
describe('<%= camelize(name) %>Guard', () => {
|
||||
const executeGuard: <%= guardType %><% if (guardType === 'CanDeactivateFn') { %><unknown><% } %> = (...guardParameters) =>
|
||||
TestBed.runInInjectionContext(() => <%= camelize(name) %>Guard(...guardParameters));
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(executeGuard).toBeTruthy();
|
||||
});
|
||||
});
|
||||
Generated
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
import { <%= guardType %> } from '@angular/router';
|
||||
|
||||
export const <%= camelize(name) %>Guard: <%= guardType %><% if (guardType === 'CanDeactivateFn') { %><unknown><% } %> = <%
|
||||
if (guardType === 'CanMatchFn') { %>(route, segments)<% }
|
||||
%><% if (guardType === 'CanActivateFn') { %>(route, state)<% }
|
||||
%><% if (guardType === 'CanActivateChildFn') { %>(childRoute, state)<% }
|
||||
%><% if (guardType === 'CanDeactivateFn') { %>(component, currentRoute, currentState, nextState)<% } %> => {
|
||||
return true;
|
||||
};
|
||||
Reference in New Issue
Block a user