@reventlessdev/reventless-seed 1.0.0-alpha.1 → 1.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/lib/bs/.compiler.log +2 -2
  3. package/lib/bs/ReventlessSeed.cmi +0 -0
  4. package/lib/bs/ReventlessSeed.cmt +0 -0
  5. package/lib/bs/ReventlessSeed.mlmap +2 -0
  6. package/lib/bs/compiler-info.json +1 -1
  7. package/lib/bs/src/Seed-ReventlessSeed.cmi +0 -0
  8. package/lib/bs/src/Seed-ReventlessSeed.cmt +0 -0
  9. package/lib/bs/src/Seed.ast +0 -0
  10. package/lib/bs/src/Seed.res +18 -0
  11. package/lib/bs/src/Seed.res.mjs +6 -0
  12. package/lib/bs/src/Seed_Client-ReventlessSeed.cmi +0 -0
  13. package/lib/bs/src/Seed_Client-ReventlessSeed.cmj +0 -0
  14. package/lib/bs/src/Seed_Client-ReventlessSeed.cmt +0 -0
  15. package/lib/bs/src/Seed_Client.ast +0 -0
  16. package/lib/bs/src/Seed_Client.res +22 -11
  17. package/lib/bs/src/Seed_Client.res.mjs +23 -5
  18. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmi +0 -0
  19. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmj +0 -0
  20. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmt +0 -0
  21. package/lib/bs/src/Seed_Connect.ast +0 -0
  22. package/lib/bs/src/Seed_Connect.res +84 -0
  23. package/lib/bs/src/Seed_Connect.res.mjs +62 -0
  24. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmi +0 -0
  25. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmj +0 -0
  26. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmt +0 -0
  27. package/lib/bs/src/Seed_Prompt.ast +0 -0
  28. package/lib/bs/src/Seed_Prompt.res +188 -0
  29. package/lib/bs/src/Seed_Prompt.res.mjs +172 -0
  30. package/lib/bs/src/Seed_Random-ReventlessSeed.cmt +0 -0
  31. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmi +0 -0
  32. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmj +0 -0
  33. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmt +0 -0
  34. package/lib/bs/src/Seed_Runner.ast +0 -0
  35. package/lib/bs/src/Seed_Runner.res +119 -1
  36. package/lib/bs/src/Seed_Runner.res.mjs +87 -1
  37. package/lib/bs/src/Seed_Types-ReventlessSeed.cmt +0 -0
  38. package/lib/bs/src/Seed_Upload-ReventlessSeed.cmt +0 -0
  39. package/lib/ocaml/.compiler.log +2 -2
  40. package/lib/ocaml/ReventlessSeed.cmi +0 -0
  41. package/lib/ocaml/ReventlessSeed.cmt +0 -0
  42. package/lib/ocaml/ReventlessSeed.mlmap +2 -0
  43. package/lib/ocaml/Seed-ReventlessSeed.cmi +0 -0
  44. package/lib/ocaml/Seed-ReventlessSeed.cmt +0 -0
  45. package/lib/ocaml/Seed.ast +0 -0
  46. package/lib/ocaml/Seed.res +18 -0
  47. package/lib/ocaml/Seed_Client-ReventlessSeed.cmi +0 -0
  48. package/lib/ocaml/Seed_Client-ReventlessSeed.cmj +0 -0
  49. package/lib/ocaml/Seed_Client-ReventlessSeed.cmt +0 -0
  50. package/lib/ocaml/Seed_Client.ast +0 -0
  51. package/lib/ocaml/Seed_Client.res +22 -11
  52. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmi +0 -0
  53. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmj +0 -0
  54. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmt +0 -0
  55. package/lib/ocaml/Seed_Connect.ast +0 -0
  56. package/lib/ocaml/Seed_Connect.res +84 -0
  57. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmi +0 -0
  58. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmj +0 -0
  59. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmt +0 -0
  60. package/lib/ocaml/Seed_Prompt.ast +0 -0
  61. package/lib/ocaml/Seed_Prompt.res +188 -0
  62. package/lib/ocaml/Seed_Random-ReventlessSeed.cmt +0 -0
  63. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmi +0 -0
  64. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmj +0 -0
  65. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmt +0 -0
  66. package/lib/ocaml/Seed_Runner.ast +0 -0
  67. package/lib/ocaml/Seed_Runner.res +119 -1
  68. package/lib/ocaml/Seed_Types-ReventlessSeed.cmt +0 -0
  69. package/lib/ocaml/Seed_Upload-ReventlessSeed.cmt +0 -0
  70. package/package.json +1 -1
  71. package/src/Seed.res +18 -0
  72. package/src/Seed.res.mjs +6 -0
  73. package/src/Seed_Client.res +22 -11
  74. package/src/Seed_Client.res.mjs +23 -5
  75. package/src/Seed_Connect.res +84 -0
  76. package/src/Seed_Connect.res.mjs +62 -0
  77. package/src/Seed_Prompt.res +188 -0
  78. package/src/Seed_Prompt.res.mjs +172 -0
  79. package/src/Seed_Runner.res +119 -1
  80. package/src/Seed_Runner.res.mjs +87 -1
