astro-ghostcms/.pnpm-store/v3/files/27/e3ed344ba87c3ab6c6b395a169c...

1 line
7.8 KiB
Plaintext
Raw Normal View History

2024-02-14 14:10:47 +00:00
{"version":3,"names":["_gensync","data","require","ChainFormatter","exports","Programmatic","Config","Formatter","title","type","callerName","filepath","loc","index","envName","optionsAndDescriptors","opt","content","Object","assign","options","overrides","env","pluginDescriptors","plugins","length","map","d","descriptorToConfig","presetDescriptors","presets","JSON","stringify","undefined","_d$file","name","file","request","value","toString","slice","ConfigPrinter","constructor","_stack","configure","enabled","push","format","config","output","configs","gensync","all","s","join"],"sources":["../../src/config/printer.ts"],"sourcesContent":["import gensync from \"gensync\";\n\nimport type { Handler } from \"gensync\";\n\nimport type {\n OptionsAndDescriptors,\n UnloadedDescriptor,\n} from \"./config-descriptors.ts\";\n\n// todo: Use flow enums when @babel/transform-flow-types supports it\nexport const ChainFormatter = {\n Programmatic: 0,\n Config: 1,\n};\n\ntype PrintableConfig = {\n content: OptionsAndDescriptors;\n type: (typeof ChainFormatter)[keyof typeof ChainFormatter];\n callerName: string | undefined | null;\n filepath: string | undefined | null;\n index: number | undefined | null;\n envName: string | undefined | null;\n};\n\nconst Formatter = {\n title(\n type: (typeof ChainFormatter)[keyof typeof ChainFormatter],\n callerName?: string | null,\n filepath?: string | null,\n ): string {\n let title = \"\";\n if (type === ChainFormatter.Programmatic) {\n title = \"programmatic options\";\n if (callerName) {\n title += \" from \" + callerName;\n }\n } else {\n title = \"config \" + filepath;\n }\n return title;\n },\n loc(index?: number | null, envName?: string | null): string {\n let loc = \"\";\n if (index != null) {\n loc += `.overrides[${index}]`;\n }\n if (envName != null) {\n loc += `.env[\"${envName}\"]`;\n }\n return loc;\n },\n\n *optionsAndDescriptors(opt: OptionsAndDescriptors) {\n const content = { ...opt.options };\n // overrides and env will be printed as separated config items\n delete content.overrides;\n delete content.env;\n // resolve to descriptors\n const pluginDescriptors = [...(yield* opt.plugins())];\n if (pluginDescriptors.length) {\n content.plugins = pluginDescriptors.map(d => descriptorToConfig(d));\n }\n const presetDescriptors = [...(yield* opt.presets())];\n if (presetDescriptors.length) {\n content.presets = [...presetDescriptors].map(d => descriptorToConfig(d));\n }\n return JSON.stringify(content, undefined, 2);\n },\n};\n\nfunction descriptorToConfig<API>(\n d: UnloadedDescriptor<API>,\n): object | string | [string, unknown] | [string, unknown, string] {\n let name: object | string = d.file?.request;\n if (name == null) {\n if (typeof d.value === \"object\") {\n name = d.value;\n } else if (typeof d.value === \"function\") {\n // If the unloaded descriptor is a function, i.e. `plugins: [ require(\"my-plugin\") ]`,\n // we print the first 50 characters of the function source code and hopefully we can see\n // `name: 'my-plugin'` in the source\n name = `[Function: ${d.value.toString().slice(0, 50)} ... ]`;\n }\n }\n if (name == null) {\n name = \"[Unknown]\";\n }\n if (d.options === undefined) {\n return name;\n } else if (d.name == null) {\n return [name, d.options];\n } else {\n return [name, d.options, d.name];\n }\n}\n\nexport class ConfigPrinter {\n _stack: Array<PrintableConfig> = [];\n configure(\n enabled: boolean,\n type: (typeof ChainFormatter)[keyof typeof ChainFormatter],\n {\n callerName,\n filepath,\n }: {\n callerName?: string;\n filepath?: string;\n },\n ) {\n if (!enabled) return () => {};\n return (\n content: OptionsAndDescriptors,\n index?: number | null,\n envName?: string | null,\n ) => {\n this._stack.push({\n type,\n callerName,\n filepath,\n content,\n index,\n