import camelcaseKeys from "camelcase-keys" ; import yml from "js-yaml" ; import * as util from "util" ; import { UserError } from "../logging/user-error" ; import * as config from "." ; /** provides the Data containing the configuration settings in the given configuration file content */ export function parseYML ( configText ) { const result = config . defaultValues ( ) as config . ChangeableData ; if ( configText . trim ( ) === "" ) { return result ; } try { var parsed = camelcaseKeys ( yml . load ( configText ) as Record < string , unknown > ) ; } catch ( e ) { throw new UserError ( `Prettifier configuration is not valid YML` , `File \`.github/prettifier.yml\` contains this invalid content:\n\n\`\`\`\n${ configText }\n\`\`\`\n` , e ) ; } if ( typeof parsed . cannotPrettifyMessage === "string" ) { result . cannotPrettifyMessage = parsed . cannotPrettifyMessage ; delete parsed . cannotPrettifyMessage ; } else if ( typeof parsed . cannotPrettifyMessage === "boolean" && parsed . cannotPrettifyMessage === false ) { result . cannotPrettifyMessage = "" ; delete parsed . cannotPrettifyMessage ; } else if ( parsed . cannotPrettifyMessage != null ) { throw new UserError ( `Unexpected value for "cannotPrettifyMessage" in .github/prettifier.yml: ${ util . inspect ( parsed . cannotPrettifyMessage ) }` , "This setting should be a string, or set it to `false` to disable the feature." , new Error ( ) , { configuration : configText } ) ; } if ( typeof parsed . commitMessage === "string" ) { result . commitMessage = parsed . commitMessage ; delete parsed . commitMessage ; } else if ( parsed . commitMessage != null ) { throw new UserError ( `Unexpected value for "commitMessage" in .github/prettifier.yml: ${ util . inspect ( parsed . commitMessage ) }` , "This setting should be a string" , new Error ( ) , { configuration : configText } ) ; } if ( Array . isArray ( parsed . excludeBranches ) ) { result . excludeBranches = parsed . excludeBranches ; delete parsed . excludeBranches ; } else if ( typeof parsed . excludeBranches === "string" ) { result . excludeBranches = [ parsed . excludeBranches ] ; delete parsed . excludeBranches ; } else if ( parsed . excludeBranches != null ) { throw new UserError ( `Unexpected value for "excludeBranches" in .github/prettifier.yml: ${ util . inspect ( parsed . excludeBranches ) }` , "This setting should be a list of strings" , new Error ( ) , { configuration : configText } ) ; } if ( Array . isArray ( parsed . excludeFiles ) ) { result . excludeFiles = parsed . excludeFiles ; delete parsed . excludeFiles ; } else if ( typeof parsed . excludeFiles === "string" ) { result . excludeFiles = [ parsed . excludeFiles ] ; delete parsed . excludeFiles ; } else if ( parsed . excludeFiles != null ) { throw new UserError ( `Unexpected value "excludeFiles" in .github/prettifier.yml: ${ util . inspect ( parsed . excludeFiles ) }` , "This setting should be a list of strings" , new Error ( ) , { configuration : configText } ) ; } for ( const attr of [ "prettifiedMessage" , "prettificationNotification" ] ) { if ( parsed [ attr ] != null ) { if ( typeof parsed [ attr ] === "string" ) { result . prettifiedMessage = parsed [ attr ] as string ; delete parsed [ attr ] ; } else { throw new UserError ( `Unexpected value for "${ attr }" in .github/prettifier.yml: ${ util . inspect ( parsed [ attr ] ) }` , "This setting should be a string, or set it to `false` to disable the feature." , new Error ( ) , { configuration : configText } ) ; } } } if ( typeof parsed . pullsOnly === "boolean" ) { result . pullsOnly = parsed . pullsOnly ; delete parsed . pullsOnly ; } else if ( parsed . pullsOnly != null ) { throw new UserError ( `Unexpected value for "pullsOnly" in .github/prettifier.yml: ${ util . inspect ( parsed . pullsOnly ) }` , "This setting should be a boolean value" , new Error ( ) , { configuration : configText } ) ; } const leftovers = Object . keys ( parsed ) ; if ( leftovers . length > 0 ) { throw new UserError ( `I found unexpected elements in .github/prettifier.yml: ${ leftovers . join ( ", " ) }` , "I assume this is a mistake on your end and won't make any changes to your code until this is fixed, because I'm not sure what you want me to do. Please see https://www.prettifier.io/configure/prettifier-yml for my configuration settings." , new Error ( ) , { configuration : configText } ) ; } return result ; }