import { Configuration , } from "@css-blocks/core" ; import { TransformSync , cosmiconfigSync as cosmiconfig } from "cosmiconfig" ; import * as debugGenerator from "debug" ; import merge = require ( "lodash.merge" ) ; import { dirname , resolve } from "path" ; const debug = debugGenerator ( "css-blocks:config" ) ; type UnknownObject = { [ k ] : unknown ; } ; /**
 * Resolves paths against the file's directory and recursively processes
 * the 'extends' option.
 *
 * If preprocessors is a string, attempts to load a javascript file from that location.
 */ const transform = ( result ) => { if ( ! result ) return null ; debug ( `Processing raw configuration loaded from ${ result . filepath }` ) ; let dir = dirname ( result . filepath ) ; let config = result . config ; if ( typeof config . rootDir === "string" ) { config . rootDir = resolve ( dir , config . rootDir ) ; } if ( typeof config . preprocessors === "string" ) { let file = resolve ( dir , config . preprocessors ) ; debug ( `Loading preprocessors from ${ file }` ) ; config . preprocessors = require ( file ) as UnknownObject ; } if ( typeof config . importer === "string" ) { let file = resolve ( dir , config . importer ) ; debug ( `Loading importer from ${ file }` ) ; let { importer , data } = require ( file ) as UnknownObject ; config . importer = importer ; if ( data ) { config . importerData = data ; } } if ( typeof config . extends === "string" ) { let baseConfigFile = resolve ( dir , config . extends ) ; delete config . extends ; debug ( `Extending configuration found at: ${ baseConfigFile }` ) ; let baseConfig = _load ( baseConfigFile , transform ) ; let importer = config . importer || baseConfig . importer ; let importerData = config . importerData || baseConfig . importerData ; config = merge ( { } , baseConfig , config ) ; if ( importer ) { config . importer = importer ; } if ( importerData ) { config . importerData = importerData ; } } result . config = config ; return result ; } ; /**
 * This transform only runs on the final configuration file. It does not run on
 * any configuration file that is being extended.
 */ const transformFinal = ( result ) => { if ( ! result ) return null ; debug ( `Using configuration file found at ${ result . filepath }` ) ; result = transform ( result ) ; if ( ! result ) return null ; if ( ! result . config . rootDir ) { let dir = dirname ( result . filepath ) ; debug ( `No rootDir specified. Defaulting to: ${ dir }` ) ; result . config . rootDir = dir ; } return result ; } ; const SEARCH_PLACES = [ "css-blocks.config.json" , "css-blocks.config.js" , "package.json" , ] ; param searchDirectory export function searchSync ( searchDirectory ? ) { let loader = cosmiconfig ( "css-blocks" , { transform : transformFinal , searchPlaces : SEARCH_PLACES , } ) ; let result = loader . search ( searchDirectory ) ; return result && result . config as Partial < Configuration > ; } see { searchSync } export function search ( searchDirectory ? ) { try { return Promise . resolve ( searchSync ( searchDirectory ) ) ; } catch ( e ) { return Promise . reject ( e ) ; } } param configPath throws returns export async function load ( configPath ) { return _load ( configPath , transformFinal ) ; } function _load ( configPath , transform ) { let loader = cosmiconfig ( "css-blocks" , { transform , } ) ; let result = loader . load ( configPath ) ; return result ! . config as Partial < Configuration > ; }