Skip to content

Commit dd13b19

Browse files
Copilotanupriya13
andcommitted
Fix codegen directory detection and module name display in setup-module-windows
Co-authored-by: anupriya13 <54227869+anupriya13@users.noreply.github.com>
1 parent c1469af commit dd13b19

2 files changed

Lines changed: 110 additions & 59 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,26 @@ export class SetupModuleWindows {
113113
spinner.succeed();
114114

115115
// Print success message with helpful information
116-
const moduleName = await getFinalModuleName(this.root, this.actualModuleName);
117116
const paths = this.getActualProjectPaths();
117+
118+
// Get the actual module name from the project path
119+
const moduleName = await getFinalModuleName(this.root, this.actualModuleName);
120+
let displayModuleName = moduleName;
121+
if (this.actualProjectPath) {
122+
// Extract the module name from the actual project path
123+
const pathParts = this.actualProjectPath.split(/[/\\]/);
124+
displayModuleName = pathParts[pathParts.length - 1]; // Get the last part (file name without extension)
125+
}
118126

119127
console.log(
120128
`\n${chalk.green('Success!')} Windows module setup completed.\n`,
121129
);
122130

123-
console.log(`${chalk.bold('Module Name:')} ${moduleName}`);
131+
console.log(`${chalk.bold('Module Name:')} ${displayModuleName}`);
124132
console.log(`${chalk.bold('Generated Files:')}`);
125133
console.log(` • ${paths.headerPath}`);
126134
console.log(` • ${paths.cppPath}`);
127-
console.log(` • codegen/Native${moduleName}Spec.g.h`);
135+
console.log(` • codegen/Native${displayModuleName}Spec.g.h`);
128136

129137
console.log(`\n${chalk.bold('Next Steps:')}`);
130138
console.log('1. Implement your module methods in the generated C++ files');

packages/@react-native-windows/cli/src/commands/setupModuleWindows/utils/templateGenerationUtils.ts

Lines changed: 99 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,87 @@ export async function generateStubFiles(
2323
console.log('[SetupModuleWindows] Generating C++ stub files...');
2424
}
2525

26-
// Look for codegen directory
26+
// First, find the actual Windows project directory that was created by init-windows
27+
const windowsDir = path.join(root, 'windows');
28+
let actualProjectName = '';
29+
30+
try {
31+
const windowsDirContents = await fs.readdir(windowsDir);
32+
const projectDirs = [];
33+
34+
for (const item of windowsDirContents) {
35+
const itemPath = path.join(windowsDir, item);
36+
const stats = await fs.stat(itemPath);
37+
38+
if (
39+
stats.isDirectory() &&
40+
!item.startsWith('.') &&
41+
item !== 'ExperimentalFeatures.props' &&
42+
!item.endsWith('.sln')
43+
) {
44+
// Check if this directory contains typical project files
45+
const possibleHeaderFile = path.join(itemPath, `${item}.h`);
46+
const possibleCppFile = path.join(itemPath, `${item}.cpp`);
47+
if (
48+
(await fs.exists(possibleHeaderFile)) ||
49+
(await fs.exists(possibleCppFile))
50+
) {
51+
projectDirs.push(item);
52+
}
53+
}
54+
}
55+
56+
if (projectDirs.length > 0) {
57+
actualProjectName = projectDirs[0];
58+
if (logging) {
59+
console.log(
60+
`[SetupModuleWindows] Found Windows project directory: ${actualProjectName}`,
61+
);
62+
}
63+
}
64+
} catch (error) {
65+
if (logging) {
66+
console.log(
67+
`[SetupModuleWindows] Error searching for Windows project directory: ${error}`,
68+
);
69+
}
70+
}
71+
72+
// Look for codegen directory - try multiple possible locations
2773
let codegenDir = path.join(root, 'codegen');
28-
if (!(await fs.exists(codegenDir))) {
74+
let foundCodegen = false;
75+
76+
// Check root level first
77+
if (await fs.exists(codegenDir)) {
78+
foundCodegen = true;
79+
if (logging) {
80+
console.log(`[SetupModuleWindows] Found codegen directory at: ${codegenDir}`);
81+
}
82+
}
83+
// Check inside Windows project directory
84+
else if (actualProjectName) {
85+
const projectCodegenDir = path.join(windowsDir, actualProjectName, 'codegen');
86+
if (await fs.exists(projectCodegenDir)) {
87+
codegenDir = projectCodegenDir;
88+
foundCodegen = true;
89+
if (logging) {
90+
console.log(`[SetupModuleWindows] Found codegen directory at: ${codegenDir}`);
91+
}
92+
}
93+
// Also check with different casing (Codegen vs codegen)
94+
else {
95+
const projectCodegenDirAlt = path.join(windowsDir, actualProjectName, 'Codegen');
96+
if (await fs.exists(projectCodegenDirAlt)) {
97+
codegenDir = projectCodegenDirAlt;
98+
foundCodegen = true;
99+
if (logging) {
100+
console.log(`[SetupModuleWindows] Found codegen directory at: ${codegenDir}`);
101+
}
102+
}
103+
}
104+
}
105+
106+
if (!foundCodegen) {
29107
if (logging) {
30108
console.log(
31109
'[SetupModuleWindows] No codegen directory found, skipping stub generation',
@@ -50,67 +128,32 @@ export async function generateStubFiles(
50128
);
51129
}
52130

53-
const windowsDir = path.join(root, 'windows');
54-
const moduleName = await getFinalModuleName(root, actualModuleName);
55-
let moduleDir = path.join(windowsDir, moduleName);
131+
// Use the actual project name we found, or fall back to calculated module name
132+
let projectName = actualProjectName;
56133
let actualProjectPath: string | undefined;
134+
let moduleDir: string;
57135

58-
// If the expected directory doesn't exist, find any existing project directory
59-
if (!(await fs.exists(moduleDir))) {
60-
try {
61-
const windowsDirContents = await fs.readdir(windowsDir);
62-
const projectDirs = [];
63-
64-
for (const item of windowsDirContents) {
65-
const itemPath = path.join(windowsDir, item);
66-
const stats = await fs.stat(itemPath);
67-
68-
if (
69-
stats.isDirectory() &&
70-
!item.startsWith('.') &&
71-
item !== 'ExperimentalFeatures.props' &&
72-
!item.endsWith('.sln')
73-
) {
74-
// Check if this directory contains typical project files
75-
const possibleHeaderFile = path.join(itemPath, `${item}.h`);
76-
const possibleCppFile = path.join(itemPath, `${item}.cpp`);
77-
if (
78-
(await fs.exists(possibleHeaderFile)) ||
79-
(await fs.exists(possibleCppFile))
80-
) {
81-
projectDirs.push(item);
82-
}
83-
}
84-
}
85-
86-
if (projectDirs.length > 0) {
87-
const projectName = projectDirs[0];
88-
moduleDir = path.join(windowsDir, projectName);
89-
actualProjectPath = path.join('windows', projectName, projectName);
90-
if (logging) {
91-
console.log(
92-
`[SetupModuleWindows] Found existing Windows project directory: ${moduleDir}`,
93-
);
94-
}
95-
} else {
96-
await fs.mkdir(moduleDir, {recursive: true});
97-
actualProjectPath = path.join('windows', moduleName, moduleName);
98-
}
99-
} catch (error) {
100-
if (logging) {
101-
console.log(
102-
`[SetupModuleWindows] Error searching for Windows project directory: ${error}`,
103-
);
104-
}
105-
await fs.mkdir(moduleDir, {recursive: true});
106-
actualProjectPath = path.join('windows', moduleName, moduleName);
136+
if (projectName) {
137+
// Use the actual project directory that was found
138+
moduleDir = path.join(windowsDir, projectName);
139+
actualProjectPath = path.join('windows', projectName, projectName);
140+
if (logging) {
141+
console.log(
142+
`[SetupModuleWindows] Using existing Windows project directory: ${moduleDir}`,
143+
);
107144
}
108145
} else {
146+
// Fall back to calculated module name if no project directory was found
147+
const moduleName = await getFinalModuleName(root, actualModuleName);
148+
projectName = moduleName;
149+
moduleDir = path.join(windowsDir, moduleName);
109150
actualProjectPath = path.join('windows', moduleName, moduleName);
151+
152+
if (!(await fs.exists(moduleDir))) {
153+
await fs.mkdir(moduleDir, {recursive: true});
154+
}
110155
}
111156

112-
const projectName = path.basename(moduleDir);
113-
114157
// Parse methods from codegen files
115158
const methods = await parseMethodsFromCodegen(codegenDir, specFiles, logging);
116159

0 commit comments

Comments
 (0)