Skip to content

@rescript/tools package #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,19 @@ jobs:
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Copy analysis binaries to tools folder
run: cp -r server/analysis_binaries/* tools/analysis_binaries

- name: Build @rescript/tools package
working-directory: tools
run: |
npm ci
npm run build

- name: Publish @rescript/tools package
if: ${{ startsWith(github.event.head_commit.message, 'publish tools') && (github.ref == 'refs/heads/master') }}
working-directory: tools
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ analysis/_build
analysis/tests/.merlin
analysis/rescript-editor-analysis.exe
analysis/_opam
tools/node_modules
tools/lib
tools/**/*.bs.js
46 changes: 46 additions & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# ReScript Tools

## Install

```sh
npm install --save-dev @rescript/tools
```

## CLI Usage

```sh
restools --help
```

### Generate documentation

Print JSON:

```sh
restools doc src/EntryPointLibFile.res
```

Write JSON:

```sh
restools doc src/EntryPointLibFile.res > doc.json
```

### Reanalyze

```sh
restools reanalyze --help
```

## Decode JSON

Add to `bs-dev-dependencies`:

```json
"bs-dev-dependencies": ["@rescript/tools"]
```

```rescript
// Read JSON file and parse with `Js.Json.parseExn`
json->RescriptTools.Docgen.decodeFromJson
```
Empty file.
43 changes: 43 additions & 0 deletions tools/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions tools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@rescript/tools",
"description": "ReScript Tools",
"version": "0.1.0",
"author": "chenglou",
"license": "MIT",
"bin": {
"restools": "./src/Cli.bs.js"
},
"keywords": [
"ReScript",
"Tools",
"Docgen"
],
"files": [
"src/Cli.bs.js",
"src/*.res",
"src/*.resi",
"analysis_binaries/",
"README.md"
],
"engines": {
"node": "*"
},
"homepage": "https://github.com/rescript-lang/rescript-vscode/tools/README.md",
"repository": {
"type": "git",
"url": "https://github.com/rescript-lang/rescript-vscode",
"directory": "tools"
},
"bugs": {
"url": "https://github.com/rescript-lang/rescript-vscode/issues"
},
"scripts": {
"build": "rescript build"
},
"dependencies": {
"rescript": "^11.0.0-rc.4"
}
}
15 changes: 15 additions & 0 deletions tools/rescript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@rescript/tools",
"version": "0.1.0",
"sources": [
{
"dir": "src",
"public": ["RescriptTools"]
}
],
"suffix": ".bs.js",
"package-specs": {
"module": "commonjs",
"in-source": true
}
}
102 changes: 102 additions & 0 deletions tools/src/Cli.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
@@directive("#!/usr/bin/env node")

@module("fs") external readFileSync: string => string = "readFileSync"
@variadic @module("path") external join: array<string> => string = "join"
@module("path") external dirname: string => string = "dirname"
@val external __dirname: string = "__dirname"

module Buffer = {
type t

@send external toString: t => string = "toString"
}

type spawnSyncResult = {
stdout: Buffer.t,
stderr: Buffer.t,
status: Js.Null.t<int>,
}
@module("child_process")
external spawnSync: (string, array<string>) => spawnSyncResult = "spawnSync"

@val @scope("process")
external exit: int => unit = "exit"

@val
external process: {"arch": string, "platform": string, "argv": array<string>} = "process"

let argv = process["argv"]

let args = argv->Js.Array2.slice(~start=2, ~end_=Js.Array2.length(argv))

let platformDir =
process["arch"] === "arm64" ? process["platform"] ++ process["arch"] : process["platform"]

let analysisProdPath = join([
dirname(__dirname),
"analysis_binaries",
platformDir,
"rescript-editor-analysis.exe",
])

let docHelp = `ReScript Tools

Output documentation to standard output

Usage: restools doc <FILE>

Example: restools doc ./path/to/EntryPointLib.res`

let help = `ReScript Tools

Usage: restools [command]

Commands:

doc Generate documentation
reanalyze Reanalyze
-v, --version Print version
-h, --help Print help`

let logAndExit = (~log, ~code) => {
Js.log(log)
exit(code)
}

switch args->Belt.List.fromArray {
| list{"doc", ...rest} =>
switch rest {
| list{"-h" | "--help"} => logAndExit(~log=docHelp, ~code=0)
| list{filePath} =>
let spawn = spawnSync(analysisProdPath, ["extractDocs", filePath])

switch spawn.status->Js.Null.toOption {
| Some(code) if code !== 0 => logAndExit(~log=spawn.stderr->Buffer.toString, ~code)
| Some(code) => logAndExit(~log=spawn.stdout->Buffer.toString, ~code)
| None => logAndExit(~log=`error: unexpected error to extract docs for ${filePath}`, ~code=1)
}
| _ => logAndExit(~log=docHelp, ~code=1)
}
| list{"reanalyze", ...rest} =>
let args = ["reanalyze"]->Js.Array2.concat(Belt.List.toArray(rest))
let spawn = spawnSync(analysisProdPath, args)

switch spawn.status->Js.Null.toOption {
| Some(code) if code !== 0 => logAndExit(~log=spawn.stderr->Buffer.toString, ~code)
| Some(code) => logAndExit(~log=spawn.stdout->Buffer.toString, ~code)
| None =>
logAndExit(
~log=`error: unexpected error to run reanalyze with arguments: ${args->Js.Array2.joinWith(
" ",
)}`,
~code=1,
)
}
| list{"-h" | "--help"} => logAndExit(~log=help, ~code=0)
| list{"-v" | "--version"} =>
switch readFileSync("./package.json")->Js.Json.parseExn->Js.Json.decodeObject {
| None => logAndExit(~log="error: failed to find version in package.json", ~code=1)
| Some(dict) => logAndExit(~log=dict->Js.Dict.unsafeGet("version"), ~code=0)
}
| _ => logAndExit(~log=help, ~code=1)
}
1 change: 1 addition & 0 deletions tools/src/RescriptTools.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Docgen = Tools_Docgen
Loading