forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-api-docs.js
90 lines (75 loc) · 2.25 KB
/
generate-api-docs.js
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
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node
/**
* 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 SignedSource = require('signedsource');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const reactDocs = require('react-docgen');
const GENERATE_ANNOTATION = '@' + 'generate-docs';
const RN_ROOT = path.join(__dirname, '..');
const OUTPUT_PATH = path.join(RN_ROOT, 'docs', 'generatedComponentApiDocs.js');
const TEMPLATE = `/**
* 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.
*
* This file is used by the React Native website to show the props of core components
* This file was generated by running scripts/generate-api-docs.js
*
* ::_SIGNING_TOKEN_::
*/
'use strict';
module.exports = ::_CONTENT_::
`;
const allComponentFiles = glob.sync(
path.join(RN_ROOT, '/Libraries/Components/**/*.js'),
{
nodir: true,
},
);
const docs = allComponentFiles.reduce((acc, file) => {
const contents = fs.readFileSync(file, {encoding: 'utf-8'});
if (!contents.includes(GENERATE_ANNOTATION)) {
return acc;
}
const result = reactDocs.parse(
contents,
reactDocs.resolver.findExportedComponentDefinition,
);
acc.push(cleanComponentResult(result));
return acc;
}, []);
const content = TEMPLATE.replace(
'::_CONTENT_::',
JSON.stringify(docs, null, 2),
).replace('::_SIGNING_TOKEN_::', SignedSource.getSigningToken());
const signedContent = SignedSource.signFile(content);
if (process.env.NODE_ENV === 'test') {
const existingContent = fs.readFileSync(OUTPUT_PATH, 'utf8');
if (signedContent !== existingContent) {
console.error(
path.relative(RN_ROOT, OUTPUT_PATH),
'is not up to date. Run',
'scripts/generate-api-docs.js',
'to regenerate the file.',
);
process.exit(1);
}
} else {
fs.writeFileSync(OUTPUT_PATH, SignedSource.signFile(content));
}
function cleanComponentResult(component) {
return {
...component,
methods: component.methods.filter(method => !method.name.startsWith('_')),
};
}