mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
77e0ba2fb0
Summary: * Allow generate-native-modules-specs-cli.js to generate Android specific files * Add stub impl for Java spec generation (the output are still incomplete, see the next diffs for missing impl + tests) * Adjust the CLI to only produce modules stuffs, there's no need to produce components code Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D22860936 fbshipit-source-id: 38aae390189f143f6c6216995437ac1ff15a1788
88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its 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 RNCodegen = require('../packages/react-native-codegen/lib/generators/RNCodegen.js');
|
|
const fs = require('fs');
|
|
const mkdirp = require('mkdirp');
|
|
const os = require('os');
|
|
const path = require('path');
|
|
|
|
function generateSpec(platform, schemaPath, outputDirectory) {
|
|
const libraryName = 'FBReactNativeSpec';
|
|
const moduleSpecName = 'FBReactNativeSpec';
|
|
const schemaText = fs.readFileSync(schemaPath, 'utf-8');
|
|
|
|
if (schemaText == null) {
|
|
throw new Error(`Can't find schema at ${schemaPath}`);
|
|
}
|
|
|
|
const tempOutputDirectory = fs.mkdtempSync(
|
|
path.join(os.tmpdir(), 'react-native-codegen-'),
|
|
);
|
|
|
|
let schema;
|
|
try {
|
|
schema = JSON.parse(schemaText);
|
|
} catch (err) {
|
|
throw new Error(`Can't parse schema to JSON. ${schemaPath}`);
|
|
}
|
|
|
|
RNCodegen.generate(
|
|
{libraryName, schema, outputDirectory: tempOutputDirectory, moduleSpecName},
|
|
{
|
|
generators: ['modules'],
|
|
},
|
|
);
|
|
|
|
if (!outputDirectory) {
|
|
outputDirectory = path.resolve(
|
|
__dirname,
|
|
'..',
|
|
'Libraries',
|
|
libraryName,
|
|
moduleSpecName,
|
|
);
|
|
}
|
|
mkdirp.sync(outputDirectory);
|
|
|
|
if (platform === 'ios') {
|
|
const fileNames = [`${moduleSpecName}.h`, `${moduleSpecName}-generated.mm`];
|
|
fileNames.forEach(fileName => {
|
|
const newOutput = `${tempOutputDirectory}/${fileName}`;
|
|
const prevOutput = `${outputDirectory}/${fileName}`;
|
|
fs.copyFileSync(newOutput, prevOutput);
|
|
});
|
|
} else if (platform === 'android') {
|
|
// Copy all .java files for now.
|
|
// TODO: Build sufficient support for producing Java package directories based
|
|
// on preferred package name.
|
|
const files = fs.readdirSync(tempOutputDirectory);
|
|
files
|
|
.filter(f => f.endsWith('.java'))
|
|
.forEach(f => {
|
|
fs.copyFileSync(
|
|
`${tempOutputDirectory}/${f}`,
|
|
`${outputDirectory}/${f}`,
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|
|
function main() {
|
|
const args = process.argv.slice(2);
|
|
const platform = args[0];
|
|
const schemaPath = args[1];
|
|
const outputDir = args[2];
|
|
generateSpec(platform, schemaPath, outputDir);
|
|
}
|
|
|
|
main();
|