first commit

This commit is contained in:
2026-03-10 16:18:05 +00:00
commit 11f9c069b5
31635 changed files with 3187747 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
/**
* 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.
*
*
* @format
*/
'use strict';
const {
combineSchemasInFileListAndWriteToFile,
} = require('./combine-js-to-schema');
const yargs = require('yargs');
const argv = yargs
.usage('Usage: $0 <outfile> <file1> [<file2> ...]')
.option('p', {
describe:
'Platforms to generate schema for, this works on filenames: <filename>[.<platform>].(js|tsx?)',
alias: 'platform',
default: null,
})
.option('e', {
describe: 'Regular expression to exclude files from schema generation',
alias: 'exclude',
default: null,
})
.option('l', {
describe: 'Library name to use for schema generation',
alias: 'libraryName',
default: null,
})
.parseSync();
const [outfile, ...fileList] = argv._;
const platform = argv.platform;
const exclude = argv.exclude;
const excludeRegExp =
exclude != null && exclude !== '' ? new RegExp(exclude) : null;
const libraryName = argv.libraryName;
combineSchemasInFileListAndWriteToFile(
fileList,
platform != null ? platform.toLowerCase() : platform,
outfile,
excludeRegExp,
libraryName,
);

View File

@@ -0,0 +1,51 @@
/**
* 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 strict-local
* @format
*/
'use strict';
const {
combineSchemasInFileListAndWriteToFile,
} = require('./combine-js-to-schema');
const yargs = require('yargs');
const argv = yargs
.usage('Usage: $0 <outfile> <file1> [<file2> ...]')
.option('p', {
describe:
'Platforms to generate schema for, this works on filenames: <filename>[.<platform>].(js|tsx?)',
alias: 'platform',
default: null,
})
.option('e', {
describe: 'Regular expression to exclude files from schema generation',
alias: 'exclude',
default: null,
})
.option('l', {
describe: 'Library name to use for schema generation',
alias: 'libraryName',
default: null,
})
.parseSync();
const [outfile, ...fileList] = argv._;
const platform: ?string = argv.platform;
const exclude: string = argv.exclude;
const excludeRegExp: ?RegExp =
exclude != null && exclude !== '' ? new RegExp(exclude) : null;
const libraryName: ?string = argv.libraryName;
combineSchemasInFileListAndWriteToFile(
fileList,
platform != null ? platform.toLowerCase() : platform,
outfile,
excludeRegExp,
libraryName,
);

View File

