mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-20 05:24:57 +08:00
An internal schematics helper rule has been introduced for Angular schematics that only generate a set of project files from a set of templated files. Multiple current generation schematics contained essentially the same code which increased the sustainment burden and made refactoring and improvements more complicated.
43 lines
1.4 KiB
TypeScript
43 lines
1.4 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.');
|
|
}
|
|
|
|
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.CanLoad)) {
|
|
routerNamedImports.push('Route', 'UrlSegment');
|
|
|
|
if (options.implements.length > 1) {
|
|
routerNamedImports.push(...commonRouterNameImports);
|
|
}
|
|
} else {
|
|
routerNamedImports.push(...commonRouterNameImports);
|
|
}
|
|
|
|
routerNamedImports.sort();
|
|
|
|
const implementationImports = routerNamedImports.join(', ');
|
|
|
|
return generateFromFiles(options, {
|
|
implementations,
|
|
implementationImports,
|
|
});
|
|
}
|