|
| 1 | +@@directive("#!/usr/bin/env node") |
| 2 | + |
| 3 | +@module("fs") external readFileSync: string => string = "readFileSync" |
| 4 | +@variadic @module("path") external join: array<string> => string = "join" |
| 5 | +@module("path") external dirname: string => string = "dirname" |
| 6 | +@val external __dirname: string = "__dirname" |
| 7 | + |
| 8 | +module Buffer = { |
| 9 | + type t |
| 10 | + |
| 11 | + @send external toString: t => string = "toString" |
| 12 | +} |
| 13 | + |
| 14 | +type spawnSyncResult = { |
| 15 | + stdout: Buffer.t, |
| 16 | + stderr: Buffer.t, |
| 17 | + status: Js.Null.t<int>, |
| 18 | +} |
| 19 | +@module("child_process") |
| 20 | +external spawnSync: (string, array<string>) => spawnSyncResult = "spawnSync" |
| 21 | + |
| 22 | +@val @scope("process") |
| 23 | +external exit: int => unit = "exit" |
| 24 | + |
| 25 | +@val |
| 26 | +external process: {"arch": string, "platform": string, "argv": array<string>} = "process" |
| 27 | + |
| 28 | +let argv = process["argv"] |
| 29 | + |
| 30 | +let args = argv->Js.Array2.slice(~start=2, ~end_=Js.Array2.length(argv)) |
| 31 | + |
| 32 | +let platformDir = |
| 33 | + process["arch"] === "arm64" ? process["platform"] ++ process["arch"] : process["platform"] |
| 34 | + |
| 35 | +let analysisProdPath = join([ |
| 36 | + dirname(__dirname), |
| 37 | + "analysis_binaries", |
| 38 | + platformDir, |
| 39 | + "rescript-editor-analysis.exe", |
| 40 | +]) |
| 41 | + |
| 42 | +let docHelp = `ReScript Tools |
| 43 | +
|
| 44 | +Output documentation to standard output |
| 45 | +
|
| 46 | +Usage: restools doc <FILE> |
| 47 | +
|
| 48 | +Example: restools doc ./path/to/EntryPointLib.res` |
| 49 | + |
| 50 | +let help = `ReScript Tools |
| 51 | +
|
| 52 | +Usage: restools [command] |
| 53 | +
|
| 54 | +Commands: |
| 55 | +
|
| 56 | +doc Generate documentation |
| 57 | +reanalyze Reanalyze |
| 58 | +-v, --version Print version |
| 59 | +-h, --help Print help` |
| 60 | + |
| 61 | +let logAndExit = (~log, ~code) => { |
| 62 | + Js.log(log) |
| 63 | + exit(code) |
| 64 | +} |
| 65 | + |
| 66 | +switch args->Belt.List.fromArray { |
| 67 | +| list{"doc", ...rest} => |
| 68 | + switch rest { |
| 69 | + | list{"-h" | "--help"} => logAndExit(~log=docHelp, ~code=0) |
| 70 | + | list{filePath} => |
| 71 | + let spawn = spawnSync(analysisProdPath, ["extractDocs", filePath]) |
| 72 | + |
| 73 | + switch spawn.status->Js.Null.toOption { |
| 74 | + | Some(code) if code !== 0 => logAndExit(~log=spawn.stderr->Buffer.toString, ~code) |
| 75 | + | Some(code) => logAndExit(~log=spawn.stdout->Buffer.toString, ~code) |
| 76 | + | None => logAndExit(~log=`error: unexpected error to extract docs for ${filePath}`, ~code=1) |
| 77 | + } |
| 78 | + | _ => logAndExit(~log=docHelp, ~code=1) |
| 79 | + } |
| 80 | +| list{"reanalyze", ...rest} => |
| 81 | + let args = ["reanalyze"]->Js.Array2.concat(Belt.List.toArray(rest)) |
| 82 | + let spawn = spawnSync(analysisProdPath, args) |
| 83 | + |
| 84 | + switch spawn.status->Js.Null.toOption { |
| 85 | + | Some(code) if code !== 0 => logAndExit(~log=spawn.stderr->Buffer.toString, ~code) |
| 86 | + | Some(code) => logAndExit(~log=spawn.stdout->Buffer.toString, ~code) |
| 87 | + | None => |
| 88 | + logAndExit( |
| 89 | + ~log=`error: unexpected error to run reanalyze with arguments: ${args->Js.Array2.joinWith( |
| 90 | + " ", |
| 91 | + )}`, |
| 92 | + ~code=1, |
| 93 | + ) |
| 94 | + } |
| 95 | +| list{"-h" | "--help"} => logAndExit(~log=help, ~code=0) |
| 96 | +| list{"-v" | "--version"} => |
| 97 | + switch readFileSync("./package.json")->Js.Json.parseExn->Js.Json.decodeObject { |
| 98 | + | None => logAndExit(~log="error: failed to find version in package.json", ~code=1) |
| 99 | + | Some(dict) => logAndExit(~log=dict->Js.Dict.unsafeGet("version"), ~code=0) |
| 100 | + } |
| 101 | +| _ => logAndExit(~log=help, ~code=1) |
| 102 | +} |
0 commit comments