UNPKG

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