| 1 |
import gensync from "gensync"; |
| 2 |
|
| 3 |
import type { Handler } from "gensync"; |
| 4 |
|
| 5 |
import type { |
| 6 |
OptionsAndDescriptors, |
| 7 |
UnloadedDescriptor, |
| 8 |
} from "./config-descriptors"; |
| 9 |
|
| 10 |
// todo: Use flow enums when @babel/transform-flow-types supports it |
| 11 |
export const ChainFormatter = { |
| 12 |
Programmatic: 0, |
| 13 |
Config: 1, |
| 14 |
}; |
| 15 |
|
| 16 |
type PrintableConfig = { |
| 17 |
content: OptionsAndDescriptors; |
| •18 |
type: (typeof ChainFormatter)[keyof typeof ChainFormatter]; |
| 19 |
callerName: string | undefined | null; |
| 20 |
filepath: string | undefined | null; |
| 21 |
index: number | undefined | null; |
| 22 |
envName: string | undefined | null; |
| 23 |
}; |
| 24 |
|
| 25 |
const Formatter = { |
| 26 |
title( |
| •27 |
type: (typeof ChainFormatter)[keyof typeof ChainFormatter], |
| 28 |
callerName?: string | null, |
| 29 |
filepath?: string | null, |
| 30 |
): string { |
| 31 |
let title = ""; |
| 32 |
if (type === ChainFormatter.Programmatic) { |
| 33 |
title = "programmatic options"; |
| 34 |
if (callerName) { |
| 35 |
title += " from " + callerName; |
| 36 |
} |
| 37 |
} else { |
| 38 |
title = "config " + filepath; |
| 39 |
} |
| 40 |
return title; |
| 41 |
}, |
| 42 |
loc(index?: number | null, envName?: string | null): string { |
| 43 |
let loc = ""; |
| 44 |
if (index != null) { |
| 45 |
loc += `.overrides[${index}]`; |
| 46 |
} |
| 47 |
if (envName != null) { |
| 48 |
loc += `.env["${envName}"]`; |
| 49 |
} |
| 50 |
return loc; |
| 51 |
}, |
| 52 |
|
| 53 |
*optionsAndDescriptors(opt: OptionsAndDescriptors) { |
| 54 |
const content = { ...opt.options }; |
| 55 |
// overrides and env will be printed as separated config items |
| 56 |
delete content.overrides; |
| 57 |
delete content.env; |
| 58 |
// resolve to descriptors |
| 59 |
const pluginDescriptors = [...(yield* opt.plugins())]; |
| 60 |
if (pluginDescriptors.length) { |
| 61 |
content.plugins = pluginDescriptors.map(d => descriptorToConfig(d)); |
| 62 |
} |
| 63 |
const presetDescriptors = [...(yield* opt.presets())]; |
| 64 |
if (presetDescriptors.length) { |
| 65 |
content.presets = [...presetDescriptors].map(d => descriptorToConfig(d)); |
| 66 |
} |
| 67 |
return JSON.stringify(content, undefined, 2); |
| 68 |
}, |
| 69 |
}; |
| 70 |
|
| 71 |
function descriptorToConfig( |
| 72 |
d: UnloadedDescriptor, |
| 73 |
): string | {} | Array<unknown> { |
| 74 |
let name = d.file?.request; |
| 75 |
if (name == null) { |
| 76 |
if (typeof d.value === "object") { |
| 77 |
name = d.value; |
| 78 |
} else if (typeof d.value === "function") { |
| 79 |
// If the unloaded descriptor is a function, i.e. `plugins: [ require("my-plugin") ]`, |
| 80 |
// we print the first 50 characters of the function source code and hopefully we can see |
| 81 |
// `name: 'my-plugin'` in the source |
| 82 |
name = `[Function: ${d.value.toString().slice(0, 50)} ... ]`; |
| 83 |
} |
| 84 |
} |
| 85 |
if (name == null) { |
| 86 |
name = "[Unknown]"; |
| 87 |
} |
| 88 |
if (d.options === undefined) { |
| 89 |
return name; |
| 90 |
} else if (d.name == null) { |
| 91 |
return [name, d.options]; |
| 92 |
} else { |
| 93 |
return [name, d.options, d.name]; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
export class ConfigPrinter { |
| 98 |
_stack: Array<PrintableConfig> = []; |
| 99 |
configure( |
| 100 |
enabled: boolean, |
| •101 |
type: (typeof ChainFormatter)[keyof typeof ChainFormatter], |
| 102 |
{ |
| 103 |
callerName, |
| 104 |
filepath, |
| 105 |
}: { |
| 106 |
callerName?: string; |
| 107 |
filepath?: string; |
| 108 |
}, |
| 109 |
) { |
| 110 |
if (!enabled) return () => {}; |
| 111 |
return ( |
| 112 |
content: OptionsAndDescriptors, |
| 113 |
index?: number | null, |
| 114 |
envName?: string | null, |
| 115 |
) => { |
| 116 |
this._stack.push({ |
| 117 |
type, |
| 118 |
callerName, |
| 119 |
filepath, |
| 120 |
content, |
| 121 |
index, |
| 122 |
envName, |
| 123 |
}); |
| 124 |
}; |
| 125 |
} |
| 126 |
static *format(config: PrintableConfig): Handler<string> { |
| 127 |
let title = Formatter.title( |
| 128 |
config.type, |
| 129 |
config.callerName, |
| 130 |
config.filepath, |
| 131 |
); |
| 132 |
const loc = Formatter.loc(config.index, config.envName); |
| 133 |
if (loc) title += ` ${loc}`; |
| 134 |
const content = yield* Formatter.optionsAndDescriptors(config.content); |
| 135 |
return `${title}\n${content}`; |
| 136 |
} |
| 137 |
|
| 138 |
*output(): Handler<string> { |
| 139 |
if (this._stack.length === 0) return ""; |
| 140 |
const configs = yield* gensync.all( |
| 141 |
this._stack.map(s => ConfigPrinter.format(s)), |
| 142 |
); |
| 143 |
return configs.join("\n\n"); |
| 144 |
} |
| 145 |
} |