import * as TOML from "@iarna/toml" ; import yml from "js-yaml" ; import JSON5 from "json5" ; import prettier from "prettier" ; import { UserError } from "../logging/user-error" ; export interface ConfigResult { readonly package_json : string ; readonly prettierrc : string ; readonly prettierrc_json : string ; readonly prettierrc_json5 : string ; readonly prettierrc_toml : string ; readonly prettierrc_yaml : string ; readonly prettierrc_yml : string ; } export function loadConfig ( result ) { return ( loadConfigFile ( result . prettierrc , ".prettierrc" , parseYML ) || loadConfigFile ( result . prettierrc_json , ".prettierrc.json" , JSON5 . parse ) || loadConfigFile ( result . prettierrc_json5 , ".prettierrc.json5" , JSON5 . parse ) || loadConfigFile ( result . prettierrc_toml , ".prettierrc.toml" , TOML . parse ) || loadConfigFile ( result . prettierrc_yml , ".prettierrc.yml" , parseYML ) || loadConfigFile ( result . prettierrc_yaml , ".prettierrc.yaml" , parseYML ) || loadConfigFile ( result . package_json , "package.json" , parsePackageJson ) || { } ) ; } export function parseYML ( text ) { return yml . load ( text ) as Record < string , unknown > ; } export function loadConfigFile ( configText , filename , loader ) { if ( configText === "" ) { return ; } try { return loader ( configText ) ; } catch ( e ) { throw new UserError ( `invalid content in file "${ filename }"` , `\`\`\`\n${ configText }\n\`\`\`\n` , e ) ; } } function parsePackageJson ( configText ) { try { const parsed = JSON . parse ( configText ) ; return ( parsed . prettier || { } ) as Record < string , unknown > ; } catch ( e ) { throw new UserError ( "invalid content in file `.prettierrc`" , `\`\`\`\n${ configText }\n\`\`\`\n` , e ) ; } }