@reventlessdev/reventless-seed 1.0.0-alpha.2 → 1.0.0-alpha.20

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 (158) hide show
  1. package/CHANGELOG.md +137 -0
  2. package/lib/bs/.compiler.log +2 -2
  3. package/lib/bs/.sourcedirs.json +1 -0
  4. package/lib/bs/ReventlessSeed.cmi +0 -0
  5. package/lib/bs/ReventlessSeed.cmt +0 -0
  6. package/lib/bs/ReventlessSeed.mlmap +5 -0
  7. package/lib/bs/compiler-info.json +2 -2
  8. package/lib/bs/src/Seed-ReventlessSeed.cmi +0 -0
  9. package/lib/bs/src/Seed-ReventlessSeed.cmt +0 -0
  10. package/lib/bs/src/Seed.ast +0 -0
  11. package/lib/bs/src/Seed.res +6 -3
  12. package/lib/bs/src/Seed.res.mjs +3 -0
  13. package/lib/bs/src/Seed_Client-ReventlessSeed.cmi +0 -0
  14. package/lib/bs/src/Seed_Client-ReventlessSeed.cmj +0 -0
  15. package/lib/bs/src/Seed_Client-ReventlessSeed.cmt +0 -0
  16. package/lib/bs/src/Seed_Client.ast +0 -0
  17. package/lib/bs/src/Seed_Client.res +228 -15
  18. package/lib/bs/src/Seed_Client.res.mjs +196 -23
  19. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmi +0 -0
  20. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmj +0 -0
  21. package/lib/bs/src/Seed_Connect-ReventlessSeed.cmt +0 -0
  22. package/lib/bs/src/Seed_Connect.ast +0 -0
  23. package/lib/bs/src/Seed_Connect.res +27 -18
  24. package/lib/bs/src/Seed_Connect.res.mjs +10 -6
  25. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmi +0 -0
  26. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmj +0 -0
  27. package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmt +0 -0
  28. package/lib/bs/src/Seed_Prompt.ast +0 -0
  29. package/lib/bs/src/Seed_Prompt.res +110 -35
  30. package/lib/bs/src/Seed_Prompt.res.mjs +73 -8
  31. package/lib/bs/src/Seed_Random-ReventlessSeed.cmt +0 -0
  32. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmi +0 -0
  33. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmj +0 -0
  34. package/lib/bs/src/Seed_Runner-ReventlessSeed.cmt +0 -0
  35. package/lib/bs/src/Seed_Runner.ast +0 -0
  36. package/lib/bs/src/Seed_Runner.res +38 -7
  37. package/lib/bs/src/Seed_Runner.res.mjs +19 -1
  38. package/lib/bs/src/Seed_Types-ReventlessSeed.cmt +0 -0
  39. package/lib/bs/src/Seed_Upload-ReventlessSeed.cmi +0 -0
  40. package/lib/bs/src/Seed_Upload-ReventlessSeed.cmj +0 -0
  41. package/lib/bs/src/Seed_Upload-ReventlessSeed.cmt +0 -0
  42. package/lib/bs/src/Seed_Upload.ast +0 -0
  43. package/lib/bs/src/Seed_Upload.res +40 -42
  44. package/lib/bs/src/Seed_Upload.res.mjs +65 -69
  45. package/lib/bs/src/Seed_Users-ReventlessSeed.cmi +0 -0
  46. package/lib/bs/src/Seed_Users-ReventlessSeed.cmj +0 -0
  47. package/lib/bs/src/Seed_Users-ReventlessSeed.cmt +0 -0
  48. package/lib/bs/src/Seed_Users.ast +0 -0
  49. package/lib/bs/src/Seed_Users.res +80 -0
  50. package/lib/bs/src/Seed_Users.res.mjs +90 -0
  51. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmi +0 -0
  52. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmj +0 -0
  53. package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmt +0 -0
  54. package/lib/bs/tests/Seed_ClientTest.ast +0 -0
  55. package/lib/bs/tests/Seed_ClientTest.res +157 -0
  56. package/lib/bs/tests/Seed_ClientTest.res.mjs +167 -0
  57. package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmi +0 -0
  58. package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmj +0 -0
  59. package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmt +0 -0
  60. package/lib/bs/tests/Seed_PromptTest.ast +0 -0
  61. package/lib/bs/tests/Seed_PromptTest.res +59 -0
  62. package/lib/bs/tests/Seed_PromptTest.res.mjs +63 -0
  63. package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmi +0 -0
  64. package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmj +0 -0
  65. package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmt +0 -0
  66. package/lib/bs/tests/Seed_RandomTest.ast +0 -0
  67. package/lib/bs/tests/Seed_RandomTest.res +160 -0
  68. package/lib/bs/tests/Seed_RandomTest.res.mjs +208 -0
  69. package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmi +0 -0
  70. package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmj +0 -0
  71. package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmt +0 -0
  72. package/lib/bs/tests/Seed_UsersTest.ast +0 -0
  73. package/lib/bs/tests/Seed_UsersTest.res +92 -0
  74. package/lib/bs/tests/Seed_UsersTest.res.mjs +95 -0
  75. package/lib/ocaml/.compiler.log +2 -2
  76. package/lib/ocaml/ReventlessSeed.cmi +0 -0
  77. package/lib/ocaml/ReventlessSeed.cmt +0 -0
  78. package/lib/ocaml/ReventlessSeed.mlmap +5 -0
  79. package/lib/ocaml/Seed-ReventlessSeed.cmi +0 -0
  80. package/lib/ocaml/Seed-ReventlessSeed.cmt +0 -0
  81. package/lib/ocaml/Seed.ast +0 -0
  82. package/lib/ocaml/Seed.res +6 -3
  83. package/lib/ocaml/Seed_Client-ReventlessSeed.cmi +0 -0
  84. package/lib/ocaml/Seed_Client-ReventlessSeed.cmj +0 -0
  85. package/lib/ocaml/Seed_Client-ReventlessSeed.cmt +0 -0
  86. package/lib/ocaml/Seed_Client.ast +0 -0
  87. package/lib/ocaml/Seed_Client.res +228 -15
  88. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmi +0 -0
  89. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmj +0 -0
  90. package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmt +0 -0
  91. package/lib/ocaml/Seed_ClientTest.ast +0 -0
  92. package/lib/ocaml/Seed_ClientTest.res +157 -0
  93. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmi +0 -0
  94. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmj +0 -0
  95. package/lib/ocaml/Seed_Connect-ReventlessSeed.cmt +0 -0
  96. package/lib/ocaml/Seed_Connect.ast +0 -0
  97. package/lib/ocaml/Seed_Connect.res +27 -18
  98. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmi +0 -0
  99. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmj +0 -0
  100. package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmt +0 -0
  101. package/lib/ocaml/Seed_Prompt.ast +0 -0
  102. package/lib/ocaml/Seed_Prompt.res +110 -35
  103. package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmi +0 -0
  104. package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmj +0 -0
  105. package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmt +0 -0
  106. package/lib/ocaml/Seed_PromptTest.ast +0 -0
  107. package/lib/ocaml/Seed_PromptTest.res +59 -0
  108. package/lib/ocaml/Seed_Random-ReventlessSeed.cmt +0 -0
  109. package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmi +0 -0
  110. package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmj +0 -0
  111. package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmt +0 -0
  112. package/lib/ocaml/Seed_RandomTest.ast +0 -0
  113. package/lib/ocaml/Seed_RandomTest.res +160 -0
  114. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmi +0 -0
  115. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmj +0 -0
  116. package/lib/ocaml/Seed_Runner-ReventlessSeed.cmt +0 -0
  117. package/lib/ocaml/Seed_Runner.ast +0 -0
  118. package/lib/ocaml/Seed_Runner.res +38 -7
  119. package/lib/ocaml/Seed_Types-ReventlessSeed.cmt +0 -0
  120. package/lib/ocaml/Seed_Upload-ReventlessSeed.cmi +0 -0
  121. package/lib/ocaml/Seed_Upload-ReventlessSeed.cmj +0 -0
  122. package/lib/ocaml/Seed_Upload-ReventlessSeed.cmt +0 -0
  123. package/lib/ocaml/Seed_Upload.ast +0 -0
  124. package/lib/ocaml/Seed_Upload.res +40 -42
  125. package/lib/ocaml/Seed_Users-ReventlessSeed.cmi +0 -0
  126. package/lib/ocaml/Seed_Users-ReventlessSeed.cmj +0 -0
  127. package/lib/ocaml/Seed_Users-ReventlessSeed.cmt +0 -0
  128. package/lib/ocaml/Seed_Users.ast +0 -0
  129. package/lib/ocaml/Seed_Users.res +80 -0
  130. package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmi +0 -0
  131. package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmj +0 -0
  132. package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmt +0 -0
  133. package/lib/ocaml/Seed_UsersTest.ast +0 -0
  134. package/lib/ocaml/Seed_UsersTest.res +92 -0
  135. package/package.json +7 -2
  136. package/rescript.json +9 -1
  137. package/src/Seed.res +6 -3
  138. package/src/Seed.res.mjs +3 -0
  139. package/src/Seed_Client.res +228 -15
  140. package/src/Seed_Client.res.mjs +196 -23
  141. package/src/Seed_Connect.res +27 -18
  142. package/src/Seed_Connect.res.mjs +10 -6
  143. package/src/Seed_Prompt.res +110 -35
  144. package/src/Seed_Prompt.res.mjs +73 -8
  145. package/src/Seed_Runner.res +38 -7
  146. package/src/Seed_Runner.res.mjs +19 -1
  147. package/src/Seed_Upload.res +40 -42
  148. package/src/Seed_Upload.res.mjs +65 -69
  149. package/src/Seed_Users.res +80 -0
  150. package/src/Seed_Users.res.mjs +90 -0
  151. package/tests/Seed_ClientTest.res +157 -0
  152. package/tests/Seed_ClientTest.res.mjs +167 -0
  153. package/tests/Seed_PromptTest.res +59 -0
  154. package/tests/Seed_PromptTest.res.mjs +63 -0
  155. package/tests/Seed_RandomTest.res +160 -0
  156. package/tests/Seed_RandomTest.res.mjs +208 -0
  157. package/tests/Seed_UsersTest.res +92 -0
  158. package/tests/Seed_UsersTest.res.mjs +95 -0
