import type { CompilerSystem , Diagnostic } from '../declarations' ; import { isString , normalizePath , buildError } from '@utils' ; export const findConfig = async ( opts ) => { const sys = opts . sys ; const cwd = sys . getCurrentDirectory ( ) ; const results = { configPath : null as string , rootDir : normalizePath ( cwd ) , diagnostics : [ ] as Diagnostic [ ] , } ; let configPath = opts . configPath ; if ( isString ( configPath ) ) { if ( ! sys . platformPath . isAbsolute ( configPath ) ) { configPath = normalizePath ( sys . platformPath . join ( cwd , configPath ) ) ; } else { configPath = normalizePath ( opts . configPath ) ; } } else { configPath = results . rootDir ; } const stat = await sys . stat ( configPath ) ; if ( stat . error ) { const diagnostic = buildError ( results . diagnostics ) ; diagnostic . absFilePath = configPath ; diagnostic . header = `Invalid config path` ; diagnostic . messageText = `Config path "${ configPath }" not found` ; return results ; } if ( stat . isFile ) { results . configPath = configPath ; results . rootDir = sys . platformPath . dirname ( configPath ) ; } else if ( stat . isDirectory ) { for ( const configName of [ 'stencil.config.ts' , 'stencil.config.js' ] ) { const testConfigFilePath = sys . platformPath . join ( configPath , configName ) ; const stat = await sys . stat ( testConfigFilePath ) ; if ( stat . isFile ) { results . configPath = testConfigFilePath ; results . rootDir = sys . platformPath . dirname ( testConfigFilePath ) ; break ; } } } return results ; } ;