index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.default = default_1;
  11. const schematics_1 = require("@angular-devkit/schematics");
  12. const generate_from_files_1 = require("../utility/generate-from-files");
  13. const schema_1 = require("./schema");
  14. function default_1(options) {
  15. if (!options.implements) {
  16. throw new schematics_1.SchematicsException('Option "implements" is required.');
  17. }
  18. if (options.implements.length > 1 && options.functional) {
  19. throw new schematics_1.SchematicsException('Can only specify one value for implements when generating a functional guard.');
  20. }
  21. if (options.functional) {
  22. const guardType = options.implements[0] + 'Fn';
  23. return (0, generate_from_files_1.generateFromFiles)({ ...options, templateFilesDirectory: './type-files' }, { guardType });
  24. }
  25. else {
  26. const implementations = options.implements
  27. .map((implement) => (implement === 'CanDeactivate' ? 'CanDeactivate<unknown>' : implement))
  28. .join(', ');
  29. const commonRouterNameImports = ['ActivatedRouteSnapshot', 'RouterStateSnapshot'];
  30. const routerNamedImports = [...options.implements, 'MaybeAsync', 'GuardResult'];
  31. if (options.implements.includes(schema_1.Implement.CanMatch)) {
  32. routerNamedImports.push('Route', 'subPath');
  33. if (options.implements.length > 1) {
  34. routerNamedImports.push(...commonRouterNameImports);
  35. }
  36. }
  37. else {
  38. routerNamedImports.push(...commonRouterNameImports);
  39. }
  40. routerNamedImports.sort();
  41. const routerImports = routerNamedImports.join(', ');
  42. return (0, generate_from_files_1.generateFromFiles)({ ...options, templateFilesDirectory: './implements-files' }, {
  43. implementations,
  44. routerImports,
  45. });
  46. }
  47. }