@@ -4,13 +4,13 @@ Generate docs from ReScript Compiler
44## Run
55
66```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
88```
99
1010## Examples
1111
1212```bash
13- node scripts/gendocs.mjs path/to/rescript-monorepo latest true
13+ node scripts/gendocs.mjs path/to/rescript-compiler latest true
1414```
1515*/
1616@val @scope (("import" , "meta" )) external url : string = "url"
@@ -25,16 +25,21 @@ let dirname =
2525 -> Path .dirname
2626
2727let compilerLibPath = switch args -> Array .get (0 ) {
28- | Some (path ) => Path .join ([path , "runtime " ])
28+ | Some (path ) => Path .join ([path , "jscomp" , "others " ])
2929| None => failwith ("First argument should be path to rescript-compiler repo" )
3030}
3131
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 ) {
3338| 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`" )
3540}
3641
37- let forceReWrite = switch args -> Array .get (2 ) {
42+ let forceReWrite = switch args -> Array .get (3 ) {
3843| Some ("true" ) => true
3944| _ => false
4045}
@@ -50,7 +55,7 @@ if Fs.existsSync(dirVersion) {
5055 Fs .mkdirSync (dirVersion )
5156}
5257
53- let entryPointFiles = ["Belt.res " , "Dom .res" , "Js.res" , "Stdlib .res" ]
58+ let entryPointFiles = ["js.ml " , "belt .res" , "dom .res" ]
5459
5560let hiddenModules = ["Js.Internal" , "Js.MapperRt" ]
5661
@@ -71,32 +76,33 @@ type section = {
7176
7277let env = Process .env
7378
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 )
7781
78- let output = ChildProcess .execSync (
82+ Dict .set (env , "FROM_COMPILER" , "true" )
83+
84+ let output =
85+ ChildProcess .execSync (
7986 ` ./node_modules/.bin/rescript-tools doc ${entryPointFile}` ,
80- ~options = {
81- maxBuffer : 30_000_000 .,
82- },
8387 )-> Buffer .toString
8488
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" )
9896
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 ])
100106
101107let docs = docsDecoded -> Array .map (doc => {
102108 let topLevelItems = doc .items -> Array .filterMap (item =>
@@ -117,6 +123,9 @@ let docs = docsDecoded->Array.map(doc => {
117123 if Array .includes (hiddenModules , id ) {
118124 getModules (rest , moduleNames )
119125 } else {
126+ let id = String .startsWith (id , "RescriptCore" )
127+ ? String .replace (id , "RescriptCore" , "Core" )
128+ : id
120129 getModules (
121130 list {... rest , ... List .fromArray (items )},
122131 list {{id , items , name , docstrings }, ... moduleNames },
@@ -126,7 +135,9 @@ let docs = docsDecoded->Array.map(doc => {
126135 | list {} => moduleNames
127136 }
128137
129- let id = doc .name
138+ let id = String .startsWith (doc .name , "RescriptCore" )
139+ ? String .replace (doc .name , "RescriptCore" , "Core" )
140+ : doc .name
130141
131142 let top = {id , name : id , docstrings : doc .docstrings , items : topLevelItems }
132143 let submodules = getModules (doc .items -> List .fromArray , list {})-> List .toArray
@@ -140,6 +151,9 @@ let allModules = {
140151 let encodeItem = (docItem : Docgen .item ) => {
141152 switch docItem {
142153 | Value ({id , name , docstrings , signature , ?deprecated }) => {
154+ let id = String .startsWith (id , "RescriptCore" )
155+ ? String .replace (id , "RescriptCore" , "Core" )
156+ : id
143157 let dict = Dict .fromArray (
144158 [
145159 ("id" , id -> String ),
@@ -148,15 +162,10 @@ let allModules = {
148162 (
149163 "docstrings" ,
150164 docstrings
151- -> Array .map (s => s -> removeStdlibOrPrimitive -> String )
165+ -> Array .map (s => s -> String )
152166 -> Array ,
153167 ),
154- (
155- "signature" ,
156- signature
157- -> removeStdlibOrPrimitive
158- -> String ,
159- ),
168+ ("signature" , signature -> String ),
160169 ]-> Array .concat (
161170 switch deprecated {
162171 | Some (v ) => [("deprecated" , v -> String )]
@@ -168,13 +177,16 @@ let allModules = {
168177 }
169178
170179 | Type ({id , name , docstrings , signature , ?deprecated }) =>
180+ let id = String .startsWith (id , "RescriptCore" )
181+ ? String .replace (id , "RescriptCore" , "Core" )
182+ : id
171183 let dict = Dict .fromArray (
172184 [
173185 ("id" , id -> String ),
174186 ("kind" , "type" -> String ),
175187 ("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 ),
178190 ]-> Array .concat (
179191 switch deprecated {
180192 | Some (v ) => [("deprecated" , v -> String )]
@@ -197,8 +209,10 @@ let allModules = {
197209 -> Array .filterMap (item => encodeItem (item ))
198210 -> Array
199211
212+ let id = String .startsWith (mod .id , "RescriptCore" ) ? "Core" : mod .id
213+
200214 let rest = Dict .fromArray ([
201- ("id" , mod . id -> String ),
215+ ("id" , id -> String ),
202216 ("name" , mod .name -> String ),
203217 ("docstrings" , mod .docstrings -> Array .map (s => s -> String )-> Array ),
204218 ("items" , items ),
@@ -221,6 +235,8 @@ let () = {
221235 allModules -> Array .forEach (((topLevelName , mod )) => {
222236 let json = JSON .Object (mod )
223237
238+ let topLevelName = String .startsWith (topLevelName , "RescriptCore" ) ? "Core" : topLevelName
239+
224240 Fs .writeFileSync (
225241 Path .join ([dirVersion , ` ${topLevelName-> String.toLowerCase}.json` ]),
226242 json -> JSON .stringify (~space = 2 ),
@@ -263,6 +279,7 @@ let () = {
263279 }
264280
265281 let tocTree = docsDecoded -> Array .map (({name , items }) => {
282+ let name = String .startsWith (name , "RescriptCore" ) ? "Core" : name
266283 let path = name -> String .toLowerCase
267284 (
268285 path ,
0 commit comments