forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-native-modules-specs-cli.js
More file actions
78 lines (67 loc) · 1.84 KB
/
generate-native-modules-specs-cli.js
File metadata and controls
78 lines (67 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* 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();