generate-from-files.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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.generateFromFiles = generateFromFiles;
  11. const schematics_1 = require("@angular-devkit/schematics");
  12. const parse_name_1 = require("./parse-name");
  13. const validation_1 = require("./validation");
  14. const workspace_1 = require("./workspace");
  15. function generateFromFiles(options, extraTemplateValues = {}) {
  16. return async (host) => {
  17. options.path ??= await (0, workspace_1.createDefaultPath)(host, options.project);
  18. options.prefix ??= '';
  19. options.flat ??= true;
  20. // Schematic templates require a defined type value
  21. options.type ??= '';
  22. const parsedPath = (0, parse_name_1.parseName)(options.path, options.name);
  23. options.name = parsedPath.name;
  24. options.path = parsedPath.path;
  25. (0, validation_1.validateClassName)(schematics_1.strings.classify(options.name));
  26. const templateFilesDirectory = options.templateFilesDirectory ?? './files';
  27. const templateSource = (0, schematics_1.apply)((0, schematics_1.url)(templateFilesDirectory), [
  28. options.skipTests ? (0, schematics_1.filter)((path) => !path.endsWith('.spec.ts.template')) : (0, schematics_1.noop)(),
  29. (0, schematics_1.applyTemplates)({
  30. ...schematics_1.strings,
  31. ...options,
  32. ...extraTemplateValues,
  33. }),
  34. !options.type
  35. ? (0, schematics_1.forEach)((file) => {
  36. let filePath = file.path;
  37. while (filePath.includes('..')) {
  38. filePath = filePath.replaceAll('..', '.');
  39. }
  40. return {
  41. content: file.content,
  42. path: filePath,
  43. };
  44. })
  45. : (0, schematics_1.noop)(),
  46. (0, schematics_1.move)(parsedPath.path + (options.flat ? '' : '/' + schematics_1.strings.dasherize(options.name))),
  47. ]);
  48. return (0, schematics_1.chain)([(0, schematics_1.mergeWith)(templateSource)]);
  49. };
  50. }