Skip to content

Commit b976648

Browse files
Copilotanupriya13
andcommitted
Fix spec file discovery to exclude node_modules and validate TurboModule files
Co-authored-by: anupriya13 <54227869+anupriya13@users.noreply.github.com>
1 parent c338a07 commit b976648

1 file changed

Lines changed: 56 additions & 5 deletions

File tree

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

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,66 @@ export class ModuleWindowsSetup {
111111
private async checkAndCreateSpecFile(): Promise<void> {
112112
this.verboseMessage('Checking for TurboModule spec file...');
113113

114-
const specPattern = '**/Native*.[jt]s';
115-
const specFiles = glob.sync(specPattern, {cwd: this.root});
114+
// Look for spec files in common locations, excluding node_modules
115+
const specPatterns = [
116+
'Native*.[jt]s',
117+
'src/**/Native*.[jt]s',
118+
'lib/**/Native*.[jt]s',
119+
'js/**/Native*.[jt]s',
120+
'ts/**/Native*.[jt]s'
121+
];
122+
123+
const specFiles: string[] = [];
124+
for (const pattern of specPatterns) {
125+
const matches = glob.sync(pattern, {
126+
cwd: this.root,
127+
ignore: ['**/node_modules/**', '**/build/**', '**/dist/**']
128+
});
129+
specFiles.push(...matches);
130+
}
116131

117-
if (specFiles.length === 0) {
118-
this.verboseMessage('No spec file found, analyzing existing APIs...');
132+
// Remove duplicates and filter for actual TurboModule specs
133+
const uniqueSpecFiles = Array.from(new Set(specFiles));
134+
const validSpecFiles = await this.filterValidSpecFiles(uniqueSpecFiles);
135+
136+
if (validSpecFiles.length === 0) {
137+
this.verboseMessage('No valid TurboModule spec file found, analyzing existing APIs...');
119138
await this.analyzeAndCreateSpecFile();
120139
} else {
121-
this.verboseMessage(`Found spec file(s): ${specFiles.join(', ')}`);
140+
this.verboseMessage(`Found valid spec file(s): ${validSpecFiles.join(', ')}`);
141+
}
142+
}
143+
144+
private async filterValidSpecFiles(specFiles: string[]): Promise<string[]> {
145+
const validFiles: string[] = [];
146+
147+
for (const file of specFiles) {
148+
try {
149+
const filePath = path.join(this.root, file);
150+
if (await fs.exists(filePath)) {
151+
const content = await fs.readFile(filePath, 'utf8');
152+
153+
// Check if it's a valid TurboModule spec file
154+
if (this.isValidTurboModuleSpec(content)) {
155+
validFiles.push(file);
156+
}
157+
}
158+
} catch (error) {
159+
this.verboseMessage(`Could not read spec file ${file}: ${error}`);
160+
}
122161
}
162+
163+
return validFiles;
164+
}
165+
166+
private isValidTurboModuleSpec(content: string): boolean {
167+
// Check for TurboModule indicators
168+
return (
169+
content.includes('TurboModule') &&
170+
(content.includes('export interface Spec') ||
171+
content.includes('extends TurboModule') ||
172+
content.includes('TurboModuleRegistry'))
173+
);
123174
}
124175

125176
private async analyzeAndCreateSpecFile(): Promise<void> {

0 commit comments

Comments
 (0)