import path from 'path' ; import { cosmiconfigSync } from 'cosmiconfig' ; import { Beta1 } from './version' ; export class OutputConfig { constructor ( private readonly data ? ) { } public getDir ( ) { return this . data ?. dir ?? process . cwd ( ) ; } } export class ScopeConfig { constructor ( private readonly data ? ) { } public getIncludesPattarns ( ) { return this . data ?. includes ?? [ '**/*.rediagram.{jsx,tsx}' ] ; } public getExcludesPattarns ( ) { return this . data ?. excludes ?? [ '**/node_modules/**/*' ] ; } } export class DotConfig { constructor ( private readonly data ? ) { } public getTimeout ( ) { return this . data ?. timeout ?? 10000 ; } } export type RediagramGlobalConfig = { readonly filepath : string | null ; readonly output : OutputConfig ; readonly scope : ScopeConfig ; readonly dot : DotConfig ; } ; const MODULE_NAME = 'rediagram' ; function createDefaultConfig ( ) { return { filepath : null , scope : new ScopeConfig ( ) , output : new OutputConfig ( ) , dot : new DotConfig ( ) , } ; } namespace beta1 { type Config = { version : Beta1 ; includes ? : string [ ] ; excludes ? : string [ ] ; output ? : { dir ? : string ; } ; dot ? : { timeout ? : number ; } ; } ; export function load ( filepath , data ) { return { filepath , output : new OutputConfig ( { dir : path . resolve ( path . dirname ( filepath ) , data . output ?. dir ?? '.' ) , } ) , scope : new ScopeConfig ( { includes : data . includes , excludes : data . excludes , } ) , dot : new DotConfig ( { timeout : data . dot ?. timeout , } ) , } ; } } function loadConfig ( ) { const { search } = cosmiconfigSync ( MODULE_NAME ) ; const result = search ( ) ; if ( result !== null ) { if ( result . config . version === 'beta1' ) { return beta1 . load ( result . filepath , result . config ) ; } } return createDefaultConfig ( ) ; } export const CONFIG = loadConfig ( ) ;