-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathCli.res
92 lines (71 loc) · 2.64 KB
/
Cli.res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
@@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"
type processEnvOptions = {stdio?: string}
type spawnSyncResult = {status: Js.Null.t<int>}
@module("child_process")
external spawnSync: (string, array<string>, option<processEnvOptions>) => 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: rescript-tools doc <FILE>
Example: rescript-tools doc ./path/to/EntryPointLib.res`
let help = `ReScript Tools
Usage: rescript-tools [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], Some({stdio: "inherit"}))
switch spawn.status->Js.Null.toOption {
| Some(code) => exit(code)
| None => ()
}
| _ => logAndExit(~log=docHelp, ~code=1)
}
| list{"reanalyze", ...rest} =>
let args = ["reanalyze"]->Js.Array2.concat(Belt.List.toArray(rest))
let spawn = spawnSync(analysisProdPath, args, Some({stdio: "inherit"}))
switch spawn.status->Js.Null.toOption {
| Some(code) => exit(code)
| None => ()
}
| list{"-h" | "--help"} => logAndExit(~log=help, ~code=0)
| list{"-v" | "--version"} =>
let packageJson = join([__dirname, "..", "package.json"])
switch readFileSync(packageJson)->Js.Json.parseExn->Js.Json.decodeObject {
| None => logAndExit(~log="error: failed to find version in package.json", ~code=1)
| Some(dict) =>
switch dict->Js.Dict.get("version") {
| Some(version) => logAndExit(~log=version, ~code=0)
| None => logAndExit(~log="error: failed to find version in package.json", ~code=1)
}
}
| _ => logAndExit(~log=help, ~code=1)
}