@@ -0,0 +1,103 @@
/**
* 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.
*
*
* @format
*/
'use strict';
const {FlowParser} = require('../../parsers/flow/parser');
const {TypeScriptParser} = require('../../parsers/typescript/parser');
const {filterJSFile} = require('./combine-utils');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const flowParser = new FlowParser();
const typescriptParser = new TypeScriptParser();
function combineSchemas(files, libraryName) {
const combined = files.reduce(
(merged, filename) => {
const contents = fs.readFileSync(filename, 'utf8');
if (
contents &&
(/export\s+default\s+\(?codegenNativeComponent</.test(contents) ||
/extends TurboModule/.test(contents))
) {
const isTypeScript =
path.extname(filename) === '.ts' || path.extname(filename) === '.tsx';
const parser = isTypeScript ? typescriptParser : flowParser;
const schema = parser.parseFile(filename);
if (schema && schema.modules) {
merged.modules = {
...merged.modules,
...schema.modules,
};
}
}
return merged;
},
{
modules: {} /*:: as SchemaType['modules'] */,
},
);
return {
libraryName: libraryName || '',
modules: combined.modules,
};
}
function expandDirectoriesIntoFiles(fileList, platform, exclude) {
return fileList
.flatMap(file => {
if (!fs.lstatSync(file).isDirectory()) {
return [file];
}
const filePattern = path.sep === '\\' ? file.replace(/\\/g, '/') : file;
return glob.sync(`${filePattern}/**/*{,.fb}.{js,ts,tsx}`, {
nodir: true,
// TODO: This will remove the need of slash substitution above for Windows,
// but it requires glob@v9+; with the package currenlty relying on
// glob@7.1.1; and flow-typed repo not having definitions for glob@9+.
// windowsPathsNoEscape: true,
});
})
.filter(element => filterJSFile(element, platform, exclude));
}
function combineSchemasInFileList(fileList, platform, exclude, libraryName) {
const expandedFileList = expandDirectoriesIntoFiles(
fileList,
platform,
exclude,
);
const combined = combineSchemas(expandedFileList, libraryName);
if (Object.keys(combined.modules).length === 0) {
console.error(
'No modules to process in combine-js-to-schema-cli. If this is unexpected, please check if you set up your NativeComponent correctly. See combine-js-to-schema.js for how codegen finds modules.',
);
}
return combined;
}
function combineSchemasInFileListAndWriteToFile(
fileList,
platform,
outfile,
exclude,
libraryName,
) {
const combined = combineSchemasInFileList(
fileList,
platform,
exclude,
libraryName,
);
const formattedSchema = JSON.stringify(combined);
fs.writeFileSync(outfile, formattedSchema);
}
module.exports = {
combineSchemas,
combineSchemasInFileList,
combineSchemasInFileListAndWriteToFile,
};

View File

@@ -0,0 +1,122 @@
/**
* 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 strict-local
* @format
*/
'use strict';
import type {SchemaType} from '../../CodegenSchema.js';
const {FlowParser} = require('../../parsers/flow/parser');
const {TypeScriptParser} = require('../../parsers/typescript/parser');
const {filterJSFile} = require('./combine-utils');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const flowParser = new FlowParser();
const typescriptParser = new TypeScriptParser();
function combineSchemas(
files: Array<string>,
libraryName: ?string,
): SchemaType {
const combined = files.reduce(
(merged, filename) => {
const contents = fs.readFileSync(filename, 'utf8');
if (
contents &&
(/export\s+default\s+\(?codegenNativeComponent</.test(contents) ||
/extends TurboModule/.test(contents))
) {
const isTypeScript =
path.extname(filename) === '.ts' || path.extname(filename) === '.tsx';
const parser = isTypeScript ? typescriptParser : flowParser;
const schema = parser.parseFile(filename);
if (schema && schema.modules) {
merged.modules = {...merged.modules, ...schema.modules};
}
}
return merged;
},
{modules: {} /*:: as SchemaType['modules'] */},
);
return {
libraryName: libraryName || '',
modules: combined.modules,
};
}
function expandDirectoriesIntoFiles(
fileList: Array<string>,
platform: ?string,
exclude: ?RegExp,
): Array<string> {
return fileList
.flatMap(file => {
if (!fs.lstatSync(file).isDirectory()) {
return [file];
}
const filePattern = path.sep === '\\' ? file.replace(/\\/g, '/') : file;
return glob.sync(`${filePattern}/**/*{,.fb}.{js,ts,tsx}`, {
nodir: true,
// TODO: This will remove the need of slash substitution above for Windows,
// but it requires glob@v9+; with the package currenlty relying on
// glob@7.1.1; and flow-typed repo not having definitions for glob@9+.
// windowsPathsNoEscape: true,
});
})
.filter(element => filterJSFile(element, platform, exclude));
}
function combineSchemasInFileList(
fileList: Array<string>,
platform: ?string,
exclude: ?RegExp,
libraryName: ?string,
): SchemaType {
const expandedFileList = expandDirectoriesIntoFiles(
fileList,
platform,
exclude,
);
const combined = combineSchemas(expandedFileList, libraryName);
if (Object.keys(combined.modules).length === 0) {
console.error(
'No modules to process in combine-js-to-schema-cli. If this is unexpected, please check if you set up your NativeComponent correctly. See combine-js-to-schema.js for how codegen finds modules.',
);
}
return combined;
}
function combineSchemasInFileListAndWriteToFile(
fileList: Array<string>,
platform: ?string,
outfile: string,
exclude: ?RegExp,
libraryName: ?string,
): void {
const combined = combineSchemasInFileList(
fileList,
platform,
exclude,
libraryName,
);
const formattedSchema = JSON.stringify(combined);
fs.writeFileSync(outfile, formattedSchema);
}
module.exports = {
combineSchemas,
combineSchemasInFileList,
combineSchemasInFileListAndWriteToFile,
};

View File

@@ -0,0 +1,108 @@
/**
* 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.
*
*
* @format
*/
'use strict';
const assert = require('assert');
const fs = require('fs');
const yargs = require('yargs');
const argv = yargs
.option('p', {
alias: 'platform',
type: 'string',
demandOption: true,
})
.option('o', {
alias: 'output',
})
.option('s', {
alias: 'schema-query',
})
.parseSync();
const platform = argv.platform.toLowerCase();
const output = argv.output;
const schemaQuery = argv.s;
if (!['ios', 'android'].includes(platform)) {
throw new Error(`Invalid platform ${platform}`);
}
if (!schemaQuery.startsWith('@')) {
throw new Error(
"The argument provided to --schema-query must be a filename that starts with '@'.",
);
}
const schemaQueryOutputFile = schemaQuery.replace(/^@/, '');
const schemaQueryOutput = fs.readFileSync(schemaQueryOutputFile, 'utf8');
const modules = {};
const specNameToFile = {};
const schemaFiles =
schemaQueryOutput.length > 0 ? schemaQueryOutput.split(' ') : [];
for (const file of schemaFiles) {
const schema = JSON.parse(fs.readFileSync(file, 'utf8'));
if (schema.modules) {
for (const specName in schema.modules) {
var _module$excludedPlatf;
const module = schema.modules[specName];
if (modules[specName]) {
assert.deepEqual(
module,
modules[specName],
`App contained two specs with the same file name '${specName}'. Schemas: ${specNameToFile[specName]}, ${file}. Please rename one of the specs.`,
);
}
const excludedPlatforms =
(_module$excludedPlatf = module.excludedPlatforms) === null ||
_module$excludedPlatf === void 0
? void 0
: _module$excludedPlatf.map(excludedPlatform =>
excludedPlatform.toLowerCase(),
);
if (excludedPlatforms != null) {
const cxxOnlyModule =
excludedPlatforms.includes('ios') &&
excludedPlatforms.includes('android');
if (!cxxOnlyModule && excludedPlatforms.includes(platform)) {
continue;
}
}
if (module.type === 'Component') {
const components = module.components || {};
const isExcludedForPlatform = Object.values(components).some(
component => {
var _component$excludedPl;
return (_component$excludedPl = component.excludedPlatforms) ===
null || _component$excludedPl === void 0
? void 0
: _component$excludedPl
.map(p => p.toLowerCase())
.includes(platform);
},
);
if (isExcludedForPlatform) {
continue;
}
}
if (
module.type === 'Component' &&
schema.libraryName === 'FBReactNativeSpec'
) {
continue;
} else {
modules[specName] = module;
specNameToFile[specName] = file;
}
}
}
}
fs.writeFileSync(
output,
JSON.stringify({
modules,
}),
);

View File

@@ -0,0 +1,116 @@
/**
* 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 strict-local
* @format
*/
'use strict';
import type {
ComponentSchema,
NativeModuleSchema,
SchemaType,
} from '../../CodegenSchema.js';
const assert = require('assert');
const fs = require('fs');
const yargs = require('yargs');
const argv = yargs
.option('p', {
alias: 'platform',
type: 'string',
demandOption: true,
})
.option('o', {
alias: 'output',
})
.option('s', {
alias: 'schema-query',
})
.parseSync();
const platform: string = argv.platform.toLowerCase();
const output: string = argv.output;
const schemaQuery: string = argv.s;
if (!['ios', 'android'].includes(platform)) {
throw new Error(`Invalid platform ${platform}`);
}
if (!schemaQuery.startsWith('@')) {
throw new Error(
"The argument provided to --schema-query must be a filename that starts with '@'.",
);
}
const schemaQueryOutputFile = schemaQuery.replace(/^@/, '');
const schemaQueryOutput = fs.readFileSync(schemaQueryOutputFile, 'utf8');
const modules: {
[hasteModuleName: string]: NativeModuleSchema | ComponentSchema,
} = {};
const specNameToFile: {[hasteModuleName: string]: string} = {};
const schemaFiles =
schemaQueryOutput.length > 0 ? schemaQueryOutput.split(' ') : [];
for (const file of schemaFiles) {
const schema: SchemaType = JSON.parse(fs.readFileSync(file, 'utf8'));
if (schema.modules) {
for (const specName in schema.modules) {
const module = schema.modules[specName];
if (modules[specName]) {
assert.deepEqual(
module,
modules[specName],
`App contained two specs with the same file name '${specName}'. Schemas: ${specNameToFile[specName]}, ${file}. Please rename one of the specs.`,
);
}
const excludedPlatforms = module.excludedPlatforms?.map(
excludedPlatform => excludedPlatform.toLowerCase(),
);
if (excludedPlatforms != null) {
const cxxOnlyModule =
excludedPlatforms.includes('ios') &&
excludedPlatforms.includes('android');
if (!cxxOnlyModule && excludedPlatforms.includes(platform)) {
continue;
}
}
if (module.type === 'Component') {
const components = module.components || {};
const isExcludedForPlatform = Object.values(components).some(
component =>
component.excludedPlatforms
?.map(p => p.toLowerCase())
.includes(platform),
);
if (isExcludedForPlatform) {
continue;
}
}
if (
module.type === 'Component' &&
schema.libraryName === 'FBReactNativeSpec'
) {
continue;
} else {
modules[specName] = module;
specNameToFile[specName] = file;
}
}
}
}
fs.writeFileSync(output, JSON.stringify({modules}));

View File

@@ -0,0 +1,59 @@
/**
* 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.
*
*
* @format
*/
'use strict';
const path = require('path');
/**
* This function is used by the CLI to decide whether a JS/TS file has to be
* processed or not by the Codegen.
*
* Parameters:
* - originalFilePath: the path to the file
* - currentPlatform: the platform for which we are creating the specs
* Returns: `true` if the file can be used to generate code; `false` otherwise
*/
function filterJSFile(originalFilePath, currentPlatform, excludeRegExp) {
// Remove `.fb` if it exists (see `react-native.cconf`).
const filePath = originalFilePath.replace(/\.fb(\.|$)/, '$1');
const basename = path.basename(filePath);
const isSpecFile = /^(Native.+|.+NativeComponent)/.test(basename);
const isNotNativeUIManager = !filePath.endsWith('NativeUIManager.js');
const isNotTest = !filePath.includes('__tests');
const isNotExcluded = excludeRegExp == null || !excludeRegExp.test(filePath);
const isNotTSTypeDefinition = !filePath.endsWith('.d.ts');
const isValidCandidate =
isSpecFile &&
isNotNativeUIManager &&
isNotExcluded &&
isNotTest &&
isNotTSTypeDefinition;
const filenameComponents = basename.split('.');
const isPlatformAgnostic = filenameComponents.length === 2;
if (currentPlatform == null) {
// need to accept only files that are platform agnostic
return isValidCandidate && isPlatformAgnostic;
}
// If a platform is passed, accept both platform agnostic specs...
if (isPlatformAgnostic) {
return isValidCandidate;
}
// ...and specs that share the same platform as the one passed.
// specfiles must follow the pattern: <filename>[.<platform>].(js|ts|tsx)
const filePlatform =
filenameComponents.length > 2 ? filenameComponents[1] : 'unknown';
return isValidCandidate && currentPlatform === filePlatform;
}
module.exports = {
filterJSFile,
};

View File

@@ -0,0 +1,68 @@
/**
* 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 strict-local
* @format
*/
'use strict';
const path = require('path');
/**
* This function is used by the CLI to decide whether a JS/TS file has to be
* processed or not by the Codegen.
*
* Parameters:
* - originalFilePath: the path to the file
* - currentPlatform: the platform for which we are creating the specs
* Returns: `true` if the file can be used to generate code; `false` otherwise
*/
function filterJSFile(
originalFilePath: string,
currentPlatform: ?string,
excludeRegExp: ?RegExp,
): boolean {
// Remove `.fb` if it exists (see `react-native.cconf`).
const filePath = originalFilePath.replace(/\.fb(\.|$)/, '$1');
const basename = path.basename(filePath);
const isSpecFile = /^(Native.+|.+NativeComponent)/.test(basename);
const isNotNativeUIManager = !filePath.endsWith('NativeUIManager.js');
const isNotTest = !filePath.includes('__tests');
const isNotExcluded = excludeRegExp == null || !excludeRegExp.test(filePath);
const isNotTSTypeDefinition = !filePath.endsWith('.d.ts');
const isValidCandidate =
isSpecFile &&
isNotNativeUIManager &&
isNotExcluded &&
isNotTest &&
isNotTSTypeDefinition;
const filenameComponents = basename.split('.');
const isPlatformAgnostic = filenameComponents.length === 2;
if (currentPlatform == null) {
// need to accept only files that are platform agnostic
return isValidCandidate && isPlatformAgnostic;
}
// If a platform is passed, accept both platform agnostic specs...
if (isPlatformAgnostic) {
return isValidCandidate;
}
// ...and specs that share the same platform as the one passed.
// specfiles must follow the pattern: <filename>[.<platform>].(js|ts|tsx)
const filePlatform =
filenameComponents.length > 2 ? filenameComponents[1] : 'unknown';
return isValidCandidate && currentPlatform === filePlatform;
}
module.exports = {
filterJSFile,
};