printer.js 2.8 KB

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