@reventlessdev/reventless-seed 1.0.0-alpha.16 → 1.0.0-alpha.17
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.
- package/CHANGELOG.md +7 -0
- package/lib/bs/.compiler.log +2 -2
- package/lib/bs/ReventlessSeed.cmi +0 -0
- package/lib/bs/ReventlessSeed.cmt +0 -0
- package/lib/bs/ReventlessSeed.mlmap +2 -0
- package/lib/bs/compiler-info.json +1 -1
- package/lib/bs/src/Seed-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed.ast +0 -0
- package/lib/bs/src/Seed.res +1 -0
- package/lib/bs/src/Seed.res.mjs +3 -0
- package/lib/bs/src/Seed_Connect-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Connect-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Prompt.ast +0 -0
- package/lib/bs/src/Seed_Prompt.res +82 -15
- package/lib/bs/src/Seed_Prompt.res.mjs +59 -7
- package/lib/bs/src/Seed_Runner-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Runner-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Runner.res.mjs +1 -1
- package/lib/bs/src/Seed_Upload-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Upload-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Users-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Users-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Users-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Users.ast +0 -0
- package/lib/bs/src/Seed_Users.res +80 -0
- package/lib/bs/src/Seed_Users.res.mjs +90 -0
- package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmi +0 -0
- package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmt +0 -0
- package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmi +0 -0
- package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmj +0 -0
- package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmt +0 -0
- package/lib/bs/tests/Seed_UsersTest.ast +0 -0
- package/lib/bs/tests/Seed_UsersTest.res +92 -0
- package/lib/bs/tests/Seed_UsersTest.res.mjs +95 -0
- package/lib/ocaml/.compiler.log +2 -2
- package/lib/ocaml/ReventlessSeed.cmi +0 -0
- package/lib/ocaml/ReventlessSeed.cmt +0 -0
- package/lib/ocaml/ReventlessSeed.mlmap +2 -0
- package/lib/ocaml/Seed-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed.ast +0 -0
- package/lib/ocaml/Seed.res +1 -0
- package/lib/ocaml/Seed_Connect-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Connect-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Prompt.ast +0 -0
- package/lib/ocaml/Seed_Prompt.res +82 -15
- package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Runner-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Runner-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Upload-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Upload-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Users-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Users-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Users-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Users.ast +0 -0
- package/lib/ocaml/Seed_Users.res +80 -0
- package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_UsersTest.ast +0 -0
- package/lib/ocaml/Seed_UsersTest.res +92 -0
- package/package.json +2 -1
- package/src/Seed.res +1 -0
- package/src/Seed.res.mjs +3 -0
- package/src/Seed_Prompt.res +82 -15
- package/src/Seed_Prompt.res.mjs +59 -7
- package/src/Seed_Runner.res.mjs +1 -1
- package/src/Seed_Users.res +80 -0
- package/src/Seed_Users.res.mjs +90 -0
- package/tests/Seed_UsersTest.res +92 -0
- package/tests/Seed_UsersTest.res.mjs +95 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Seed_Users$ReventlessSeed from "../src/Seed_Users.res.mjs";
|
|
4
|
+
|
|
5
|
+
let document = `
|
|
6
|
+
# Local dev users — a comment block like the real file carries.
|
|
7
|
+
|
|
8
|
+
- username: admin
|
|
9
|
+
password: admin
|
|
10
|
+
groups: [Admin, Shopper]
|
|
11
|
+
userId: local-admin
|
|
12
|
+
|
|
13
|
+
- username: shopper
|
|
14
|
+
password: Shopper-2026!
|
|
15
|
+
groups: [Shopper]
|
|
16
|
+
|
|
17
|
+
- username: noGroups
|
|
18
|
+
password: pw
|
|
19
|
+
`;
|
|
20
|
+
|
|
21
|
+
globalThis.describe("Seed_Users.parseString:", () => {
|
|
22
|
+
globalThis.test("reads every account, in the order the file defines them", () => {
|
|
23
|
+
let users = Seed_Users$ReventlessSeed.parseString(document);
|
|
24
|
+
globalThis.expect(users.map(u => u.username)).toEqual([
|
|
25
|
+
"admin",
|
|
26
|
+
"shopper",
|
|
27
|
+
"noGroups"
|
|
28
|
+
]);
|
|
29
|
+
});
|
|
30
|
+
globalThis.test("carries the password beside the username", () => {
|
|
31
|
+
let users = Seed_Users$ReventlessSeed.parseString(document);
|
|
32
|
+
globalThis.expect(users.map(u => u.password)).toEqual([
|
|
33
|
+
"admin",
|
|
34
|
+
"Shopper-2026!",
|
|
35
|
+
"pw"
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
globalThis.test("keeps the groups, and reads a missing groups key as none", () => {
|
|
39
|
+
let users = Seed_Users$ReventlessSeed.parseString(document);
|
|
40
|
+
globalThis.expect(users.map(u => u.groups)).toEqual([
|
|
41
|
+
[
|
|
42
|
+
"Admin",
|
|
43
|
+
"Shopper"
|
|
44
|
+
],
|
|
45
|
+
["Shopper"],
|
|
46
|
+
[]
|
|
47
|
+
]);
|
|
48
|
+
});
|
|
49
|
+
globalThis.test("skips an entry that names no password", () => {
|
|
50
|
+
let users = Seed_Users$ReventlessSeed.parseString(`
|
|
51
|
+
- username: admin
|
|
52
|
+
password: admin
|
|
53
|
+
- username: halfWritten
|
|
54
|
+
groups: [Shopper]
|
|
55
|
+
`);
|
|
56
|
+
globalThis.expect(users.map(u => u.username)).toEqual(["admin"]);
|
|
57
|
+
});
|
|
58
|
+
globalThis.test("yields no accounts for a document that is not a list", () => {
|
|
59
|
+
globalThis.expect(Seed_Users$ReventlessSeed.parseString("users: {}").length).toBe(0);
|
|
60
|
+
});
|
|
61
|
+
globalThis.test("yields no accounts for malformed YAML instead of throwing", () => {
|
|
62
|
+
globalThis.expect(Seed_Users$ReventlessSeed.parseString("- username: [unclosed").length).toBe(0);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
globalThis.describe("Seed_Users.load:", () => {
|
|
67
|
+
globalThis.test("is silent about a file that is not there", () => {
|
|
68
|
+
globalThis.expect(Seed_Users$ReventlessSeed.load("/nonexistent/.reventless/users.yaml")).toBe(undefined);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
globalThis.describe("Seed_Users.label:", () => {
|
|
73
|
+
globalThis.test("names the groups an account carries", () => {
|
|
74
|
+
globalThis.expect(Seed_Users$ReventlessSeed.label({
|
|
75
|
+
username: "admin",
|
|
76
|
+
password: "x",
|
|
77
|
+
groups: [
|
|
78
|
+
"Admin",
|
|
79
|
+
"Shopper"
|
|
80
|
+
]
|
|
81
|
+
})).toBe("admin [Admin, Shopper]");
|
|
82
|
+
});
|
|
83
|
+
globalThis.test("is the bare username when the file records no groups", () => {
|
|
84
|
+
globalThis.expect(Seed_Users$ReventlessSeed.label({
|
|
85
|
+
username: "admin",
|
|
86
|
+
password: "x",
|
|
87
|
+
groups: []
|
|
88
|
+
})).toBe("admin");
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
export {
|
|
93
|
+
document,
|
|
94
|
+
}
|
|
95
|
+
/* Not a pure module */
|
package/lib/ocaml/.compiler.log
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
#Start(
|
|
2
|
-
#Done(
|
|
1
|
+
#Start(1787252810629)
|
|
2
|
+
#Done(1787252810671)
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/lib/ocaml/Seed.ast
CHANGED
|
Binary file
|
package/lib/ocaml/Seed.res
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -121,11 +121,15 @@ let isAffirmative = (answer: string): bool =>
|
|
|
121
121
|
* Auto-returns when there is a single option, so a lone data set or stack needs
|
|
122
122
|
* no keypress. `env` (e.g. `SEED_SET`) preselects by matching label or 1-based
|
|
123
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.
|
|
124
127
|
*/
|
|
125
128
|
let select = async (
|
|
126
129
|
~title: string,
|
|
127
130
|
~options: array<(string, 'a)>,
|
|
128
131
|
~env=?,
|
|
132
|
+
~defaultIndex=?,
|
|
129
133
|
): 'a =>
|
|
130
134
|
switch options {
|
|
131
135
|
| [(_, only)] => only
|
|
@@ -153,29 +157,38 @@ let select = async (
|
|
|
153
157
|
options->Array.forEachWithIndex(((label, _), i) =>
|
|
154
158
|
Console.log(` ${(i + 1)->Int.toString}) ${label}`)
|
|
155
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
|
+
}
|
|
156
165
|
let rec pick = async (): 'a => {
|
|
157
|
-
let answer = await ask(
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
166
|
+
let answer = await ask(
|
|
167
|
+
`\nSelect [1-${(options->Array.length)->Int.toString}]${hint}: `,
|
|
168
|
+
)
|
|
169
|
+
switch (answer, fallback) {
|
|
170
|
+
| ("", Some((_, value))) => value
|
|
162
171
|
| _ =>
|
|
163
|
-
|
|
164
|
-
|
|
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
|
+
}
|
|
165
180
|
}
|
|
166
181
|
}
|
|
167
182
|
await pick()
|
|
168
183
|
}
|
|
169
184
|
}
|
|
170
185
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
let credentials = async (~localDefaults: bool=false): (string, string) => {
|
|
178
|
-
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 {
|
|
179
192
|
| Some(u) => u
|
|
180
193
|
| None =>
|
|
181
194
|
requireTty()
|
|
@@ -189,6 +202,60 @@ let credentials = async (~localDefaults: bool=false): (string, string) => {
|
|
|
189
202
|
let entered = await askHidden(localDefaults ? "Password [admin]: " : "Password: ")
|
|
190
203
|
entered == "" && localDefaults ? "admin" : entered
|
|
191
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
|
+
}
|
|
192
259
|
if username == "" || password == "" {
|
|
193
260
|
throw(Failed("username and password are required."))
|
|
194
261
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// The dev accounts a seed run can log in as, read from `.reventless/users.yaml`.
|
|
2
|
+
//
|
|
3
|
+
// Both platforms keep that file beside the package the seed runs from: locally
|
|
4
|
+
// the in-memory auth adapter loads it at startup, and on AWS it is the record of
|
|
5
|
+
// what was created in the Cognito pool. Either way it already holds the
|
|
6
|
+
// credentials an operator would otherwise retype, so the credential prompt
|
|
7
|
+
// offers its entries instead of asking for a password nobody remembers.
|
|
8
|
+
//
|
|
9
|
+
// Absence is normal and silent — the caller falls back to asking. Nothing here
|
|
10
|
+
// throws: a missing, unreadable or malformed file yields no accounts.
|
|
11
|
+
|
|
12
|
+
@module("yaml") external parseYaml: string => JSON.t = "parse"
|
|
13
|
+
|
|
14
|
+
/** One account: what to log in as, and what to send with it. */
|
|
15
|
+
type user = {username: string, password: string, groups: array<string>}
|
|
16
|
+
|
|
17
|
+
let asString = (json: JSON.t): option<string> =>
|
|
18
|
+
switch json {
|
|
19
|
+
| String(s) => Some(s)
|
|
20
|
+
| _ => None
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let userOf = (json: JSON.t): option<user> =>
|
|
24
|
+
switch json {
|
|
25
|
+
| Object(obj) =>
|
|
26
|
+
switch (
|
|
27
|
+
obj->Dict.get("username")->Option.flatMap(asString),
|
|
28
|
+
obj->Dict.get("password")->Option.flatMap(asString),
|
|
29
|
+
) {
|
|
30
|
+
| (Some(username), Some(password)) =>
|
|
31
|
+
let groups = switch obj->Dict.get("groups") {
|
|
32
|
+
| Some(JSON.Array(items)) => items->Array.filterMap(asString)
|
|
33
|
+
| _ => []
|
|
34
|
+
}
|
|
35
|
+
Some({username, password, groups})
|
|
36
|
+
| _ => None
|
|
37
|
+
}
|
|
38
|
+
| _ => None
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The accounts a users.yaml document defines, in the order it defines them —
|
|
43
|
+
* the order is what makes "the first one" a stable default.
|
|
44
|
+
*
|
|
45
|
+
* An entry without both a username and a password is skipped rather than
|
|
46
|
+
* rejected: the file is hand-maintained, and one incomplete entry should not
|
|
47
|
+
* cost the operator the rest of the list.
|
|
48
|
+
*/
|
|
49
|
+
let parseString = (yamlText: string): array<user> => {
|
|
50
|
+
let doc = try parseYaml(yamlText) catch {
|
|
51
|
+
| _ => JSON.Null
|
|
52
|
+
}
|
|
53
|
+
switch doc {
|
|
54
|
+
| Array(items) => items->Array.filterMap(userOf)
|
|
55
|
+
| _ => []
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Where the file lives for the platform being seeded — the seed runs from that
|
|
60
|
+
package's directory, which is the same cwd the local auth adapter reads. */
|
|
61
|
+
let defaultPath = (): string => NodePath.join([NodeProcess.cwd(), ".reventless", "users.yaml"])
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The accounts on disk, with the path they came from. `None` when there is no
|
|
65
|
+
* readable file with at least one usable account, which the caller reads as
|
|
66
|
+
* "ask for credentials instead".
|
|
67
|
+
*/
|
|
68
|
+
let load = (~path: option<string>=?): option<(string, array<user>)> => {
|
|
69
|
+
let file = path->Option.getOr(defaultPath())
|
|
70
|
+
let users = try {
|
|
71
|
+
NodeFs.existsSync(file) ? parseString(NodeFs.readFileSync(file)) : []
|
|
72
|
+
} catch {
|
|
73
|
+
| _ => []
|
|
74
|
+
}
|
|
75
|
+
users->Array.length == 0 ? None : Some((file, users))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Menu label: the username, plus whatever groups the file records for it. */
|
|
79
|
+
let label = (u: user): string =>
|
|
80
|
+
u.groups->Array.length == 0 ? u.username : `${u.username} [${u.groups->Array.join(", ")}]`
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
open JestGlobals
|
|
2
|
+
|
|
3
|
+
// `.reventless/users.yaml` is hand-maintained and shared with the auth adapter,
|
|
4
|
+
// so the seed reads it defensively: it must survive the comment blocks both
|
|
5
|
+
// copies carry, must not reorder the accounts (the first one is the prompt's
|
|
6
|
+
// default, so order IS the contract), and must never throw — a broken file costs
|
|
7
|
+
// the menu, not the run.
|
|
8
|
+
|
|
9
|
+
let document = `
|
|
10
|
+
# Local dev users — a comment block like the real file carries.
|
|
11
|
+
|
|
12
|
+
- username: admin
|
|
13
|
+
password: admin
|
|
14
|
+
groups: [Admin, Shopper]
|
|
15
|
+
userId: local-admin
|
|
16
|
+
|
|
17
|
+
- username: shopper
|
|
18
|
+
password: Shopper-2026!
|
|
19
|
+
groups: [Shopper]
|
|
20
|
+
|
|
21
|
+
- username: noGroups
|
|
22
|
+
password: pw
|
|
23
|
+
`
|
|
24
|
+
|
|
25
|
+
describe("Seed_Users.parseString:", () => {
|
|
26
|
+
testSync("reads every account, in the order the file defines them", () => {
|
|
27
|
+
let users = Seed_Users.parseString(document)
|
|
28
|
+
expect(users->Array.map(u => u.Seed_Users.username))->Expect.toEqual([
|
|
29
|
+
"admin",
|
|
30
|
+
"shopper",
|
|
31
|
+
"noGroups",
|
|
32
|
+
])
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
testSync("carries the password beside the username", () => {
|
|
36
|
+
let users = Seed_Users.parseString(document)
|
|
37
|
+
expect(users->Array.map(u => u.Seed_Users.password))->Expect.toEqual([
|
|
38
|
+
"admin",
|
|
39
|
+
"Shopper-2026!",
|
|
40
|
+
"pw",
|
|
41
|
+
])
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
testSync("keeps the groups, and reads a missing groups key as none", () => {
|
|
45
|
+
let users = Seed_Users.parseString(document)
|
|
46
|
+
expect(users->Array.map(u => u.Seed_Users.groups))->Expect.toEqual([
|
|
47
|
+
["Admin", "Shopper"],
|
|
48
|
+
["Shopper"],
|
|
49
|
+
[],
|
|
50
|
+
])
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
// An entry half-written by hand is the likely flaw in this file. Dropping it
|
|
54
|
+
// keeps the other accounts selectable; failing the parse would not.
|
|
55
|
+
testSync("skips an entry that names no password", () => {
|
|
56
|
+
let users = Seed_Users.parseString(`
|
|
57
|
+
- username: admin
|
|
58
|
+
password: admin
|
|
59
|
+
- username: halfWritten
|
|
60
|
+
groups: [Shopper]
|
|
61
|
+
`)
|
|
62
|
+
expect(users->Array.map(u => u.Seed_Users.username))->Expect.toEqual(["admin"])
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
testSync("yields no accounts for a document that is not a list", () => {
|
|
66
|
+
expect(Seed_Users.parseString("users: {}")->Array.length)->Expect.toBe(0)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
// The whole point of returning accounts rather than raising: the caller falls
|
|
70
|
+
// back to prompting, and it can only do that if nothing here escapes.
|
|
71
|
+
testSync("yields no accounts for malformed YAML instead of throwing", () => {
|
|
72
|
+
expect(Seed_Users.parseString("- username: [unclosed")->Array.length)->Expect.toBe(0)
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
describe("Seed_Users.load:", () => {
|
|
77
|
+
testSync("is silent about a file that is not there", () => {
|
|
78
|
+
expect(Seed_Users.load(~path="/nonexistent/.reventless/users.yaml"))->Expect.toBe(None)
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
describe("Seed_Users.label:", () => {
|
|
83
|
+
testSync("names the groups an account carries", () => {
|
|
84
|
+
expect(
|
|
85
|
+
Seed_Users.label({username: "admin", password: "x", groups: ["Admin", "Shopper"]}),
|
|
86
|
+
)->Expect.toBe("admin [Admin, Shopper]")
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
testSync("is the bare username when the file records no groups", () => {
|
|
90
|
+
expect(Seed_Users.label({username: "admin", password: "x", groups: []}))->Expect.toBe("admin")
|
|
91
|
+
})
|
|
92
|
+
})
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reventlessdev/reventless-seed",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.17",
|
|
4
4
|
"description": "Reusable harness for seeding a Reventless platform through its public GraphQL command API",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"dependencies": {
|
|
7
|
+
"yaml": "^2.8.3",
|
|
7
8
|
"@reventlessdev/rescript-node": "2.0.0-alpha.8"
|
|
8
9
|
},
|
|
9
10
|
"devDependencies": {
|
package/src/Seed.res
CHANGED
package/src/Seed.res.mjs
CHANGED
package/src/Seed_Prompt.res
CHANGED
|
@@ -121,11 +121,15 @@ let isAffirmative = (answer: string): bool =>
|
|
|
121
121
|
* Auto-returns when there is a single option, so a lone data set or stack needs
|
|
122
122
|
* no keypress. `env` (e.g. `SEED_SET`) preselects by matching label or 1-based
|
|
123
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.
|
|
124
127
|
*/
|
|
125
128
|
let select = async (
|
|
126
129
|
~title: string,
|
|
127
130
|
~options: array<(string, 'a)>,
|
|
128
131
|
~env=?,
|
|
132
|
+
~defaultIndex=?,
|
|
129
133
|
): 'a =>
|
|
130
134
|
switch options {
|
|
131
135
|
| [(_, only)] => only
|
|
@@ -153,29 +157,38 @@ let select = async (
|
|
|
153
157
|
options->Array.forEachWithIndex(((label, _), i) =>
|
|
154
158
|
Console.log(` ${(i + 1)->Int.toString}) ${label}`)
|
|
155
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
|
+
}
|
|
156
165
|
let rec pick = async (): 'a => {
|
|
157
|
-
let answer = await ask(
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
166
|
+
let answer = await ask(
|
|
167
|
+
`\nSelect [1-${(options->Array.length)->Int.toString}]${hint}: `,
|
|
168
|
+
)
|
|
169
|
+
switch (answer, fallback) {
|
|
170
|
+
| ("", Some((_, value))) => value
|
|
162
171
|
| _ =>
|
|
163
|
-
|
|
164
|
-
|
|
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
|
+
}
|
|
165
180
|
}
|
|
166
181
|
}
|
|
167
182
|
await pick()
|
|
168
183
|
}
|
|
169
184
|
}
|
|
170
185
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
let credentials = async (~localDefaults: bool=false): (string, string) => {
|
|
178
|
-
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 {
|
|
179
192
|
| Some(u) => u
|
|
180
193
|
| None =>
|
|
181
194
|
requireTty()
|
|
@@ -189,6 +202,60 @@ let credentials = async (~localDefaults: bool=false): (string, string) => {
|
|
|
189
202
|
let entered = await askHidden(localDefaults ? "Password [admin]: " : "Password: ")
|
|
190
203
|
entered == "" && localDefaults ? "admin" : entered
|
|
191
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
|
+
}
|
|
192
259
|
if username == "" || password == "" {
|
|
193
260
|
throw(Failed("username and password are required."))
|
|
194
261
|
}
|