|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Script to create symlinks for header files in React/includes/React |
| 13 | + * |
| 14 | + * This script: |
| 15 | + * 1. Scans the React and Libraries directories to build a map of header files |
| 16 | + * 2. Iterates over an array of headers |
| 17 | + * 3. Looks up each header in the map and creates a symlink in React/includes/React |
| 18 | + */ |
| 19 | + |
| 20 | +// Import the headers array from headers.js |
| 21 | +const {HEADERS} = require('./headers'); |
| 22 | +const fs = require('fs'); |
| 23 | +const path = require('path'); |
| 24 | + |
| 25 | +// Define paths |
| 26 | +const ROOT_DIR = path.resolve(__dirname, '../../../..'); |
| 27 | +const REACT_DIR = path.join(ROOT_DIR, 'packages/react-native/React'); |
| 28 | +const LIBRARIES_DIR = path.join(ROOT_DIR, 'packages/react-native/Libraries'); |
| 29 | +const DESTINATION_DIR = path.join( |
| 30 | + ROOT_DIR, |
| 31 | + 'packages/react-native/React/includes/React', |
| 32 | +); |
| 33 | + |
| 34 | +// Ensure destination directory exists |
| 35 | +if (!fs.existsSync(DESTINATION_DIR)) { |
| 36 | + console.log(`Creating directory: ${DESTINATION_DIR}`); |
| 37 | + fs.mkdirSync(DESTINATION_DIR, {recursive: true}); |
| 38 | +} |
| 39 | + |
| 40 | +console.log('Building header file map...'); |
| 41 | + |
| 42 | +// Function to recursively scan directories and build a map of header files |
| 43 | +function buildHeaderMap(directory) { |
| 44 | + const headerMap = new Map(); |
| 45 | + |
| 46 | + function scanDirectory(dir) { |
| 47 | + const entries = fs.readdirSync(dir, {withFileTypes: true}); |
| 48 | + |
| 49 | + for (const entry of entries) { |
| 50 | + const fullPath = path.join(dir, entry.name); |
| 51 | + |
| 52 | + if (entry.isDirectory()) { |
| 53 | + scanDirectory(fullPath); |
| 54 | + } else if (entry.isFile() && entry.name.endsWith('.h')) { |
| 55 | + // Store by filename only, without any subpath |
| 56 | + headerMap.set(entry.name, fullPath); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + scanDirectory(directory); |
| 62 | + return headerMap; |
| 63 | +} |
| 64 | + |
| 65 | +// Build a map of all header files in React and Libraries directories |
| 66 | +const reactHeaderMap = buildHeaderMap(REACT_DIR); |
| 67 | +const librariesHeaderMap = buildHeaderMap(LIBRARIES_DIR); |
| 68 | + |
| 69 | +// Merge the two maps, with React headers taking precedence |
| 70 | +const headerMap = new Map([...librariesHeaderMap, ...reactHeaderMap]); |
| 71 | + |
| 72 | +console.log(`Found ${headerMap.size} unique header files`); |
| 73 | + |
| 74 | +// Counter for statistics |
| 75 | +let found = 0; |
| 76 | +let notFound = 0; |
| 77 | +let errors = 0; |
| 78 | + |
| 79 | +// Arrays to collect headers that couldn't be found or had errors |
| 80 | +const notFoundHeaders = []; |
| 81 | +const errorHeaders = []; |
| 82 | + |
| 83 | +// Process each header |
| 84 | +HEADERS.forEach(header => { |
| 85 | + try { |
| 86 | + // Extract just the filename for both search and target |
| 87 | + let targetFilename = header; |
| 88 | + |
| 89 | + // Handle headers with path components |
| 90 | + if (header.includes('/')) { |
| 91 | + const parts = header.split('/'); |
| 92 | + targetFilename = parts[parts.length - 1]; |
| 93 | + } |
| 94 | + |
| 95 | + // Look up the header in our map using just the filename |
| 96 | + const sourcePath = headerMap.get(targetFilename); |
| 97 | + |
| 98 | + if (sourcePath) { |
| 99 | + const destPath = path.join(DESTINATION_DIR, targetFilename); |
| 100 | + |
| 101 | + // Create symlink |
| 102 | + if (fs.existsSync(destPath)) { |
| 103 | + fs.unlinkSync(destPath); |
| 104 | + } |
| 105 | + |
| 106 | + // Create relative symlink |
| 107 | + const relativeSourcePath = path.relative(DESTINATION_DIR, sourcePath); |
| 108 | + fs.symlinkSync(relativeSourcePath, destPath); |
| 109 | + |
| 110 | + console.log( |
| 111 | + `Created symlink: ${targetFilename} -> ${relativeSourcePath}`, |
| 112 | + ); |
| 113 | + found++; |
| 114 | + } else { |
| 115 | + console.warn( |
| 116 | + `Warning: Could not find header file: ${header} (filename: ${targetFilename})`, |
| 117 | + ); |
| 118 | + notFoundHeaders.push({header, targetFilename}); |
| 119 | + notFound++; |
| 120 | + } |
| 121 | + } catch (error) { |
| 122 | + console.error(`Error processing ${header}: ${error.message}`); |
| 123 | + errorHeaders.push({header, error: error.message}); |
| 124 | + errors++; |
| 125 | + } |
| 126 | +}); |
| 127 | + |
| 128 | +console.log('\nSummary:'); |
| 129 | +console.log(`- Found and linked: ${found} files`); |
| 130 | +console.log(`- Not found: ${notFound} files`); |
| 131 | +console.log(`- Errors: ${errors} files`); |
| 132 | + |
| 133 | +if (notFound > 0) { |
| 134 | + console.log('\nHeaders that could not be found:'); |
| 135 | + notFoundHeaders.forEach(({header, targetFilename}) => { |
| 136 | + console.log(` - ${header} (filename: ${targetFilename})`); |
| 137 | + }); |
| 138 | +} |
| 139 | + |
| 140 | +if (errors > 0) { |
| 141 | + console.log('\nHeaders that had errors:'); |
| 142 | + errorHeaders.forEach(({header, error}) => { |
| 143 | + console.log(` - ${header}: ${error}`); |
| 144 | + }); |
| 145 | +} |
| 146 | + |
| 147 | +if (notFound > 0 || errors > 0) { |
| 148 | + console.log('\nSome headers could not be found or had errors.'); |
| 149 | + process.exit(1); |
| 150 | +} else { |
| 151 | + console.log('\nAll headers were successfully linked.'); |
| 152 | +} |
0 commit comments