Files
react-native/scripts/generate-native-modules-specs-cli.js
T
Héctor Ramos 2e756e024f Remove flow-node requirement from native modules codegen script
Summary:
Use transpiled `react-native-codegen` scripts. By avoiding use of flow-node, we no longer need to upgrade flow-remove-types in order to run the native modules codegen script.

The interface for the script remains the same:

```
./scripts/generate-native-modules-specs.sh [optionalOutputDir]
```

Changelog: [Internal]

Reviewed By: TheSavior

Differential Revision: D21629705

fbshipit-source-id: 7714a67e36c8151a3b0b49285b6a6c93a525d7bc
2020-05-20 18:48:49 -07:00

79 lines
1.8 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(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: [
'descriptors',
'events',
'props',
'tests',
'shadow-nodes',
'modules',
],
},
);
if (!outputDirectory) {
outputDirectory = path.resolve(
__dirname,
'..',
'Libraries',
libraryName,
moduleSpecName,
);
}
mkdirp.sync(outputDirectory);
const fileNames = [`${moduleSpecName}.h`, `${moduleSpecName}-generated.mm`];
fileNames.forEach(fileName => {
const newOutput = `${tempOutputDirectory}/${fileName}`;
const prevOutput = `${outputDirectory}/${fileName}`;
fs.copyFileSync(newOutput, prevOutput);
});
}
function main() {
const args = process.argv.slice(2);
const schemaPath = args[0];
const outputDir = args[1];
generateSpec(schemaPath, outputDir);
}
main();