mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-15 01:54:04 +08:00
Angular v15.1 deprecated the `CanLoad` guard in favor of `CanMatch`. This commit removes the support for `CanLoad` when generating a guard with the CLI, to encourage developers to use `CanMatch` instead. BREAKING CHANGE: The CLI no longer allows to generate `CanLoad` guards. Use `CanMatch` instead.
59 lines
1.8 KiB
TypeScript
59 lines
1.8 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, SchematicsException } from '@angular-devkit/schematics';
|
|
|
|
import { generateFromFiles } from '../utility/generate-from-files';
|
|
|
|
import { Implement as GuardInterface, Schema as GuardOptions } from './schema';
|
|
|
|
export default function (options: GuardOptions): Rule {
|
|
if (!options.implements) {
|
|
throw new SchematicsException('Option "implements" is required.');
|
|
}
|
|
if (options.implements.length > 1 && options.functional) {
|
|
throw new SchematicsException(
|
|
'Can only specify one value for implements when generating a functional guard.',
|
|
);
|
|
}
|
|
|
|
if (options.functional) {
|
|
const guardType = options.implements[0] + 'Fn';
|
|
|
|
return 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: string[] = [...options.implements, 'UrlTree'];
|
|
|
|
if (options.implements.includes(GuardInterface.CanMatch)) {
|
|
routerNamedImports.push('Route', 'UrlSegment');
|
|
|
|
if (options.implements.length > 1) {
|
|
routerNamedImports.push(...commonRouterNameImports);
|
|
}
|
|
} else {
|
|
routerNamedImports.push(...commonRouterNameImports);
|
|
}
|
|
|
|
routerNamedImports.sort();
|
|
|
|
const routerImports = routerNamedImports.join(', ');
|
|
|
|
return generateFromFiles(
|
|
{ ...options, templateFilesDirectory: './implements-files' },
|
|
{
|
|
implementations,
|
|
routerImports,
|
|
},
|
|
);
|
|
}
|
|
}
|