printer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ConfigPrinter = exports.ChainFormatter = void 0;
  6. const ChainFormatter = {
  7. Programmatic: 0,
  8. Config: 1
  9. };
  10. exports.ChainFormatter = ChainFormatter;
  11. const Formatter = {
  12. title(type, callerName, filepath) {
  13. let title = "";
  14. if (type === ChainFormatter.Programmatic) {
  15. title = "programmatic options";
  16. if (callerName) {
  17. title += " from " + callerName;
  18. }
  19. } else {
  20. title = "config " + filepath;
  21. }
  22. return title;
  23. },
  24. loc(index, envName) {
  25. let loc = "";
  26. if (index != null) {
  27. loc += `.overrides[${index}]`;
  28. }
  29. if (envName != null) {
  30. loc += `.env["${envName}"]`;
  31. }
  32. return loc;
  33. },
  34. optionsAndDescriptors(opt) {
  35. const content = Object.assign({}, opt.options);
  36. delete content.overrides;
  37. delete content.env;
  38. const pluginDescriptors = [...opt.plugins()];
  39. if (pluginDescriptors.length) {
  40. content.plugins = pluginDescriptors.map(d => descriptorToConfig(d));
  41. }
  42. const presetDescriptors = [...opt.presets()];
  43. if (presetDescriptors.length) {
  44. content.presets = [...presetDescriptors].map(d => descriptorToConfig(d));
  45. }
  46. return JSON.stringify(content, undefined, 2);
  47. }
  48. };
  49. function descriptorToConfig(d) {
  50. var _d$file;
  51. let name = (_d$file = d.file) == null ? void 0 : _d$file.request;
  52. if (name == null) {
  53. if (typeof d.value === "object") {
  54. name = d.value;
  55. } else if (typeof d.value === "function") {
  56. name = `[Function: ${d.value.toString().substr(0, 50)} ... ]`;
  57. }
  58. }
  59. if (name == null) {
  60. name = "[Unknown]";
  61. }
  62. if (d.options === undefined) {
  63. return name;
  64. } else if (d.name == null) {
  65. return [name, d.options];
  66. } else {
  67. return [name, d.options, d.name];
  68. }
  69. }
  70. class ConfigPrinter {
  71. constructor() {
  72. this._stack = [];
  73. }
  74. configure(enabled, type, {
  75. callerName,
  76. filepath
  77. }) {
  78. if (!enabled) return () => {};
  79. return (content, index, envName) => {
  80. this._stack.push({
  81. type,
  82. callerName,
  83. filepath,
  84. content,
  85. index,
  86. envName
  87. });
  88. };
  89. }
  90. static format(config) {
  91. let title = Formatter.title(config.type, config.callerName, config.filepath);
  92. const loc = Formatter.loc(config.index, config.envName);
  93. if (loc) title += ` ${loc}`;
  94. const content = Formatter.optionsAndDescriptors(config.content);
  95. return `${title}\n${content}`;
  96. }
  97. output() {
  98. if (this._stack.length === 0) return "";
  99. return this._stack.map(s => ConfigPrinter.format(s)).join("\n\n");
  100. }
  101. }
  102. exports.ConfigPrinter = ConfigPrinter;