@mccaskie-labs/workbench-settings 0.0.0-stage → 0.2.0
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 +25 -0
- package/README.md +46 -2
- package/dist/check.d.ts +27 -0
- package/dist/check.js +176 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +50 -0
- package/dist/design.d.ts +20 -0
- package/dist/design.js +59 -0
- package/dist/email.d.ts +2 -0
- package/dist/email.js +46 -0
- package/dist/errors.d.ts +31 -0
- package/dist/errors.js +14 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +18 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +329 -0
- package/dist/page.d.ts +58 -0
- package/dist/page.js +215 -0
- package/dist/samples.d.ts +18 -0
- package/dist/samples.js +90 -0
- package/dist/settings.d.ts +136 -0
- package/dist/settings.js +286 -0
- package/dist/styles.d.ts +34 -0
- package/dist/styles.js +83 -0
- package/dist/types.d.ts +109 -0
- package/dist/types.js +5 -0
- package/dist/words.d.ts +13 -0
- package/dist/words.js +221 -0
- package/package.json +52 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @mccaskie-labs/workbench-settings
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Design mode's settings (docs/look-studio.md §8):
|
|
8
|
+
|
|
9
|
+
- `designSettings({ looks, schemes, siteLayouts, toolLayouts, check })` declares look, scheme, layout and a `layouts` map keyed by tool or `tool@/path`.
|
|
10
|
+
- A new `map` setting type, shown read only with Put back.
|
|
11
|
+
- A setting's own `check(value)`, run at every save; the words come back on `bad_value` as `error.problem.words`.
|
|
12
|
+
- `carries: true`, with `exportGroup`, `importGroup` and `markCarried`, and the `workbench_settings_carried` table in the one migration, so an approved release takes the test site's design to the live site. The page says "On the test site only" until then.
|
|
13
|
+
- `sameValue`, and three new classes: `wb-settings-carry`, `wb-settings-map` and `wb-settings-map-item`.
|
|
14
|
+
|
|
15
|
+
## 0.1.1
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- `./samples` (`SETTINGS_SAMPLES`): the Settings page with a new address waiting for its code, just saved, and something to fix, drawn by the new `drawSettingsPage` (the page's body from settings already read), for the style guide and the look previewer.
|
|
20
|
+
|
|
21
|
+
## 0.1.0
|
|
22
|
+
|
|
23
|
+
### Minor Changes
|
|
24
|
+
|
|
25
|
+
- First version: one Settings page in the app's admin (`renderSettingsPage`, `settingsAction`), the store in the app's own database (`settingsMigration`, `npx workbench-settings migration`), values worked out as the owner's change over the project's answer over the tool's default (`createSettings`, `settings.get`), Put back, Recent changes kept 90 days, and a new email address proved with an emailed six-digit code (five tries, 24 hours, at most one code a minute and five a day; Cancel; a failed send leaves nothing waiting) before it is used. Email's settings ship here (`EMAIL_SETTINGS`: where replies go, the name emails come from), with `withReplyTo` for tools that take a `business` callback. Words in English, Tagalog and Taglish; every element has a `wb-settings-*` class (`SETTINGS_STYLES`) whose colours, fonts and corners default to the shared theme tokens. The store already takes an object value up to 8 KB, so a later map setting (Design mode's per-tool layouts) needs no new migration.
|
package/README.md
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mccaskie-labs/workbench-settings
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
One Settings page in a Workbench app's admin, where the app's owner changes the settings Workbench's tools allow, with no code change and no redeploy. The design is `docs/project-settings.md` in the Workbench repo.
|
|
4
|
+
|
|
5
|
+
- Each tool declares its owner settings as data (`ToolSettings`). Email's ship here as `EMAIL_SETTINGS`: where customers' replies go, and the name emails come from.
|
|
6
|
+
- A value is the owner's change, else the project's answer (`workbench/<tool>.json`), else the tool's default. Tools read it when they run: `settings.get("email", "reply_to")`, or `withReplyTo(settings, business)` for a tool that takes a `business` callback.
|
|
7
|
+
- Saves are kept in the app's own database with who made them; Recent changes lists them for 90 days, and Put back returns a setting to how the app was built.
|
|
8
|
+
- A new email address is proved first: a six-digit code is emailed to it and the owner types it in. The old address stays until then. Five tries, 24 hours, at most one code a minute and five a day, and only a hash of the code is stored. Cancel drops a waiting change, and a code email that fails to send leaves nothing waiting.
|
|
9
|
+
|
|
10
|
+
## Use
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { createSettings, EMAIL_SETTINGS, renderSettingsPage, settingsAction, withReplyTo } from "@mccaskie-labs/workbench-settings";
|
|
14
|
+
|
|
15
|
+
const settings = createSettings({ db: serviceClient, tools: [EMAIL_SETTINGS], appName: brand.name, mailer, answers: { email: emailAnswers } });
|
|
16
|
+
|
|
17
|
+
// GET and POST /admin/settings, behind the sign-in and the admins check:
|
|
18
|
+
const outcome = request.method === "POST" ? await settingsAction(settings, new URLSearchParams(await request.text()), admin.email) : undefined;
|
|
19
|
+
const body = await renderSettingsPage(settings, { endpoint: "/admin/settings", outcome, history: await settings.history() });
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Refuse a post from another site before calling `settingsAction` (check the Origin, or pass the app's anti-forgery token in `renderSettingsPage`'s `fields`). Then:
|
|
23
|
+
|
|
24
|
+
1. `npx workbench-settings migration` writes the tables and functions into `supabase/migrations` with a down file.
|
|
25
|
+
2. Add the email template `settings_code` to the app's email catalogue.
|
|
26
|
+
3. Call `settings.cleanup()` once a day from the app's cron.
|
|
27
|
+
|
|
28
|
+
Restyle the page in the project's `public/styles/project.css` with the `wb-settings-*` classes and `--wb-settings-*` variables (`SETTINGS_STYLES`); they default to the project's theme tokens.
|
|
29
|
+
|
|
30
|
+
## Design mode (0.2.0)
|
|
31
|
+
|
|
32
|
+
`designSettings({ looks, schemes, siteLayouts, toolLayouts, check })` declares the "design" settings that design mode's "Keep this" saves (`docs/look-studio.md` §8). Pass in the choices: workbench-ui's `LOOKS`, the schemes, and each tool's `ToolStyles.layouts` ids. The settings are:
|
|
33
|
+
|
|
34
|
+
- `look`, `scheme` and `layout`, as choices;
|
|
35
|
+
- `layouts`, a **map** of a tool (`bookings`) or a tool on one page (`orders@/shop`) to a layout id, checked against `toolLayouts` at every save.
|
|
36
|
+
|
|
37
|
+
Read them on every request with `settings.get("design", "look")`. One read covers every setting and is held 60 seconds per isolate.
|
|
38
|
+
|
|
39
|
+
Any `SettingDef` can take a `check(value)` that returns `null`, or the words to show. A refused save throws `bad_value` with `error.problem.words`.
|
|
40
|
+
|
|
41
|
+
The design group is marked `carries: true`. The owner tries values on the test site, and the next release the owner approves takes them to the live site:
|
|
42
|
+
|
|
43
|
+
1. On the test site, `settings.exportGroup("design")` gives every key, with `null` where the owner hasn't changed it.
|
|
44
|
+
2. On the live site, `settings.importGroup("design", rows, by)` checks every row, then saves or puts back each one that differs.
|
|
45
|
+
3. Back on the test site, `settings.markCarried("design", rows, by)` records what the live site now holds.
|
|
46
|
+
|
|
47
|
+
Until step 3, the page says "On the test site only" under each changed value. Workbench never reads the rows. The project's own Worker serves steps 1 to 3 to the release run, behind the project key.
|
package/dist/check.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { SettingDef, SettingValue, ToolSettings, Words } from "./types.ts";
|
|
2
|
+
import type { SettingsWordKey } from "./words.ts";
|
|
3
|
+
/** The longest text any setting may hold. */
|
|
4
|
+
export declare const MAX_TEXT = 500;
|
|
5
|
+
export declare const MAX_EMAIL = 254;
|
|
6
|
+
/** A map's most names, and its largest size as JSON (the store holds up to 8 KB). */
|
|
7
|
+
export declare const MAX_MAP_ENTRIES = 200;
|
|
8
|
+
export declare const MAX_MAP_BYTES = 6000;
|
|
9
|
+
export type Checked = {
|
|
10
|
+
ok: true;
|
|
11
|
+
value: SettingValue | null;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
problem: SettingsWordKey;
|
|
15
|
+
values?: Record<string, string>;
|
|
16
|
+
words?: Words;
|
|
17
|
+
};
|
|
18
|
+
/** True when two values are the same, maps included. */
|
|
19
|
+
export declare function sameValue(a: SettingValue | null | undefined, b: SettingValue | null | undefined): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Checks what the owner typed for a setting, then the setting's own `check`.
|
|
22
|
+
* Text is trimmed; an empty field means "no value" (null), which only a
|
|
23
|
+
* setting with no `min` above 0 may hold. A map takes an object or its JSON.
|
|
24
|
+
*/
|
|
25
|
+
export declare function checkValue(def: SettingDef, raw: unknown): Checked;
|
|
26
|
+
/** Every problem with a set of declarations, or an empty list. */
|
|
27
|
+
export declare function settingsProblems(tools: readonly ToolSettings[]): string[];
|
package/dist/check.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// The one check for a declared setting, used by the page and by every save,
|
|
2
|
+
// and the check of the declarations themselves.
|
|
3
|
+
const KEY = /^[a-z][a-z0-9_]{0,39}$/;
|
|
4
|
+
const TOOL = /^[a-z][a-z0-9-]{0,39}$/;
|
|
5
|
+
/** Plain enough for a reply-to address: one @, no spaces or angle brackets, a dot in the domain. */
|
|
6
|
+
const EMAIL = /^[^\s@<>()",;:\\[\]]+@[^\s@<>()",;:\\[\]]+\.[^\s@<>()",;:\\[\]]+$/;
|
|
7
|
+
/** Control, invisible and direction-changing characters never belong in a setting (they could disguise a sender's name). */
|
|
8
|
+
// eslint-disable-next-line no-control-regex
|
|
9
|
+
const CONTROL = /[\u0000-\u001f\u007f-\u009f\u00ad\u061c\u180e\u200b-\u200f\u2028-\u202e\u2060-\u206f\ufeff]/;
|
|
10
|
+
/** The longest text any setting may hold. */
|
|
11
|
+
export const MAX_TEXT = 500;
|
|
12
|
+
export const MAX_EMAIL = 254;
|
|
13
|
+
/** A map's most names, and its largest size as JSON (the store holds up to 8 KB). */
|
|
14
|
+
export const MAX_MAP_ENTRIES = 200;
|
|
15
|
+
export const MAX_MAP_BYTES = 6000;
|
|
16
|
+
/** A map's name: a tool, or a tool on one page (`orders@/shop`). */
|
|
17
|
+
const MAP_NAME = /^[a-z][a-z0-9-]{0,39}(@\/[A-Za-z0-9/_.~-]{0,200})?$/;
|
|
18
|
+
/** A map's value: a name such as a layout's id. */
|
|
19
|
+
const MAP_VALUE = /^[a-z][a-z0-9-]{0,39}$/;
|
|
20
|
+
/** A map's names in a fixed order, so two equal maps store and compare the same. */
|
|
21
|
+
function checkMap(raw) {
|
|
22
|
+
let v = raw;
|
|
23
|
+
if (typeof v === "string") {
|
|
24
|
+
if (!v.trim())
|
|
25
|
+
return { ok: true, value: null };
|
|
26
|
+
try {
|
|
27
|
+
v = JSON.parse(v);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return { ok: false, problem: "problem.map" };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (typeof v !== "object" || v === null || Array.isArray(v))
|
|
34
|
+
return { ok: false, problem: "problem.map" };
|
|
35
|
+
const entries = Object.entries(v);
|
|
36
|
+
if (!entries.length)
|
|
37
|
+
return { ok: true, value: null };
|
|
38
|
+
if (entries.length > MAX_MAP_ENTRIES)
|
|
39
|
+
return { ok: false, problem: "problem.map" };
|
|
40
|
+
const out = {};
|
|
41
|
+
for (const [name, value] of entries.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
42
|
+
if (!MAP_NAME.test(name) || typeof value !== "string" || !MAP_VALUE.test(value))
|
|
43
|
+
return { ok: false, problem: "problem.map" };
|
|
44
|
+
out[name] = value;
|
|
45
|
+
}
|
|
46
|
+
if (new TextEncoder().encode(JSON.stringify(out)).length > MAX_MAP_BYTES)
|
|
47
|
+
return { ok: false, problem: "problem.map" };
|
|
48
|
+
return { ok: true, value: Object.freeze(out) };
|
|
49
|
+
}
|
|
50
|
+
/** True when two values are the same, maps included. */
|
|
51
|
+
export function sameValue(a, b) {
|
|
52
|
+
if (a === b)
|
|
53
|
+
return true;
|
|
54
|
+
if (a === null || a === undefined || b === null || b === undefined)
|
|
55
|
+
return false;
|
|
56
|
+
return typeof a === "object" && typeof b === "object" && JSON.stringify(a) === JSON.stringify(b);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Checks what the owner typed for a setting, then the setting's own `check`.
|
|
60
|
+
* Text is trimmed; an empty field means "no value" (null), which only a
|
|
61
|
+
* setting with no `min` above 0 may hold. A map takes an object or its JSON.
|
|
62
|
+
*/
|
|
63
|
+
export function checkValue(def, raw) {
|
|
64
|
+
const c = checkType(def, raw);
|
|
65
|
+
if (!c.ok || c.value === null || !def.check)
|
|
66
|
+
return c;
|
|
67
|
+
let words;
|
|
68
|
+
try {
|
|
69
|
+
words = def.check(c.value);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return { ok: false, problem: "problem.text" };
|
|
73
|
+
}
|
|
74
|
+
return words ? { ok: false, problem: "problem.check", words } : c;
|
|
75
|
+
}
|
|
76
|
+
function checkType(def, raw) {
|
|
77
|
+
if (raw === null || raw === undefined)
|
|
78
|
+
return { ok: true, value: null };
|
|
79
|
+
if (def.type === "map")
|
|
80
|
+
return checkMap(raw);
|
|
81
|
+
const text = typeof raw === "number" ? String(raw) : typeof raw === "string" ? raw.trim() : null;
|
|
82
|
+
if (text === null)
|
|
83
|
+
return { ok: false, problem: "problem.text" };
|
|
84
|
+
if (text === "")
|
|
85
|
+
return (def.min ?? 0) > 0 && def.type !== "whole" ? { ok: false, problem: "problem.required" } : { ok: true, value: null };
|
|
86
|
+
if (CONTROL.test(text))
|
|
87
|
+
return { ok: false, problem: "problem.text" };
|
|
88
|
+
switch (def.type) {
|
|
89
|
+
case "text": {
|
|
90
|
+
const max = Math.min(def.max ?? 200, MAX_TEXT);
|
|
91
|
+
const min = def.min ?? 1;
|
|
92
|
+
const n = [...text].length;
|
|
93
|
+
if (n > max)
|
|
94
|
+
return { ok: false, problem: "problem.too_long", values: { max: String(max) } };
|
|
95
|
+
if (n < min)
|
|
96
|
+
return { ok: false, problem: "problem.too_short", values: { min: String(min) } };
|
|
97
|
+
return { ok: true, value: text };
|
|
98
|
+
}
|
|
99
|
+
case "email": {
|
|
100
|
+
if (text.length > MAX_EMAIL || !EMAIL.test(text))
|
|
101
|
+
return { ok: false, problem: "problem.email" };
|
|
102
|
+
// Domains are case-blind; the local part is left as typed.
|
|
103
|
+
const at = text.lastIndexOf("@");
|
|
104
|
+
return { ok: true, value: `${text.slice(0, at)}@${text.slice(at + 1).toLowerCase()}` };
|
|
105
|
+
}
|
|
106
|
+
case "choice": {
|
|
107
|
+
if (!def.choices?.some((c) => c.value === text))
|
|
108
|
+
return { ok: false, problem: "problem.choice" };
|
|
109
|
+
return { ok: true, value: text };
|
|
110
|
+
}
|
|
111
|
+
case "whole": {
|
|
112
|
+
if (!/^-?\d{1,12}$/.test(text))
|
|
113
|
+
return { ok: false, problem: "problem.whole" };
|
|
114
|
+
const n = Number(text);
|
|
115
|
+
if (def.min !== undefined && n < def.min)
|
|
116
|
+
return { ok: false, problem: "problem.too_small", values: { min: String(def.min) } };
|
|
117
|
+
if (def.max !== undefined && n > def.max)
|
|
118
|
+
return { ok: false, problem: "problem.too_big", values: { max: String(def.max) } };
|
|
119
|
+
return { ok: true, value: n };
|
|
120
|
+
}
|
|
121
|
+
default:
|
|
122
|
+
return { ok: false, problem: "problem.text" };
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** Every problem with a set of declarations, or an empty list. */
|
|
126
|
+
export function settingsProblems(tools) {
|
|
127
|
+
const out = [];
|
|
128
|
+
const seen = new Set();
|
|
129
|
+
for (const t of tools) {
|
|
130
|
+
const at = `settings for ${t.tool}`;
|
|
131
|
+
if (t.format !== 1)
|
|
132
|
+
out.push(`${at}: format must be 1`);
|
|
133
|
+
if (!TOOL.test(t.tool))
|
|
134
|
+
out.push(`${at}: tool must be lower case letters, digits and dashes`);
|
|
135
|
+
if (seen.has(t.tool))
|
|
136
|
+
out.push(`${at}: declared twice`);
|
|
137
|
+
seen.add(t.tool);
|
|
138
|
+
if (!t.title?.en)
|
|
139
|
+
out.push(`${at}: title.en is missing`);
|
|
140
|
+
const keys = new Set();
|
|
141
|
+
if (!t.settings.length)
|
|
142
|
+
out.push(`${at}: no settings`);
|
|
143
|
+
for (const s of t.settings) {
|
|
144
|
+
const where = `${at}: ${s.key}`;
|
|
145
|
+
if (!KEY.test(s.key))
|
|
146
|
+
out.push(`${where}: key must be lower case letters, digits and underscores`);
|
|
147
|
+
if (keys.has(s.key))
|
|
148
|
+
out.push(`${where}: declared twice`);
|
|
149
|
+
keys.add(s.key);
|
|
150
|
+
if (!s.label?.en)
|
|
151
|
+
out.push(`${where}: label.en is missing`);
|
|
152
|
+
if (s.personal && s.kind === "live")
|
|
153
|
+
out.push(`${where}: decides where personal information goes, so it can't be live: make it checked`);
|
|
154
|
+
if (s.kind === "checked" && s.type !== "email")
|
|
155
|
+
out.push(`${where}: only an email address can be checked (by an emailed code)`);
|
|
156
|
+
if (t.carries && s.kind === "checked")
|
|
157
|
+
out.push(`${where}: a carried tool can't hold a checked setting (a code proves an address on one site only)`);
|
|
158
|
+
if (s.type === "map" && s.default !== undefined && s.default !== null)
|
|
159
|
+
out.push(`${where}: a map has no default: leave it out`);
|
|
160
|
+
if (s.check !== undefined && typeof s.check !== "function")
|
|
161
|
+
out.push(`${where}: check must be a function`);
|
|
162
|
+
if (s.type === "choice" && !(s.choices && s.choices.length >= 2))
|
|
163
|
+
out.push(`${where}: a choice needs at least two choices`);
|
|
164
|
+
if (s.type !== "choice" && s.choices)
|
|
165
|
+
out.push(`${where}: only a choice has choices`);
|
|
166
|
+
for (const [name, n] of [["min", s.min], ["max", s.max]])
|
|
167
|
+
if (n !== undefined && !Number.isSafeInteger(n))
|
|
168
|
+
out.push(`${where}: ${name} must be a whole number`);
|
|
169
|
+
if (s.type === "text" && s.max !== undefined && (s.max < 1 || s.max > MAX_TEXT))
|
|
170
|
+
out.push(`${where}: max must be 1 to ${MAX_TEXT}`);
|
|
171
|
+
if (s.default !== undefined && s.default !== null && !checkValue(s, s.default).ok)
|
|
172
|
+
out.push(`${where}: its default doesn't pass its own check`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
}
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// `npx workbench-settings migration [dir]`, run in a project's folder: writes
|
|
3
|
+
// settingsMigration into supabase/migrations/<timestamp>_workbench_settings.sql,
|
|
4
|
+
// and its down file into supabase/rollback/, unless a settings migration is
|
|
5
|
+
// already there.
|
|
6
|
+
import { existsSync, mkdirSync, readdirSync, realpathSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { join, resolve } from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { settingsMigration } from "./migration.js";
|
|
10
|
+
const USAGE = "usage: workbench-settings migration [dir]";
|
|
11
|
+
function migrationStamp(at) {
|
|
12
|
+
return at.toISOString().replace(/[-:T]/g, "").slice(0, 14);
|
|
13
|
+
}
|
|
14
|
+
/** Runs one command; returns the exit code. */
|
|
15
|
+
export function run(argv, log = console.log, err = console.error, now = () => new Date()) {
|
|
16
|
+
const [command, ...rest] = argv;
|
|
17
|
+
if (command !== "migration" || rest.length > 1 || rest.some((a) => a.startsWith("--"))) {
|
|
18
|
+
err(USAGE);
|
|
19
|
+
return 2;
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const dir = resolve(rest[0] ?? ".");
|
|
23
|
+
const folder = join(dir, "supabase/migrations");
|
|
24
|
+
if (existsSync(folder) && readdirSync(folder).some((f) => f.endsWith("_workbench_settings.sql"))) {
|
|
25
|
+
log("supabase/migrations already has the settings migration; nothing written");
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
const name = `${migrationStamp(now())}_workbench_settings.sql`;
|
|
29
|
+
mkdirSync(folder, { recursive: true });
|
|
30
|
+
mkdirSync(join(dir, "supabase/rollback"), { recursive: true });
|
|
31
|
+
writeFileSync(join(folder, name), `-- workbench-settings: the owner's Settings page\n${settingsMigration.up}`);
|
|
32
|
+
writeFileSync(join(dir, "supabase/rollback", name.replace(/\.sql$/, "_down.sql")), `-- Reverses ${name}\n${settingsMigration.down}`);
|
|
33
|
+
log(`wrote supabase/migrations/${name} and its down file`);
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
err(e.message);
|
|
38
|
+
return 1;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const isMain = (() => {
|
|
42
|
+
try {
|
|
43
|
+
return process.argv[1] !== undefined && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
})();
|
|
49
|
+
if (isMain)
|
|
50
|
+
process.exitCode = run(process.argv.slice(2));
|
package/dist/design.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ToolSettings, Words } from "./types.ts";
|
|
2
|
+
/** One thing to choose from: an id and its name. */
|
|
3
|
+
export interface DesignChoice {
|
|
4
|
+
id: string;
|
|
5
|
+
title: string | Words;
|
|
6
|
+
}
|
|
7
|
+
export interface DesignOptions {
|
|
8
|
+
/** The looks (workbench-ui's LOOKS): Clean, Warm, Calm... */
|
|
9
|
+
looks: readonly DesignChoice[];
|
|
10
|
+
/** The colour schemes: light and dark. */
|
|
11
|
+
schemes: readonly DesignChoice[];
|
|
12
|
+
/** The site's page layouts. Leave out when the site has one. */
|
|
13
|
+
siteLayouts?: readonly DesignChoice[];
|
|
14
|
+
/** Each tool's layouts by tool id (its ToolStyles.layouts), for the layouts map. */
|
|
15
|
+
toolLayouts: Readonly<Record<string, readonly string[]>>;
|
|
16
|
+
/** A check on the look and scheme together, such as the contrast fixer's. Null to accept. */
|
|
17
|
+
check?: (key: "look" | "scheme" | "layout", value: string) => Words | null;
|
|
18
|
+
}
|
|
19
|
+
/** The "design" settings, from the looks, schemes and layouts the app has. */
|
|
20
|
+
export declare function designSettings(o: DesignOptions): ToolSettings;
|
package/dist/design.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Design mode's settings (docs/look-studio.md §8): the look, the colour
|
|
2
|
+
// scheme, the site layout and each tool's layout that an admin chose with
|
|
3
|
+
// "Keep this" on the test site. They carry: an approved release takes them to
|
|
4
|
+
// the live site (settings.exportGroup, importGroup, markCarried).
|
|
5
|
+
//
|
|
6
|
+
// The choices come from the caller (workbench-ui's LOOKS and each tool's
|
|
7
|
+
// ToolStyles.layouts), so this package depends on neither. A layout is
|
|
8
|
+
// checked against the tool's own list at every save; a tool or layout the
|
|
9
|
+
// app no longer has is refused, and a stored one is ignored.
|
|
10
|
+
const words = (t) => (typeof t === "string" ? { en: t } : t);
|
|
11
|
+
const choices = (list) => list.map((c) => ({ value: c.id, label: words(c.title) }));
|
|
12
|
+
const NOT_A_LAYOUT = {
|
|
13
|
+
en: "One of those layouts isn't one this site has. Choose it again in design mode.",
|
|
14
|
+
tl: "Ang isa sa mga layout na iyan ay wala sa site na ito. Piliin ulit sa design mode.",
|
|
15
|
+
taglish: "Isa sa mga layout na yan ay wala sa site na ito. Piliin ulit sa design mode.",
|
|
16
|
+
};
|
|
17
|
+
/** The "design" settings, from the looks, schemes and layouts the app has. */
|
|
18
|
+
export function designSettings(o) {
|
|
19
|
+
const outer = o.check;
|
|
20
|
+
const checkOf = (key) => (outer ? { check: (v) => (typeof v === "string" ? outer(key, v) : null) } : {});
|
|
21
|
+
const layouts = (v) => {
|
|
22
|
+
for (const [name, layout] of Object.entries(v)) {
|
|
23
|
+
const tool = name.split("@")[0];
|
|
24
|
+
if (!o.toolLayouts[tool]?.includes(layout))
|
|
25
|
+
return NOT_A_LAYOUT;
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
format: 1,
|
|
31
|
+
tool: "design",
|
|
32
|
+
carries: true,
|
|
33
|
+
title: { en: "Design", tl: "Disenyo", taglish: "Design" },
|
|
34
|
+
about: {
|
|
35
|
+
en: "The look and layouts chosen in design mode. Try them on the test site; the live site gets them with the next release you approve.",
|
|
36
|
+
tl: "Ang itsura at mga layout na pinili sa design mode. Subukan sa test site; makukuha ito ng live site sa susunod na release na aaprubahan mo.",
|
|
37
|
+
taglish: "Ang look at layouts na pinili sa design mode. I-try sa test site; makukuha ito ng live site sa next release na i-approve mo.",
|
|
38
|
+
},
|
|
39
|
+
settings: [
|
|
40
|
+
{ key: "look", type: "choice", kind: "live", label: { en: "Look", tl: "Itsura", taglish: "Look" }, choices: choices(o.looks), ...checkOf("look") },
|
|
41
|
+
{ key: "scheme", type: "choice", kind: "live", label: { en: "Colours", tl: "Mga kulay", taglish: "Colours" }, choices: choices(o.schemes), ...checkOf("scheme") },
|
|
42
|
+
...(o.siteLayouts && o.siteLayouts.length >= 2
|
|
43
|
+
? [{ key: "layout", type: "choice", kind: "live", label: { en: "Page layout", tl: "Ayos ng page", taglish: "Page layout" }, choices: choices(o.siteLayouts), ...checkOf("layout") }]
|
|
44
|
+
: []),
|
|
45
|
+
{
|
|
46
|
+
key: "layouts",
|
|
47
|
+
type: "map",
|
|
48
|
+
kind: "live",
|
|
49
|
+
label: { en: "Tool layouts", tl: "Ayos ng mga tool", taglish: "Tool layouts" },
|
|
50
|
+
help: {
|
|
51
|
+
en: "How each tool is laid out, on every page or on one. Change these in design mode.",
|
|
52
|
+
tl: "Kung paano nakaayos ang bawat tool, sa lahat ng page o sa isa. Baguhin ito sa design mode.",
|
|
53
|
+
taglish: "Kung paano naka-layout ang bawat tool, sa lahat ng page o sa isa. I-change ito sa design mode.",
|
|
54
|
+
},
|
|
55
|
+
check: layouts,
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
}
|
package/dist/email.d.ts
ADDED
package/dist/email.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// The email settings an owner may change: where customers' replies go, and
|
|
2
|
+
// the name emails come from. Email is plumbing every tool sends through, so
|
|
3
|
+
// its declarations ship here; other tools ship their own next to their code
|
|
4
|
+
// (docs/project-settings.md §3).
|
|
5
|
+
//
|
|
6
|
+
// The app reads them where it builds its mailer and its tools:
|
|
7
|
+
// from: { address, name: (await settings.get("email", "from_name")) ?? brand.name }
|
|
8
|
+
// business: withReplyTo(settings, async () => ({ name: brand.name }))
|
|
9
|
+
export const EMAIL_SETTINGS = {
|
|
10
|
+
format: 1,
|
|
11
|
+
tool: "email",
|
|
12
|
+
title: { en: "Emails", tl: "Mga email", taglish: "Emails" },
|
|
13
|
+
about: {
|
|
14
|
+
en: "How the emails your app sends to customers look to them, and where their replies go.",
|
|
15
|
+
tl: "Kung paano lumalabas sa mga customer ang mga email ng app mo, at kung saan napupunta ang mga sagot nila.",
|
|
16
|
+
taglish: "Kung paano lumalabas sa customers ang emails ng app mo, at saan napupunta ang replies nila.",
|
|
17
|
+
},
|
|
18
|
+
settings: [
|
|
19
|
+
{
|
|
20
|
+
key: "reply_to",
|
|
21
|
+
type: "email",
|
|
22
|
+
kind: "checked",
|
|
23
|
+
personal: true,
|
|
24
|
+
answers: "reply_to",
|
|
25
|
+
label: { en: "Replies go to", tl: "Saan napupunta ang mga sagot", taglish: "Saan napupunta ang replies" },
|
|
26
|
+
help: {
|
|
27
|
+
en: "When a customer presses Reply on one of your app's emails, it goes to this address. We email a code to a new address first, to make sure it's right.",
|
|
28
|
+
tl: "Kapag pinindot ng customer ang Reply sa email ng app mo, dito ito mapupunta. Mag-e-email muna kami ng code sa bagong address para masigurong tama ito.",
|
|
29
|
+
taglish: "Pag nag-Reply ang customer sa email ng app mo, dito ito mapupunta. Mag-e-email muna kami ng code sa bagong address para sure na tama.",
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
key: "from_name",
|
|
34
|
+
type: "text",
|
|
35
|
+
kind: "live",
|
|
36
|
+
max: 60,
|
|
37
|
+
answers: "from_name",
|
|
38
|
+
label: { en: "Emails come from", tl: "Pangalan ng nagpapadala", taglish: "Pangalan ng sender" },
|
|
39
|
+
help: {
|
|
40
|
+
en: "The name customers see beside your emails in their inbox, such as your business's name. Left empty, it's your brand name.",
|
|
41
|
+
tl: "Ang pangalang nakikita ng mga customer sa tabi ng mga email mo sa inbox nila, gaya ng pangalan ng negosyo mo. Kung walang laman, ang pangalan ng brand mo.",
|
|
42
|
+
taglish: "Ang name na nakikita ng customers sa tabi ng emails mo sa inbox nila, like ang name ng business mo. Kung empty, ang brand name mo.",
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
};
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type SettingsErrorCode =
|
|
2
|
+
/** A tool's declarations are out of shape (settingsProblems names each problem). */
|
|
3
|
+
"bad_declaration"
|
|
4
|
+
/** No such tool or setting among the declarations given to createSettings. */
|
|
5
|
+
| "unknown_setting"
|
|
6
|
+
/** The value doesn't pass the setting's check. */
|
|
7
|
+
| "bad_value"
|
|
8
|
+
/** A checked setting needs a mailer to send its code, and none was given. */
|
|
9
|
+
| "mailer_needed"
|
|
10
|
+
/** A code was sent for this setting less than a minute ago. */
|
|
11
|
+
| "too_soon"
|
|
12
|
+
/** Five codes were sent for this setting in the last day. */
|
|
13
|
+
| "too_many"
|
|
14
|
+
/** The code email couldn't be sent; nothing is left waiting. */
|
|
15
|
+
| "send_failed"
|
|
16
|
+
/** A call's arguments are out of shape (the admin's name). */
|
|
17
|
+
| "bad_input"
|
|
18
|
+
/** The database refused or failed. */
|
|
19
|
+
| "store_failed";
|
|
20
|
+
export declare class SettingsError extends Error {
|
|
21
|
+
readonly code: SettingsErrorCode;
|
|
22
|
+
/** For bad_value: what's wrong, in the page's words (a SettingsWordKey) and its values. */
|
|
23
|
+
readonly problem?: {
|
|
24
|
+
word: string;
|
|
25
|
+
values?: Readonly<Record<string, string>>;
|
|
26
|
+
words?: {
|
|
27
|
+
en: string;
|
|
28
|
+
} & Partial<Record<string, string>>;
|
|
29
|
+
};
|
|
30
|
+
constructor(code: SettingsErrorCode, message: string, problem?: SettingsError["problem"]);
|
|
31
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Every failure carries a permanent code, so an app can act on it without
|
|
2
|
+
// reading the words. Messages never hold a value an owner typed.
|
|
3
|
+
export class SettingsError extends Error {
|
|
4
|
+
code;
|
|
5
|
+
/** For bad_value: what's wrong, in the page's words (a SettingsWordKey) and its values. */
|
|
6
|
+
problem;
|
|
7
|
+
constructor(code, message, problem) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "SettingsError";
|
|
10
|
+
this.code = code;
|
|
11
|
+
if (problem)
|
|
12
|
+
this.problem = problem;
|
|
13
|
+
}
|
|
14
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { createSettings, withReplyTo, type ConfirmResult, type SaveResult, type Settings, type SettingsClient, type SettingsMailer, type SettingsOptions } from "./settings.ts";
|
|
2
|
+
export { checkValue, MAX_EMAIL, MAX_MAP_BYTES, MAX_MAP_ENTRIES, MAX_TEXT, sameValue, settingsProblems, type Checked } from "./check.ts";
|
|
3
|
+
export { drawSettingsPage, renderSettingsPage, settingsAction, type SettingsOutcome, type SettingsPageOptions } from "./page.ts";
|
|
4
|
+
export { EMAIL_SETTINGS } from "./email.ts";
|
|
5
|
+
export { designSettings, type DesignChoice, type DesignOptions } from "./design.ts";
|
|
6
|
+
export { settingsMigration } from "./migration.ts";
|
|
7
|
+
export { phrase, SETTINGS_WORD_KEYS, SETTINGS_WORDS, settingsWord, type SettingsStrings, type SettingsWordKey } from "./words.ts";
|
|
8
|
+
export { SETTINGS_STYLES } from "./styles.ts";
|
|
9
|
+
export { SETTINGS_SAMPLES } from "./samples.ts";
|
|
10
|
+
export { SettingsError, type SettingsErrorCode } from "./errors.ts";
|
|
11
|
+
export type { CarriedSetting, SettingChange, SettingMap, SettingDef, SettingKind, SettingsLang, SettingSource, SettingState, SettingType, SettingValue, ToolSettings, Words } from "./types.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// @mccaskie-labs/workbench-settings: one Settings page in a client's admin
|
|
2
|
+
// (docs/project-settings.md). Each tool declares the settings an owner may
|
|
3
|
+
// change (ToolSettings); the page shows them, checks them and saves them in
|
|
4
|
+
// the app's own database (settingsMigration), and the tools read them when
|
|
5
|
+
// they run with settings.get(tool, key), so a change shows in the next email
|
|
6
|
+
// with no redeploy. A new email address is proved with an emailed code before
|
|
7
|
+
// it is used. No AI and no Gateway call of its own: the code email goes
|
|
8
|
+
// through the app's own mailer.
|
|
9
|
+
export { createSettings, withReplyTo } from "./settings.js";
|
|
10
|
+
export { checkValue, MAX_EMAIL, MAX_MAP_BYTES, MAX_MAP_ENTRIES, MAX_TEXT, sameValue, settingsProblems } from "./check.js";
|
|
11
|
+
export { drawSettingsPage, renderSettingsPage, settingsAction } from "./page.js";
|
|
12
|
+
export { EMAIL_SETTINGS } from "./email.js";
|
|
13
|
+
export { designSettings } from "./design.js";
|
|
14
|
+
export { settingsMigration } from "./migration.js";
|
|
15
|
+
export { phrase, SETTINGS_WORD_KEYS, SETTINGS_WORDS, settingsWord } from "./words.js";
|
|
16
|
+
export { SETTINGS_STYLES } from "./styles.js";
|
|
17
|
+
export { SETTINGS_SAMPLES } from "./samples.js";
|
|
18
|
+
export { SettingsError } from "./errors.js";
|