import path from "path" ; import pkgDir from "pkg-dir" ; import findUp from "find-up" ; import { LogLevel , Logger } from "backfill-logger" ; import { CacheStorageConfig } from "./cacheConfig" ; import { getEnvConfig } from "./envConfig" ; export * from "./cacheConfig" ; export * from "./envConfig" ; export type HashGlobs = string [ ] ; export const modesObject = { READ_ONLY : "" , WRITE_ONLY : "" , READ_WRITE : "" , PASS : "" , } ; export type BackfillModes = keyof typeof modesObject ; export type Config = { cacheStorageConfig : CacheStorageConfig ; clearOutput : boolean ; internalCacheFolder : string ; logFolder : string ; logLevel : LogLevel ; name : string ; mode : BackfillModes ; outputGlob : string [ ] ; packageRoot : string ; performanceReportName ? : string ; producePerformanceLogs : boolean ; validateOutput : boolean ; } ; export function isCorrectMode ( mode ) { return modesObject . hasOwnProperty ( mode ) ; } export function getName ( packageRoot ) { return ( require ( path . join ( packageRoot , "package.json" ) ) . name || path . basename ( path . dirname ( packageRoot ) ) ) ; } export function getSearchPaths ( fromPath ) { const searchPaths = [ ] ; let nextPath = fromPath ; while ( nextPath ) { const configLocation = findUp . sync ( "backfill.config.js" , { cwd : nextPath } ) ; if ( configLocation ) { searchPaths . push ( configLocation ) ; nextPath = path . join ( path . dirname ( configLocation ) , ".." ) ; } else { nextPath = undefined ; } } return searchPaths . reverse ( ) ; } export function createDefaultConfig ( fromPath ) { const packageRoot = pkgDir . sync ( fromPath ) || fromPath ; const defaultCacheFolder = path . join ( packageRoot , "node_modules" , ".cache" , "backfill" ) ; const outputGlob = [ "lib/**" ] ; return { cacheStorageConfig : { provider : "local" , } , clearOutput : false , internalCacheFolder : defaultCacheFolder , logFolder : defaultCacheFolder , logLevel : "info" , name : getName ( packageRoot ) , mode : "READ_WRITE" , outputGlob , packageRoot , producePerformanceLogs : false , validateOutput : false , } ; } export function createConfig ( logger , fromPath ) { const defaultConfig = createDefaultConfig ( fromPath ) ; const fileBasedConfig = getSearchPaths ( fromPath ) . reduce ( ( acc , configPath ) => { const config = require ( configPath ) ; if ( config [ "mode" ] ) { if ( ! isCorrectMode ( config [ "mode" ] ) ) { throw `Backfill config option "mode" was set, but with the wrong value: "${ config [ "mode" ] }".` ; } } return { ... acc , ... config , } ; } , { } ) ; const envBasedConfig = getEnvConfig ( logger ) ; return { ... defaultConfig , ... fileBasedConfig , ... envBasedConfig , } ; }