@@ -41,10 +41,79 @@ interface MethodSignature {
4141}
4242
4343export class ModuleWindowsSetup {
44- constructor (
45- readonly root : string ,
46- readonly options : ModuleWindowsSetupOptions ,
47- ) { }
44+ private actualModuleName ?: string ;
45+
46+ private async validateEnvironment ( ) : Promise < void > {
47+ this . verboseMessage ( 'Validating environment...' ) ;
48+
49+ // Check if package.json exists
50+ const packageJsonPath = path . join ( this . root , 'package.json' ) ;
51+ if ( ! ( await fs . exists ( packageJsonPath ) ) ) {
52+ throw new CodedError (
53+ 'NoPackageJson' ,
54+ 'No package.json found. Make sure you are in a React Native project directory.' ,
55+ ) ;
56+ }
57+
58+ // Check if it's a valid npm package
59+ try {
60+ const pkgJson = JSON . parse ( await fs . readFile ( packageJsonPath , 'utf8' ) ) ;
61+ if ( ! pkgJson . name ) {
62+ throw new CodedError (
63+ 'NoProjectName' ,
64+ 'package.json must have a "name" field.' ,
65+ ) ;
66+ }
67+ this . verboseMessage ( `Project name: ${ pkgJson . name } ` ) ;
68+ } catch ( error : any ) {
69+ if ( error . code === 'NoProjectName' ) {
70+ throw error ;
71+ }
72+ throw new CodedError (
73+ 'NoPackageJson' ,
74+ 'package.json is not valid JSON.' ,
75+ ) ;
76+ }
77+
78+ // Check if yarn is available
79+ try {
80+ execSync ( 'yarn --version' , { stdio : 'ignore' } ) ;
81+ this . verboseMessage ( 'Yarn found' ) ;
82+ } catch {
83+ throw new CodedError (
84+ 'Unknown' ,
85+ 'Yarn is required but not found. Please install Yarn first.' ,
86+ ) ;
87+ }
88+ }
89+
90+ private async extractModuleNameFromExistingSpec ( specFilePath : string ) : Promise < void > {
91+ try {
92+ const fullPath = path . join ( this . root , specFilePath ) ;
93+ const content = await fs . readFile ( fullPath , 'utf8' ) ;
94+
95+ // Extract the module name from TurboModuleRegistry.getEnforcing<Spec>('ModuleName')
96+ const exportMatch = content . match ( / T u r b o M o d u l e R e g i s t r y \. g e t E n f o r c i n g < S p e c > \( [ ' " ` ] ( [ ^ ' " ` ] + ) [ ' " ` ] \) / ) ;
97+ if ( exportMatch ) {
98+ this . actualModuleName = exportMatch [ 1 ] ;
99+ this . verboseMessage ( `Extracted actual module name: ${ this . actualModuleName } ` ) ;
100+ } else {
101+ this . verboseMessage ( 'Could not extract module name from spec file, using package name conversion' ) ;
102+ }
103+ } catch ( error ) {
104+ this . verboseMessage ( `Error reading spec file: ${ error } ` ) ;
105+ }
106+ }
107+
108+ private getActualModuleName ( packageName : string ) : string {
109+ // If we extracted the actual module name from an existing spec, use that
110+ if ( this . actualModuleName ) {
111+ return this . actualModuleName ;
112+ }
113+
114+ // Otherwise, fall back to the package name conversion
115+ return this . getModuleName ( packageName ) ;
116+ }
48117
49118 private async validateEnvironment ( ) : Promise < void > {
50119 this . verboseMessage ( 'Validating environment...' ) ;
@@ -138,6 +207,8 @@ export class ModuleWindowsSetup {
138207 await this . analyzeAndCreateSpecFile ( ) ;
139208 } else {
140209 this . verboseMessage ( `Found valid spec file(s): ${ validSpecFiles . join ( ', ' ) } ` ) ;
210+ // Extract the actual module name from the existing spec file
211+ await this . extractModuleNameFromExistingSpec ( validSpecFiles [ 0 ] ) ;
141212 }
142213 }
143214
@@ -177,7 +248,7 @@ export class ModuleWindowsSetup {
177248 const pkgJson = JSON . parse (
178249 await fs . readFile ( path . join ( this . root , 'package.json' ) , 'utf8' ) ,
179250 ) ;
180- const moduleName = this . getModuleName ( pkgJson . name || 'SampleModule' ) ;
251+ const moduleName = this . getActualModuleName ( pkgJson . name || 'SampleModule' ) ;
181252
182253 // Try to analyze existing API from multiple sources
183254 const apiMethods = await this . discoverApiMethods ( ) ;
@@ -500,7 +571,7 @@ export default TurboModuleRegistry.getEnforcing<Spec>('${moduleName}');
500571 const pkgJson = JSON . parse ( await fs . readFile ( packageJsonPath , 'utf8' ) ) ;
501572
502573 if ( ! pkgJson . codegenConfig ) {
503- const moduleName = this . getModuleName ( pkgJson . name || 'SampleModule' ) ;
574+ const moduleName = this . getActualModuleName ( pkgJson . name || 'SampleModule' ) ;
504575
505576 pkgJson . codegenConfig = {
506577 name : moduleName ,
@@ -514,7 +585,7 @@ export default TurboModuleRegistry.getEnforcing<Spec>('${moduleName}');
514585 } ;
515586
516587 await fs . writeFile ( packageJsonPath , JSON . stringify ( pkgJson , null , 2 ) ) ;
517- this . verboseMessage ( ' Added codegenConfig to package.json' ) ;
588+ this . verboseMessage ( ` Added codegenConfig to package.json with module name: ${ moduleName } ` ) ;
518589 } else {
519590 this . verboseMessage ( 'codegenConfig already exists in package.json' ) ;
520591 }
0 commit comments