-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bootstrap experimental types build for main package (#48661)
Summary: Changelog: [Internal] Reviewed By: iwoplaza Differential Revision: D68102875
- Loading branch information
1 parent
e39776b
commit 17d52b1
Showing
3 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
const {PACKAGES_DIR} = require('../consts'); | ||
const translate = require('flow-api-translator'); | ||
const {promises: fs} = require('fs'); | ||
const glob = require('glob'); | ||
const path = require('path'); | ||
|
||
const TYPES_DIR = 'new-types'; | ||
const PACKAGE_NAME = 'react-native'; | ||
|
||
// Start with Animated only as it using the export syntax. | ||
const PATHS = ['Libraries/Animated']; | ||
|
||
async function buildRNTypes() { | ||
const files = PATHS.flatMap(src_path => | ||
glob.sync(path.resolve(PACKAGES_DIR, PACKAGE_NAME, src_path, '**/*.js'), { | ||
nodir: true, | ||
}), | ||
); | ||
|
||
console.log('Building RN types...'); | ||
for (const file of files) { | ||
// Don't build tests | ||
if (/\/__tests__\//.test(file)) { | ||
continue; | ||
} | ||
|
||
const buildPath = getBuildPath(file); | ||
const source = await fs.readFile(file, 'utf-8'); | ||
const prettierConfig = {parser: 'babel'}; | ||
|
||
await fs.mkdir(path.dirname(buildPath), {recursive: true}); | ||
|
||
try { | ||
const typescriptDef = await translate.translateFlowToTSDef( | ||
source, | ||
prettierConfig, | ||
); | ||
|
||
if ( | ||
/Unsupported feature: Translating ".*" is currently not supported/.test( | ||
typescriptDef, | ||
) | ||
) { | ||
throw new Error('Syntax unsupported by flow-api-translator used in ' + file); | ||
} | ||
|
||
await fs.writeFile(buildPath, typescriptDef); | ||
} catch (e) { | ||
console.error(`Failed to build ${file.replace(PACKAGES_DIR, '')}`); | ||
} | ||
} | ||
} | ||
|
||
function getBuildPath(file /*: string */) /*: string */ { | ||
const packageDir = path.join(PACKAGES_DIR, PACKAGE_NAME); | ||
|
||
return path.join( | ||
packageDir, | ||
file | ||
.replace(packageDir, TYPES_DIR) | ||
.replace(/\.flow\.js$/, '.js') | ||
.replace(/\.js$/, '.d.ts'), | ||
); | ||
} | ||
|
||
module.exports = buildRNTypes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters