fix(views): what the throwaway view revealed (views phase 6) #105
11 changed files with 79 additions and 47 deletions
|
|
@ -22,7 +22,7 @@ import {
|
|||
resolveCliFormat,
|
||||
} from "../core/cli-views";
|
||||
import {
|
||||
RECIPE_VIEW_SETTINGS,
|
||||
EXPORT_VIEW_SETTINGS,
|
||||
STEP_QTY_ALIAS_ARG,
|
||||
readRecipeSettingArgs,
|
||||
readViewSettingArgs,
|
||||
|
|
@ -35,7 +35,7 @@ export default defineCommand({
|
|||
meta: {
|
||||
name: "export",
|
||||
description:
|
||||
"Export a view of a recipe (the recipe or its work plan) to Markdown or print-ready HTML",
|
||||
"Export a view of a recipe (see --view) to Markdown, print-ready HTML or plain text",
|
||||
},
|
||||
args: {
|
||||
file: {
|
||||
|
|
@ -46,13 +46,13 @@ export default defineCommand({
|
|||
format: {
|
||||
type: "string",
|
||||
description:
|
||||
"Output format: md (Markdown), html (print-ready A4 HTML) or text (plain text). Each view offers only the formats it has (the work plan all three)",
|
||||
"Output format: md (Markdown), html (print-ready A4 HTML) or text (plain text), among those the chosen view has",
|
||||
},
|
||||
output: {
|
||||
type: "string",
|
||||
alias: "o",
|
||||
description:
|
||||
"Output file path (default: same directory as input, with new extension; .work-plan before it for the work plan)",
|
||||
"Output file path (default: same directory as input, with new extension; the view's name before it for any view but the recipe, e.g. .work-plan)",
|
||||
},
|
||||
scale: {
|
||||
type: "string",
|
||||
|
|
@ -68,7 +68,7 @@ export default defineCommand({
|
|||
description: "Skip ingredient database",
|
||||
default: false,
|
||||
},
|
||||
...viewSettingArgs(RECIPE_VIEW_SETTINGS),
|
||||
...viewSettingArgs(EXPORT_VIEW_SETTINGS),
|
||||
...STEP_QTY_ALIAS_ARG,
|
||||
...recipeSettingArgs(),
|
||||
"bakers-reference": {
|
||||
|
|
@ -110,7 +110,7 @@ export default defineCommand({
|
|||
const bakersReference =
|
||||
(args["bakers-reference"] as string) ||
|
||||
(args["bakers-math"] ? "" : undefined);
|
||||
const settings = readViewSettingArgs(args, RECIPE_VIEW_SETTINGS);
|
||||
const settings = readViewSettingArgs(args, EXPORT_VIEW_SETTINGS);
|
||||
const recipe = readRecipeSettingArgs(args);
|
||||
let view: ReturnType<typeof resolveExportView>;
|
||||
let format: ReturnType<typeof resolveCliFormat>;
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ import {
|
|||
import { DEFAULT_VIEW_SETTINGS } from "@gram-lang/renderer";
|
||||
import { cliViewName } from "../core/cli-views";
|
||||
import {
|
||||
RECIPE_VIEW_SETTINGS,
|
||||
EXPORT_VIEW_SETTINGS,
|
||||
STEP_QTY_ALIAS_ARG,
|
||||
readRecipeSettingArgs,
|
||||
readViewSettingArgs,
|
||||
|
|
@ -29,7 +29,7 @@ export default defineCommand({
|
|||
meta: {
|
||||
name: "print",
|
||||
description:
|
||||
"Generate a print-ready HTML of a view (the recipe or its work plan) and open it in the browser",
|
||||
"Generate a print-ready HTML of a view of a recipe (see --view) and open it in the browser",
|
||||
},
|
||||
args: {
|
||||
file: {
|
||||
|
|
@ -57,7 +57,7 @@ export default defineCommand({
|
|||
description: "Skip ingredient database",
|
||||
default: false,
|
||||
},
|
||||
...viewSettingArgs(RECIPE_VIEW_SETTINGS),
|
||||
...viewSettingArgs(EXPORT_VIEW_SETTINGS),
|
||||
...STEP_QTY_ALIAS_ARG,
|
||||
...recipeSettingArgs(),
|
||||
"bakers-reference": {
|
||||
|
|
@ -91,7 +91,7 @@ export default defineCommand({
|
|||
const bakersReference =
|
||||
(args["bakers-reference"] as string) ||
|
||||
(args["bakers-math"] ? "" : undefined);
|
||||
const settings = readViewSettingArgs(args, RECIPE_VIEW_SETTINGS);
|
||||
const settings = readViewSettingArgs(args, EXPORT_VIEW_SETTINGS);
|
||||
const recipe = readRecipeSettingArgs(args);
|
||||
let view: ReturnType<typeof resolveExportView>;
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -84,6 +84,13 @@ export function resolveCliFormat(
|
|||
formats: CliFormats,
|
||||
): Format {
|
||||
const offered = cliFormatsOf(view, formats);
|
||||
if (offered.length === 0) {
|
||||
// A view shown on screen only (html alone): nothing to list.
|
||||
throw new GramCLIError(
|
||||
`The ${cliViewName(view.id)} view has none of the formats this command writes.`,
|
||||
ExitCode.Error,
|
||||
);
|
||||
}
|
||||
if (!offered.includes(name)) {
|
||||
const known = name in formats;
|
||||
throw new GramCLIError(
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ const SETTING = VIEW_SETTINGS.comments;
|
|||
|
||||
export const COMMENTS_FLAG_VALUES: readonly string[] = flagValues(SETTING);
|
||||
|
||||
export const COMMENTS_FLAG_DESCRIPTION = `Whether to show the comments of the recipe (// like this): ${flagValueList(SETTING)} (default: ${flagDefault(SETTING)}). Applies to the recipe and to the work plan.`;
|
||||
export const COMMENTS_FLAG_DESCRIPTION = `Whether to show the comments of the recipe (// like this): ${flagValueList(SETTING)} (default: ${flagDefault(SETTING)}).`;
|
||||
|
||||
/** Reports and exits on an unusable value, like every setting flag. */
|
||||
export function parseComments(value: unknown): CommentsMode | undefined {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import {
|
|||
type ViewSettings,
|
||||
VIEW_SETTINGS,
|
||||
parseRecipeSettings,
|
||||
views,
|
||||
} from "@gram-lang/renderer";
|
||||
import type { ArgDef } from "citty";
|
||||
import { COMMENTS_FLAG_DESCRIPTION } from "./comments-flag";
|
||||
|
|
@ -183,15 +184,16 @@ export const STEP_QTY_ALIAS_ARG: Record<string, ArgDef> = {
|
|||
},
|
||||
};
|
||||
|
||||
/** Every view setting the recipe view honors: what `print`, `export` and `view` expose. */
|
||||
export const RECIPE_VIEW_SETTINGS: readonly ViewSettingKey[] = [
|
||||
"miseEnPlace",
|
||||
"rests",
|
||||
"comments",
|
||||
"nutritionBasis",
|
||||
"bakersMathOnly",
|
||||
"stepQuantities",
|
||||
];
|
||||
/**
|
||||
* Every view setting a view of the registry honors: what `print` and `export`
|
||||
* expose, since their `--view` picks any view. Read from the registry, so the
|
||||
* setting a new view brings gets its flag here with no edit.
|
||||
*/
|
||||
export const EXPORT_VIEW_SETTINGS: readonly ViewSettingKey[] = (
|
||||
Object.keys(VIEW_SETTINGS) as ViewSettingKey[]
|
||||
).filter((key) =>
|
||||
views.some((view) => (view.honors as readonly string[]).includes(key)),
|
||||
);
|
||||
|
||||
/** What the work plan honors: `gram plan`. */
|
||||
export const WORK_PLAN_SETTINGS: readonly ViewSettingKey[] = [
|
||||
|
|
|
|||
|
|
@ -66,6 +66,15 @@ describe("formats", () => {
|
|||
);
|
||||
});
|
||||
|
||||
it("says so when a view has none of the formats a command writes", () => {
|
||||
// A view on screen only: gram export's html is the print layout.
|
||||
const screenOnly = { ...recipe, id: "notes", formats: ["html"] } as const;
|
||||
expect(cliFormatsOf(screenOnly, EXPORT_FORMATS)).toEqual([]);
|
||||
expect(() => resolveCliFormat(screenOnly, "html", EXPORT_FORMATS)).toThrow(
|
||||
"The notes view has none of the formats this command writes.",
|
||||
);
|
||||
});
|
||||
|
||||
it("gives every export format a file extension", () => {
|
||||
for (const name of Object.keys(EXPORT_FORMATS)) {
|
||||
expect(EXPORT_EXTENSIONS[name]).toBeString();
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { runPipeline } from "../src/core/pipeline";
|
|||
import { cleanupTmpFile, makeDb, writeTmpFile } from "./helpers";
|
||||
import { RECIPE_SETTINGS, VIEW_SETTINGS } from "@gram-lang/renderer";
|
||||
import {
|
||||
RECIPE_VIEW_SETTINGS,
|
||||
EXPORT_VIEW_SETTINGS,
|
||||
STEP_QTY_ALIAS_ARG,
|
||||
readRecipeSettingArgs,
|
||||
readViewSettingArgs,
|
||||
|
|
@ -19,12 +19,12 @@ describe("setting flags", () => {
|
|||
expect(settingFlagName("massStandardization")).toBe("mass-standardization");
|
||||
});
|
||||
|
||||
it("give the recipe view a flag for every view setting", () => {
|
||||
expect([...RECIPE_VIEW_SETTINGS].sort()).toEqual(
|
||||
it("give export and print a flag for every setting a view honors", () => {
|
||||
expect([...EXPORT_VIEW_SETTINGS].sort()).toEqual(
|
||||
Object.keys(VIEW_SETTINGS).sort(),
|
||||
);
|
||||
const args = viewSettingArgs(RECIPE_VIEW_SETTINGS);
|
||||
for (const key of RECIPE_VIEW_SETTINGS) {
|
||||
const args = viewSettingArgs(EXPORT_VIEW_SETTINGS);
|
||||
for (const key of EXPORT_VIEW_SETTINGS) {
|
||||
expect(args[settingFlagName(key)]).toBeDefined();
|
||||
}
|
||||
});
|
||||
|
|
@ -46,7 +46,7 @@ describe("setting flags", () => {
|
|||
|
||||
describe("readViewSettingArgs", () => {
|
||||
it("leaves out a setting whose flag is absent", () => {
|
||||
expect(readViewSettingArgs({}, RECIPE_VIEW_SETTINGS)).toEqual({});
|
||||
expect(readViewSettingArgs({}, EXPORT_VIEW_SETTINGS)).toEqual({});
|
||||
});
|
||||
|
||||
it("reads the CLI spelling back to the setting's value", () => {
|
||||
|
|
@ -58,7 +58,7 @@ describe("readViewSettingArgs", () => {
|
|||
"bakers-math-only": true,
|
||||
"step-quantities": "hide",
|
||||
},
|
||||
RECIPE_VIEW_SETTINGS,
|
||||
EXPORT_VIEW_SETTINGS,
|
||||
),
|
||||
).toEqual({
|
||||
miseEnPlace: "perDay",
|
||||
|
|
@ -72,7 +72,7 @@ describe("readViewSettingArgs", () => {
|
|||
expect(
|
||||
readViewSettingArgs(
|
||||
{ nutrition: "per-100g", "step-qty": false },
|
||||
RECIPE_VIEW_SETTINGS,
|
||||
EXPORT_VIEW_SETTINGS,
|
||||
),
|
||||
).toEqual({ nutritionBasis: "per100g", stepQuantities: "hide" });
|
||||
});
|
||||
|
|
@ -86,7 +86,7 @@ describe("readViewSettingArgs", () => {
|
|||
"step-quantities": "show",
|
||||
"step-qty": false,
|
||||
},
|
||||
RECIPE_VIEW_SETTINGS,
|
||||
EXPORT_VIEW_SETTINGS,
|
||||
),
|
||||
).toEqual({ nutritionBasis: "total", stepQuantities: "show" });
|
||||
});
|
||||
|
|
@ -107,7 +107,7 @@ describe("readViewSettingArgs", () => {
|
|||
const out = spyOn(process.stdout, "write").mockImplementation(() => true);
|
||||
try {
|
||||
expect(() =>
|
||||
readViewSettingArgs({ nutrition: "bad" }, RECIPE_VIEW_SETTINGS),
|
||||
readViewSettingArgs({ nutrition: "bad" }, EXPORT_VIEW_SETTINGS),
|
||||
).toThrow("exit");
|
||||
} finally {
|
||||
exit.mockRestore();
|
||||
|
|
|
|||
|
|
@ -31,14 +31,26 @@ describe("the view tabs and the inspector menu", () => {
|
|||
const t = getDictionary("en");
|
||||
const tabs = viewTabs(t);
|
||||
const menu = inspectorOptions(t);
|
||||
expect(tabs).toEqual([
|
||||
{ label: "Recipe", value: "recipe" },
|
||||
{ label: "Work plan", value: "workPlan" },
|
||||
]);
|
||||
expect(menu.map((o) => o.value)).toEqual(["ast", "json", "json-tree"]);
|
||||
// Read from the registry, not listed here: a view added there needs no
|
||||
// edit in this test.
|
||||
expect(tabs.map((o) => o.value)).toEqual(
|
||||
outputs.filter((o) => o.kind === "renderer").map((o) => o.id),
|
||||
);
|
||||
expect(menu.map((o) => o.value)).toEqual(
|
||||
outputs.filter((o) => o.kind === "inspector").map((o) => o.id),
|
||||
);
|
||||
expect([...tabs, ...menu].map((o) => o.value)).toEqual(
|
||||
outputs.map((o) => o.id),
|
||||
);
|
||||
expect(tabs).toContainEqual({ label: "Recipe", value: "recipe" });
|
||||
expect(tabs).toContainEqual({ label: "Work plan", value: "workPlan" });
|
||||
});
|
||||
|
||||
it("names every output, in every language", () => {
|
||||
for (const lang of ["en", "fr"] as const) {
|
||||
const names = getDictionary(lang).renderer.outputs;
|
||||
for (const output of outputs) expect(names[output.id]).toBeTruthy();
|
||||
}
|
||||
});
|
||||
|
||||
it("names them in the playground's language", () => {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { afterEach, describe, expect, it } from "bun:test";
|
||||
import { outputs } from "@gram-lang/renderer";
|
||||
import { Client, type Note, URI } from "./helpers/lsp-client";
|
||||
|
||||
/*
|
||||
|
|
@ -76,14 +77,8 @@ describe("the views protocol", () => {
|
|||
};
|
||||
};
|
||||
const listed = result.capabilities.experimental?.gramViews?.outputs ?? [];
|
||||
expect(listed.map((o) => o.id)).toEqual([
|
||||
"recipe",
|
||||
"workPlan",
|
||||
"ast",
|
||||
"json",
|
||||
"json-tree",
|
||||
]);
|
||||
expect(listed[1]?.label).toBe("Work plan");
|
||||
expect(listed.map((o) => o.id)).toEqual(outputs.map((o) => o.id));
|
||||
expect(listed.find((o) => o.id === "workPlan")?.label).toBe("Work plan");
|
||||
}, 20_000);
|
||||
|
||||
it("sends nothing to a views client until it says what is open", async () => {
|
||||
|
|
|
|||
|
|
@ -16,12 +16,13 @@ A view never imports another view. What two views share goes in `shared/`. `test
|
|||
## Adding a view
|
||||
|
||||
1. Create `src/views/<id>/`. It imports only its own files, `shared/`, the contract, the settings, the types, `prepareViews` and the pipeline packages.
|
||||
2. Write its `view.ts`, a `ViewDefinition`: its `formats` (only those it can really express), the settings it `honors`, what it `needs` from the input (`schedule`, `runSheet`, `serveTimes`), and `render()`. Return `null` when the recipe has nothing for it.
|
||||
2. Write its `view.ts`, a `ViewDefinition`: its `formats` (only those it can really express, and always `html`: the editor's panel and the playground show a view in html; `gram export` writes `markdown`, `print` and `text`, and `gram print` needs `print`, so a view with `html` alone is on screen only), the settings it `honors`, what it `needs` from the input (`schedule`, `runSheet`, `serveTimes`), and `render()`. Return `null` when the recipe has nothing for it.
|
||||
3. Add it to `views` in `src/views/registry.ts`, and to `outputs` in `src/outputs.ts` (written out, not spread, so a bundler can drop the views a page does not use). Give it a name in every language, under `renderer.outputs` of `@gram-lang/i18n`: the surfaces list the outputs by that name.
|
||||
4. Run `tests/views-contract.test.ts`. It renders the view in each format for every fixture, checks the escaping, and checks that the view reacts to each setting it honors and to no other. If it honors a setting none of the fixtures shows, extend a fixture.
|
||||
5. If the view brings a new setting: add its descriptor to `VIEW_SETTINGS` in `src/settings.ts`, a probe to `PROBES` in the contract test, a `gram.*` key in the VS Code extension's `package.json` (the parity test fails until it exists), and the docs in English and French.
|
||||
6. Document the view in English and French (what it shows, its formats, the settings it follows), and add a changeset: a new view is a new feature of every surface.
|
||||
|
||||
Nothing else should need an edit: the surfaces list their outputs from the registry, so a new view appears in the CLI, VS Code and the playground. Removing a view is the reverse.
|
||||
Nothing else should need an edit, not even a test of a surface: the surfaces list their outputs from the registry, so a new view appears in the CLI, VS Code and the playground. Removing a view is the reverse.
|
||||
|
||||
Every surface lists its outputs from the registry: the language server and the VS Code extension, the playground (as tabs, with the formats of each view), and the CLI (`--view` of `gram export` and `gram print`, each `--format` checked against the view's `formats`). The terminal views of `gram view` and `gram cook` are not renderer strings and stay apart.
|
||||
|
||||
|
|
|
|||
|
|
@ -46,11 +46,17 @@ const input = prepareViews(compiled, settings, { lang: "fr" });
|
|||
|
||||
describe("the views registry", () => {
|
||||
it("lists each view once, with formats and settings that exist", () => {
|
||||
// Read from the registry, not listed here: a view added there needs no
|
||||
// edit in this test.
|
||||
const ids = views.map((view) => view.id);
|
||||
expect(ids).toEqual(["recipe", "workPlan"]);
|
||||
expect(new Set(ids).size).toBe(ids.length);
|
||||
expect(ids).toContain("recipe");
|
||||
expect(ids).toContain("workPlan");
|
||||
for (const view of views) {
|
||||
expect(view.kind).toBe("renderer");
|
||||
expect(view.formats.length).toBeGreaterThan(0);
|
||||
// Every view can be shown on screen: the editor's panel and the
|
||||
// playground's preview render it in html.
|
||||
expect(view.formats).toContain("html");
|
||||
for (const format of view.formats) expect(FORMATS).toContain(format);
|
||||
for (const key of view.honors)
|
||||
expect(Object.keys(VIEW_SETTINGS)).toContain(key);
|
||||
|
|
|
|||
Loading…
Reference in a new issue