import { join , dirname , extname } from 'path' ; import { pathExists } from 'fs-extra' ; import { isObject } from '@app-config/utils' ; import { named } from '@app-config/extension-utils' ; import { ParsingExtension , Root } from '@app-config/core' ; import { FileSource } from '@app-config/node' ; import { logger } from '@app-config/logging' ; /** V1 app-config compatibility */ export default function v1Compat ( shouldShowDeprecationNotice ? ) { return named ( 'v1-compat' , ( value , [ _ , key ] , parentKeys ) => { if ( parentKeys [ parentKeys . length - 1 ] ?. [ 0 ] !== Root ) { return false ; } if ( key === 'app-config' && isObject ( value ) ) { return async ( parse , _ , ctx ) => { const resolveAmbiguousFilename = async ( filepath ) => { let resolvedPath = filepath ; if ( ctx instanceof FileSource ) { resolvedPath = join ( dirname ( ctx . filePath ) , filepath ) ; } switch ( extname ( resolvedPath ) ) { case '.yml' : case '.yaml' : case '.json' : case '.json5' : case '.toml' : return resolvedPath ; default : { if ( await pathExists ( `${ resolvedPath }.yml` ) ) return `${ resolvedPath }.yml` ; if ( await pathExists ( `${ resolvedPath }.yaml` ) ) return `${ resolvedPath }.yaml` ; if ( await pathExists ( `${ resolvedPath }.json` ) ) return `${ resolvedPath }.json` ; if ( await pathExists ( `${ resolvedPath }.json5` ) ) return `${ resolvedPath }.json5` ; if ( await pathExists ( `${ resolvedPath }.toml` ) ) return `${ resolvedPath }.toml` ; return resolvedPath ; } } } ; if ( 'extends' in value ) { if ( shouldShowDeprecationNotice ) { logger . warn ( 'Detected deprecated use of @app-config/v1-compat parsing extension. Please install @app-config/v1-compat and add it to your meta file "parsingExtensions".' ) ; } return parse ( { $extends : await resolveAmbiguousFilename ( value . extends as string ) } , { shouldMerge : true } ) ; } if ( 'extendsOptional' in value ) { if ( shouldShowDeprecationNotice ) { logger . warn ( 'Detected deprecated use of @app-config/v1-compat parsing extension. Please install @app-config/v1-compat and add it to your meta file "parsingExtensions".' ) ; } return parse ( { $extends : { path : await resolveAmbiguousFilename ( value . extendsOptional as string ) , optional : true , } , } , { shouldMerge : true } ) ; } if ( 'override' in value ) { if ( shouldShowDeprecationNotice ) { logger . warn ( 'Detected deprecated use of @app-config/v1-compat parsing extension. Please install @app-config/v1-compat and add it to your meta file "parsingExtensions".' ) ; } return parse ( { $override : await resolveAmbiguousFilename ( value . override as string ) } , { shouldOverride : true } ) ; } if ( 'overrideOptional' in value ) { if ( shouldShowDeprecationNotice ) { logger . warn ( 'Detected deprecated use of @app-config/v1-compat parsing extension. Please install @app-config/v1-compat and add it to your meta file "parsingExtensions".' ) ; } return parse ( { $override : { path : await resolveAmbiguousFilename ( value . overrideOptional as string ) , optional : true , } , } , { shouldOverride : true } ) ; } return parse ( value ) ; } ; } return false ; } ) ; }