index.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. options.module = (0, find_module_1.findModuleFromOptions)(host, options);
  39. const parsedPath = (0, parse_name_1.parseName)(options.path, options.name);
  40. options.name = parsedPath.name;
  41. options.path = parsedPath.path;
  42. options.selector =
  43. options.selector || buildSelector(options, (project && project.prefix) || '');
  44. (0, validation_1.validateHtmlSelector)(options.selector);
  45. (0, validation_1.validateClassName)(schematics_1.strings.classify(options.name));
  46. const skipStyleFile = options.inlineStyle || options.style === schema_1.Style.None;
  47. const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
  48. options.skipTests ? (0, schematics_1.filter)((path) => !path.endsWith('.spec.ts.template')) : (0, schematics_1.noop)(),
  49. skipStyleFile ? (0, schematics_1.filter)((path) => !path.endsWith('.__style__.template')) : (0, schematics_1.noop)(),
  50. options.inlineTemplate ? (0, schematics_1.filter)((path) => !path.endsWith('.html.template')) : (0, schematics_1.noop)(),
  51. (0, schematics_1.applyTemplates)({
  52. ...schematics_1.strings,
  53. 'if-flat': (s) => (options.flat ? '' : s),
  54. ...options,
  55. }),
  56. !options.type
  57. ? (0, schematics_1.forEach)(((file) => {
  58. return file.path.includes('..')
  59. ? {
  60. content: file.content,
  61. path: file.path.replace('..', '.'),
  62. }
  63. : file;
  64. }))
  65. : (0, schematics_1.noop)(),
  66. (0, schematics_1.move)(parsedPath.path),
  67. ]);
  68. return (0, schematics_1.chain)([
  69. (0, add_declaration_to_ng_module_1.addDeclarationToNgModule)({
  70. type: 'component',
  71. ...options,
  72. }),
  73. (0, schematics_1.mergeWith)(templateSource),
  74. ]);
  75. };
  76. }