@mccaskie-labs/workbench-site 0.12.0 → 0.13.1

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @mccaskie-labs/workbench-site
2
2
 
3
+ ## 0.13.1
4
+
5
+ ### Patch Changes
6
+
7
+ - A page with a tool's own section imports `../lib/wiring.ts`, the template's tools wiring registry, which installs itself on import. A tool's section function reads its settings and database client from there, even when the page is drawn directly (as the generated pages test does). Pages without a tool section are unchanged.
8
+
9
+ ## 0.13.0
10
+
11
+ ### Minor Changes
12
+
13
+ - `fromAnswers(answers, project, options?)` (builder-ready rule B3): the site question's answer, the pages kept, as `build.add` for `buildSite` (the always set, then the kept pages in the catalogue's order). It writes no file, since `workbench/site.json` is the builder's own record. `askedPages()` lists the pages the question asks about; `siteAnswersProblems` checks a list. A bad answer throws `SiteAnswersError` (code `bad_answers`). Strict by default: a must-ask question left out is a `bad_answers` problem naming it. With `{ draft: true }` (for init) it gives the result anyway and lists what is left as todo lines `{ question, text }` in the owner's words.
14
+
3
15
  ## 0.12.0
4
16
 
5
17
  ### Minor Changes
package/dist/builder.js CHANGED
@@ -344,6 +344,8 @@ function pageFile(r, version, base, pathOf) {
344
344
  "",
345
345
  ...(used.length ? [`import { ${used.join(", ")} } from "${PACKAGE}";`] : []),
346
346
  ...toolImports,
347
+ // The tools' wiring registry (the template's src/lib/wiring.ts installs it on import), which a tool's section reads.
348
+ ...(tools.length ? [`import "../lib/wiring.ts";`] : []),
347
349
  ...(store.length ? [`import { ${store.join(", ")} } from "./enquiry-store.ts";`] : []),
348
350
  ...(guide ? [`import { guideView, styleGuide } from "@mccaskie-labs/workbench-ui";`, `import { SAMPLES } from "../samples.generated.ts";`] : []),
349
351
  ...(looks ? [`import { lookPreview } from "@mccaskie-labs/workbench-ui";`] : []),
@@ -0,0 +1,54 @@
1
+ import type { Recipe } from "./recipe.ts";
2
+ /** What fromAnswers needs to know about the project (the same shape as every tool's). */
3
+ export interface ProjectFacts {
4
+ name: string;
5
+ country: string;
6
+ time_zone: string;
7
+ currency?: string;
8
+ language?: "en" | "tl" | "taglish";
9
+ }
10
+ /** One answer: a card's value, or an "Other" answer's typed value. */
11
+ type AnswerValue = string | number | boolean | null | readonly unknown[] | {
12
+ readonly [key: string]: unknown;
13
+ };
14
+ /** The answers, by question id. */
15
+ export type ToolAnswers = {
16
+ readonly [id: string]: AnswerValue | undefined;
17
+ };
18
+ export interface SiteAnswersResult {
19
+ /** None: workbench/site.json is buildSite's own record. */
20
+ files: Record<string, never>;
21
+ /** The recipes to build: the always set, then the pages the owner kept. Hand `add` to buildSite. */
22
+ build: {
23
+ add: string[];
24
+ };
25
+ todo: TodoLine[];
26
+ }
27
+ /** Something still to answer, in the owner's words. */
28
+ interface TodoLine {
29
+ question?: string;
30
+ text: string;
31
+ }
32
+ interface AnswersOptions {
33
+ /** Build the ticked pages when site.pages isn't answered, listing it in todo (init uses this). Without it, an unanswered site.pages is a bad_answers problem. */
34
+ draft?: boolean;
35
+ /** The recipes to choose from; the package's own by default. */
36
+ catalogue?: readonly Recipe[];
37
+ }
38
+ /** fromAnswers was handed an answer that isn't one of its question's (nothing is written). */
39
+ export declare class SiteAnswersError extends Error {
40
+ name: string;
41
+ readonly code: "bad_answers";
42
+ }
43
+ /** The site question ids fromAnswers reads. */
44
+ export declare const SITE_ANSWER_IDS: readonly ["site.pages"];
45
+ /** The pages the site question asks about, with which are ticked to start with. */
46
+ export declare function askedPages(catalogue?: readonly Recipe[]): {
47
+ id: string;
48
+ ticked: boolean;
49
+ }[];
50
+ /** The problems with a fromAnswers result's pages, in plain words: each must be a recipe, once. */
51
+ export declare function siteAnswersProblems(add: readonly string[], catalogue?: readonly Recipe[]): string[];
52
+ /** The client's answer as the pages to build. Throws SiteAnswersError naming a page that isn't one the question asks about. */
53
+ export declare function fromAnswers(answers: ToolAnswers, _project: ProjectFacts, options?: AnswersOptions): SiteAnswersResult;
54
+ export {};
@@ -0,0 +1,57 @@
1
+ // fromAnswers (docs/builder-ready.md, rule B3): the client's answer to the
2
+ // site question (capabilities/site.json, offer) is the list of pages they kept
3
+ // from those that aren't always built (docs/default-pages.md §3). The site's
4
+ // answers file, workbench/site.json, is written by buildSite alone, never from
5
+ // answers, so fromAnswers writes no file: it gives the pages to build, the
6
+ // always set first, for `buildSite({ add })` (and `npx workbench-site add`).
7
+ // Pure: no node:fs, so it runs in a Worker as well as on the command line.
8
+ import { alwaysSet } from "./builder.js";
9
+ import { RECIPES } from "./recipes/index.js";
10
+ /** fromAnswers was handed an answer that isn't one of its question's (nothing is written). */
11
+ export class SiteAnswersError extends Error {
12
+ name = "SiteAnswersError";
13
+ code = "bad_answers";
14
+ }
15
+ /** The site question ids fromAnswers reads. */
16
+ export const SITE_ANSWER_IDS = ["site.pages"];
17
+ /** The pages the site question asks about, with which are ticked to start with. */
18
+ export function askedPages(catalogue = RECIPES) {
19
+ return catalogue.filter((r) => r.tier !== "always").map((r) => ({ id: r.id, ticked: r.tier === "ticked" }));
20
+ }
21
+ /** The problems with a fromAnswers result's pages, in plain words: each must be a recipe, once. */
22
+ export function siteAnswersProblems(add, catalogue = RECIPES) {
23
+ const ids = new Set(catalogue.map((r) => r.id));
24
+ const out = add.filter((id) => !ids.has(id)).map((id) => `${id} isn't one of the site's pages`);
25
+ if (new Set(add).size !== add.length)
26
+ out.push("a page is listed twice");
27
+ for (const id of alwaysSet(catalogue))
28
+ if (!add.includes(id))
29
+ out.push(`${id} is always built`);
30
+ return out;
31
+ }
32
+ /** The client's answer as the pages to build. Throws SiteAnswersError naming a page that isn't one the question asks about. */
33
+ export function fromAnswers(answers, _project, options = {}) {
34
+ const catalogue = options.catalogue ?? RECIPES;
35
+ const asked = askedPages(catalogue);
36
+ const todo = [];
37
+ const v = answers["site.pages"];
38
+ let kept;
39
+ if (v === undefined) {
40
+ if (!options.draft)
41
+ throw new SiteAnswersError("The site answers can't be used: site.pages isn't answered yet.");
42
+ kept = asked.filter((p) => p.ticked).map((p) => p.id);
43
+ todo.push({ question: "site.pages", text: "Which of these pages would you like as well: About, Questions and answers, Accessibility?" });
44
+ }
45
+ else if (Array.isArray(v) && v.every((x) => typeof x === "string"))
46
+ kept = [...new Set(v)];
47
+ else if (typeof v === "string")
48
+ kept = [...new Set(v.split(",").map((x) => x.trim()).filter(Boolean))];
49
+ else
50
+ throw new SiteAnswersError("The site answers can't be used: site.pages must be a list of page ids.");
51
+ const unknown = kept.filter((id) => !asked.some((p) => p.id === id));
52
+ if (unknown.length)
53
+ throw new SiteAnswersError(`The site answers can't be used: site.pages: ${unknown.join(", ")} isn't one of ${asked.map((p) => p.id).join(", ")}.`);
54
+ // In the catalogue's order, so the same answers always give the same list.
55
+ const order = asked.map((p) => p.id).filter((id) => kept.includes(id));
56
+ return { files: {}, build: { add: [...alwaysSet(catalogue), ...order] }, todo };
57
+ }
package/dist/index.d.ts CHANGED
@@ -5,3 +5,4 @@ export { recipeKeys, recipeProblems, TIERS, type ActionSpec, type Area, type Gro
5
5
  export { RECIPES } from "./recipes/index.ts";
6
6
  export { gatherRecipes, RECIPES_EXPORT, type GatheredRecipes, type RecipeSource } from "./gather.ts";
7
7
  export { alwaysSet, buildSite, SITE_FILE, siteProblems, type BuildOptions, type BuildResult, type SiteCheckInput, type SitePageRecord, type SiteRecord } from "./builder.ts";
8
+ export { askedPages, fromAnswers, SITE_ANSWER_IDS, SiteAnswersError, siteAnswersProblems, type ProjectFacts as SiteProjectFacts, type SiteAnswersResult, type ToolAnswers as SiteToolAnswers } from "./from-answers.ts";
package/dist/index.js CHANGED
@@ -8,3 +8,4 @@ export { recipeKeys, recipeProblems, TIERS } from "./recipe.js";
8
8
  export { RECIPES } from "./recipes/index.js";
9
9
  export { gatherRecipes, RECIPES_EXPORT } from "./gather.js";
10
10
  export { alwaysSet, buildSite, SITE_FILE, siteProblems } from "./builder.js";
11
+ export { askedPages, fromAnswers, SITE_ANSWER_IDS, SiteAnswersError, siteAnswersProblems } from "./from-answers.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mccaskie-labs/workbench-site",
3
- "version": "0.12.0",
3
+ "version": "0.13.1",
4
4
  "description": "A Workbench project's standard pages: page recipes, the builder that turns them into thin page files calling Workbench's tools, and the page frames and sections they are drawn with (wb-site-*, restylable, on the project's theme tokens).",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",