@@ -4,19 +4,24 @@ import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
4
4
  import * as Seed_Types$ReventlessSeed from "./Seed_Types.res.mjs";
5
5
  import * as Seed_Client$ReventlessSeed from "./Seed_Client.res.mjs";
6
6
  import * as Seed_Prompt$ReventlessSeed from "./Seed_Prompt.res.mjs";
7
+ import * as Seed_Upload$ReventlessSeed from "./Seed_Upload.res.mjs";
7
8
 
8
- async function make(label, endpoint, uploadEndpoint, login, localDefaultsOpt) {
9
+ async function make(label, endpoint, login, localDefaultsOpt) {
9
10
  let localDefaults = localDefaultsOpt !== undefined ? localDefaultsOpt : false;
10
- let uploadEndpoint$1 = Stdlib_Option.isSome(Seed_Prompt$ReventlessSeed.envValue("SEED_SKIP_UPLOADS")) ? "" : uploadEndpoint;
11
+ let uploadsSkipped = Seed_Upload$ReventlessSeed.uploadsSkipped();
11
12
  let match = await Seed_Prompt$ReventlessSeed.credentials(localDefaults);
12
13
  let token = await login(match[0], match[1]);
13
14
  let client = Seed_Client$ReventlessSeed.make({
14
15
  endpoint: endpoint
15
16
  });
16
17
  Seed_Client$ReventlessSeed.useToken(client, token);
18
+ let summary = Seed_Client$ReventlessSeed.identitySummary(client);
19
+ if (summary !== undefined) {
20
+ console.log(`Acting as: ` + summary);
21
+ }
17
22
  return {
18
23
  client: client,
19
- uploadEndpoint: uploadEndpoint$1,
24
+ uploadsSkipped: uploadsSkipped,
20
25
  label: label
21
26
  };
22
27
  }
@@ -46,11 +51,10 @@ function envOr(key, fallback) {
46
51
  return Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue(key), fallback);
47
52
  }
