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 * @format 8 */ 9 10'use strict'; 11 12let RNCodegen; 13try { 14 RNCodegen = require('../packages/react-native-codegen/lib/generators/RNCodegen.js'); 15} catch (e) { 16 RNCodegen = require('react-native-codegen/lib/generators/RNCodegen.js'); 17 if (!RNCodegen) { 18 throw 'RNCodegen not found.'; 19 } 20} 21 22const fs = require('fs'); 23const mkdirp = require('mkdirp'); 24const yargs = require('yargs'); 25 26const argv = yargs 27 .option('p', { 28 alias: 'platform', 29 describe: 'Platform to generate native code artifacts for.', 30 }) 31 .option('s', { 32 alias: 'schemaListPath', 33 describe: 'The path to the schema list file.', 34 }) 35 .option('o', { 36 alias: 'outputDir', 37 describe: 38 'Path to directory where native code source files should be saved.', 39 }) 40 .usage('Usage: $0 <args>') 41 .demandOption( 42 ['platform', 'schemaListPath', 'outputDir'], 43 'Please provide platform, schema path, and output directory.', 44 ).argv; 45 46const GENERATORS = { 47 android: [], 48 ios: ['providerIOS'], 49}; 50 51function generateProvider(platform, schemaListPath, outputDirectory) { 52 const schemaListText = fs.readFileSync(schemaListPath, 'utf-8'); 53 54 if (schemaListText == null) { 55 throw new Error(`Can't find schema list file at ${schemaListPath}`); 56 } 57 58 if (!outputDirectory) { 59 throw new Error('outputDir is required'); 60 } 61 mkdirp.sync(outputDirectory); 62 63 let schemaPaths; 64 try { 65 schemaPaths = JSON.parse(schemaListText); 66 } catch (err) { 67 throw new Error(`Can't parse schema to JSON. ${schemaListPath}`); 68 } 69 70 const schemas = {}; 71 try { 72 for (const libraryName of Object.keys(schemaPaths)) { 73 const tmpSchemaText = fs.readFileSync(schemaPaths[libraryName], 'utf-8'); 74 schemas[libraryName] = JSON.parse(tmpSchemaText); 75 } 76 } catch (err) { 77 throw new Error(`Failed to read schema file. ${err.message}`); 78 } 79 80 if (GENERATORS[platform] == null) { 81 throw new Error(`Invalid platform type. ${platform}`); 82 } 83 84 RNCodegen.generateFromSchemas( 85 { 86 schemas, 87 outputDirectory, 88 }, 89 { 90 generators: GENERATORS[platform], 91 }, 92 ); 93} 94 95function main() { 96 generateProvider(argv.platform, argv.schemaListPath, argv.outputDir); 97} 98 99main(); 100