@@ -12,7 +12,7 @@ export type PluginOutput = {
1212} ;
1313
1414export type PlatformOutput = PluginOutput & {
15- autolinkingConfig : object | undefined ;
15+ autolinkingConfig : { project : Record < string , unknown > | undefined } ;
1616} ;
1717
1818export type PluginApi = {
@@ -76,6 +76,7 @@ export type ConfigType = {
7676} ;
7777
7878export type ConfigOutput = {
79+ root : string ;
7980 commands ?: Array < CommandType > ;
8081 platforms ?: Record < string , PlatformOutput > ;
8182} & PluginApi ;
@@ -85,7 +86,11 @@ const extensions = ['.js', '.ts', '.mjs'];
8586const importUp = async (
8687 dir : string ,
8788 name : string
88- ) : Promise < { config : ConfigType ; filePathWithExt : string } > => {
89+ ) : Promise < {
90+ config : ConfigType ;
91+ filePathWithExt : string ;
92+ configDir : string ;
93+ } > => {
8994 const filePath = path . join ( dir , name ) ;
9095
9196 for ( const ext of extensions ) {
@@ -100,7 +105,7 @@ const importUp = async (
100105 config = require ( filePathWithExt ) ;
101106 }
102107
103- return { config, filePathWithExt } ;
108+ return { config, filePathWithExt, configDir : dir } ;
104109 }
105110 }
106111
@@ -112,11 +117,11 @@ const importUp = async (
112117 return importUp ( parentDir , name ) ;
113118} ;
114119
115- export async function getConfig (
116- dir : string = process . cwd ( )
117- ) : Promise < ConfigOutput > {
118- // eslint-disable-next-line prefer-const
119- let { config , filePathWithExt } = await importUp ( dir , 'rnef.config' ) ;
120+ export async function getConfig ( dir : string ) : Promise < ConfigOutput > {
121+ const { config , filePathWithExt , configDir } = await importUp (
122+ dir ,
123+ 'rnef.config'
124+ ) ;
120125
121126 const { error, value : validatedConfig } = ConfigTypeSchema . validate (
122127 config
@@ -128,61 +133,63 @@ export async function getConfig(
128133 if ( error ) {
129134 logger . error (
130135 `Invalid ${ color . cyan (
131- path . relative ( process . cwd ( ) , filePathWithExt )
136+ path . relative ( configDir , filePathWithExt )
132137 ) } file:\n` + formatValidationError ( config , error )
133138 ) ;
134139 process . exit ( 1 ) ;
135140 }
136141
137- config = {
138- root : dir ,
139- get reactNativePath ( ) {
140- return resolveReactNativePath ( config . root || dir ) ;
141- } ,
142- get reactNativeVersion ( ) {
143- return getReactNativeVersion ( config . root || dir ) ;
144- } ,
145- ... validatedConfig ,
146- } ;
142+ const projectRoot = validatedConfig . root
143+ ? path . resolve ( configDir , validatedConfig . root )
144+ : configDir ;
145+
146+ if ( ! fs . existsSync ( projectRoot ) ) {
147+ logger . error (
148+ `Project root ${ projectRoot } does not exist. Please check your config file.`
149+ ) ;
150+ process . exit ( 1 ) ;
151+ }
147152
148153 const api = {
149154 registerCommand : ( command : CommandType ) => {
150- config . commands = [ ...( config . commands || [ ] ) , command ] ;
155+ validatedConfig . commands = [ ...( validatedConfig . commands || [ ] ) , command ] ;
151156 } ,
152- getProjectRoot : ( ) => path . resolve ( config . root as string ) ,
153- getReactNativeVersion : ( ) => config . reactNativeVersion as string ,
154- getReactNativePath : ( ) => config . reactNativePath as string ,
155- getPlatforms : ( ) => config . platforms as { [ platform : string ] : object } ,
156- getRemoteCacheProvider : ( ) => config . remoteCacheProvider ,
157+ getProjectRoot : ( ) => projectRoot ,
158+ getReactNativeVersion : ( ) => getReactNativeVersion ( projectRoot ) ,
159+ getReactNativePath : ( ) => resolveReactNativePath ( projectRoot ) ,
160+ getPlatforms : ( ) =>
161+ validatedConfig . platforms as { [ platform : string ] : object } ,
162+ getRemoteCacheProvider : ( ) => validatedConfig . remoteCacheProvider ,
157163 getFingerprintOptions : ( ) =>
158- config . fingerprint as {
164+ validatedConfig . fingerprint as {
159165 extraSources : string [ ] ;
160166 ignorePaths : string [ ] ;
161167 } ,
162168 } ;
163169
164- if ( config . plugins ) {
170+ if ( validatedConfig . plugins ) {
165171 // plugins register commands
166- for ( const plugin of config . plugins ) {
167- assignOriginToCommand ( plugin , api , config ) ;
172+ for ( const plugin of validatedConfig . plugins ) {
173+ assignOriginToCommand ( plugin , api , validatedConfig ) ;
168174 }
169175 }
170176
171177 const platforms : Record < string , PlatformOutput > = { } ;
172- if ( config . platforms ) {
178+ if ( validatedConfig . platforms ) {
173179 // platforms register commands and custom platform functionality (TBD)
174- for ( const platform in config . platforms ) {
175- const platformOutput = config . platforms [ platform ] ( api ) ;
180+ for ( const platform in validatedConfig . platforms ) {
181+ const platformOutput = validatedConfig . platforms [ platform ] ( api ) ;
176182 platforms [ platform ] = platformOutput ;
177183 }
178184 }
179185
180- if ( config . bundler ) {
181- assignOriginToCommand ( config . bundler , api , config ) ;
186+ if ( validatedConfig . bundler ) {
187+ assignOriginToCommand ( validatedConfig . bundler , api , validatedConfig ) ;
182188 }
183189
184190 const outputConfig : ConfigOutput = {
185- commands : config . commands ?? [ ] ,
191+ root : projectRoot ,
192+ commands : validatedConfig . commands ?? [ ] ,
186193 platforms : platforms ?? { } ,
187194 ...api ,
188195 } ;
0 commit comments