48
53
 
49
- function local(graphql, upload, login, param) {
54
+ function local(graphql, login, param) {
50
55
  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
56
  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);
57
+ return () => make("local", endpoint, viaLoginEndpoint(loginEndpoint), true);
54
58
  }
55
59
 
56
60
  export {
@@ -1,7 +1,7 @@
1
1
  // One shared readline interface for every interactive prompt in a seed run.
2
2
  //
3
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
4
+ // interface per prompt leaves NodeProcess.stdin paused after the first `close`, so the
5
5
  // second prompt (password after username) never fires and the run hangs. A
6
6
  // single interface, created lazily and closed exactly once, avoids that — and a
7
7
  // `muted` gate on its output lets password entry reuse the same interface
@@ -14,18 +14,8 @@ open Seed_Types
14
14
 
15
15
  // ── Node bindings ─────────────────────────────────────────────────────────────
16
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
17
  type rl
28
- type rlOptions = {input: stream, output: stream, terminal?: bool}
18
+ type rlOptions = {input: NodeProcess.stream, output: NodeProcess.stream, terminal?: bool}
29
19
  @module("node:readline") external createInterface: rlOptions => rl = "createInterface"
30
20
  @send external question: (rl, string, string => unit) => unit = "question"
31
21
  @send external closeRl: rl => unit = "close"
@@ -42,10 +32,10 @@ let iface = (): rl =>
42
32
  switch rlRef.contents {
43
33
  | Some(rl) => rl
44
34
  | None =>
45
- let rl = createInterface({input: stdin, output: stdout, terminal: true})
35
+ let rl = createInterface({input: NodeProcess.stdin, output: NodeProcess.stdout, terminal: true})
46
36
  rl->setWriteToOutput(s =>
47
37
  if !muted.contents {
48
- stdout->writeStream(s)
38
+ NodeProcess.stdout->NodeProcess.write(s)
49
39
  }
50
40
  )
51
41
  rlRef := Some(rl)
@@ -58,23 +48,23 @@ let close = (): unit =>
58
48
  rl->closeRl
59
49
  rlRef := None
60
50
  // 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
51
+ // NodeProcess.stdin resumed and reffed, which keeps the event loop alive so the CLI
62
52
  // never exits after seeding. Release it explicitly.
63
- stdin->pauseStream
64
- stdin->unrefStream
53
+ NodeProcess.stdin->NodeProcess.pause
54
+ NodeProcess.stdin->NodeProcess.unref
65
55
  | None => ()
66
56
  }
67
57
 
68
58
  // ── Env + TTY ─────────────────────────────────────────────────────────────────
69
59
 
70
60
  let envValue = (key: string): option<string> =>
71
- switch processEnv->Dict.get(key) {
61
+ switch NodeProcess.env->Dict.get(key) {
72
62
  | Some(v) if v->String.trim != "" => Some(v->String.trim)
73
63
  | _ => None
74
64
  }
75
65
 
76
66
  let requireTty = (): unit =>
77
- if !(stdin->isTTY) {
67
+ if !(NodeProcess.stdin->NodeProcess.isTTY->Option.getOr(false)) {
78
68
  throw(
79
69
  Failed(
80
70
  "no TTY for an interactive prompt — set the documented SEED_* / REVENTLESS_DEMO_* " ++
@@ -101,23 +91,45 @@ let askHidden = (query: string): promise<string> =>
101
91
  let rl = iface()
102
92
  rl->question(query, answer => {
103
93
  muted := false
104
- stdout->writeStream("\n")
94
+ NodeProcess.stdout->NodeProcess.write("\n")
105
95
  resolve(answer)
106
96
  })
107
97
  muted := true
108
98
  })
109
99
 
100
+ /**
101
+ * Whether an answer means yes — the one vocabulary, for the typed reply and the
102
+ * env var that stands in for it alike.
103
+ *
104
+ * It exists because those two drifted: a `[y/N]` prompt took `y`, and the env
105
+ * var beside it took only `1` or `yes`, so the obvious `SEED_RESET_CONFIRM=y`
106
+ * silently fell through to the interactive branch — which, on the CI runs the
107
+ * variable exists for, has no TTY and throws. Two spellings of one question are
108
+ * one spelling too many; callers ask this instead of comparing strings.
109
+ *
110
+ * Trimmed and case-folded, so `Yes`/` y ` answer the same as `y`.
111
+ */
112
+ let isAffirmative = (answer: string): bool =>
113
+ switch answer->String.trim->String.toLowerCase {
114
+ | "y" | "yes" | "1" => true
115
+ | _ => false
116
+ }
117
+
110
118
  /**
111
119
  * Chooses one of `options` (a label paired with the value it selects).
112
120
  *
113
121
  * Auto-returns when there is a single option, so a lone data set or stack needs
114
122
  * no keypress. `env` (e.g. `SEED_SET`) preselects by matching label or 1-based
115
123
  * index, so CI runs without a TTY. Otherwise it prints a numbered menu.
124
+ *
125
+ * `defaultIndex` makes Enter a valid answer, selecting that 0-based option;
126
+ * without one every reply must name a choice.
116
127
  */
117
128
  let select = async (
118
129
  ~title: string,
119
130
  ~options: array<(string, 'a)>,
120
131
  ~env=?,
132
+ ~defaultIndex=?,
121
133
  ): 'a =>
122
134
  switch options {
123
135
  | [(_, only)] => only
@@ -145,29 +157,38 @@ let select = async (
145
157
  options->Array.forEachWithIndex(((label, _), i) =>
146
158
  Console.log(` ${(i + 1)->Int.toString}) ${label}`)
147
159
  )
160
+ let fallback = defaultIndex->Option.flatMap(i => options->Array.get(i))
161
+ let hint = switch (defaultIndex, fallback) {
162
+ | (Some(i), Some(_)) => ` (default ${(i + 1)->Int.toString})`
163
+ | _ => ""
164
+ }
148
165
  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
166
+ let answer = await ask(
167
+ `\nSelect [1-${(options->Array.length)->Int.toString}]${hint}: `,
168
+ )
169
+ switch (answer, fallback) {
170
+ | ("", Some((_, value))) => value
154
171
  | _ =>
155
- Console.log(" Not a valid choice.")
156
- await pick()
172
+ switch Int.fromString(answer) {
173
+ | Some(n) if n >= 1 && n <= options->Array.length =>
174
+ let (_, value) = options->Array.getUnsafe(n - 1)
175
+ value
176
+ | _ =>
177
+ Console.log(" Not a valid choice.")
178
+ await pick()
179
+ }
157
180
  }
158
181
  }
159
182
  await pick()
160
183
  }
161
184
  }
162
185
 
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") {
186
+ // Typing both halves is the fallback, not the first offer: it is what happens
187
+ // when the platform keeps no accounts file. `envUser` is threaded in so a
188
+ // `REVENTLESS_DEMO_USER` that named nobody in the file still skips the username
189
+ // prompt rather than asking for a name that was already given.
190
+ let askCredentials = async (~localDefaults: bool, ~envUser: option<string>): (string, string) => {
191
+ let username = switch envUser {
171
192
  | Some(u) => u
172
193
  | None =>
173
194
  requireTty()
@@ -181,6 +202,60 @@ let credentials = async (~localDefaults: bool=false): (string, string) => {
181
202
  let entered = await askHidden(localDefaults ? "Password [admin]: " : "Password: ")
182
203
  entered == "" && localDefaults ? "admin" : entered
183
204
  }
205
+ (username, password)
206
+ }
207
+
208
+ // The accounts file the platform already keeps (`.reventless/users.yaml`, or
209
+ // `SEED_USERS_FILE`). It records the password beside the username, so choosing
210
+ // an account is the whole credential step — nothing is typed.
211
+ //
212
+ // A named `REVENTLESS_DEMO_USER` picks its entry directly; otherwise the file's
213
+ // accounts are offered in the order it defines them, first as the default.
214
+ // `None` means the file has nothing to offer and the caller should ask.
215
+ let fromUsersFile = async (~envUser: option<string>): option<(string, string)> =>
216
+ switch Seed_Users.load(~path=?envValue("SEED_USERS_FILE")) {
217
+ | None => None
218
+ | Some((path, users)) =>
219
+ let chosen = switch envUser {
220
+ | Some(name) => users->Array.find(u => u.username == name)
221
+ | None =>
222
+ Some(
223
+ await select(
224
+ ~title=`User (${path}):`,
225
+ ~options=users->Array.map(u => (Seed_Users.label(u), u)),
226
+ ~env="SEED_USER",
227
+ ~defaultIndex=0,
228
+ ),
229
+ )
230
+ }
231
+ // Logged for every arm, including the single-account file that selects
232
+ // itself without a keypress: which identity seeded the data is the thing an
233
+ // operator checks when owner-scoped rows turn up under the wrong account.
234
+ chosen->Option.map(u => {
235
+ Console.log(`Logging in as ${u.username} (from ${path})`)
236
+ (u.username, u.password)
237
+ })
238
+ }
239
+
240
+ /**
241
+ * Resolves the credentials a seed run logs in with.
242
+ *
243
+ * `REVENTLESS_DEMO_USER` + `REVENTLESS_DEMO_PASSWORD` together are the
244
+ * non-interactive path and skip everything below. Otherwise the platform's
245
+ * `.reventless/users.yaml` supplies the accounts to choose from, and only a
246
+ * platform without one falls back to typing both halves — where, with
247
+ * `localDefaults`, empty input means `admin`/`admin`.
248
+ */
249
+ let credentials = async (~localDefaults: bool=false): (string, string) => {
250
+ let envUser = envValue("REVENTLESS_DEMO_USER")
251
+ let (username, password) = switch (envUser, envValue("REVENTLESS_DEMO_PASSWORD")) {
252
+ | (Some(u), Some(p)) => (u, p)
253
+ | _ =>
254
+ switch await fromUsersFile(~envUser) {
255
+ | Some(pair) => pair
256
+ | None => await askCredentials(~localDefaults, ~envUser)
257
+ }
258
+ }
184
259
  if username == "" || password == "" {
185
260
  throw(Failed("username and password are required."))
186
261
  }
@@ -5,6 +5,7 @@ import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
5
5
  import * as Nodereadline from "node:readline";
6
6
  import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
7
7
  import * as Seed_Types$ReventlessSeed from "./Seed_Types.res.mjs";
8
+ import * as Seed_Users$ReventlessSeed from "./Seed_Users.res.mjs";
8
9
 
9
10
  let rlRef = {
10
11
  contents: undefined
@@ -53,7 +54,7 @@ function envValue(key) {
53
54
  }
54
55
 
55
56
  function requireTty() {
56
- if (process.stdin.isTTY) {
57
+ if (Stdlib_Option.getOr(process.stdin.isTTY, false)) {
57
58
  return;
58
59
  }
59
60
  throw {
@@ -82,7 +83,19 @@ function askHidden(query) {
82
83
  });
83
84
  }
84
85
 
85
- async function select(title, options, env) {
86
+ function isAffirmative(answer) {
87
+ let match = answer.trim().toLowerCase();
88
+ switch (match) {
89
+ case "1" :
90
+ case "y" :
91
+ case "yes" :
92
+ return true;
93
+ default:
94
+ return false;
95
+ }
96
+ }
97
+
98
+ async function select(title, options, env, defaultIndex) {
86
99
  if (options.length === 1) {
87
100
  return options[0][1];
88
101
  }
@@ -106,8 +119,13 @@ async function select(title, options, env) {
106
119
  options.forEach((param, i) => {
107
120
  console.log(` ` + (i + 1 | 0).toString() + `) ` + param[0]);
108
121
  });
122
+ let fallback = Stdlib_Option.flatMap(defaultIndex, i => options[i]);
123
+ let hint = defaultIndex !== undefined && fallback !== undefined ? ` (default ` + (defaultIndex + 1 | 0).toString() + `)` : "";
109
124
  let pick = async () => {
110
- let answer = await ask(`\nSelect [1-` + options.length.toString() + `]: `);
125
+ let answer = await ask(`\nSelect [1-` + options.length.toString() + `]` + hint + `: `);
126
+ if (answer === "" && fallback !== undefined) {
127
+ return fallback[1];
128
+ }
111
129
  let n = Stdlib_Int.fromString(answer, undefined);
112
130
  if (n !== undefined) {
113
131
  if (n >= 1 && n <= options.length) {
@@ -124,12 +142,10 @@ async function select(title, options, env) {
124
142
  return await pick();
125
143
  }
126
144
 
127
- async function credentials(localDefaultsOpt) {
128
- let localDefaults = localDefaultsOpt !== undefined ? localDefaultsOpt : false;
129
- let u = envValue("REVENTLESS_DEMO_USER");
145
+ async function askCredentials(localDefaults, envUser) {
130
146
  let username;
131
- if (u !== undefined) {
132
- username = u;
147
+ if (envUser !== undefined) {
148
+ username = envUser;
133
149
  } else {
134
150
  requireTty();
135
151
  let entered = await ask(localDefaults ? "Username [admin]: " : "Username: ");
@@ -144,6 +160,52 @@ async function credentials(localDefaultsOpt) {
144
160
  let entered$1 = await askHidden(localDefaults ? "Password [admin]: " : "Password: ");
145
161
  password = entered$1 === "" && localDefaults ? "admin" : entered$1;
146
162
  }
163
+ return [
164
+ username,
165
+ password
166
+ ];
167
+ }
168
+
169
+ async function fromUsersFile(envUser) {
170
+ let match = Seed_Users$ReventlessSeed.load(envValue("SEED_USERS_FILE"));
171
+ if (match === undefined) {
172
+ return;
173
+ }
174
+ let users = match[1];
175
+ let path = match[0];
176
+ let chosen = envUser !== undefined ? users.find(u => u.username === envUser) : await select(`User (` + path + `):`, users.map(u => [
177
+ Seed_Users$ReventlessSeed.label(u),
178
+ u
179
+ ]), "SEED_USER", 0);
180
+ return Stdlib_Option.map(chosen, u => {
181
+ console.log(`Logging in as ` + u.username + ` (from ` + path + `)`);
182
+ return [
183
+ u.username,
184
+ u.password
185
+ ];
186
+ });
187
+ }
188
+
189
+ async function credentials(localDefaultsOpt) {
190
+ let localDefaults = localDefaultsOpt !== undefined ? localDefaultsOpt : false;
191
+ let envUser = envValue("REVENTLESS_DEMO_USER");
192
+ let match = envValue("REVENTLESS_DEMO_PASSWORD");
193
+ let match$1;
194
+ let exit = 0;
195
+ if (envUser !== undefined && match !== undefined) {
196
+ match$1 = [
197
+ envUser,
198
+ match
199
+ ];
200
+ } else {
201
+ exit = 1;
202
+ }
203
+ if (exit === 1) {
204
+ let pair = await fromUsersFile(envUser);
205
+ match$1 = pair !== undefined ? pair : await askCredentials(localDefaults, envUser);
206
+ }
207
+ let password = match$1[1];
208
+ let username = match$1[0];
147
209
  if (username === "" || password === "") {
148
210
  throw {
149
211
  RE_EXN_ID: Seed_Types$ReventlessSeed.Failed,
@@ -166,7 +228,10 @@ export {
166
228
  requireTty,
167
229
  ask,
168
230
  askHidden,
231
+ isAffirmative,
169
232
  select,
233
+ askCredentials,
234
+ fromUsersFile,
170
235
  credentials,
171
236
  }
172
237
  /* node:readline Not a pure module */
@@ -2,11 +2,9 @@
2
2
 
3
3
  open Seed_Types
4
4
 
5
- @scope("process") @val external exit: int => unit = "exit"
6
- @scope("process") @val external env: dict<string> = "env"
7
5
 
8
6
  let envOr = (key: string, fallback: string): string =>
9
- env->Dict.get(key)->Option.getOr(fallback)
7
+ NodeProcess.env->Dict.get(key)->Option.getOr(fallback)
10
8
 
11
9
  /** One line of progress per phase, so a slow run never looks hung. */
12
10
  let report = (line: string): unit => Console.log(` ${line}`)
@@ -90,6 +88,33 @@ let warn = (warnings: array<string>): unit =>
90
88
  warnings->Array.forEach(w => Console.log(` - ${w}`))
91
89
  }
92
90
 
91
+ /**
92
+ * Pre-flight: refuse to seed onto a store that already holds data.
93
+ *
94
+ * The harness is a one-shot against a fresh store — a re-run would hit the
95
+ * domain's own duplicate rejections (`CategoryAlreadyExists`, …) and abort
96
+ * mid-phase. Catching it here, before any command is sent, turns that into an
97
+ * honest "nothing was written" startup abort instead of a misleading
98
+ * half-seeded one. It is the inverse of `verifyViews`: seeding refuses to start
99
+ * unless the store reads empty. Stops at the first non-empty probe view, so a
100
+ * populated store aborts after a single query.
101
+ */
102
+ let assertStoreEmpty = async (client: Seed_Client.t, ~probeViews: array<string>): unit =>
103
+ for i in 0 to probeViews->Array.length - 1 {
104
+ switch probeViews->Array.get(i) {
105
+ | Some(name) =>
106
+ let count = await Seed_Client.countNodes(client, ~field=name)
107
+ if count > 0 {
108
+ throw(
109
+ Failed(
110
+ `the target store is not empty — "${name}" already holds ${count->Int.toString} row(s). Seeding is a one-shot against a fresh store; reset the store before re-running.`,
111
+ ),
112
+ )
113
+ }
114
+ | None => ()
115
+ }
116
+ }
117
+
93
118
  /**
94
119
  * Wraps a seed run. On failure it prints the cause and exits non-zero, saying
95
120
  * plainly that the store is now half-seeded — recovery is a reset, not a re-run.
@@ -100,7 +125,7 @@ let run = async (main: unit => promise<unit>): unit =>
100
125
  // Exit cleanly: HTTP keep-alive sockets (and, on the interactive path, the
101
126
  // readline/stdin handle) can otherwise keep the event loop alive so the CLI
102
127
  // appears to hang after a successful run.
103
- exit(0)
128
+ NodeProcess.exit(0)
104
129
  } catch {
105
130
  | Failed(message) =>
106
131
  Console.error("")
@@ -108,7 +133,7 @@ let run = async (main: unit => promise<unit>): unit =>
108
133
  Console.error("fresh store once the cause below is fixed.")
109
134
  Console.error("")
110
135
  Console.error(message)
111
- exit(1)
136
+ NodeProcess.exit(1)
112
137
  | exn =>
113
138
  Console.error("")
114
139
  Console.error("Seeding aborted with an unexpected error:")
@@ -116,7 +141,7 @@ let run = async (main: unit => promise<unit>): unit =>
116
141
  Console.error(
117
142
  exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("unknown"),
118
143
  )
119
- exit(1)
144
+ NodeProcess.exit(1)
120
145
  }
121
146
 
122
147
  /**
@@ -128,6 +153,9 @@ type dataSet = {
128
153
  name: string,
129
154
  label: string,
130
155
  seed: Seed_Connect.connection => promise<unit>,
156
+ // View names probed by the fresh-store guard before any command is sent; a
157
+ // non-empty one aborts the run at startup. Omit to skip the guard.
158
+ probeViews?: array<string>,
131
159
  }
132
160
 
133
161
  /**
@@ -142,7 +170,7 @@ let abortStartup = (message: string): unit => {
142
170
  Console.error("Seeding did not start — nothing was written. Fix the cause below and re-run.")
143
171
  Console.error("")
144
172
  Console.error(message)
145
- exit(1)
173
+ NodeProcess.exit(1)
146
174
  }
147
175
 
148
176
  /**
@@ -179,6 +207,9 @@ let seed = (~sets: array<dataSet>, ~connect: unit => promise<Seed_Connect.connec
179
207
  }
180
208
  let connection = await connect()
181
209
  Seed_Prompt.close()
210
+ // Runs inside this pre-seed `try`, so refusing a non-empty store (or a
211
+ // failed probe query) is reported as "did not start", not "half-seeded".
212
+ await assertStoreEmpty(connection.client, ~probeViews=chosen.probeViews->Option.getOr([]))
182
213
  Some((chosen, connection))
183
214
  } catch {
184
215
  | Failed(message) =>
@@ -83,6 +83,22 @@ function warn(warnings) {
83
83
  }
84
84
  }
85
85
 
86
+ async function assertStoreEmpty(client, probeViews) {
87
+ for (let i = 0, i_finish = probeViews.length; i < i_finish; ++i) {
88
+ let name = probeViews[i];
89
+ if (name !== undefined) {
90
+ let count = await Seed_Client$ReventlessSeed.countNodes(client, name);
91
+ if (count > 0) {
92
+ throw {
93
+ RE_EXN_ID: Seed_Types$ReventlessSeed.Failed,
94
+ _1: `the target store is not empty — "` + name + `" already holds ` + count.toString() + ` row(s). Seeding is a one-shot against a fresh store; reset the store before re-running.`,
95
+ Error: new Error()
96
+ };
97
+ }
98
+ }
99
+ }
100
+ }
101
+
86
102
  async function run(main) {
87
103
  try {
88
104
  await main();
@@ -139,13 +155,14 @@ function seed(sets, connect) {
139
155
  chosen = await Seed_Prompt$ReventlessSeed.select("Data set:", sets.map(s => [
140
156
  s.label,
141
157
  s
142
- ]), undefined);
158
+ ]), undefined, undefined);
143
159
  }
144
160
  } else {
145
161
  chosen = sets[0];
146
162
  }
147
163
  let connection = await connect();
148
164
  Seed_Prompt$ReventlessSeed.close();
165
+ await assertStoreEmpty(connection.client, Stdlib_Option.getOr(chosen.probeViews, []));
149
166
  started = [
150
167
  chosen,
151
168
  connection
@@ -179,6 +196,7 @@ export {
179
196
  verifyViews,
180
197
  unfillableWarnings,
181
198
  warn,
199
+ assertStoreEmpty,
182
200
  run,
183
201
  abortStartup,
184
202
  seed,