@@ -4,13 +4,13 @@ Generate docs from ReScript Compiler
4
4
## Run
5
5
6
6
```bash
7
- node scripts/gendocs.mjs path/to/rescript-monorepo version forceReWrite
7
+ node scripts/gendocs.mjs path/to/rescript-compiler path/to/rescript-core/src/RescriptCore.res version forceReWrite
8
8
```
9
9
10
10
## Examples
11
11
12
12
```bash
13
- node scripts/gendocs.mjs path/to/rescript-monorepo latest true
13
+ node scripts/gendocs.mjs path/to/rescript-compiler latest true
14
14
```
15
15
*/
16
16
@val @scope (("import" , "meta" )) external url : string = "url"
@@ -25,16 +25,21 @@ let dirname =
25
25
-> Path .dirname
26
26
27
27
let compilerLibPath = switch args -> Array .get (0 ) {
28
- | Some (path ) => Path .join ([path , "runtime " ])
28
+ | Some (path ) => Path .join ([path , "jscomp" , "others " ])
29
29
| None => failwith ("First argument should be path to rescript-compiler repo" )
30
30
}
31
31
32
- let version = switch args -> Array .get (1 ) {
32
+ let corePath = switch args -> Array .get (1 ) {
33
+ | Some (path ) => path
34
+ | _ => failwith ("Second argument should be path to rescript-core/src/RescriptCore.res" )
35
+ }
36
+
37
+ let version = switch args -> Array .get (2 ) {
33
38
| Some (version ) => version
34
- | None => failwith ("Second argument should be a version, `latest`, `v10`" )
39
+ | None => failwith ("Third argument should be a version, `latest`, `v10`" )
35
40
}
36
41
37
- let forceReWrite = switch args -> Array .get (2 ) {
42
+ let forceReWrite = switch args -> Array .get (3 ) {
38
43
| Some ("true" ) => true
39
44
| _ => false
40
45
}
@@ -50,7 +55,7 @@ if Fs.existsSync(dirVersion) {
50
55
Fs .mkdirSync (dirVersion )
51
56
}
52
57
53
- let entryPointFiles = ["Belt.res " , "Dom .res" , "Js.res" , "Stdlib .res" ]
58
+ let entryPointFiles = ["js.ml " , "belt .res" , "dom .res" ]
54
59
55
60
let hiddenModules = ["Js.Internal" , "Js.MapperRt" ]
56
61
@@ -71,32 +76,33 @@ type section = {
71
76
72
77
let env = Process .env
73
78
74
- let docsDecoded = entryPointFiles -> Array .map (libFile =>
75
- try {
76
- let entryPointFile = Path .join2 (compilerLibPath , libFile )
79
+ let docsDecoded = entryPointFiles -> Array .map (libFile => {
80
+ let entryPointFile = Path .join2 (compilerLibPath , libFile )
77
81
78
- let output = ChildProcess .execSync (
82
+ Dict .set (env , "FROM_COMPILER" , "true" )
83
+
84
+ let output =
85
+ ChildProcess .execSync (
79
86
` ./node_modules/.bin/rescript-tools doc ${entryPointFile}` ,
80
- ~options = {
81
- maxBuffer : 30_000_000 .,
82
- },
83
87
)-> Buffer .toString
84
88
85
- output
86
- -> JSON .parseExn
87
- -> Docgen .decodeFromJson
88
- } catch {
89
- | Exn .Error (error ) =>
90
- Console .error (
91
- ` Error while generating docs from ${libFile}: ${error
92
- -> Error.message
93
- -> Option.getOr("[no message]" )}` ,
94
- )
95
- Error .raise (error )
96
- }
97
- )
89
+ output
90
+ -> JSON .parseExn
91
+ -> Docgen .decodeFromJson
92
+ })
93
+
94
+ let coreDocs = {
95
+ Dict .set (env , "FROM_COMPILER" , "false" )
98
96
99
- let removeStdlibOrPrimitive = s => s -> String .replaceAllRegExp (/ Stdlib_ |Primitive_js_extern \./ g , "" )
97
+ let output =
98
+ ChildProcess .execSync (` ./node_modules/.bin/rescript-tools doc ${corePath}` )-> Buffer .toString
99
+
100
+ output
101
+ -> JSON .parseExn
102
+ -> Docgen .decodeFromJson
103
+ }
104
+
105
+ let docsDecoded = Array .concat (docsDecoded , [coreDocs ])
100
106
101
107
let docs = docsDecoded -> Array .map (doc => {
102
108
let topLevelItems = doc .items -> Array .filterMap (item =>
@@ -117,6 +123,9 @@ let docs = docsDecoded->Array.map(doc => {
117
123
if Array .includes (hiddenModules , id ) {
118
124
getModules (rest , moduleNames )
119
125
} else {
126
+ let id = String .startsWith (id , "RescriptCore" )
127
+ ? String .replace (id , "RescriptCore" , "Core" )
128
+ : id
120
129
getModules (
121
130
list {... rest , ... List .fromArray (items )},
122
131
list {{id , items , name , docstrings }, ... moduleNames },
@@ -126,7 +135,9 @@ let docs = docsDecoded->Array.map(doc => {
126
135
| list {} => moduleNames
127
136
}
128
137
129
- let id = doc .name
138
+ let id = String .startsWith (doc .name , "RescriptCore" )
139
+ ? String .replace (doc .name , "RescriptCore" , "Core" )
140
+ : doc .name
130
141
131
142
let top = {id , name : id , docstrings : doc .docstrings , items : topLevelItems }
132
143
let submodules = getModules (doc .items -> List .fromArray , list {})-> List .toArray
@@ -140,6 +151,9 @@ let allModules = {
140
151
let encodeItem = (docItem : Docgen .item ) => {
141
152
switch docItem {
142
153
| Value ({id , name , docstrings , signature , ?deprecated }) => {
154
+ let id = String .startsWith (id , "RescriptCore" )
155
+ ? String .replace (id , "RescriptCore" , "Core" )
156
+ : id
143
157
let dict = Dict .fromArray (
144
158
[
145
159
("id" , id -> String ),
@@ -148,15 +162,10 @@ let allModules = {
148
162
(
149
163
"docstrings" ,
150
164
docstrings
151
- -> Array .map (s => s -> removeStdlibOrPrimitive -> String )
165
+ -> Array .map (s => s -> String )
152
166
-> Array ,
153
167
),
154
- (
155
- "signature" ,
156
- signature
157
- -> removeStdlibOrPrimitive
158
- -> String ,
159
- ),
168
+ ("signature" , signature -> String ),
160
169
]-> Array .concat (
161
170
switch deprecated {
162
171
| Some (v ) => [("deprecated" , v -> String )]
@@ -168,13 +177,16 @@ let allModules = {
168
177
}
169
178
170
179
| Type ({id , name , docstrings , signature , ?deprecated }) =>
180
+ let id = String .startsWith (id , "RescriptCore" )
181
+ ? String .replace (id , "RescriptCore" , "Core" )
182
+ : id
171
183
let dict = Dict .fromArray (
172
184
[
173
185
("id" , id -> String ),
174
186
("kind" , "type" -> String ),
175
187
("name" , name -> String ),
176
- ("docstrings" , docstrings -> Array .map (s => s -> removeStdlibOrPrimitive -> String )-> Array ),
177
- ("signature" , signature -> removeStdlibOrPrimitive -> String ),
188
+ ("docstrings" , docstrings -> Array .map (s => s -> String )-> Array ),
189
+ ("signature" , signature -> String ),
178
190
]-> Array .concat (
179
191
switch deprecated {
180
192
| Some (v ) => [("deprecated" , v -> String )]
@@ -197,8 +209,10 @@ let allModules = {
197
209
-> Array .filterMap (item => encodeItem (item ))
198
210
-> Array
199
211
212
+ let id = String .startsWith (mod .id , "RescriptCore" ) ? "Core" : mod .id
213
+
200
214
let rest = Dict .fromArray ([
201
- ("id" , mod . id -> String ),
215
+ ("id" , id -> String ),
202
216
("name" , mod .name -> String ),
203
217
("docstrings" , mod .docstrings -> Array .map (s => s -> String )-> Array ),
204
218
("items" , items ),
@@ -221,6 +235,8 @@ let () = {
221
235
allModules -> Array .forEach (((topLevelName , mod )) => {
222
236
let json = JSON .Object (mod )
223
237
238
+ let topLevelName = String .startsWith (topLevelName , "RescriptCore" ) ? "Core" : topLevelName
239
+
224
240
Fs .writeFileSync (
225
241
Path .join ([dirVersion , ` ${topLevelName-> String.toLowerCase}.json` ]),
226
242
json -> JSON .stringify (~space = 2 ),
@@ -263,6 +279,7 @@ let () = {
263
279
}
264
280
265
281
let tocTree = docsDecoded -> Array .map (({name , items }) => {
282
+ let name = String .startsWith (name , "RescriptCore" ) ? "Core" : name
266
283
let path = name -> String .toLowerCase
267
284
(
268
285
path ,
0 commit comments