mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-17 02:54:21 +08:00
Functional guards and resolvers were introduced in the Angular router in v14.2. This commit adds the ability to generate functional router guards by specifying `--guardType` instead of `--implements`. These guards are also accompanied by a test that includes a helper function for executing the guard in the `TestBed` environment so that any `inject` calls in the guard will work properly. Functional resolvers are generated by adding the `--functional` flag.
65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
/**
|
|
* @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.io/license
|
|
*/
|
|
|
|
import {
|
|
Rule,
|
|
Tree,
|
|
apply,
|
|
applyTemplates,
|
|
chain,
|
|
filter,
|
|
mergeWith,
|
|
move,
|
|
noop,
|
|
strings,
|
|
url,
|
|
} from '@angular-devkit/schematics';
|
|
import { parseName } from './parse-name';
|
|
import { validateClassName } from './validation';
|
|
import { createDefaultPath } from './workspace';
|
|
|
|
export interface GenerateFromFilesOptions {
|
|
flat?: boolean;
|
|
name: string;
|
|
path?: string;
|
|
prefix?: string;
|
|
project: string;
|
|
skipTests?: boolean;
|
|
templateFilesDirectory?: string;
|
|
}
|
|
|
|
export function generateFromFiles(
|
|
options: GenerateFromFilesOptions,
|
|
extraTemplateValues: Record<string, string | ((v: string) => string)> = {},
|
|
): Rule {
|
|
return async (host: Tree) => {
|
|
options.path ??= await createDefaultPath(host, options.project);
|
|
options.prefix ??= '';
|
|
options.flat ??= true;
|
|
|
|
const parsedPath = parseName(options.path, options.name);
|
|
options.name = parsedPath.name;
|
|
options.path = parsedPath.path;
|
|
|
|
validateClassName(strings.classify(options.name));
|
|
|
|
const templateFilesDirectory = options.templateFilesDirectory ?? './files';
|
|
const templateSource = apply(url(templateFilesDirectory), [
|
|
options.skipTests ? filter((path) => !path.endsWith('.spec.ts.template')) : noop(),
|
|
applyTemplates({
|
|
...strings,
|
|
...options,
|
|
...extraTemplateValues,
|
|
}),
|
|
move(parsedPath.path + (options.flat ? '' : '/' + strings.dasherize(options.name))),
|
|
]);
|
|
|
|
return chain([mergeWith(templateSource)]);
|
|
};
|
|
}
|