generate-impl.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const schematic_command_1 = require("../models/schematic-command");
  4. const color_1 = require("../utilities/color");
  5. const json_schema_1 = require("../utilities/json-schema");
  6. class GenerateCommand extends schematic_command_1.SchematicCommand {
  7. async initialize(options) {
  8. // Fill up the schematics property of the command description.
  9. const [collectionName, schematicName] = this.parseSchematicInfo(options);
  10. this.collectionName = collectionName;
  11. this.schematicName = schematicName;
  12. await super.initialize(options);
  13. const collection = this.getCollection(collectionName);
  14. const subcommands = {};
  15. const schematicNames = schematicName ? [schematicName] : collection.listSchematicNames();
  16. // Sort as a courtesy for the user.
  17. schematicNames.sort();
  18. for (const name of schematicNames) {
  19. const schematic = this.getSchematic(collection, name, true);
  20. this.longSchematicName = schematic.description.name;
  21. let subcommand;
  22. if (schematic.description.schemaJson) {
  23. subcommand = await json_schema_1.parseJsonSchemaToSubCommandDescription(name, schematic.description.path, this._workflow.registry, schematic.description.schemaJson);
  24. }
  25. else {
  26. continue;
  27. }
  28. if (this.getDefaultSchematicCollection() == collectionName) {
  29. subcommands[name] = subcommand;
  30. }
  31. else {
  32. subcommands[`${collectionName}:${name}`] = subcommand;
  33. }
  34. }
  35. this.description.options.forEach(option => {
  36. if (option.name == 'schematic') {
  37. option.subcommands = subcommands;
  38. }
  39. });
  40. }
  41. async run(options) {
  42. if (!this.schematicName || !this.collectionName) {
  43. return this.printHelp(options);
  44. }
  45. return this.runSchematic({
  46. collectionName: this.collectionName,
  47. schematicName: this.schematicName,
  48. schematicOptions: options['--'] || [],
  49. debug: !!options.debug || false,
  50. dryRun: !!options.dryRun || false,
  51. force: !!options.force || false,
  52. });
  53. }
  54. async reportAnalytics(paths, options) {
  55. const [collectionName, schematicName] = this.parseSchematicInfo(options);
  56. if (!schematicName || !collectionName) {
  57. return;
  58. }
  59. const escapedSchematicName = (this.longSchematicName || schematicName).replace(/\//g, '_');
  60. return super.reportAnalytics(['generate', collectionName.replace(/\//g, '_'), escapedSchematicName], options);
  61. }
  62. parseSchematicInfo(options) {
  63. let collectionName = this.getDefaultSchematicCollection();
  64. let schematicName = options.schematic;
  65. if (schematicName) {
  66. if (schematicName.includes(':')) {
  67. [collectionName, schematicName] = schematicName.split(':', 2);
  68. }
  69. }
  70. return [collectionName, schematicName];
  71. }
  72. async printHelp(options) {
  73. await super.printHelp(options);
  74. this.logger.info('');
  75. // Find the generate subcommand.
  76. const subcommand = this.description.options.filter(x => x.subcommands)[0];
  77. if (Object.keys((subcommand && subcommand.subcommands) || {}).length == 1) {
  78. this.logger.info(`\nTo see help for a schematic run:`);
  79. this.logger.info(color_1.colors.cyan(` ng generate <schematic> --help`));
  80. }
  81. return 0;
  82. }
  83. }
  84. exports.GenerateCommand = GenerateCommand;