|
| 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 |
| 8 | + * @format |
| 9 | + * @oncall react_native |
| 10 | + */ |
| 11 | + |
| 12 | +import type {PluginObj} from '@babel/core'; |
| 13 | + |
| 14 | +const {PACKAGES_DIR, REACT_NATIVE_PACKAGE_DIR} = require('../consts'); |
| 15 | +const {API_EXTRACTOR_CONFIG_FILE, TYPES_OUTPUT_DIR} = require('./config'); |
| 16 | +const apiSnapshotTemplate = require('./templates/ReactNativeApi.d.ts-template.js'); |
| 17 | +const resolveCyclicImportsInDefinition = require('./transforms/resolveCyclicImportsInDefinition'); |
| 18 | +const babel = require('@babel/core'); |
| 19 | +const { |
| 20 | + Extractor, |
| 21 | + ExtractorConfig, |
| 22 | + // $FlowFixMe[cannot-resolve-module] |
| 23 | +} = require('@microsoft/api-extractor'); |
| 24 | +const {promises: fs} = require('fs'); |
| 25 | +const glob = require('glob'); |
| 26 | +const path = require('path'); |
| 27 | +const prettier = require('prettier'); |
| 28 | +const osTempDir = require('temp-dir'); |
| 29 | + |
| 30 | +const postTransforms: $ReadOnlyArray<PluginObj<mixed>> = [ |
| 31 | + require('./transforms/fixReactImportName'), |
| 32 | + require('./transforms/sortTypeDefinitions'), |
| 33 | + require('./transforms/sortProperties'), |
| 34 | + require('./transforms/sortUnions'), |
| 35 | +]; |
| 36 | + |
| 37 | +async function buildAPISnapshot() { |
| 38 | + const tempDirectory = await createTempDir('react-native-js-api-snapshot'); |
| 39 | + const packages = await findPackagesWithTypedef(); |
| 40 | + |
| 41 | + await preparePackagesInTempDir(tempDirectory, packages); |
| 42 | + await rewriteLocalImports(tempDirectory, packages); |
| 43 | + |
| 44 | + const extractorConfig = ExtractorConfig.loadFileAndPrepare( |
| 45 | + path.join(tempDirectory, API_EXTRACTOR_CONFIG_FILE), |
| 46 | + ); |
| 47 | + |
| 48 | + const extractorResult = Extractor.invoke(extractorConfig, { |
| 49 | + localBuild: true, |
| 50 | + showVerboseMessages: true, |
| 51 | + }); |
| 52 | + |
| 53 | + if (extractorResult.succeeded) { |
| 54 | + const apiSnapshot = apiSnapshotTemplate( |
| 55 | + await getCleanedUpRollup(tempDirectory), |
| 56 | + ); |
| 57 | + |
| 58 | + await fs.writeFile( |
| 59 | + path.join(REACT_NATIVE_PACKAGE_DIR, 'ReactNativeApi.d.ts'), |
| 60 | + apiSnapshot, |
| 61 | + ); |
| 62 | + } else { |
| 63 | + process.exitCode = 1; |
| 64 | + console.error( |
| 65 | + `API Extractor failed with ${extractorResult.errorCount} errors` + |
| 66 | + ` and ${extractorResult.warningCount} warnings`, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + await fs.rm(tempDirectory, {recursive: true}); |
| 71 | +} |
| 72 | + |
| 73 | +async function findPackagesWithTypedef() { |
| 74 | + const packagesWithGeneratedTypes = glob |
| 75 | + .sync(`${PACKAGES_DIR}/**/types_generated`, {nodir: false}) |
| 76 | + .map(typesPath => |
| 77 | + path.relative(PACKAGES_DIR, typesPath).split('/').slice(0, -1).join('/'), |
| 78 | + ); |
| 79 | + |
| 80 | + const packagesWithNames = await Promise.all( |
| 81 | + packagesWithGeneratedTypes.map(async pkg => { |
| 82 | + const packageJsonContent = await fs.readFile( |
| 83 | + path.join(PACKAGES_DIR, pkg, 'package.json'), |
| 84 | + 'utf-8', |
| 85 | + ); |
| 86 | + const packageJson = JSON.parse(packageJsonContent); |
| 87 | + |
| 88 | + return { |
| 89 | + directory: pkg, |
| 90 | + name: packageJson.name as string, |
| 91 | + }; |
| 92 | + }), |
| 93 | + ); |
| 94 | + |
| 95 | + return packagesWithNames; |
| 96 | +} |
| 97 | + |
| 98 | +async function preparePackagesInTempDir( |
| 99 | + tempDirectory: string, |
| 100 | + packages: $ReadOnlyArray<{directory: string, name: string}>, |
| 101 | +) { |
| 102 | + await generateConfigFiles(tempDirectory); |
| 103 | + |
| 104 | + await Promise.all( |
| 105 | + packages.map(async pkg => { |
| 106 | + await copyDirectory( |
| 107 | + path.join(PACKAGES_DIR, pkg.directory, TYPES_OUTPUT_DIR), |
| 108 | + path.join(tempDirectory, pkg.directory), |
| 109 | + ); |
| 110 | + }), |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Rewrite imports to local packages in the temp directory. We do this to |
| 116 | + * avoid cyclic references, which API Extractor cannot process. |
| 117 | + */ |
| 118 | +async function rewriteLocalImports( |
| 119 | + tempDirectory: string, |
| 120 | + packages: $ReadOnlyArray<{directory: string, name: string}>, |
| 121 | +) { |
| 122 | + const definitions = glob.sync(`${tempDirectory}/**/*.d.ts`); |
| 123 | + |
| 124 | + await Promise.all( |
| 125 | + definitions.map(async file => { |
| 126 | + const source = await fs.readFile(file, 'utf-8'); |
| 127 | + const fixedImports = await resolveCyclicImportsInDefinition({ |
| 128 | + packages: packages, |
| 129 | + rootPath: tempDirectory, |
| 130 | + sourcePath: file, |
| 131 | + source: source, |
| 132 | + }); |
| 133 | + await fs.writeFile(file, fixedImports); |
| 134 | + }), |
| 135 | + ); |
| 136 | +} |
| 137 | + |
| 138 | +async function getCleanedUpRollup(tempDirectory: string) { |
| 139 | + const rollupPath = path.join( |
| 140 | + tempDirectory, |
| 141 | + 'react-native', |
| 142 | + 'dist', |
| 143 | + 'api-rollup.d.ts', |
| 144 | + ); |
| 145 | + const sourceRollup = await fs.readFile(rollupPath, 'utf-8'); |
| 146 | + |
| 147 | + const cleanedRollup = sourceRollup |
| 148 | + .replace(/\/\*[\s\S]*?\*\//gm, '') // Remove block comments |
| 149 | + .replace(/\\\\.*$/gm, '') // Remove inline comments |
| 150 | + .replace(/^\s+$/gm, '') // Clear whitespace-only lines |
| 151 | + .replace(/\n+/gm, '\n'); // Collapse empty lines |
| 152 | + |
| 153 | + const transformedRollup = await applyPostTransforms(cleanedRollup); |
| 154 | + |
| 155 | + const formattedRollup = prettier.format(transformedRollup, { |
| 156 | + parser: 'typescript', |
| 157 | + }); |
| 158 | + |
| 159 | + return formattedRollup; |
| 160 | +} |
| 161 | + |
| 162 | +async function applyPostTransforms(inSrc: string): Promise<string> { |
| 163 | + const result = await babel.transformAsync(inSrc, { |
| 164 | + plugins: ['@babel/plugin-syntax-typescript', ...postTransforms], |
| 165 | + }); |
| 166 | + |
| 167 | + return result.code; |
| 168 | +} |
| 169 | + |
| 170 | +async function generateConfigFiles(tempDirectory: string) { |
| 171 | + // generate tsconfig |
| 172 | + const tsConfig = { |
| 173 | + $schema: 'http://json.schemastore.org/tsconfig', |
| 174 | + }; |
| 175 | + |
| 176 | + const outPath = path.join(tempDirectory, 'tsconfig.json'); |
| 177 | + |
| 178 | + await fs.mkdir(path.dirname(outPath), {recursive: true}); |
| 179 | + await fs.writeFile(outPath, JSON.stringify(tsConfig, null, 2)); |
| 180 | + |
| 181 | + // generate api extractor config |
| 182 | + const apiExtractorConfig = await fs.readFile( |
| 183 | + path.join(__dirname, 'templates', API_EXTRACTOR_CONFIG_FILE), |
| 184 | + 'utf-8', |
| 185 | + ); |
| 186 | + const adjustedApiExtractorConfig = apiExtractorConfig.replaceAll( |
| 187 | + '${typegen_directory}', |
| 188 | + tempDirectory, |
| 189 | + ); |
| 190 | + await fs.writeFile( |
| 191 | + path.join(tempDirectory, API_EXTRACTOR_CONFIG_FILE), |
| 192 | + adjustedApiExtractorConfig, |
| 193 | + ); |
| 194 | + |
| 195 | + // generate basic package.json |
| 196 | + const packageJSON = {name: 'react-native'}; |
| 197 | + await fs.writeFile( |
| 198 | + path.join(tempDirectory, 'package.json'), |
| 199 | + JSON.stringify(packageJSON, null, 2), |
| 200 | + ); |
| 201 | +} |
| 202 | + |
| 203 | +async function copyDirectory(src: string, dest: string) { |
| 204 | + await fs.mkdir(dest, {recursive: true}); |
| 205 | + |
| 206 | + const entries = await fs.readdir(src, {withFileTypes: true}); |
| 207 | + |
| 208 | + for (let entry of entries) { |
| 209 | + // name can only be a buffer when explicitly set as the encoding option |
| 210 | + const fileName: string = entry.name as any; |
| 211 | + const srcPath = path.join(src, fileName); |
| 212 | + const destPath = path.join(dest, fileName); |
| 213 | + |
| 214 | + if (entry.isDirectory()) { |
| 215 | + await copyDirectory(srcPath, destPath); |
| 216 | + } else { |
| 217 | + await fs.copyFile(srcPath, destPath); |
| 218 | + } |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +async function createTempDir(dirName: string): Promise<string> { |
| 223 | + // $FlowExpectedError[incompatible-call] temp-dir is typed as a default export |
| 224 | + const tempDir = path.join(osTempDir, dirName); |
| 225 | + |
| 226 | + await fs.mkdir(tempDir, {recursive: true}); |
| 227 | + |
| 228 | + return tempDir; |
| 229 | +} |
| 230 | + |
| 231 | +module.exports = buildAPISnapshot; |
0 commit comments