How to automatically generate interface for all files


#1

I know how to generate an interface for a single file and output it to stdout. But I haven’t found an easy and automated way to be able to do something like this in my scripts yarn generate:interfaces src or yarn generate:interfaces src/main.re and have it generate .rei files alongside every one of my .re files inside src. Is there a way to achieve this?


#2

Not sure if this would help, but could you write a shell script that calls bsc as in https://bucklescript.github.io/docs/en/automatic-interface-generation.html ?


#3

Here is the small script I’m using

#!/bin/sh

rm -rf lib/bs/rei
mkdir -p lib/bs/rei

filepath(){
  FILENAME=`echo $(basename $1)  | cut -d'-' -f 1`
  echo "lib/bs/rei/$FILENAME.rei"
}

for i in $(find lib/bs/src -name '*.cmi');do
   bsc -bs-re-out "$(pwd)/$i" > "$(filepath $i)"
done

#4

Great script! I can stop wasting time now. Question for you. Why didnt you write the script so that it puts your .rei files next to the .re files?


#5

Probably to avoid overwriting any .rei files that may already exist in the src/ directory hierarchy.


#6

I don’t have any cmi file, how is bsc supposed to work? Is the full path required or how? it just outputs all the options every time I try to run bsc ./lib/Util.re


#7

You can find the .cmi files in the build folder after a successful build, see https://bucklescript.github.io/docs/en/automatic-interface-generation#tips-tricks


#8

thanks for your answer.
I guess that is only for bucklescript? I’m using esy.
Luckly, I found a template that makes rei files non necessary. Not sure how, I think it is thanks to some dune config