import { getRequireExtensions } from "@kosko/require" ; import { join } from "path" ; import { formatPath } from "../paths" ; import { toArray } from "../utils" ; import { createEnvironment , createLoaderReducers , createReducerList } from "./base" ; import { Environment , Loader , ReducerExecutor , ReducerList } from "./types" ; export function getNodeExtensions ( ) { return getRequireExtensions ( ) . map ( ( ext ) => ext . substring ( 1 ) ) ; } export interface NodeEnvironmentOptions { /**
     * Current working directory.
     */ cwd ? : string ; } export interface InternalNodeEnvironmentOptions extends NodeEnvironmentOptions { createReducerExecutor ( reducers ) : ReducerExecutor ; requireModule ( environment , id ) : any ; mergeValues ( data ) : any ; } export function createNodeEnvironment ( { cwd = process . cwd ( ) , createReducerExecutor , requireModule , mergeValues } ) { function requireAllEnvs ( template , component ? ) { if ( ! environment . env ) return [ ] ; const envs = toArray ( environment . env ) ; return envs . map ( ( env ) => { const path = formatPath ( template , { environment : env , ... ( component && { component } ) } ) ; return requireModule ( environment , join ( environment . cwd , path ) ) ; } ) ; } const loader = { global : ( ) => requireAllEnvs ( environment . paths . global ) , component : ( name ) => requireAllEnvs ( environment . paths . component , name ) } ; const reducers = createReducerList ( createLoaderReducers ( loader , mergeValues ) ) ; const { reduce } = createReducerExecutor ( reducers ) ; const environment = createEnvironment ( { cwd , extensions : getNodeExtensions ( ) , setReducers : reducers . setReducers , resetReducers : reducers . resetReducers , reduce } ) ; return environment ; }