import { cosmiconfig } from 'cosmiconfig' ; import { debug , error } from './log' ; export type JPCMConfig = { messagePattern : string ; jiraTicketPattern : string ; commentChar : string ; isConventionalCommit : boolean ; allowEmptyCommitMessage : boolean ; gitRoot : string ; } ; const defaultConfig = { messagePattern : '[$J] $M' , jiraTicketPattern : '([A-Z]+-\\d+)' , commentChar : '#' , isConventionalCommit : false , allowEmptyCommitMessage : false , gitRoot : '' , } as JPCMConfig ; function resolveConfig ( configPath ) { try { return require . resolve ( configPath ) ; } catch { return configPath ; } } export async function loadConfig ( configPath ? ) { try { const explorer = cosmiconfig ( 'jira-prepare-commit-msg' , { searchPlaces : [ 'package.json' , '.jirapreparecommitmsgrc' , '.jirapreparecommitmsgrc.json' , '.jirapreparecommitmsgrc.yaml' , '.jirapreparecommitmsgrc.yml' , 'jira-prepare-commit-msg.config.js' , ] , } ) ; const config = configPath ? await explorer . load ( resolveConfig ( configPath ) ) : await explorer . search ( ) ; debug ( `Loaded config: ${ JSON . stringify ( config ) }` ) ; if ( config && ! config . isEmpty ) { const result = { ... defaultConfig , ... config . config } ; debug ( `Used config: ${ JSON . stringify ( result ) }` ) ; return result as JPCMConfig ; } } catch ( err ) { error ( `Loading configuration failed with error: ${ err }` ) ; } const result = { ... defaultConfig } ; debug ( `Used config: ${ JSON . stringify ( result ) }` ) ; return result ; }