index.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 add_declaration_to_ng_module_1 = require("../utility/add-declaration-to-ng-module");
  13. const find_module_1 = require("../utility/find-module");
  14. const parse_name_1 = require("../utility/parse-name");
  15. const validation_1 = require("../utility/validation");
  16. const workspace_1 = require("../utility/workspace");
  17. const schema_1 = require("./schema");
  18. function buildSelector(options, projectPrefix) {
  19. let selector = schematics_1.strings.dasherize(options.name);
  20. if (options.prefix) {
  21. selector = `${options.prefix}-${selector}`;
  22. }
  23. else if (options.prefix === undefined && projectPrefix) {
  24. selector = `${projectPrefix}-${selector}`;
  25. }
  26. return selector;
  27. }
  28. function default_1(options) {
  29. return async (host) => {
  30. const workspace = await (0, workspace_1.getWorkspace)(host);
  31. const project = workspace.projects.get(options.project);
  32. if (!project) {
  33. throw new schematics_1.SchematicsException(`Project "${options.project}" does not exist.`);
  34. }
  35. if (options.path === undefined) {
  36. options.path = (0, workspace_1.buildDefaultPath)(project);
  37. }
  38. try {
  39. options.module = (0, find_module_1.findModuleFromOptions)(host, options);
  40. }
  41. catch {
  42. options.module = (0, find_module_1.findModuleFromOptions)(host, {
  43. ...options,
  44. moduleExt: '-module.ts',
  45. routingModuleExt: '-routing-module.ts',
  46. });
  47. }
  48. // Schematic templates require a defined type value
  49. options.type ??= '';
  50. const parsedPath = (0, parse_name_1.parseName)(options.path, options.name);
  51. options.name = parsedPath.name;
  52. options.path = parsedPath.path;
  53. options.selector =
  54. options.selector || buildSelector(options, (project && project.prefix) || '');
  55. (0, validation_1.validateHtmlSelector)(options.selector);
  56. (0, validation_1.validateClassName)(schematics_1.strings.classify(options.name));
  57. const skipStyleFile = options.inlineStyle || options.style === schema_1.Style.None;
  58. const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
  59. options.skipTests ? (0, schematics_1.filter)((path) => !path.endsWith('.spec.ts.template')) : (0, schematics_1.noop)(),
  60. skipStyleFile ? (0, schematics_1.filter)((path) => !path.endsWith('.__style__.template')) : (0, schematics_1.noop)(),
  61. options.inlineTemplate ? (0, schematics_1.filter)((path) => !path.endsWith('.html.template')) : (0, schematics_1.noop)(),
  62. (0, schematics_1.applyTemplates)({
  63. ...schematics_1.strings,
  64. 'if-flat': (s) => (options.flat ? '' : s),
  65. 'ngext': options.ngHtml ? '.ng' : '',
  66. ...options,
  67. }),
  68. !options.type
  69. ? (0, schematics_1.forEach)(((file) => {
  70. return file.path.includes('..')
  71. ? {
  72. content: file.content,
  73. path: file.path.replace('..', '.'),
  74. }
  75. : file;
  76. }))
  77. : (0, schematics_1.noop)(),
  78. (0, schematics_1.move)(parsedPath.path),
  79. ]);
  80. return (0, schematics_1.chain)([
  81. (0, add_declaration_to_ng_module_1.addDeclarationToNgModule)({
  82. type: 'component',
  83. ...options,
  84. }),
  85. (0, schematics_1.mergeWith)(templateSource),
  86. ]);
  87. };
  88. }