Skip to content

Commit

Permalink
Route aware tools (#115)
Browse files Browse the repository at this point in the history
* code lens in ReScript files detailing how many routes the file is referenced in

* changeset

* support custom LSP command for returning routes for a file

* fix wording

* fix error in code lens path order
  • Loading branch information
zth authored Nov 20, 2022
1 parent 6ad2b0f commit c51701d
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-rings-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rescript-relay-router": patch
---

LSP: Code lens in ReScript files detailing how many routes the file is referenced in.
5 changes: 5 additions & 0 deletions .changeset/silver-lemons-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rescript-relay-router": patch
---

LSP: Support custom LSP command for returning routes for a file.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module Chokidar = {
@send
external onUnlink: (t, @as(json`"unlink"`) _, string => unit) => t = "on"

@send
external onAdd: (t, @as(json`"add"`) _, string => unit) => t = "on"

@send
external close: t => Promise.t<unit> = "close"
}
Expand All @@ -38,6 +41,12 @@ module Path = {

@module("path")
external extname: string => string = "extname"

@live
type parsed = {name: string}

@module("path")
external parse: string => parsed = "parse"
}

module Node = {
Expand Down Expand Up @@ -115,6 +124,33 @@ module Fs = {
}
}

module ChildProcess = {
module Spawn = {
type stdout
type stderr
@live
type t = {stdout: stdout, stderr: stderr}

@live
type opts = {
shell?: bool,
cwd?: string,
stdio?: [#inherit | #pipe],
}

type buffer
@send external bufferToString: buffer => string = "toString"

@module("child_process")
external make: (string, array<string>, opts) => t = "spawn"

@send external onStdoutData: (stdout, @as(json`"data"`) _, buffer => unit) => unit = "on"
@send external onClose: (t, @as(json`"close"`) _, int => unit) => unit = "on"

let onData = (t, cb) => t.stdout->onStdoutData(buffer => buffer->bufferToString->cb)
}
}

module URL = {
type t

Expand Down Expand Up @@ -169,3 +205,5 @@ module LinesAndColumns = {
@send
external offsetForLocation: (t, loc) => int = "indexForLocation"
}

@val @module("os") external osEOL: string = "EOL"
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module Chokidar: {
@send
external onUnlink: (t, @as(json`"unlink"`) _, string => unit) => t = "on"

@send
external onAdd: (t, @as(json`"add"`) _, string => unit) => t = "on"

@send
external close: t => Js.Promise.t<unit> = "close"
}
Expand All @@ -36,6 +39,11 @@ module Path: {

@module("path")
external extname: string => string = "extname"

type parsed = {name: string}

@module("path")
external parse: string => parsed = "parse"
}

module Node: {
Expand Down Expand Up @@ -88,6 +96,26 @@ module Fs: {
let writeFileIfChanged: (string, string) => unit
}

module ChildProcess: {
module Spawn: {
type t

@live
type opts = {
shell?: bool,
cwd?: string,
stdio?: [#inherit | #pipe],
}

@module("child_process")
external make: (string, array<string>, opts) => t = "spawn"

@send external onClose: (t, @as(json`"close"`) _, int => unit) => unit = "on"

let onData: (t, string => unit) => unit
}
}

module URL: {
type t

Expand Down Expand Up @@ -139,3 +167,5 @@ module LinesAndColumns: {
@send
external offsetForLocation: (t, loc) => int = "indexForLocation"
}

@val @module("os") external osEOL: string = "EOL"
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ let init = () => {
Console.log("[init] Done! You can now run the `generate -scaffold-renderers` command.")
}

type cliResult = Done | Watcher({watcher: Chokidar.Watcher.t})
type cliResult = Done | Watcher({watchers: array<Chokidar.Watcher.t>})

let runCli = args => {
switch args->List.fromArray {
Expand Down Expand Up @@ -379,7 +379,7 @@ let runCli = args => {
->Watcher.onUnlink(_ => {
generateRoutesSafe()
})
Watcher({watcher: theWatcher})
Watcher({watchers: [theWatcher]})
} else {
Done
}
Expand All @@ -400,8 +400,8 @@ let runCli = args => {
} else {
NodeRpc
}
let watcher = Lsp.start(~config, ~mode)
Watcher({watcher: watcher})
let watchers = Lsp.start(~config, ~mode)
Watcher({watchers: watchers})
| _ =>
Console.log("Unknown command. Use -help or -h to see all available commands.")
Done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,6 @@ module Path = {
}
}

let f = Belt.Array.concatMany

module Validators = {
open! JsoncParser

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ module RoutePath: {
}

let getPathSegment = t => t.pathSegment
let getFullRoutePath = t => "/" ++ t.currentRoutePath->List.toArray->Array.joinWith("/")
let getFullRoutePath = t =>
"/" ++ t.currentRoutePath->List.toArray->Array.reverse->Array.joinWith("/")

let toPattern = t =>
"/" ++
Expand Down Expand Up @@ -207,4 +208,15 @@ type rec routeForCliMatching = {
type config = {
generatedPath: string,
routesFolderPath: string,
rescriptLibFolderPath: string,
}

type dependencyDeclaration = {
dependsOn: Set.t<string>,
dependents: Set.t<string>,
}

type moduleDepsCache = {
mutable cache: Dict.t<dependencyDeclaration>,
mutable compilerLastRebuilt: float,
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ module Config: {
->Option.getWithDefault(Path.join([routesFolderPath, "__generated__"]))
->resolveFullPath,
routesFolderPath,
// TODO: This won't work for monorepo setups etc where the ReScript
// lib dir isn't at the same level as the router config file. Fix
// eventually.
rescriptLibFolderPath: Path.join([Path.dirname(filepath), "lib", "bs"]),
}
}

Expand Down Expand Up @@ -294,3 +298,10 @@ let queryParamToQueryParamDecoder = (param, ~key) => {
)}),\n`
}
}

let maybePluralize = (text, ~count) =>
text ++ if count == 1 {
""
} else {
"s"
}
Loading

0 comments on commit c51701d

Please sign in to comment.