What if I have rescript
installed globally and not in my project? Then I guess it will not be found and I need to specify the path manually?
I think the best way for the VSCode extension to locate the rescript
binary is to ask the package manager that’s in use?
For yarn:
$ yarn --version
3.2.1
$ yarn bin --json
{"name":"npm-run-all","source":"npm-run-all","path":"/Users/alexander/Projects/front-end/node_modules/npm-run-all/bin/npm-run-all/index.js"}
{"name":"run-p","source":"npm-run-all","path":"/Users/alexander/Projects/front-end/node_modules/npm-run-all/bin/run-p/index.js"}
{"name":"run-s","source":"npm-run-all","path":"/Users/alexander/Projects/front-end/node_modules/npm-run-all/bin/run-s/index.js"}
{"name":"bsc","source":"rescript","path":"/Users/alexander/Projects/front-end/node_modules/rescript/bsc"}
{"name":"bsrefmt","source":"rescript","path":"/Users/alexander/Projects/front-end/node_modules/rescript/bsrefmt"}
{"name":"bstracing","source":"rescript","path":"/Users/alexander/Projects/front-end/node_modules/rescript/lib/bstracing"}
{"name":"rescript","source":"rescript","path":"/Users/alexander/Projects/front-end/node_modules/rescript/rescript"}
For npm:
$ npm --version
8.12.1
$ npm bin
/Users/alexander/Projects/front-end/node_modules/.bin
$ ls -al `npm bin`/rescript
lrwxr-xr-x 1 alexander staff 20 Jul 25 10:37 /Users/alexander/Projects/front-end/node_modules/.bin/rescript -> ../rescript/rescript
Similarly pnpm
$ pnpm bin
/Users/alexander/Projects/test/node_modules/.bin
Similarly to get the global folder containing binaries Yarn supports yarn global bin
and NPM and PNPM both support passing --global
to the <exe> bin
command.
While calling the package manager may be considered slow, this only needs to happen once after the lock file (package-lock.json
or yarn.lock
) changes and can be cached otherwise.
The package manager to use could be read from the packageManager
in package.json
(if the repository is using Node’s corepack). Alternatively it can be guessed based on the presence of a yarn.lock
or other lock file.
I also wanted to test what this would look like in a repository using Yarn PnP. It turns out that if we actually use the package manager, this will just work
$ yarn bin --json
{"name":"bsc","source":"rescript","path":"/Users/alexander/Projects/test/.yarn/unplugged/rescript-npm-9.1.4-75c65d01e7/node_modules/rescript/bsc"}
{"name":"bsrefmt","source":"rescript","path":"/Users/alexander/Projects/test/.yarn/unplugged/rescript-npm-9.1.4-75c65d01e7/node_modules/rescript/bsrefmt"}
{"name":"bstracing","source":"rescript","path":"/Users/alexander/Projects/test/.yarn/unplugged/rescript-npm-9.1.4-75c65d01e7/node_modules/rescript/lib/bstracing"}
{"name":"rescript","source":"rescript","path":"/Users/alexander/Projects/test/.yarn/unplugged/rescript-npm-9.1.4-75c65d01e7/node_modules/rescript/rescript"}
$ /Users/alexander/Projects/test/.yarn/unplugged/rescript-npm-9.1.4-75c65d01e7/node_modules/rescript/rescript -h
Available flags
-v, -version display version number
-h, -help display help
Subcommands:
build
clean
format
convert
dump
help
Run rescript subcommand -h for more details,
For example:
rescript build -h
rescript format -h
The default `rescript` is equivalent to `rescript build` subcommand
As you can see Yarn keeps an unzipped version of the command available for invocation.