11import path from "node:path"
22import os from "node:os"
3+ import fs from "node:fs/promises"
34import { log } from "./logger"
45import { AsyncResolver , LogConfig , LogLevel , ScaffoldCmdConfig , ScaffoldConfigMap } from "./types"
56import { spawn } from "node:child_process"
@@ -22,29 +23,60 @@ export async function getGitConfig(
2223 clone . on ( "error" , reject )
2324 clone . on ( "close" , async ( code ) => {
2425 if ( code === 0 ) {
25- log ( logConfig , LogLevel . info , `Loading config from git repo: ${ repoUrl } ` )
26- // TODO search for dynamic config file in repo if not provided
27- const filename = file || "scaffold.config.js"
28- const absolutePath = path . resolve ( tmpPath , filename )
29- const loadedConfig = await resolve (
30- async ( ) => ( await import ( absolutePath ) ) . default as ScaffoldConfigMap ,
31- logConfig ,
32- )
33-
34- log ( logConfig , LogLevel . info , `Loaded config from git` )
35- log ( logConfig , LogLevel . debug , `Raw config:` , loadedConfig )
36- const fixedConfig : ScaffoldConfigMap = { }
37- for ( const [ k , v ] of Object . entries ( loadedConfig ) ) {
38- fixedConfig [ k ] = {
39- ...v ,
40- templates : v . templates . map ( ( t ) => path . resolve ( tmpPath , t ) ) ,
41- }
42- }
43- res ( wrapNoopResolver ( fixedConfig ) )
26+ res ( await loadGitConfig ( { logConfig, url : repoUrl , file, tmpPath } ) )
4427 return
4528 }
4629
4730 reject ( new Error ( `Git clone failed with code ${ code } ` ) )
4831 } )
4932 } )
5033}
34+
35+ /** @internal */
36+ export async function loadGitConfig ( {
37+ logConfig,
38+ url : repoUrl ,
39+ file,
40+ tmpPath,
41+ } : {
42+ logConfig : LogConfig
43+ url : string
44+ file : string
45+ tmpPath : string
46+ } ) : Promise < AsyncResolver < ScaffoldCmdConfig , ScaffoldConfigMap > > {
47+ log ( logConfig , LogLevel . info , `Loading config from git repo: ${ repoUrl } ` )
48+ const filename = file || ( await findConfigFile ( tmpPath ) )
49+ const absolutePath = path . resolve ( tmpPath , filename )
50+ const loadedConfig = await resolve ( async ( ) => ( await import ( absolutePath ) ) . default as ScaffoldConfigMap , logConfig )
51+
52+ log ( logConfig , LogLevel . info , `Loaded config from git` )
53+ log ( logConfig , LogLevel . debug , `Raw config:` , loadedConfig )
54+ const fixedConfig : ScaffoldConfigMap = { }
55+ for ( const [ k , v ] of Object . entries ( loadedConfig ) ) {
56+ fixedConfig [ k ] = {
57+ ...v ,
58+ templates : v . templates . map ( ( t ) => path . resolve ( tmpPath , t ) ) ,
59+ }
60+ }
61+ await fs . rm ( tmpPath , { recursive : true } )
62+ return wrapNoopResolver ( fixedConfig )
63+ }
64+
65+ /** @internal */
66+ export async function findConfigFile ( root : string ) : Promise < string > {
67+ const allowed = [ "mjs" , "cjs" , "js" , "json" ] . reduce ( ( acc , ext ) => {
68+ acc . push ( `scaffold.config.${ ext } ` )
69+ acc . push ( `scaffold.${ ext } ` )
70+ return acc
71+ } , [ ] as string [ ] )
72+ for ( const file of allowed ) {
73+ const exists = await fs
74+ . stat ( path . resolve ( root , file ) )
75+ . then ( ( ) => true )
76+ . catch ( ( ) => false )
77+ if ( exists ) {
78+ return file
79+ }
80+ }
81+ throw new Error ( `Could not find config file in git repo` )
82+ }
0 commit comments