package/CHANGELOG.md CHANGED
@@ -3,6 +3,20 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # 1.0.0-alpha.3 (2026-07-27)
7
+
8
+ ### Features
9
+
10
+ * **seed:** refuse to seed onto a non-empty store (fresh-store guard) ([987613f](https://github.com/ReventlessDev/reventless-core/commit/987613f0767fc4391297dfeed3a6b19cd76d8157))
11
+
12
+
13
+ # 1.0.0-alpha.2 (2026-07-26)
14
+
15
+ ### Features
16
+
17
+ * **seed:** per-provider seed scripts, shared machinery, exportable data sets ([ba43e9e](https://github.com/ReventlessDev/reventless-core/commit/ba43e9efab84dd2955b6dafd1e187fd4aad699ad))
18
+
19
+
6
20
  # 1.0.0-alpha.1 (2026-07-26)
7
21
 
8
22
  ### Bug Fixes
@@ -1,2 +1,2 @@
1
- #Start(1785055307106)
2
- #Done(1785055311634)
1
+ #Start(1785139469133)
2
+ #Done(1785139473598)
Binary file
Binary file
@@ -1,6 +1,8 @@
1
1
  randjbuildsystem
2
2
  Seed
3
3
  Seed_Client
4
+ Seed_Connect
5
+ Seed_Prompt
4
6
  Seed_Random
5
7
  Seed_Runner
6
8
  Seed_Types
@@ -4,5 +4,5 @@
4
4
  "bsc_hash": "75e3a59c95cc953608fd3dd6ea6ed68141bba4414a20a52f114261b8a48385fa",
5
5
  "rescript_config_hash": "ad3a9d479584c1d6e4fe10cd993d9e290f74732e9b0cb3dd5d7429e88b4bba38",
6
6
  "runtime_path": "/home/runner/work/reventless-core/reventless-core/node_modules/@rescript/runtime",
7
- "generated_at": "1785055311640"
7
+ "generated_at": "1785139473607"
8
8
  }
Binary file
Binary file
Binary file
@@ -9,9 +9,27 @@ module Random = Seed_Random
9
9
  module Client = Seed_Client
10
10
  module Runner = Seed_Runner
11
11
  module Upload = Seed_Upload
12
+ module Prompt = Seed_Prompt
13
+ module Connect = Seed_Connect
12
14
 
13
15
  exception Failed = Seed_Types.Failed
14
16
 
17
+ // A live target a data set seeds against: an authenticated client, the upload
18
+ // endpoint (empty when the deployment serves no uploads), and a label.
19
+ type connection = Seed_Connect.connection = {
20
+ client: Seed_Client.t,
21
+ uploadEndpoint: string,
22
+ label: string,
23
+ }
24
+
25
+ // A named, seedable data set. `seed` owns everything domain-specific.
26
+ type dataSet = Seed_Runner.dataSet = {
27
+ name: string,
28
+ label: string,
29
+ seed: connection => promise<unit>,
30
+ probeViews?: array<string>,
31
+ }
32
+
15
33
  type rec value = Seed_Types.value =
16
34
  | String(string)
17
35
  | Id(string)
@@ -10,6 +10,10 @@ let Runner;
10
10
 
11
11
  let Upload;
12
12
 
13
+ let Prompt;
14
+
15
+ let Connect;
16
+
13
17
  let Failed = Seed_Types$ReventlessSeed.Failed;
14
18
 
15
19
  let mutation = Seed_Types$ReventlessSeed.mutation;
@@ -25,6 +29,8 @@ export {
25
29
  Client,
26
30
  Runner,
27
31
  Upload,
32
+ Prompt,
33
+ Connect,
28
34
  Failed,
29
35
  mutation,
30
36
  ids,
Binary file
@@ -27,9 +27,12 @@ let sleep = (ms: int): promise<unit> => Promise.make((resolve, _) => setTimeout(
27
27
 
28
28
  type config = {
29
29
  endpoint: string,
30
- loginEndpoint: string,
31
- username: string,
32
- password: string,
30
+ // Present only for the `login` flow (a platform that mints tokens over HTTP,
31
+ // e.g. the local dev `/__inmemory/login`). Omit when the bearer is supplied
32
+ // out-of-band via `useToken` (e.g. a Cognito id token minted by the caller).
33
+ loginEndpoint?: string,
34
+ username?: string,
35
+ password?: string,
33
36
  }
34
37
 
35
38
  type t = {config: config, mutable token: option<string>}
@@ -40,6 +43,11 @@ let make = (~config: config): t => {config, token: None}
40
43
  Seed.Upload) can authenticate side-channel requests with the same identity. */
41
44
  let currentToken = (t: t): option<string> => t.token
42
45
 
46
+ /** Injects a bearer obtained out-of-band (e.g. a Cognito id token), instead of
47
+ minting one via `login`. Provider-agnostic: the harness only cares that the
48
+ token is a valid bearer for `endpoint`. */
49
+ let useToken = (t: t, token: string): unit => t.token = Some(token)
50
+
43
51
  // ── JSON helpers ────────────────────────────────────────────────────────────
44
52
 
45
53
  let field = (json: JSON.t, key: string): option<JSON.t> =>
@@ -61,28 +69,31 @@ let nodeString = (node: JSON.t, key: string): option<string> =>
61
69
  // ── Requests ────────────────────────────────────────────────────────────────
62
70
 
63
71
  let login = async (t: t): unit => {
72
+ let loginEndpoint = switch t.config.loginEndpoint {
73
+ | Some(e) => e
74
+ | None => throw(Failed("login: no loginEndpoint configured — supply one or call useToken"))
75
+ }
76
+ let username = t.config.username->Option.getOr("")
77
+ let password = t.config.password->Option.getOr("")
64
78
  let body = JSON.stringify(
65
79
  JSON.Encode.object(
66
80
  Dict.fromArray([
67
- ("username", JSON.Encode.string(t.config.username)),
68
- ("password", JSON.Encode.string(t.config.password)),
81
+ ("username", JSON.Encode.string(username)),
82
+ ("password", JSON.Encode.string(password)),
69
83
  ]),
70
84
  ),
71
85
  )
72
86
  let res = try await fetch(
73
- t.config.loginEndpoint,
87
+ loginEndpoint,
74
88
  {method: "POST", headers: Dict.fromArray([("content-type", "application/json")]), body},
75
89
  ) catch {
76
- | _ =>
77
- throw(
78
- Failed(`login: cannot reach ${t.config.loginEndpoint} — is the platform running?`),
79
- )
90
+ | _ => throw(Failed(`login: cannot reach ${loginEndpoint} — is the platform running?`))
80
91
  }
81
92
  if !(res->responseOk) {
82
93
  let detail = await res->responseText
83
94
  throw(
84
95
  Failed(
85
- `login as "${t.config.username}" failed with HTTP ${(res->responseStatus)
96
+ `login as "${username}" failed with HTTP ${(res->responseStatus)
86
97
  ->Int.toString}: ${detail}`,
87
98
  ),
88
99
  )
@@ -21,6 +21,10 @@ function currentToken(t) {
21
21
  return t.token;
22
22
  }
23
23
 
24
+ function useToken(t, token) {
25
+ t.token = token;
26
+ }
27
+
24
28
  function field(json, key) {
25
29
  if (typeof json === "object" && json !== null && !Array.isArray(json)) {
26
30
  return json[key];
@@ -38,19 +42,32 @@ function nodeString(node, key) {
38
42
  }
39
43
 
40
44
  async function login(t) {
45
+ let e = t.config.loginEndpoint;
46
+ let loginEndpoint;
47
+ if (e !== undefined) {
48
+ loginEndpoint = e;
49
+ } else {
50
+ throw {
51
+ RE_EXN_ID: Seed_Types$ReventlessSeed.Failed,
52
+ _1: "login: no loginEndpoint configured — supply one or call useToken",
53
+ Error: new Error()
54
+ };
55
+ }
56
+ let username = Stdlib_Option.getOr(t.config.username, "");
57
+ let password = Stdlib_Option.getOr(t.config.password, "");
41
58
  let body = JSON.stringify(Object.fromEntries([
42
59
  [
43
60
  "username",
44
- t.config.username
61
+ username
45
62
  ],
46
63
  [
47
64
  "password",
48
- t.config.password
65
+ password
49
66
  ]
50
67
  ]));
51
68
  let res;
52
69
  try {
53
- res = await fetch(t.config.loginEndpoint, {
70
+ res = await fetch(loginEndpoint, {
54
71
  method: "POST",
55
72
  headers: Object.fromEntries([[
56
73
  "content-type",
@@ -61,7 +78,7 @@ async function login(t) {
61
78
  } catch (exn) {
62
79
  throw {
63
80
  RE_EXN_ID: Seed_Types$ReventlessSeed.Failed,
64
- _1: `login: cannot reach ` + t.config.loginEndpoint + ` — is the platform running?`,
81
+ _1: `login: cannot reach ` + loginEndpoint + ` — is the platform running?`,
65
82
  Error: new Error()
66
83
  };
67
84
  }
@@ -69,7 +86,7 @@ async function login(t) {
69
86
  let detail = await res.text();
70
87
  throw {
71
88
  RE_EXN_ID: Seed_Types$ReventlessSeed.Failed,
72
- _1: `login as "` + t.config.username + `" failed with HTTP ` + res.status.toString() + `: ` + detail,
89
+ _1: `login as "` + username + `" failed with HTTP ` + res.status.toString() + `: ` + detail,
73
90
  Error: new Error()
74
91
  };
75
92
  }
@@ -227,6 +244,7 @@ export {
227
244
  sleep,
228
245
  make,
229
246
  currentToken,
247
+ useToken,
230
248
  field,
231
249
  asString,
232
250
  nodeString,
Binary file
@@ -0,0 +1,84 @@
1
+ // Turns endpoints plus a login function into a ready `connection`.
2
+ //
3
+ // A connection is what a data set seeds against: an authenticated GraphQL
4
+ // client, the upload endpoint (empty when the deployment serves no uploads), and
5
+ // a human label for the target. `make` is provider-agnostic — it prompts for
6
+ // credentials, calls the supplied `login` to obtain a bearer, and wires it onto
7
+ // the client via `useToken`. Where the bearer comes from (a local `/login`
8
+ // round-trip, a Cognito id token) is the caller's concern.
9
+
10
+ open Seed_Types
11
+
12
+ type connection = {client: Seed_Client.t, uploadEndpoint: string, label: string}
13
+
14
+ /**
15
+ * Prompts for credentials, calls `login` to mint a bearer, and returns a
16
+ * connection with that bearer already applied. `login` returns the bearer so a
17
+ * single `useToken` path serves every provider.
18
+ */
19
+ let make = async (
20
+ ~label: string,
21
+ ~endpoint: string,
22
+ ~uploadEndpoint: string,
23
+ ~login: (~username: string, ~password: string) => promise<string>,
24
+ ~localDefaults: bool=false,
25
+ ): connection => {
26
+ // `SEED_SKIP_UPLOADS` forces the connection to carry no upload endpoint, so a
27
+ // data set's upload phase no-ops the same way it does when a deployment serves
28
+ // none. Use it to seed domain data fast, or to skip a broken/absent upload path
29
+ // without editing the data set. It is the single knob: an empty
30
+ // `REVENTLESS_UPLOAD_ENDPOINT` env reads as "unset" (falls back to discovery),
31
+ // so this is the reliable way to disable uploads.
32
+ let uploadEndpoint = Seed_Prompt.envValue("SEED_SKIP_UPLOADS")->Option.isSome ? "" : uploadEndpoint
33
+ let (username, password) = await Seed_Prompt.credentials(~localDefaults)
34
+ let token = await login(~username, ~password)
35
+ let client = Seed_Client.make(~config={endpoint: endpoint})
36
+ client->Seed_Client.useToken(token)
37
+ {client, uploadEndpoint, label}
38
+ }
39
+
40
+ /**
41
+ * A login function backed by an HTTP login endpoint (the local dev
42
+ * `/__inmemory/login` shape: POST `{username, password}` → `{token}`). Reuses
43
+ * `Seed_Client.login` but hands the token back for `useToken`.
44
+ */
45
+ let viaLoginEndpoint = (~loginEndpoint: string) => async (
46
+ ~username: string,
47
+ ~password: string,
48
+ ): string => {
49
+ let client = Seed_Client.make(
50
+ ~config={endpoint: loginEndpoint, loginEndpoint, username, password},
51
+ )
52
+ await Seed_Client.login(client)
53
+ switch Seed_Client.currentToken(client) {
54
+ | Some(token) => token
55
+ | None => throw(Failed(`login at ${loginEndpoint} returned no token`))
56
+ }
57
+ }
58
+
59
+ let envOr = (key: string, fallback: string): string =>
60
+ Seed_Prompt.envValue(key)->Option.getOr(fallback)
61
+
62
+ /**
63
+ * A ready `connect` thunk for the local dev platform, with the localhost
64
+ * defaults baked in and overridable per endpoint (arg, then env var, then
65
+ * default). Empty input at the credential prompt falls back to `admin`/`admin`.
66
+ */
67
+ let local = (~graphql=?, ~upload=?, ~login=?, ()): (unit => promise<connection>) => {
68
+ let endpoint =
69
+ graphql->Option.getOr(envOr("REVENTLESS_GRAPHQL_ENDPOINT", "http://localhost:4000/graphql"))
70
+ let uploadEndpoint =
71
+ upload->Option.getOr(
72
+ envOr("REVENTLESS_UPLOAD_ENDPOINT", "http://localhost:4000/__inmemory/upload"),
73
+ )
74
+ let loginEndpoint =
75
+ login->Option.getOr(envOr("REVENTLESS_LOGIN_ENDPOINT", "http://localhost:4000/__inmemory/login"))
76
+ () =>
77
+ make(
78
+ ~label="local",
79
+ ~endpoint,
80
+ ~uploadEndpoint,
81
+ ~login=viaLoginEndpoint(~loginEndpoint),
82
+ ~localDefaults=true,
83
+ )
84
+ }
@@ -0,0 +1,62 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+
3
+ import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
4
+ import * as Seed_Types$ReventlessSeed from "./Seed_Types.res.mjs";
5
+ import * as Seed_Client$ReventlessSeed from "./Seed_Client.res.mjs";
6
+ import * as Seed_Prompt$ReventlessSeed from "./Seed_Prompt.res.mjs";
7
+
8
+ async function make(label, endpoint, uploadEndpoint, login, localDefaultsOpt) {
9
+ let localDefaults = localDefaultsOpt !== undefined ? localDefaultsOpt : false;
10
+ let uploadEndpoint$1 = Stdlib_Option.isSome(Seed_Prompt$ReventlessSeed.envValue("SEED_SKIP_UPLOADS")) ? "" : uploadEndpoint;
11
+ let match = await Seed_Prompt$ReventlessSeed.credentials(localDefaults);
12
+ let token = await login(match[0], match[1]);
13
+ let client = Seed_Client$ReventlessSeed.make({
14
+ endpoint: endpoint
15
+ });
16
+ Seed_Client$ReventlessSeed.useToken(client, token);
17
+ return {
18
+ client: client,
19
+ uploadEndpoint: uploadEndpoint$1,
20
+ label: label
21
+ };
22
+ }
23
+
24
+ function viaLoginEndpoint(loginEndpoint) {
25
+ return async (username, password) => {
26
+ let client = Seed_Client$ReventlessSeed.make({
27
+ endpoint: loginEndpoint,
28
+ loginEndpoint: loginEndpoint,
29
+ username: username,
30
+ password: password
31
+ });
32
+ await Seed_Client$ReventlessSeed.login(client);
33
+ let token = Seed_Client$ReventlessSeed.currentToken(client);
34
+ if (token !== undefined) {
35
+ return token;
36
+ }
37
+ throw {
38
+ RE_EXN_ID: Seed_Types$ReventlessSeed.Failed,
39
+ _1: `login at ` + loginEndpoint + ` returned no token`,
40
+ Error: new Error()
41
+ };
42
+ };
43
+ }
44
+
45
+ function envOr(key, fallback) {
46
+ return Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue(key), fallback);
47
+ }
48
+
49
+ function local(graphql, upload, login, param) {
50
+ let endpoint = Stdlib_Option.getOr(graphql, Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue("REVENTLESS_GRAPHQL_ENDPOINT"), "http://localhost:4000/graphql"));
51
+ let uploadEndpoint = Stdlib_Option.getOr(upload, Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue("REVENTLESS_UPLOAD_ENDPOINT"), "http://localhost:4000/__inmemory/upload"));
52
+ let loginEndpoint = Stdlib_Option.getOr(login, Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue("REVENTLESS_LOGIN_ENDPOINT"), "http://localhost:4000/__inmemory/login"));
53
+ return () => make("local", endpoint, uploadEndpoint, viaLoginEndpoint(loginEndpoint), true);
54
+ }
55
+
56
+ export {
57
+ make,
58
+ viaLoginEndpoint,
59
+ envOr,
60
+ local,
61
+ }
62
+ /* Seed_Prompt-ReventlessSeed Not a pure module */
Binary file
@@ -0,0 +1,188 @@
1
+ // One shared readline interface for every interactive prompt in a seed run.
2
+ //
3
+ // The reason this module exists: creating and closing a fresh `readline`
4
+ // interface per prompt leaves stdin paused after the first `close`, so the
5
+ // second prompt (password after username) never fires and the run hangs. A
6
+ // single interface, created lazily and closed exactly once, avoids that — and a
7
+ // `muted` gate on its output lets password entry reuse the same interface
8
+ // instead of opening a second one just to suppress the echo.
9
+ //
10
+ // Node bindings are declared here as typed externals; this package is
11
+ // stdlib-only, so no `%raw`.
12
+
13
+ open Seed_Types
14
+
15
+ // ── Node bindings ─────────────────────────────────────────────────────────────
16
+
17
+ type stream
18
+ @val @scope("process") external stdin: stream = "stdin"
19
+ @val @scope("process") external stdout: stream = "stdout"
20
+ @send external writeStream: (stream, string) => unit = "write"
21
+ @get external isTTY: stream => bool = "isTTY"
22
+ @send external pauseStream: stream => unit = "pause"
23
+ @send external unrefStream: stream => unit = "unref"
24
+
25
+ @scope("process") @val external processEnv: dict<string> = "env"
26
+
27
+ type rl
28
+ type rlOptions = {input: stream, output: stream, terminal?: bool}
29
+ @module("node:readline") external createInterface: rlOptions => rl = "createInterface"
30
+ @send external question: (rl, string, string => unit) => unit = "question"
31
+ @send external closeRl: rl => unit = "close"
32
+ @set external setWriteToOutput: (rl, string => unit) => unit = "_writeToOutput"
33
+
34
+ // ── Shared interface ──────────────────────────────────────────────────────────
35
+
36
+ // Created on first use, closed once via `close`. `muted` gates the echo so the
37
+ // same interface serves both visible and hidden prompts.
38
+ let rlRef: ref<option<rl>> = ref(None)
39
+ let muted = ref(false)
40
+
41
+ let iface = (): rl =>
42
+ switch rlRef.contents {
43
+ | Some(rl) => rl
44
+ | None =>
45
+ let rl = createInterface({input: stdin, output: stdout, terminal: true})
46
+ rl->setWriteToOutput(s =>
47
+ if !muted.contents {
48
+ stdout->writeStream(s)
49
+ }
50
+ )
51
+ rlRef := Some(rl)
52
+ rl
53
+ }
54
+
55
+ let close = (): unit =>
56
+ switch rlRef.contents {
57
+ | Some(rl) =>
58
+ rl->closeRl
59
+ rlRef := None
60
+ // Closing the interface is not enough: a terminal-mode readline leaves
61
+ // stdin resumed and reffed, which keeps the event loop alive so the CLI
62
+ // never exits after seeding. Release it explicitly.
63
+ stdin->pauseStream
64
+ stdin->unrefStream
65
+ | None => ()
66
+ }
67
+
68
+ // ── Env + TTY ─────────────────────────────────────────────────────────────────
69
+
70
+ let envValue = (key: string): option<string> =>
71
+ switch processEnv->Dict.get(key) {
72
+ | Some(v) if v->String.trim != "" => Some(v->String.trim)
73
+ | _ => None
74
+ }
75
+
76
+ let requireTty = (): unit =>
77
+ if !(stdin->isTTY) {
78
+ throw(
79
+ Failed(
80
+ "no TTY for an interactive prompt — set the documented SEED_* / REVENTLESS_DEMO_* " ++
81
+ "env vars to run non-interactively.",
82
+ ),
83
+ )
84
+ }
85
+
86
+ // ── Prompts ───────────────────────────────────────────────────────────────────
87
+
88
+ let ask = (query: string): promise<string> =>
89
+ Promise.make((resolve, _) => {
90
+ let rl = iface()
91
+ rl->question(query, answer => resolve(answer->String.trim))
92
+ })
93
+
94
+ // Same interface, echo suppressed for the keystrokes. `question` writes the
95
+ // prompt synchronously first (while unmuted), then muting is switched on so only
96
+ // the typed characters are hidden — the canonical readline recipe, which keeps
97
+ // the line editor's cursor state consistent (a manual write + empty `question`
98
+ // left some terminals unable to read the next line).
99
+ let askHidden = (query: string): promise<string> =>
100
+ Promise.make((resolve, _) => {
101
+ let rl = iface()
102
+ rl->question(query, answer => {
103
+ muted := false
104
+ stdout->writeStream("\n")
105
+ resolve(answer)
106
+ })
107
+ muted := true
108
+ })
109
+
110
+ /**
111
+ * Chooses one of `options` (a label paired with the value it selects).
112
+ *
113
+ * Auto-returns when there is a single option, so a lone data set or stack needs
114
+ * no keypress. `env` (e.g. `SEED_SET`) preselects by matching label or 1-based
115
+ * index, so CI runs without a TTY. Otherwise it prints a numbered menu.
116
+ */
117
+ let select = async (
118
+ ~title: string,
119
+ ~options: array<(string, 'a)>,
120
+ ~env=?,
121
+ ): 'a =>
122
+ switch options {
123
+ | [(_, only)] => only
124
+ | _ =>
125
+ let fromEnv =
126
+ env
127
+ ->Option.flatMap(envValue)
128
+ ->Option.flatMap(v =>
129
+ switch options->Array.findIndex(((label, _)) => label == v) {
130
+ | -1 =>
131
+ switch Int.fromString(v) {
132
+ | Some(n) if n >= 1 && n <= options->Array.length => options->Array.get(n - 1)
133
+ | _ => None
134
+ }
135
+ | i => options->Array.get(i)
136
+ }
137
+ )
138
+ switch fromEnv {
139
+ | Some((_, value)) => value
140
+ | None =>
141
+ requireTty()
142
+ Console.log("")
143
+ Console.log(title)
144
+ Console.log("")
145
+ options->Array.forEachWithIndex(((label, _), i) =>
146
+ Console.log(` ${(i + 1)->Int.toString}) ${label}`)
147
+ )
148
+ let rec pick = async (): 'a => {
149
+ let answer = await ask(`\nSelect [1-${(options->Array.length)->Int.toString}]: `)
150
+ switch Int.fromString(answer) {
151
+ | Some(n) if n >= 1 && n <= options->Array.length =>
152
+ let (_, value) = options->Array.getUnsafe(n - 1)
153
+ value
154
+ | _ =>
155
+ Console.log(" Not a valid choice.")
156
+ await pick()
157
+ }
158
+ }
159
+ await pick()
160
+ }
161
+ }
162
+
163
+ /**
164
+ * Prompts for a username (visible) and password (echo muted).
165
+ *
166
+ * `REVENTLESS_DEMO_USER` / `REVENTLESS_DEMO_PASSWORD` skip the prompts. With
167
+ * `localDefaults`, empty input falls back to `admin`/`admin` — the dev login.
168
+ */
169
+ let credentials = async (~localDefaults: bool=false): (string, string) => {
170
+ let username = switch envValue("REVENTLESS_DEMO_USER") {
171
+ | Some(u) => u
172
+ | None =>
173
+ requireTty()
174
+ let entered = await ask(localDefaults ? "Username [admin]: " : "Username: ")
175
+ entered == "" && localDefaults ? "admin" : entered
176
+ }
177
+ let password = switch envValue("REVENTLESS_DEMO_PASSWORD") {
178
+ | Some(p) => p
179
+ | None =>
180
+ requireTty()
181
+ let entered = await askHidden(localDefaults ? "Password [admin]: " : "Password: ")
182
+ entered == "" && localDefaults ? "admin" : entered
183
+ }
184
+ if username == "" || password == "" {
185
+ throw(Failed("username and password are required."))
186
+ }
187
+ (username, password)
188
+ }