11import path from "node:path"
2+ import fs from "node:fs/promises"
23import {
34 ConfigLoadConfig ,
45 FileResponse ,
@@ -14,6 +15,7 @@ import { handlebarsParse } from "./parser"
1415import { log } from "./logger"
1516import { resolve , wrapNoopResolver } from "./utils"
1617import { getGitConfig } from "./git"
18+ import { isDir , pathExists } from "./file"
1719
1820/** @internal */
1921export function getOptionValueForFile < T > (
@@ -68,7 +70,7 @@ export async function parseConfigFile(config: ScaffoldCmdConfig, tmpPath: string
6870 const configFilename = config . config
6971 const configPath = isGit ? config . git : configFilename
7072
71- log ( config , LogLevel . info , `Loading config from ${ configFilename } with key ${ key } ` )
73+ log ( config , LogLevel . info , `Loading config from file ${ configFilename } with key ${ key } ` )
7274
7375 const configPromise = await ( isGit
7476 ? getRemoteConfig ( { git : configPath , config : configFilename , logLevel : config . logLevel , tmpPath } )
@@ -115,8 +117,22 @@ export function githubPartToUrl(part: string): string {
115117export async function getLocalConfig ( config : ConfigLoadConfig & Partial < LogConfig > ) : Promise < ScaffoldConfigFile > {
116118 const { config : configFile , ...logConfig } = config as Required < typeof config >
117119
118- log ( logConfig , LogLevel . info , `Loading config from file ${ configFile } ` )
119120 const absolutePath = path . resolve ( process . cwd ( ) , configFile )
121+
122+ const _isDir = await isDir ( absolutePath )
123+
124+ if ( _isDir ) {
125+ log ( logConfig , LogLevel . debug , `Resolving config file from directory ${ absolutePath } ` )
126+ const file = await findConfigFile ( absolutePath )
127+ const exists = await pathExists ( file )
128+ if ( ! exists ) {
129+ throw new Error ( `Could not find config file in directory ${ absolutePath } ` )
130+ }
131+ log ( logConfig , LogLevel . info , `Loading config from: ${ path . resolve ( absolutePath , file ) } ` )
132+ return wrapNoopResolver ( import ( path . resolve ( absolutePath , file ) ) )
133+ }
134+
135+ log ( logConfig , LogLevel . info , `Loading config from: ${ absolutePath } ` )
120136 return wrapNoopResolver ( import ( absolutePath ) )
121137}
122138
@@ -138,3 +154,22 @@ export async function getRemoteConfig(
138154
139155 return getGitConfig ( url , configFile , tmpPath , logConfig )
140156}
157+
158+ /** @internal */
159+ export async function findConfigFile ( root : string ) : Promise < string > {
160+ const allowed = [ "mjs" , "cjs" , "js" , "json" ] . reduce ( ( acc , ext ) => {
161+ acc . push ( `scaffold.config.${ ext } ` )
162+ acc . push ( `scaffold.${ ext } ` )
163+ return acc
164+ } , [ ] as string [ ] )
165+ for ( const file of allowed ) {
166+ const exists = await fs
167+ . stat ( path . resolve ( root , file ) )
168+ . then ( ( ) => true )
169+ . catch ( ( ) => false )
170+ if ( exists ) {
171+ return file
172+ }
173+ }
174+ throw new Error ( `Could not find config file in git repo` )
175+ }
0 commit comments