Skip to content

Commit c338c4e

Browse files
Copilotanupriya13
andcommitted
Fix module-windows-setup to use codegenConfig.windows.outputDirectory for correct project directory detection
Co-authored-by: anupriya13 <[email protected]>
1 parent 23a65ef commit c338c4e

File tree

1 file changed

+83
-34
lines changed

1 file changed

+83
-34
lines changed

packages/@react-native-windows/cli/src/commands/moduleWindowsSetup/moduleWindowsSetup.ts

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,31 @@ export default TurboModuleRegistry.getEnforcing<Spec>('${moduleName}');
779779
}
780780
}
781781

782+
private async getProjectDirectoryFromCodegenConfig(): Promise<string | null> {
783+
try {
784+
const packageJsonPath = path.join(this.root, 'package.json');
785+
const pkgJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
786+
787+
if (pkgJson.codegenConfig?.windows?.outputDirectory) {
788+
const outputDirectory = pkgJson.codegenConfig.windows.outputDirectory;
789+
this.verboseMessage(`Found codegenConfig.windows.outputDirectory: ${outputDirectory}`);
790+
791+
// Extract project directory from outputDirectory path
792+
// E.g., "windows/ReactNativeWebview/codegen" -> "ReactNativeWebview"
793+
const parts = outputDirectory.split('/');
794+
if (parts.length >= 2 && parts[0] === 'windows') {
795+
const projectDir = parts[1];
796+
this.verboseMessage(`Extracted project directory from outputDirectory: ${projectDir}`);
797+
return projectDir;
798+
}
799+
}
800+
} catch (error) {
801+
this.verboseMessage(`Error reading package.json codegenConfig: ${error}`);
802+
}
803+
804+
return null;
805+
}
806+
782807
private async generateStubFiles(): Promise<void> {
783808
this.verboseMessage('Generating C++ stub files...');
784809

@@ -854,53 +879,77 @@ export default TurboModuleRegistry.getEnforcing<Spec>('${moduleName}');
854879
this.verboseMessage(`Error reading codegen directory contents: ${error}`);
855880
}
856881

857-
// Find the actual Windows project directory created by init-windows
882+
// First try to get project directory from package.json codegenConfig
858883
const windowsDir = path.join(this.root, 'windows');
859-
const actualModuleName = await this.getFinalModuleName();
884+
let projectName = await this.getProjectDirectoryFromCodegenConfig();
885+
let moduleDir: string;
860886

861-
// Look for existing Windows project directory (created by init-windows)
862-
let moduleDir = path.join(windowsDir, actualModuleName);
887+
if (projectName) {
888+
moduleDir = path.join(windowsDir, projectName);
889+
this.verboseMessage(`Using project directory from codegenConfig: ${moduleDir}`);
890+
891+
// Verify the directory exists
892+
if (!(await fs.exists(moduleDir))) {
893+
this.verboseMessage(`Project directory from codegenConfig does not exist: ${moduleDir}`);
894+
projectName = null; // Fall back to search
895+
}
896+
}
863897

864-
// If the expected directory doesn't exist, find any existing project directory
865-
if (!(await fs.exists(moduleDir))) {
866-
this.verboseMessage(`Expected directory ${moduleDir} not found, searching for existing Windows project directory...`);
898+
// If no project directory from codegenConfig or it doesn't exist, search for existing directory
899+
if (!projectName) {
900+
this.verboseMessage('Searching for existing Windows project directory...');
901+
const actualModuleName = await this.getFinalModuleName();
902+
moduleDir = path.join(windowsDir, actualModuleName);
867903

868-
try {
869-
const windowsDirContents = await fs.readdir(windowsDir);
870-
const projectDirs = [];
904+
// If the expected directory doesn't exist, find any existing project directory
905+
if (!(await fs.exists(moduleDir))) {
906+
this.verboseMessage(`Expected directory ${moduleDir} not found, searching for existing Windows project directory...`);
871907

872-
for (const item of windowsDirContents) {
873-
const itemPath = path.join(windowsDir, item);
874-
const stats = await fs.stat(itemPath);
908+
try {
909+
const windowsDirContents = await fs.readdir(windowsDir);
910+
const projectDirs = [];
875911

876-
if (stats.isDirectory() && !item.startsWith('.') &&
877-
item !== 'ExperimentalFeatures.props' &&
878-
!item.endsWith('.sln')) {
879-
// Check if this directory contains typical project files
880-
const possibleHeaderFile = path.join(itemPath, `${item}.h`);
881-
const possibleCppFile = path.join(itemPath, `${item}.cpp`);
882-
if (await fs.exists(possibleHeaderFile) || await fs.exists(possibleCppFile)) {
883-
projectDirs.push(item);
912+
for (const item of windowsDirContents) {
913+
const itemPath = path.join(windowsDir, item);
914+
const stats = await fs.stat(itemPath);
915+
916+
if (stats.isDirectory() && !item.startsWith('.') &&
917+
item !== 'ExperimentalFeatures.props' &&
918+
!item.endsWith('.sln')) {
919+
// Check if this directory contains typical project files
920+
const possibleHeaderFile = path.join(itemPath, `${item}.h`);
921+
const possibleCppFile = path.join(itemPath, `${item}.cpp`);
922+
if (await fs.exists(possibleHeaderFile) || await fs.exists(possibleCppFile)) {
923+
projectDirs.push(item);
924+
}
884925
}
885926
}
886-
}
887-
888-
if (projectDirs.length > 0) {
889-
const existingProjectName = projectDirs[0];
890-
moduleDir = path.join(windowsDir, existingProjectName);
891-
this.verboseMessage(`Found existing Windows project directory: ${moduleDir}`);
892-
} else {
893-
this.verboseMessage(`No existing project directory found, creating: ${moduleDir}`);
927+
928+
if (projectDirs.length > 0) {
929+
projectName = projectDirs[0];
930+
moduleDir = path.join(windowsDir, projectName);
931+
this.verboseMessage(`Found existing Windows project directory: ${moduleDir}`);
932+
} else {
933+
this.verboseMessage(`No existing project directory found, using actualModuleName: ${actualModuleName}`);
934+
projectName = actualModuleName;
935+
moduleDir = path.join(windowsDir, projectName);
936+
await fs.mkdir(moduleDir, {recursive: true});
937+
}
938+
} catch (error) {
939+
this.verboseMessage(`Error searching for Windows project directory: ${error}`);
940+
projectName = actualModuleName;
941+
moduleDir = path.join(windowsDir, projectName);
894942
await fs.mkdir(moduleDir, {recursive: true});
895943
}
896-
} catch (error) {
897-
this.verboseMessage(`Error searching for Windows project directory: ${error}`);
898-
await fs.mkdir(moduleDir, {recursive: true});
944+
} else {
945+
projectName = actualModuleName;
899946
}
900947
}
901948

902-
// Use the project directory name for file names
903-
const projectName = path.basename(moduleDir);
949+
// Use the determined project directory name for file names
950+
if (!projectName) {
951+
projectName = path.basename(moduleDir);
952+
}
904953

905954
// Store the actual project path for the success message
906955
this.actualProjectPath = path.join('windows', projectName, projectName);

0 commit comments

Comments
 (0)