@webority/tokens 0.149.0 → 0.150.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.
Files changed (71) hide show
  1. package/dist/brand.d.ts +39 -25
  2. package/dist/brand.js +2 -2
  3. package/dist/build.d.ts +41 -0
  4. package/dist/build.js +5 -5
  5. package/dist/canvas/webority-phone.json +1 -1
  6. package/dist/canvas/webority-web-390.json +1 -1
  7. package/dist/canvas/webority-web.json +1 -1
  8. package/dist/check.d.ts +37 -0
  9. package/dist/check.js +3 -3
  10. package/dist/cli.d.ts +15 -96
  11. package/dist/cli.js +1 -1
  12. package/dist/emit/canvas.d.ts +8 -0
  13. package/dist/emit/canvas.js +1 -1
  14. package/dist/emit/email.d.ts +32 -0
  15. package/dist/emit/email.js +2 -2
  16. package/dist/emit/meanings.d.ts +3 -0
  17. package/dist/emit/phone.d.ts +26 -0
  18. package/dist/emit/phone.js +3 -3
  19. package/dist/emit/web.d.ts +18 -0
  20. package/dist/emit/web.js +14 -14
  21. package/dist/guards.d.ts +3 -0
  22. package/dist/guards.js +1 -0
  23. package/dist/index.d.ts +7 -7
  24. package/dist/meanings.json +1 -1
  25. package/dist/model/colour.d.ts +8 -0
  26. package/dist/model/colour.js +1 -1
  27. package/dist/model/contrast.d.ts +26 -0
  28. package/dist/model/contrast.js +1 -1
  29. package/dist/model/index.d.ts +52 -0
  30. package/dist/model/length.d.ts +21 -0
  31. package/dist/model/length.js +1 -1
  32. package/dist/model/load.d.ts +32 -0
  33. package/dist/model/load.js +1 -1
  34. package/dist/model/references.d.ts +3 -0
  35. package/dist/model/references.js +1 -1
  36. package/dist/model/resolve.d.ts +4 -0
  37. package/dist/model/resolve.js +2 -2
  38. package/dist/names.d.ts +1 -11
  39. package/dist/names.js +1 -1
  40. package/dist/outputs.d.ts +51 -102
  41. package/dist/present.d.ts +1 -0
  42. package/dist/present.js +1 -0
  43. package/dist/ramp.d.ts +8 -0
  44. package/dist/ramp.js +1 -1
  45. package/dist/upgrade/classes.d.ts +13 -0
  46. package/dist/upgrade/classes.js +2 -2
  47. package/dist/upgrade/components.d.ts +19 -0
  48. package/dist/upgrade/components.js +3 -3
  49. package/dist/upgrade/css.d.ts +28 -0
  50. package/dist/upgrade/css.js +2 -2
  51. package/dist/upgrade/index.d.ts +64 -0
  52. package/dist/upgrade/index.js +2 -2
  53. package/dist/upgrade/razorAttributes.d.ts +19 -0
  54. package/dist/upgrade/razorAttributes.js +2 -2
  55. package/dist/upgrade/razorTags.d.ts +19 -0
  56. package/dist/upgrade/razorTags.js +2 -2
  57. package/dist/upgrade/razorValueSplits.d.ts +19 -0
  58. package/dist/upgrade/razorValueSplits.js +2 -2
  59. package/dist/upgrade/source.d.ts +19 -0
  60. package/dist/upgrade/source.js +3 -3
  61. package/dist/upgrade/tagScan.d.ts +7 -0
  62. package/dist/upgrade/tagScan.js +1 -1
  63. package/dist/upgrade/themeReads.d.ts +55 -0
  64. package/dist/upgrade/themeReads.js +2 -2
  65. package/dist/upgrade/valueSplits.d.ts +23 -0
  66. package/dist/upgrade/valueSplits.js +3 -3
  67. package/dist/upgrade/wrapperValues.d.ts +71 -0
  68. package/dist/upgrade/wrapperValues.js +1 -1
  69. package/dist/webority.d.ts +1 -0
  70. package/package.json +7 -3
  71. package/renames/renames.d.ts +1 -1
package/dist/brand.d.ts CHANGED
@@ -1,11 +1,23 @@
1
+ /**
2
+ * A product brand file may override palette steps, colour meanings, brand-only colours and effects
3
+ * (each with a $description), fonts and radius steps. A recorded deviation lifts the refusal of the
4
+ * groups it lists. Every override of a meaning that has a dark value carries it beside the light value,
5
+ * because the dark context is built from those values alone. The email group is checked against the
6
+ * email role tables and resolved by emitEmail, so its colour references never meet the base layer.
7
+ * A palette.primary written as #RRGGBB also supplies every primary step the file leaves out (ramp.ts); a step the file writes wins.
8
+ * 1a.L7a calls loadBrand once per brand, before loadModel.
9
+ */
10
+ export type JsonValue = string | number | boolean | null | JsonValue[] | JsonObject;
11
+ export interface JsonObject {
12
+ [key: string]: JsonValue;
13
+ }
1
14
  /** The parsed library source trees the model already holds. */
2
15
  export interface LibrarySources {
3
16
  palette: object;
4
- scale: object;
17
+ scale: JsonObject;
5
18
  meaning: object;
6
19
  modeDark: object;
7
20
  }
8
-
9
21
  /** The brand file's email group as written (DTCG tokens). Each key is an email.json role key. */
10
22
  export interface BrandEmailGroup {
11
23
  /** <email colour role>: { $type: "color", $value: "#RRGGBB" | "{color.<meaning>}", $extensions.com.webority.dark?: "#RRGGBB" | "{color.<meaning>}" } */
@@ -17,38 +29,40 @@ export interface BrandEmailGroup {
17
29
  /** <email shape key>: { $type: "dimension", $value: { value, unit: "px" } } */
18
30
  shape?: Record<string, object>;
19
31
  /** radius: a dimension token; padding: { $value: "14px 28px" } */
20
- button?: { radius?: object; padding?: object };
32
+ button?: {
33
+ radius?: object;
34
+ padding?: object;
35
+ };
21
36
  }
22
-
23
37
  /** A validated brand: base overrides every mode, dark overrides the dark context only. */
24
38
  export interface BrandLayer {
25
39
  name: string;
26
- base: object;
27
- dark: object;
40
+ base: JsonObject;
41
+ dark: JsonObject;
28
42
  /** The email group, kept outside base because the web model never reads it; null when the file has none. */
29
43
  email: BrandEmailGroup | null;
30
44
  /** Phone-only overrides the brand tool derives for a readable primary pair (color.primary per mode); loadBrand never sets it. */
31
- phone?: { light: object; dark: object };
45
+ phone?: {
46
+ light: JsonObject;
47
+ dark: JsonObject;
48
+ };
32
49
  }
33
-
34
50
  /** A brand group a recorded deviation can allow beyond palette, color, effect, font and radius. */
35
51
  export type DeviationGroup = "space" | "text" | "shadow" | "section" | "component" | "email";
36
-
37
52
  /** The recorded deviations, keyed by slug (src/deviations.json); groups lists the brand groups each one allows. */
38
- export type Deviations = Readonly<Record<string, { reason: string; groups: readonly DeviationGroup[] }>>;
39
-
40
- /** Reads and validates a product brand file; throws one Error listing every problem. */
41
- export declare function loadBrand(path: string, library: LibrarySources, deviations: Deviations): BrandLayer;
42
-
43
- export declare const BRAND_OUTPUT_FILES: {
44
- readonly css: "brand.css";
45
- readonly bootstrap: "_brand-variables.scss";
46
- readonly web: "webority-web.json";
47
- readonly web390: "webority-web-390.json";
48
- readonly phoneTheme: "webority-phone.json";
49
- readonly phone: "brand.phone.js";
50
- readonly email: "email.json";
51
- };
52
-
53
+ export type Deviations = Readonly<Record<string, {
54
+ reason: string;
55
+ groups: readonly DeviationGroup[];
56
+ }>>;
57
+ export declare const BRAND_OUTPUT_FILES: Readonly<{
58
+ css: "brand.css";
59
+ bootstrap: "_brand-variables.scss";
60
+ web: "webority-web.json";
61
+ web390: "webority-web-390.json";
62
+ phoneTheme: "webority-phone.json";
63
+ phone: "brand.phone.js";
64
+ email: "email.json";
65
+ }>;
53
66
  /** The folder inside each head that receives the copies of the generated files. */
54
- export declare const HEAD_FOLDER: "generated";
67
+ export declare const HEAD_FOLDER = "generated";
68
+ export declare function loadBrand(path: string, library: LibrarySources, deviations: Deviations): BrandLayer;
package/dist/brand.js CHANGED
@@ -1,2 +1,2 @@
1
- import{EMAIL_COLOR_ROLES as e,EMAIL_PADDING_PATTERN as t,EMAIL_PADDING_ROLES as n,EMAIL_SHAPE as r,EMAIL_SPACE as i,EMAIL_TYPE_ROLES as a}from"./emit/email.js";import{RAMP_STEPS as o,deriveRamp as s}from"./ramp.js";import{readFileSync as c}from"node:fs";var l=Object.freeze({css:`brand.css`,bootstrap:`_brand-variables.scss`,web:`webority-web.json`,web390:`webority-web-390.json`,phoneTheme:`webority-phone.json`,phone:`brand.phone.js`,email:`email.json`}),u=`generated`,d=/\{([a-z0-9-]+(?:\.[a-z0-9-]+)+)\}/g,f=[`palette`,`color`,`effect`,`font`,`radius`],p=[`space`,`text`,`shadow`,`section`,`component`,`email`],m=[`radius`,`padding`],h=[`size`,`lineHeight`,`phoneSize`,`phoneLineHeight`],g=[`narrowSize`,`narrowLineHeight`],_=/^(#[0-9a-fA-F]{6}|\{color\.[a-z0-9-]+\})$/;function v(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function y(e){return v(e)&&Object.hasOwn(e,`$value`)}function b(e,t){let n=[],r=(e,t)=>{for(let i of Object.keys(e)){if(i.startsWith(`$`))continue;let a=e[i],o=`${t}.${i}`;y(a)?n.push({path:o,node:a}):v(a)&&r(a,o)}};return r(e,t),n}function x(e,t){let n=e;for(let e of t.split(`.`)){if(!v(n)||!Object.hasOwn(n,e))return!1;n=n[e]}return y(n)}function S(e){let t=e?.$extensions?.[`com.webority`];return v(t)?t:{}}function C(e){return/^[a-z0-9]+(-[a-z0-9]+)*$/.test(e)&&!/^[0-9]+$/.test(e)}function w(e){return typeof e==`string`&&_.test(e)}function T(e){return v(e)&&Number.isInteger(e.value)&&e.value>=0&&e.unit===`px`}function E(e,t){return e===`space`?n.has(t):e===`button`&&t===`padding`}function D(e,n,r,i,a){let o=S(r);for(let t of Object.keys(o))(e!==`color`||t!==`dark`)&&a(i,`$extensions.com.webority.${t} is not allowed on this token`,`Remove it`);if(e===`color`)w(r.$value)||a(i,`$value is not a colour`,`Write "#RRGGBB" or a reference such as "{color.primary}"`),Object.hasOwn(o,`dark`)&&!w(o.dark)&&a(i,`$extensions.com.webority.dark is not a colour`,`Write "#RRGGBB" or a reference such as "{color.primary}"`);else if(e===`type`){let e=r.$value,t=v(e)?Object.keys(e):[],n=t.includes(`narrowSize`)||t.includes(`narrowLineHeight`)?[...h,...g]:h;v(e)&&t.length===n.length&&n.every(t=>Number.isInteger(e[t])&&e[t]>0)||a(i,`$value is not { size, lineHeight, phoneSize, phoneLineHeight } (optionally narrowSize, narrowLineHeight) in whole px`,`Write it as { "size": 16, "lineHeight": 24, "phoneSize": 15, "phoneLineHeight": 22 }`)}else E(e,n)?typeof r.$value==`string`&&t.test(r.$value)||a(i,`$value is not CSS px padding`,`Write it as "22px 24px", one to four px values or 0`):T(r.$value)||a(i,`$value is not { value, unit }`,`Write it as { "value": 12, "unit": "px" }`)}function O(t,n){let o={color:Object.keys(e),type:Object.keys(a),space:Object.keys(i),shape:Object.keys(r),button:m};for(let e of Object.keys(t))if(!e.startsWith(`$`)){if(!Object.hasOwn(o,e))n(`email.${e}`,`is not an email group`,`Use one of ${Object.keys(o).join(`, `)}`);else if(!v(t[e]))n(`email.${e}`,`is not a group`,`Write it as an object of tokens`);else for(let r of Object.keys(t[e])){if(r.startsWith(`$`))continue;let i=`email.${e}.${r}`;if(!o[e].includes(r)){n(i,`is not an email role`,`Use one of ${o[e].join(`, `)}`);continue}let a=t[e][r];y(a)?D(e,r,a,i,n):n(i,`is not a token`,`Give it a $value`)}}}function k(e,t,n){let r;try{r=JSON.parse(c(e,`utf8`))}catch(t){throw Error(`${e}: (file): cannot be read as JSON (${t.message}). Restore the file or fix the JSON`)}if(!v(r))throw Error(`${e}: (file): is not a JSON object. Write the brand as one object with the groups palette, color, effect, font and radius`);let i=[],a=(t,n,r)=>{let a=`${e}: ${t}: ${n}. ${r}`;i.includes(a)||i.push(a)},l=S(r);for(let e of Object.keys(l))e!==`brand`&&e!==`deviation`&&a(`$extensions.com.webority.${e}`,`is not a brand file key`,`Remove it`);let u=Object.hasOwn(l,`deviation`),m=u&&typeof l.deviation==`string`&&Object.hasOwn(n,l.deviation);u&&!m&&a(`$extensions.com.webority.deviation`,`${l.deviation} is not in deviations.json`,`Use one of: ${Object.keys(n).join(`, `)}`),typeof l.brand==`string`&&/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/.test(l.brand)||a(`$extensions.com.webority.brand`,`is missing or not kebab case`,`Name the product in kebab case, such as acme`);let h=m?n[l.deviation].groups:[],g={},_=null;for(let e of Object.keys(r))e.startsWith(`$`)||(!f.includes(e)&&!p.includes(e)?a(e,`is not a brand group`,`A brand file holds only palette, color, effect, font and radius, plus the groups a recorded deviation allows`):p.includes(e)&&!h.includes(e)?a(e,`this group needs a recorded deviation`,`Add $extensions.com.webority.deviation naming an entry of deviations.json, or remove the group`):v(r[e])?e===`email`?_=r[e]:g[e]=r[e]:a(e,`is not a group`,`Write it as an object of tokens`));for(let[e,t]of Object.entries(g))for(let{path:n,node:r}of b(t,e)){let t=S(r);for(let r of Object.keys(t))r!==`platforms`&&r!==`dark`&&(r!==`faces`||e!==`font`)&&a(n,`$extensions.com.webority.${r} is not allowed on a token`,`Remove it`);if(Object.hasOwn(t,`platforms`)){let e=t.platforms;Array.isArray(e)&&e.length>0&&e.every(e=>e===`web`||e===`phone`)||a(n,`$extensions.com.webority.platforms is not a list of web and phone`,`Write it as ["phone"], ["web"] or both`)}Object.hasOwn(t,`dark`)&&e!==`color`&&e!==`effect`&&a(n,`$extensions.com.webority.dark applies to colour and effect tokens only`,`Remove it`)}for(let{path:e,node:t}of b(g.palette??{},`palette`))e.split(`.`).slice(1).every(C)||a(e,`the step name is not kebab case`,`Name it such as primary-500`),typeof t.$value!=`string`&&a(e,`$value is not a string`,`Write the colour as text such as "#0F766E" or a reference such as "{palette.primary}"`);let w=(e,n,r)=>{let i=`${e}.${n}`,o=S(r),s=x(t.meaning,i);typeof r.$value!=`string`&&a(i,`$value is not a string`,e===`color`?`Write a colour or a reference such as "{palette.primary}"`:`Write the effect as text such as a filter, colour or gradient`),s?x(t.modeDark,i)&&typeof o.dark!=`string`&&a(i,`needs $extensions.com.webority.dark because the meaning has a dark value`,`Add the dark value beside the light one`):(C(n)||a(i,`the name is not kebab case`,`Name it in kebab case`),(typeof r.$description!=`string`||r.$description===``)&&a(i,`is not a fleet meaning and has no $description`,`Add a $description saying what the colour is for, or use a meaning the library defines`)),Object.hasOwn(o,`dark`)&&typeof o.dark!=`string`&&a(i,`$extensions.com.webority.dark is not a string`,`Write a colour or a reference`)};for(let e of[`color`,`effect`]){let t=g[e]??{};for(let n of Object.keys(t))n.startsWith(`$`)||(y(t[n])?w(e,n,t[n]):a(`${e}.${n}`,`is not a token`,`Give it a $value`))}let T=Object.keys(t.scale.font??{}).filter(e=>y(t.scale.font[e]));for(let{path:e,node:n}of b(g.font??{},`font`)){x(t.scale,e)||a(e,`is not a fleet font`,`Use one of ${T.join(`, `)}`);let r=n.$value;if(Array.isArray(r)&&r.length!==0&&r.every(e=>typeof e==`string`)||a(e,`$value is not a list of font family names`,`Write it as ["Webority Sans", "system-ui", "sans-serif"]`),Object.hasOwn(S(n),`faces`)){let t=S(n).faces,r=[`normal`,`medium`,`semibold`,`bold`];v(t)&&Object.keys(t).length>0&&Object.keys(t).every(e=>r.includes(e))&&Object.values(t).every(e=>typeof e==`string`&&e!==``)||a(e,`faces is not a map of weight to font file name`,`Write it as { "normal": "AcmeSans-Regular", "semibold": "AcmeSans-SemiBold" }`)}}let E=Object.keys(t.scale.radius??{}).filter(e=>y(t.scale.radius[e]));for(let{path:e,node:n}of b(g.radius??{},`radius`)){x(t.scale,e)||a(e,`is not on the radius scale`,`Use one of ${E.join(`, `)}`);let r=n.$value;v(r)&&typeof r.value==`number`&&Number.isFinite(r.value)&&r.value>=0&&r.unit===`px`||a(e,`$value is not { value, unit }`,`Write it as { "value": 12, "unit": "px" }`)}_!==null&&O(_,a);let D=structuredClone(r);delete D.$extensions,delete D.$description,delete D.email;for(let e of[`color`,`effect`])for(let{node:t}of b(D[e]??{},e))Object.hasOwn(S(t),`dark`)&&(delete t.$extensions[`com.webority`].dark,Object.keys(t.$extensions[`com.webority`]).length===0&&delete t.$extensions[`com.webority`],Object.keys(t.$extensions).length===0&&delete t.$extensions);let k=g.palette?.primary?.$value;if(typeof k==`string`&&/^#[0-9a-fA-F]{6}$/.test(k)){let e=s(k);for(let t of o)x(D,`palette.${t}`)||(D.palette[t]={$type:`color`,$value:e[t]})}let A=e=>x(D,e)||x(t.palette,e)||x(t.scale,e)||x(t.meaning,e);for(let e of Object.keys(g))for(let{path:t,node:n}of b(g[e],e)){let e=[n.$value,S(n).dark].filter(e=>typeof e==`string`);for(let n of e)for(let e of n.matchAll(d))A(e[1])||a(t,`references {${e[1]}}, which no palette step, meaning, scale step or brand token defines`,`Name an existing token or add it to the brand file`)}if(i.length>0)throw Error(i.join(`
2
- `));let j={};for(let e of[`color`,`effect`])for(let{path:t,node:n}of b(g[e]??{},e)){let r=S(n).dark;if(typeof r!=`string`)continue;let i=t.slice(e.length+1);j[e]??={},j[e][i]=n.$type===void 0?{$value:r}:{$value:r,$type:n.$type}}return{name:l.brand,base:D,dark:j,email:_}}export{l as BRAND_OUTPUT_FILES,u as HEAD_FOLDER,k as loadBrand};
1
+ import{EMAIL_COLOR_ROLES as e,EMAIL_PADDING_PATTERN as t,EMAIL_PADDING_ROLES as n,EMAIL_SHAPE as r,EMAIL_SPACE as i,EMAIL_TYPE_ROLES as a}from"./emit/email.js";import{messageOf as o}from"./guards.js";import{RAMP_STEPS as s,deriveRamp as c}from"./ramp.js";import{readFileSync as l}from"node:fs";var u=Object.freeze({css:`brand.css`,bootstrap:`_brand-variables.scss`,web:`webority-web.json`,web390:`webority-web-390.json`,phoneTheme:`webority-phone.json`,phone:`brand.phone.js`,email:`email.json`}),d=`generated`,f=/\{([a-z0-9-]+(?:\.[a-z0-9-]+)+)\}/g,p=[`palette`,`color`,`effect`,`font`,`radius`],m=[`space`,`text`,`shadow`,`section`,`component`,`email`],h=[`radius`,`padding`],g=[`size`,`lineHeight`,`phoneSize`,`phoneLineHeight`],_=[`narrowSize`,`narrowLineHeight`],v=/^(#[0-9a-fA-F]{6}|\{color\.[a-z0-9-]+\})$/;function y(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function b(e){return y(e)?e:{}}function x(e){return y(e)&&Object.hasOwn(e,`$value`)}function S(e,t){let n=[],r=(e,t)=>{for(let i of Object.keys(e)){if(i.startsWith(`$`))continue;let a=e[i],o=`${t}.${i}`;x(a)?n.push({path:o,node:a}):y(a)&&r(a,o)}};return r(e,t),n}function C(e,t){let n=e;for(let e of t.split(`.`)){if(!y(n)||!Object.hasOwn(n,e))return!1;n=n[e]}return x(n)}function w(e){let t=y(e)?e.$extensions:void 0,n=y(t)?t[`com.webority`]:void 0;return y(n)?n:{}}function T(e){return/^[a-z0-9]+(-[a-z0-9]+)*$/.test(e)&&!/^[0-9]+$/.test(e)}function E(e){return typeof e==`string`&&v.test(e)}function D(e){return y(e)&&typeof e.value==`number`&&Number.isInteger(e.value)&&e.value>=0&&e.unit===`px`}function O(e){return typeof e==`number`&&Number.isInteger(e)&&e>0}function k(e,t){return e===`space`?n.has(t):e===`button`&&t===`padding`}function A(e,n,r,i,a){let o=w(r);for(let t of Object.keys(o))(e!==`color`||t!==`dark`)&&a(i,`$extensions.com.webority.${t} is not allowed on this token`,`Remove it`);if(e===`color`)E(r.$value)||a(i,`$value is not a colour`,`Write "#RRGGBB" or a reference such as "{color.primary}"`),Object.hasOwn(o,`dark`)&&!E(o.dark)&&a(i,`$extensions.com.webority.dark is not a colour`,`Write "#RRGGBB" or a reference such as "{color.primary}"`);else if(e===`type`){let e=r.$value,t=y(e)?Object.keys(e):[],n=t.includes(`narrowSize`)||t.includes(`narrowLineHeight`)?[...g,..._]:g;y(e)&&t.length===n.length&&n.every(t=>O(e[t]))||a(i,`$value is not { size, lineHeight, phoneSize, phoneLineHeight } (optionally narrowSize, narrowLineHeight) in whole px`,`Write it as { "size": 16, "lineHeight": 24, "phoneSize": 15, "phoneLineHeight": 22 }`)}else k(e,n)?typeof r.$value==`string`&&t.test(r.$value)||a(i,`$value is not CSS px padding`,`Write it as "22px 24px", one to four px values or 0`):D(r.$value)||a(i,`$value is not { value, unit }`,`Write it as { "value": 12, "unit": "px" }`)}function j(e){return y(e)&&[`color`,`type`,`space`,`shape`,`button`].every(t=>e[t]===void 0||y(e[t]))}function M(t,n){let o=new Map([[`color`,Object.keys(e)],[`type`,Object.keys(a)],[`space`,Object.keys(i)],[`shape`,Object.keys(r)],[`button`,h]]);for(let e of Object.keys(t)){if(e.startsWith(`$`))continue;let r=o.get(e);if(r===void 0){n(`email.${e}`,`is not an email group`,`Use one of ${[...o.keys()].join(`, `)}`);continue}let i=t[e];if(!y(i))n(`email.${e}`,`is not a group`,`Write it as an object of tokens`);else for(let t of Object.keys(i)){if(t.startsWith(`$`))continue;let a=`email.${e}.${t}`;if(!r.includes(t)){n(a,`is not an email role`,`Use one of ${r.join(`, `)}`);continue}let o=i[t];x(o)?A(e,t,o,a,n):n(a,`is not a token`,`Give it a $value`)}}}function N(e,t,n){let r;try{r=JSON.parse(l(e,`utf8`))}catch(t){throw Error(`${e}: (file): cannot be read as JSON (${o(t)}). Restore the file or fix the JSON`)}if(!y(r))throw Error(`${e}: (file): is not a JSON object. Write the brand as one object with the groups palette, color, effect, font and radius`);let i=[],a=(t,n,r)=>{let a=`${e}: ${t}: ${n}. ${r}`;i.includes(a)||i.push(a)},u=w(r);for(let e of Object.keys(u))e!==`brand`&&e!==`deviation`&&a(`$extensions.com.webority.${e}`,`is not a brand file key`,`Remove it`);let d=Object.hasOwn(u,`deviation`),h=typeof u.deviation==`string`?u.deviation:void 0,g=d&&h!==void 0&&Object.hasOwn(n,h);d&&!g&&a(`$extensions.com.webority.deviation`,`${u.deviation} is not in deviations.json`,`Use one of: ${Object.keys(n).join(`, `)}`);let _=typeof u.brand==`string`?u.brand:``;/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/.test(_)||a(`$extensions.com.webority.brand`,`is missing or not kebab case`,`Name the product in kebab case, such as acme`);let v=h===void 0?void 0:n[h],E=g&&v!==void 0?v.groups:[],D={},O=null;for(let e of Object.keys(r)){if(e.startsWith(`$`))continue;if(!p.includes(e)&&!m.includes(e)){a(e,`is not a brand group`,`A brand file holds only palette, color, effect, font and radius, plus the groups a recorded deviation allows`);continue}if(m.includes(e)&&!E.includes(e)){a(e,`this group needs a recorded deviation`,`Add $extensions.com.webority.deviation naming an entry of deviations.json, or remove the group`);continue}let t=r[e];y(t)?e===`email`?O=t:D[e]=t:a(e,`is not a group`,`Write it as an object of tokens`)}for(let[e,t]of Object.entries(D))for(let{path:n,node:r}of S(t,e)){let t=w(r);for(let r of Object.keys(t))r!==`platforms`&&r!==`dark`&&(r!==`faces`||e!==`font`)&&a(n,`$extensions.com.webority.${r} is not allowed on a token`,`Remove it`);if(Object.hasOwn(t,`platforms`)){let e=t.platforms;Array.isArray(e)&&e.length>0&&e.every(e=>e===`web`||e===`phone`)||a(n,`$extensions.com.webority.platforms is not a list of web and phone`,`Write it as ["phone"], ["web"] or both`)}Object.hasOwn(t,`dark`)&&e!==`color`&&e!==`effect`&&a(n,`$extensions.com.webority.dark applies to colour and effect tokens only`,`Remove it`)}for(let{path:e,node:t}of S(D.palette??{},`palette`))e.split(`.`).slice(1).every(T)||a(e,`the step name is not kebab case`,`Name it such as primary-500`),typeof t.$value!=`string`&&a(e,`$value is not a string`,`Write the colour as text such as "#0F766E" or a reference such as "{palette.primary}"`);let k=(e,n,r)=>{let i=`${e}.${n}`,o=w(r),s=C(t.meaning,i);typeof r.$value!=`string`&&a(i,`$value is not a string`,e===`color`?`Write a colour or a reference such as "{palette.primary}"`:`Write the effect as text such as a filter, colour or gradient`),s?C(t.modeDark,i)&&typeof o.dark!=`string`&&a(i,`needs $extensions.com.webority.dark because the meaning has a dark value`,`Add the dark value beside the light one`):(T(n)||a(i,`the name is not kebab case`,`Name it in kebab case`),(typeof r.$description!=`string`||r.$description===``)&&a(i,`is not a fleet meaning and has no $description`,`Add a $description saying what the colour is for, or use a meaning the library defines`)),Object.hasOwn(o,`dark`)&&typeof o.dark!=`string`&&a(i,`$extensions.com.webority.dark is not a string`,`Write a colour or a reference`)};for(let e of[`color`,`effect`]){let t=D[e]??{};for(let n of Object.keys(t)){if(n.startsWith(`$`))continue;let r=t[n];x(r)?k(e,n,r):a(`${e}.${n}`,`is not a token`,`Give it a $value`)}}let A=b(t.scale.font),N=Object.keys(A).filter(e=>x(A[e]));for(let{path:e,node:n}of S(D.font??{},`font`)){C(t.scale,e)||a(e,`is not a fleet font`,`Use one of ${N.join(`, `)}`);let r=n.$value;if(Array.isArray(r)&&r.length!==0&&r.every(e=>typeof e==`string`)||a(e,`$value is not a list of font family names`,`Write it as ["Webority Sans", "system-ui", "sans-serif"]`),Object.hasOwn(w(n),`faces`)){let t=w(n).faces,r=[`normal`,`medium`,`semibold`,`bold`];y(t)&&Object.keys(t).length>0&&Object.keys(t).every(e=>r.includes(e))&&Object.values(t).every(e=>typeof e==`string`&&e!==``)||a(e,`faces is not a map of weight to font file name`,`Write it as { "normal": "AcmeSans-Regular", "semibold": "AcmeSans-SemiBold" }`)}}let P=b(t.scale.radius),F=Object.keys(P).filter(e=>x(P[e]));for(let{path:e,node:n}of S(D.radius??{},`radius`)){C(t.scale,e)||a(e,`is not on the radius scale`,`Use one of ${F.join(`, `)}`);let r=n.$value,i=y(r)&&typeof r.value==`number`&&Number.isFinite(r.value)&&r.value>=0,o=y(r)?r.unit:void 0;i&&o===`px`||a(e,`$value is not { value, unit }`,`Write it as { "value": 12, "unit": "px" }`)}O!==null&&M(O,a);let I=structuredClone(r);delete I.$extensions,delete I.$description,delete I.email;for(let e of[`color`,`effect`])for(let{node:t}of S(b(I[e]),e))Object.hasOwn(w(t),`dark`)&&(delete w(t).dark,y(t.$extensions)&&(Object.keys(w(t)).length===0&&delete t.$extensions[`com.webority`],Object.keys(t.$extensions).length===0&&delete t.$extensions));let L=D.palette?.primary,R=x(L)?L.$value:void 0;if(typeof R==`string`&&/^#[0-9a-fA-F]{6}$/.test(R)){let e=c(R),t=I.palette;if(!y(t))throw Error(`webority-tokens: the palette group is missing.`);for(let n of s)C(I,`palette.${n}`)||(t[n]={$type:`color`,$value:e[n]})}let z=e=>C(I,e)||C(t.palette,e)||C(t.scale,e)||C(t.meaning,e);for(let e of Object.keys(D))for(let{path:t,node:n}of S(D[e]??{},e)){let e=[n.$value,w(n).dark].filter(e=>typeof e==`string`);for(let n of e)for(let e of n.matchAll(f)){let[,n=``]=e;z(n)||a(t,`references {${n}}, which no palette step, meaning, scale step or brand token defines`,`Name an existing token or add it to the brand file`)}}if(i.length>0)throw Error(i.join(`
2
+ `));let B={};for(let e of[`color`,`effect`])for(let{path:t,node:n}of S(D[e]??{},e)){let r=w(n).dark;if(typeof r!=`string`)continue;let i=t.slice(e.length+1),a=B[e];a===void 0&&(a={},B[e]=a),a[i]=n.$type===void 0?{$value:r}:{$value:r,$type:n.$type}}if(O!==null&&!j(O))throw Error(`${e}: email: is not an email group. Write it as an object of email role groups.`);return{name:_,base:I,dark:B,email:O}}export{u as BRAND_OUTPUT_FILES,d as HEAD_FOLDER,N as loadBrand};
@@ -0,0 +1,41 @@
1
+ export interface BrandOptions {
2
+ root?: string;
3
+ library?: boolean;
4
+ brand?: string;
5
+ out?: string;
6
+ web?: string;
7
+ phone?: string;
8
+ check?: boolean;
9
+ }
10
+ export interface BrandResult {
11
+ written: string[];
12
+ stale: string[];
13
+ }
14
+ export interface BrandEntry {
15
+ name: string;
16
+ source: string;
17
+ export: string;
18
+ }
19
+ export interface HeadEntry {
20
+ path: string;
21
+ brand: string;
22
+ kind: string;
23
+ }
24
+ export interface DesignTokens {
25
+ brands: BrandEntry[];
26
+ heads?: HeadEntry[];
27
+ }
28
+ export interface DesignMap {
29
+ tokens?: DesignTokens;
30
+ color?: {
31
+ roles?: Record<string, string>;
32
+ };
33
+ scale?: Record<string, {
34
+ roles?: Record<string, string>;
35
+ durations?: Record<string, string>;
36
+ }>;
37
+ iconography?: {
38
+ sizes?: Record<string, string>;
39
+ };
40
+ }
41
+ export declare function brand(options: BrandOptions): Promise<BrandResult>;
package/dist/build.js CHANGED
@@ -1,5 +1,5 @@
1
- import{__toESM as e}from"./_virtual/_rolldown/runtime.js";import{require__2020 as t}from"./node_modules/.pnpm/ajv@8.20.0/node_modules/ajv/dist/2020.js";import{BRAND_OUTPUT_FILES as n,HEAD_FOLDER as r,loadBrand as i}from"./brand.js";import a from"./canvas-renames.js";import o from"./deviations.js";import{emitCanvas as s}from"./emit/canvas.js";import{emitEmail as c}from"./emit/email.js";import{emitMeanings as l}from"./emit/meanings.js";import{emitPhoneBrand as u,emitPhoneTheme as d,phoneSets as f}from"./emit/phone.js";import{emitWeb as p}from"./emit/web.js";import{phonePairLayer as m}from"./model/contrast.js";import{loadModel as h}from"./model/index.js";import{mkdir as g,readFile as _,writeFile as v}from"node:fs/promises";import y from"node:path";var b=e(t(),1),x=new URL(`../schema/design-map.schema.json`,import.meta.url),S=new URL(`../package.json`,import.meta.url),C={palette:`palette.tokens.json`,scale:`scale.tokens.json`,meaning:`meaning.tokens.json`,modeDark:`mode.dark.tokens.json`},w=/^.+: \S+: .+\. .+$/;async function T(e){let t=y.resolve(e.root??process.cwd()),r=e.check===!0,{version:C}=JSON.parse(await _(S,`utf8`)),T=[],k=[],A=async(e,t)=>{if(r){let n=null;try{n=await _(e,`utf8`)}catch(e){if(e.code!==`ENOENT`)throw e}n!==t&&k.push(e)}else await g(y.dirname(e),{recursive:!0}),await v(e,t,`utf8`),T.push(e)};if(e.library===!0){if(e.brand!==void 0)throw Error(`brand: --brand: not allowed with --library. Remove it.`);if(!e.out)throw Error(`brand: --out: required with --library. Pass --out <dir>.`);let r=y.resolve(t,e.out),i=await h(),o=s(i),c=[[y.join(r,`canvas`,n.web),E(o.web)],[y.join(r,`canvas`,n.web390),E(o.web390)],[y.join(r,`canvas`,n.phoneTheme),E(o.phone)],[y.join(r,`meanings.json`),E(l(i))],[y.join(r,`canvas`,`renames.json`),E(a)]];if(e.web){let n=p(i),r=y.resolve(t,e.web);c.push([r,n.tokens]),c.push([y.join(y.dirname(r),`_bootstrap.generated.scss`),n.bootstrap])}if(e.phone){let n={light:f(i,`light`),dark:f(i,`dark`)},r=d(n);c.push([y.resolve(t,e.phone),r.contents])}for(let[e,t]of c)await A(e,t);return{written:T,stale:k}}let j=y.join(t,`.webority`,`design.json`),M;try{M=await _(j,`utf8`)}catch(e){throw e.code===`ENOENT`?Error(`${j}: file missing. Run from the repo root or create the design map.`):e}let N;try{N=JSON.parse(M)}catch(e){throw Error(`${j}: /: ${e.message}. Fix the design map.`)}let P=JSON.parse(await _(x,`utf8`)),F=new b.Ajv2020({allErrors:!0,strict:!1}).compile(P);if(!F(N))throw Error(F.errors.map(e=>`${j}: ${e.instancePath||`/`}: ${e.message}. Fix the design map.`).join(`
2
- `));if(N.tokens===void 0)throw Error(`${j}: /tokens: missing. Add tokens.brands to .webority/design.json.`);let I=N.tokens.brands,L=N.tokens.heads??[];if((e.brand!==void 0||e.out!==void 0)&&I.length>1)throw Error(`brand: --brand/--out: the repo has ${I.length} brands. Remove the flag to build every brand.`);let R=[];if(I.forEach((e,t)=>{e.source===`library`&&R.push(`${j}: tokens.brands[${t}].source: "library" is built with --library. Run webority brand --library --out <dir>.`)}),L.forEach((e,t)=>{I.some(t=>t.name===e.brand)||R.push(`${j}: tokens.heads[${t}].brand: no brand named ${e.brand}. Add it to tokens.brands.`)}),R.length>0)throw Error(R.join(`
3
- `));let z=await O(),B=await h(),V={light:f(B,`light`),dark:f(B,`dark`)},H=[];for(let r of I){let a=I.length===1&&e.brand!==void 0?e.brand:r.source,l=I.length===1&&e.out!==void 0?e.out:r.export,d=y.resolve(t,a);try{let e=i(d,z,o),a=await h({brand:e}),g=m(a,B),_=Object.keys(g.light).length+Object.keys(g.dark).length>0?await h({brand:{...e,phone:g}}):a,v=s(_),b=p(_,{library:B}),x={light:f(_,`light`),dark:f(_,`dark`)},S=u(V,x,{package:`@webority/tokens`,version:C,brand:e.name}),w=E(c(v.web,v.web390,e.email)),T={css:b.css,phone:S.contents,email:w},O=y.resolve(t,l),k=[[y.join(O,n.css),b.css],[y.join(O,n.bootstrap),b.variables],[y.join(O,n.web),E(v.web)],[y.join(O,n.web390),E(v.web390)],[y.join(O,n.phoneTheme),E(v.phone)],[y.join(O,n.phone),S.contents],[y.join(O,n.email),w]];H.push(...k);for(let e of L)e.brand===r.name&&H.push(D(t,e,T))}catch(e){for(let t of String(e?.message??e).split(`
4
- `))t.length!==0&&R.push(w.test(t)?t:`${d}: -: ${t}. Fix the source.`)}}if(R.length>0)throw Error(R.join(`
5
- `));for(let[e,t]of H)await A(e,t);return{written:T,stale:k}}function E(e){return`${JSON.stringify(e,null,4)}\n`}function D(e,t,i){let a=y.resolve(e,t.path);return t.kind===`razor`?[y.join(a,`wwwroot`,r,n.css),i.css]:t.kind===`spa`?[y.join(a,`src`,r,n.css),i.css]:t.kind===`phone`?[y.join(a,r,n.phone),i.phone]:[y.join(a,r,n.email),i.email]}async function O(){let e={};for(let[t,n]of Object.entries(C))e[t]=JSON.parse(await _(new URL(`./source/${n}`,import.meta.url),`utf8`));return e}export{T as brand};
1
+ import{__toESM as e}from"./_virtual/_rolldown/runtime.js";import{require__2020 as t}from"./node_modules/.pnpm/ajv@8.20.0/node_modules/ajv/dist/2020.js";import{BRAND_OUTPUT_FILES as n,HEAD_FOLDER as r,loadBrand as i}from"./brand.js";import a from"./canvas-renames.js";import o from"./deviations.js";import{emitCanvas as s}from"./emit/canvas.js";import{emitEmail as c}from"./emit/email.js";import{emitMeanings as l}from"./emit/meanings.js";import{emitPhoneBrand as u,emitPhoneTheme as d,phoneSets as f}from"./emit/phone.js";import{emitWeb as p}from"./emit/web.js";import{messageOf as m}from"./guards.js";import{phonePairLayer as h}from"./model/contrast.js";import{loadModel as g}from"./model/index.js";import{mkdir as _,readFile as v,writeFile as y}from"node:fs/promises";import b from"node:path";var x=e(t(),1),S=new URL(`../schema/design-map.schema.json`,import.meta.url),C=new URL(`../package.json`,import.meta.url),w={palette:`palette.tokens.json`,scale:`scale.tokens.json`,meaning:`meaning.tokens.json`,modeDark:`mode.dark.tokens.json`},T=/^.+: \S+: .+\. .+$/;async function E(e){let t=b.resolve(e.root??process.cwd()),r=e.check===!0,{version:w}=JSON.parse(await v(C,`utf8`)),E=[],j=[],M=async(e,t)=>{if(r){let n=null;try{n=await v(e,`utf8`)}catch(e){if(O(e)!==`ENOENT`)throw e}n!==t&&j.push(e)}else await _(b.dirname(e),{recursive:!0}),await y(e,t,`utf8`),E.push(e)};if(e.library===!0){if(e.brand!==void 0)throw Error(`brand: --brand: not allowed with --library. Remove it.`);if(!e.out)throw Error(`brand: --out: required with --library. Pass --out <dir>.`);let r=b.resolve(t,e.out),i=await g(),o=s(i),c=[[b.join(r,`canvas`,n.web),D(o.web)],[b.join(r,`canvas`,n.web390),D(o.web390)],[b.join(r,`canvas`,n.phoneTheme),D(o.phone)],[b.join(r,`meanings.json`),D(l(i))],[b.join(r,`canvas`,`renames.json`),D(a)]];if(e.web){let n=p(i),r=b.resolve(t,e.web);c.push([r,n.tokens]),c.push([b.join(b.dirname(r),`_bootstrap.generated.scss`),n.bootstrap])}if(e.phone){let n={light:f(i,`light`),dark:f(i,`dark`)},r=d(n);c.push([b.resolve(t,e.phone),r.contents])}for(let[e,t]of c)await M(e,t);return{written:E,stale:j}}let P=b.join(t,`.webority`,`design.json`),F;try{F=await v(P,`utf8`)}catch(e){throw O(e)===`ENOENT`?Error(`${P}: file missing. Run from the repo root or create the design map.`):e}let I;try{I=JSON.parse(F)}catch(e){throw Error(`${P}: /: ${m(e)}. Fix the design map.`)}let L=JSON.parse(await v(S,`utf8`)),R=new x.Ajv2020({allErrors:!0,strict:!1}).compile(L);if(!R(I))throw Error((R.errors??[]).map(e=>`${P}: ${e.instancePath||`/`}: ${e.message}. Fix the design map.`).join(`
2
+ `));if(I.tokens===void 0)throw Error(`${P}: /tokens: missing. Add tokens.brands to .webority/design.json.`);let z=I.tokens.brands,B=I.tokens.heads??[];if((e.brand!==void 0||e.out!==void 0)&&z.length>1)throw Error(`brand: --brand/--out: the repo has ${z.length} brands. Remove the flag to build every brand.`);let V=[];if(z.forEach((e,t)=>{e.source===`library`&&V.push(`${P}: tokens.brands[${t}].source: "library" is built with --library. Run webority brand --library --out <dir>.`)}),B.forEach((e,t)=>{z.some(t=>t.name===e.brand)||V.push(`${P}: tokens.heads[${t}].brand: no brand named ${e.brand}. Add it to tokens.brands.`)}),V.length>0)throw Error(V.join(`
3
+ `));let H=await A(),U=await g(),W={light:f(U,`light`),dark:f(U,`dark`)},G=N(o),K=[];for(let r of z){let a=z.length===1&&e.brand!==void 0?e.brand:r.source,o=z.length===1&&e.out!==void 0?e.out:r.export,l=b.resolve(t,a);try{let e=i(l,H,G),a=await g({brand:e}),d=h(a,U),m=Object.keys(d.light).length+Object.keys(d.dark).length>0?await g({brand:{...e,phone:d}}):a,_=s(m),v=p(m,{library:U}),y={light:f(m,`light`),dark:f(m,`dark`)},x=u(W,y,{package:`@webority/tokens`,version:w,brand:e.name}),S=D(c(_.web,_.web390,e.email)),C={css:v.css,phone:x.contents,email:S},T=b.resolve(t,o),E=[[b.join(T,n.css),v.css],[b.join(T,n.bootstrap),v.variables],[b.join(T,n.web),D(_.web)],[b.join(T,n.web390),D(_.web390)],[b.join(T,n.phoneTheme),D(_.phone)],[b.join(T,n.phone),x.contents],[b.join(T,n.email),S]];K.push(...E);for(let e of B)e.brand===r.name&&K.push(k(t,e,C))}catch(e){for(let t of m(e).split(`
4
+ `))t.length!==0&&V.push(T.test(t)?t:`${l}: -: ${t}. Fix the source.`)}}if(V.length>0)throw Error(V.join(`
5
+ `));for(let[e,t]of K)await M(e,t);return{written:E,stale:j}}function D(e){return`${JSON.stringify(e,null,4)}\n`}function O(e){return e instanceof Error&&`code`in e?e.code:void 0}function k(e,t,i){let a=b.resolve(e,t.path);return t.kind===`razor`?[b.join(a,`wwwroot`,r,n.css),i.css]:t.kind===`spa`?[b.join(a,`src`,r,n.css),i.css]:t.kind===`phone`?[b.join(a,r,n.phone),i.phone]:[b.join(a,r,n.email),i.email]}async function A(){return{palette:await P(w.palette),scale:await P(w.scale),meaning:await P(w.meaning),modeDark:await P(w.modeDark)}}var j=[`space`,`text`,`shadow`,`section`,`component`,`email`];function M(e){return j.some(t=>t===e)}function N(e){let t={};for(let[n,r]of Object.entries(e)){let e=[];for(let t of r.groups){if(!M(t))throw Error(`webority-tokens: deviations.json names an unknown group "${t}".`);e.push(t)}t[n]={reason:r.reason,groups:e}}return t}async function P(e){return JSON.parse(await v(new URL(`./source/${e}`,import.meta.url),`utf8`))}export{E as brand};
@@ -2,7 +2,7 @@
2
2
  "format": "webority-theme-tokens/2",
3
3
  "source": {
4
4
  "package": "@webority/tokens",
5
- "version": "0.149.0",
5
+ "version": "0.150.0",
6
6
  "brand": null,
7
7
  "platform": "phone",
8
8
  "width": null
@@ -2,7 +2,7 @@
2
2
  "format": "webority-theme-tokens/2",
3
3
  "source": {
4
4
  "package": "@webority/tokens",
5
- "version": "0.149.0",
5
+ "version": "0.150.0",
6
6
  "brand": null,
7
7
  "platform": "web",
8
8
  "width": 390
@@ -2,7 +2,7 @@
2
2
  "format": "webority-theme-tokens/2",
3
3
  "source": {
4
4
  "package": "@webority/tokens",
5
- "version": "0.149.0",
5
+ "version": "0.150.0",
6
6
  "brand": null,
7
7
  "platform": "web",
8
8
  "width": 1440
@@ -0,0 +1,37 @@
1
+ import type { BrandOptions, BrandResult } from "./build.ts";
2
+ import { loadModel as realLoadModel } from "./model/index.ts";
3
+ export interface CheckFinding {
4
+ check: "generated" | "design-map" | "repo-checks";
5
+ file: string | null;
6
+ message: string;
7
+ }
8
+ export interface CheckResult {
9
+ findings: CheckFinding[];
10
+ }
11
+ export interface CheckOptions {
12
+ /** Defaults to process.cwd(). */
13
+ root?: string;
14
+ }
15
+ export interface RepoCheckEntry {
16
+ pkg: string;
17
+ id: string;
18
+ message: string;
19
+ }
20
+ export interface RepoChecksBody {
21
+ new: RepoCheckEntry[];
22
+ root?: string;
23
+ known?: unknown[];
24
+ stale?: unknown[];
25
+ warnings?: unknown[];
26
+ }
27
+ type Model = Awaited<ReturnType<typeof realLoadModel>>;
28
+ type BrandFn = (options: BrandOptions) => Promise<BrandResult>;
29
+ type RepoChecksFn = (root: string) => RepoChecksBody | Promise<RepoChecksBody>;
30
+ export interface CheckSeams {
31
+ brand?: BrandFn;
32
+ repoChecks?: RepoChecksFn;
33
+ loadModel?: () => Promise<Model>;
34
+ }
35
+ export declare function check(options?: CheckOptions, seams?: CheckSeams): Promise<CheckResult>;
36
+ export declare function parseRepoChecksOutput(stdout: string): RepoChecksBody;
37
+ export {};
package/dist/check.js CHANGED
@@ -1,4 +1,4 @@
1
- import{__toESM as e}from"./_virtual/_rolldown/runtime.js";import{require__2020 as t}from"./node_modules/.pnpm/ajv@8.20.0/node_modules/ajv/dist/2020.js";import{BRAND_OUTPUT_FILES as n}from"./brand.js";import{brand as r}from"./build.js";import{loadModel as i}from"./model/index.js";import{readFileSync as a,readdirSync as o}from"node:fs";import{join as s,resolve as c}from"node:path";import{spawnSync as l}from"node:child_process";var u=e(t(),1),d=`.webority/design.json`,f=c(import.meta.dirname,`../schema/design-map.schema.json`),p=new Set([`vite.config.js`,`vite.config.mjs`,`vite.config.cjs`,`vite.config.ts`,`vite.config.mts`,`vite.config.cts`]);async function m(e={},t={}){let{brand:n=r,repoChecks:a=w,loadModel:o=i}=t,s=e.root??process.cwd(),c=[],l=h(s,c);return l!==null&&(g(l,c),c.length===0&&l.tokens!==void 0&&v(l,s,c)),c.length===0&&(await b(l,s,o,c),l.tokens!==void 0&&await S(l,s,n,c)),await C(s,a,c),{findings:c}}function h(e,t){let n;try{n=a(s(e,d),`utf8`)}catch{return t.push({check:`design-map`,file:d,message:`Cannot read the file. Run webority check from the repo root and create the file.`}),null}try{return JSON.parse(n)}catch(e){return t.push({check:`design-map`,file:d,message:`${e.message}. Fix the file.`}),null}}function g(e,t){let n=new u.Ajv2020({allErrors:!0}).compile(JSON.parse(a(f,`utf8`)));if(!n(e))for(let r of n.errors)t.push({check:`design-map`,file:d,message:_(r,e)})}function _(e,t){let n=e.instancePath.split(`/`).filter(Boolean),r=n.length>0?n.join(`.`):`design.json`;if(e.keyword===`additionalProperties`)return r+=`.${e.params.additionalProperty}`,`${r}: is not a design map key, retired or misspelled. Remove it.`;if(e.keyword===`required`)return r+=`.${e.params.missingProperty}`,`${r}: is required. Add it.`;let i=n.reduce((e,t)=>e?.[t],t);return typeof i==`string`&&(i.startsWith(`--`)||i.includes(`var(`))?`${r}: a CSS variable is not a role. Use the meaning name, or <group>.<name> for a scale role.`:`${r}: ${e.message}. Fix the file.`}function v(e,t,n){let r=e.tokens.brands,i=e.tokens.heads??[],a=e=>n.push({check:`design-map`,file:d,message:e}),o=new Map;for(let e of r)o.set(e.name,(o.get(e.name)??0)+1);for(let[e,t]of o)t>1&&a(`tokens.brands: ${e} is listed more than once. Give each brand its own name.`);let c=new Set(r.map(e=>e.name));i.forEach((e,t)=>{c.has(e.brand)||a(`tokens.heads[${t}]: brand ${e.brand} is not in tokens.brands. Use a listed brand name.`)});let l=new Set(i.map(e=>e.brand));for(let e of r)e.source!==`library`&&!l.has(e.name)&&a(`tokens.brands: ${e.name} has no head. List a head for it or remove the brand.`);if(r.length>1)for(let e of r)e.source===`library`&&a(`tokens.brands: a library source cannot be combined with other brands. Keep one brand.`);i.forEach((e,n)=>{let r=y(s(t,e.path));r!==null&&(e.kind===`spa`&&r.some(e=>e.endsWith(`.csproj`))&&a(`tokens.heads[${n}]: ${e.path} holds a .csproj, so its kind is razor. Set kind razor.`),e.kind===`razor`&&r.some(e=>p.has(e))&&a(`tokens.heads[${n}]: ${e.path} holds a Vite project, so its kind is spa. Set kind spa.`))})}function y(e){try{return o(e)}catch{return null}}async function b(e,t,r,i){let a=e=>i.push({check:`design-map`,file:d,message:e});if(e.tokens!==void 0)for(let r of e.tokens.brands){let i=r.source===`library`?`${r.export}/canvas`:r.export,o=x(s(t,i));if(o!==null)for(let[t,s]of Object.entries(e.color?.roles??{}))o.has(s)||a(`color.roles.${t}: ${s} is not a colour of brand ${r.name}. Use a name from ${i}/${n.web}.`)}let o=[];for(let[t,n]of Object.entries(e.scale??{})){for(let[e,r]of Object.entries(n.roles??{}))o.push([`scale.${t}.roles.${e}`,r]);for(let[e,r]of Object.entries(n.durations??{}))o.push([`scale.${t}.durations.${e}`,r])}for(let[t,n]of Object.entries(e.iconography?.sizes??{}))o.push([`iconography.sizes.${t}`,n]);if(o.length===0)return;let c;try{c=await r()}catch(e){i.push({check:`design-map`,file:null,message:`${e.message}. Fix the token source.`});return}let l=new Set;for(let e of c.tokens.values())l.add(`${e.group}.${e.name}`);for(let[e,t]of o)l.has(t)||a(`${e}: ${t} is not a token. Use a <group>.<name> the library defines.`)}function x(e){let t=new Set,r=!1;for(let i of[n.web,n.phoneTheme]){let n;try{n=JSON.parse(a(s(e,i),`utf8`))}catch{continue}r=!0;for(let e of Object.keys(n?.colors?.light??{}))t.add(e)}return r?t:null}async function S(e,t,n,r){let i=e.tokens.brands,a=i.length===1&&i[0].source===`library`,o;try{o=a?await n({root:t,library:!0,out:i[0].export,check:!0}):await n({root:t,check:!0})}catch(e){for(let t of String(e?.message??e).split(`
2
- `))t.trim()!==``&&r.push({check:`generated`,file:null,message:t});return}let s=a?`stale. Run webority brand --library --out ${i[0].export}`:`stale. Run webority brand`;for(let e of o.stale)r.push({check:`generated`,file:e,message:s})}async function C(e,t,n){let r;try{r=await t(e)}catch{n.push({check:`repo-checks`,file:null,message:`repo-checks did not run. Add @webority/repo-checks at the lockstep version.`});return}for(let e of r.new)n.push({check:`repo-checks`,file:e.pkg===`.`?null:e.pkg,message:`${e.id}: ${e.message}`})}function w(e){return T(l(`corepack pnpm exec webority-repo-checks --json`,{cwd:e,encoding:`utf8`,shell:!0,maxBuffer:67108864}).stdout??``)}function T(e){let t=e.split(`
1
+ import{__toESM as e}from"./_virtual/_rolldown/runtime.js";import{require__2020 as t}from"./node_modules/.pnpm/ajv@8.20.0/node_modules/ajv/dist/2020.js";import{BRAND_OUTPUT_FILES as n}from"./brand.js";import{brand as r}from"./build.js";import{messageOf as i}from"./guards.js";import{loadModel as a}from"./model/index.js";import{readFileSync as o,readdirSync as s}from"node:fs";import{join as c,resolve as l}from"node:path";import{spawnSync as u}from"node:child_process";var d=e(t(),1),f=`.webority/design.json`,p=l(import.meta.dirname,`../schema/design-map.schema.json`),m=new Set([`vite.config.js`,`vite.config.mjs`,`vite.config.cjs`,`vite.config.ts`,`vite.config.mts`,`vite.config.cts`]);async function h(e={},t={}){let{brand:n=r,repoChecks:i=E,loadModel:o=a}=t,s=e.root??process.cwd(),c=[],l=g(s,c);return l!==null&&(_(l,c),c.length===0&&l.tokens!==void 0&&b(l.tokens,s,c)),c.length===0&&l!==null&&(await S(l,s,o,c),l.tokens!==void 0&&await w(l.tokens,s,n,c)),await T(s,i,c),{findings:c}}function g(e,t){let n;try{n=o(c(e,f),`utf8`)}catch{return t.push({check:`design-map`,file:f,message:`Cannot read the file. Run webority check from the repo root and create the file.`}),null}try{return JSON.parse(n)}catch(e){return t.push({check:`design-map`,file:f,message:`${i(e)}. Fix the file.`}),null}}function _(e,t){let n=new d.Ajv2020({allErrors:!0}),r=JSON.parse(o(p,`utf8`)),i=n.compile(r);if(!i(e))for(let n of i.errors??[])t.push({check:`design-map`,file:f,message:v(n,e)})}function v(e,t){let n=e.instancePath.split(`/`).filter(Boolean),r=n.length>0?n.join(`.`):`design.json`;if(e.keyword===`additionalProperties`)return r+=`.${e.params.additionalProperty}`,`${r}: is not a design map key, retired or misspelled. Remove it.`;if(e.keyword===`required`)return r+=`.${e.params.missingProperty}`,`${r}: is required. Add it.`;let i=n.reduce((e,t)=>y(e,t),t);return typeof i==`string`&&(i.startsWith(`--`)||i.includes(`var(`))?`${r}: a CSS variable is not a role. Use the meaning name, or <group>.<name> for a scale role.`:`${r}: ${e.message}. Fix the file.`}function y(e,t){return typeof e==`object`&&e?Reflect.get(e,t):void 0}function b(e,t,n){let r=e.brands,i=e.heads??[],a=e=>{n.push({check:`design-map`,file:f,message:e})},o=new Map;for(let e of r)o.set(e.name,(o.get(e.name)??0)+1);for(let[e,t]of o)t>1&&a(`tokens.brands: ${e} is listed more than once. Give each brand its own name.`);let s=new Set(r.map(e=>e.name));i.forEach((e,t)=>{s.has(e.brand)||a(`tokens.heads[${t}]: brand ${e.brand} is not in tokens.brands. Use a listed brand name.`)});let l=new Set(i.map(e=>e.brand));for(let e of r)e.source!==`library`&&!l.has(e.name)&&a(`tokens.brands: ${e.name} has no head. List a head for it or remove the brand.`);if(r.length>1)for(let e of r)e.source===`library`&&a(`tokens.brands: a library source cannot be combined with other brands. Keep one brand.`);i.forEach((e,n)=>{let r=x(c(t,e.path));r!==null&&(e.kind===`spa`&&r.some(e=>e.endsWith(`.csproj`))&&a(`tokens.heads[${n}]: ${e.path} holds a .csproj, so its kind is razor. Set kind razor.`),e.kind===`razor`&&r.some(e=>m.has(e))&&a(`tokens.heads[${n}]: ${e.path} holds a Vite project, so its kind is spa. Set kind spa.`))})}function x(e){try{return s(e)}catch{return null}}async function S(e,t,r,a){let o=e=>{a.push({check:`design-map`,file:f,message:e})};if(e.tokens!==void 0)for(let r of e.tokens.brands){let i=r.source===`library`?`${r.export}/canvas`:r.export,a=C(c(t,i));if(a!==null)for(let[t,s]of Object.entries(e.color?.roles??{}))a.has(s)||o(`color.roles.${t}: ${s} is not a colour of brand ${r.name}. Use a name from ${i}/${n.web}.`)}let s=[];for(let[t,n]of Object.entries(e.scale??{})){for(let[e,r]of Object.entries(n.roles??{}))s.push([`scale.${t}.roles.${e}`,r]);for(let[e,r]of Object.entries(n.durations??{}))s.push([`scale.${t}.durations.${e}`,r])}for(let[t,n]of Object.entries(e.iconography?.sizes??{}))s.push([`iconography.sizes.${t}`,n]);if(s.length===0)return;let l;try{l=await r()}catch(e){a.push({check:`design-map`,file:null,message:`${i(e)}. Fix the token source.`});return}let u=new Set;for(let e of l.tokens.values())u.add(`${e.group}.${e.name}`);for(let[e,t]of s)u.has(t)||o(`${e}: ${t} is not a token. Use a <group>.<name> the library defines.`)}function C(e){let t=new Set,r=!1;for(let i of[n.web,n.phoneTheme]){let n;try{n=JSON.parse(o(c(e,i),`utf8`))}catch{continue}r=!0;for(let e of Object.keys(n?.colors?.light??{}))t.add(e)}return r?t:null}async function w(e,t,n,r){let a=e.brands,o=a.length===1?a[0]:void 0,s=o?.source===`library`,c;try{c=s?await n({root:t,library:!0,out:o?.export,check:!0}):await n({root:t,check:!0})}catch(e){for(let t of i(e).split(`
2
+ `))t.trim()!==``&&r.push({check:`generated`,file:null,message:t});return}let l=s?`stale. Run webority brand --library --out ${o?.export}`:`stale. Run webority brand`;for(let e of c.stale)r.push({check:`generated`,file:e,message:l})}async function T(e,t,n){let r;try{r=await t(e)}catch{n.push({check:`repo-checks`,file:null,message:`repo-checks did not run. Add @webority/repo-checks at the lockstep version.`});return}for(let e of r.new)n.push({check:`repo-checks`,file:e.pkg===`.`?null:e.pkg,message:`${e.id}: ${e.message}`})}function E(e){return D(u(`corepack pnpm exec webority-repo-checks --json`,{cwd:e,encoding:`utf8`,shell:!0,maxBuffer:67108864}).stdout??``)}function D(e){let t=e.split(`
3
3
  `),n=t.findIndex(e=>e.startsWith(`{`)),r;try{if(n===-1)throw Error(`no JSON line`);r=JSON.parse(t.slice(n).join(`
4
- `))}catch{throw Error(`repo-checks printed no JSON`)}if(!Array.isArray(r?.new))throw Error(`repo-checks printed no new array`);return r}export{m as check,T as parseRepoChecksOutput};
4
+ `))}catch{throw Error(`repo-checks printed no JSON`)}if(!Array.isArray(r?.new))throw Error(`repo-checks printed no new array`);return r}export{h as check,D as parseRepoChecksOutput};
package/dist/cli.d.ts CHANGED
@@ -1,100 +1,19 @@
1
+ import type { BrandOptions, BrandResult } from "./build.ts";
2
+ import { brand } from "./build.ts";
3
+ import type { CheckResult } from "./check.ts";
4
+ import { check } from "./check.ts";
5
+ import type { UpgradeOptions, UpgradeResult } from "./upgrade/index.ts";
6
+ import { upgrade } from "./upgrade/index.ts";
7
+ export type { BrandOptions, BrandResult } from "./build.ts";
8
+ export type { CheckFinding, CheckOptions, CheckResult } from "./check.ts";
9
+ export type { UpgradeChoice, UpgradeHit, UpgradeOptions, UpgradeResult } from "./upgrade/index.ts";
10
+ export { brand, check, upgrade };
1
11
  /** 0 ok, 1 refused or stale, 2 usage error. */
2
12
  export type ExitCode = 0 | 1 | 2;
3
-
4
13
  export type CliCommand = "brand" | "upgrade" | "check";
5
-
6
- /**
7
- * webority brand [--brand <file>] [--library] [--out <dir>] [--web <scss>] [--phone <ts>] [--check]
8
- *
9
- * Product mode (no --library) reads .webority/design.json and builds every entry of tokens.brands: it writes the
10
- * seven BRAND_OUTPUT_FILES into the brand's export folder, then copies brand.css into <head>/wwwroot/generated/,
11
- * brand.phone.js into <head>/generated/ and email.json into <head>/generated/ for each head of that brand, by its
12
- * kind. --brand and --out replace the source and export of the repo's one brand, and are refused when it has more.
13
- * Library mode (--library) writes canvas/*.json, meanings.json and canvas/renames.json into --out; --web and --phone
14
- * name the generated SCSS and TS files. --check compares instead of writing.
15
- */
16
- export interface BrandOptions {
17
- brand?: string;
18
- library?: boolean;
19
- out?: string;
20
- web?: string;
21
- phone?: string;
22
- check?: boolean;
23
- /** Defaults to process.cwd(). */
24
- root?: string;
14
+ export interface Commands {
15
+ brand(options: BrandOptions): Promise<BrandResult>;
16
+ upgrade(options: UpgradeOptions): Promise<UpgradeResult>;
17
+ check(): Promise<CheckResult>;
25
18
  }
26
-
27
- export interface BrandResult {
28
- written: string[];
29
- /** Files whose contents differ from what brand would write; only filled with check. */
30
- stale: string[];
31
- }
32
-
33
- /**
34
- * webority upgrade <version> [--dry-run] [--app <folder>] [--path <p>]...
35
- *
36
- * With no --path, moves every pin in the lockstep list to <version>, installs, applies every rename table from the
37
- * installed version up to <version> in order, then runs brand. With --app, the same run covers one app folder inside
38
- * the repo: the design map is read from the repo root, and the pins, install (pnpm or npm, by the folder's lockfile)
39
- * and tables are the app's. With --path (repeatable), applies the tables to those paths only, with no pin move,
40
- * install or brand run; webority-ui uses it on itself. --alias <pkg>=<library pkg> (repeatable) matches the wrapper
41
- * package as its library. --report <path> is written by the command line only: it stores the result as JSON.
42
- */
43
- export interface UpgradeOptions {
44
- version: string;
45
- dryRun?: boolean;
46
- /** An app folder inside the repo, relative to root. */
47
- app?: string;
48
- paths?: string[];
49
- /** Wrapper package name to the library package its rules name: `{ "@acme/ui": "@webority/ui-react" }`. */
50
- aliases?: Record<string, string>;
51
- /** Path of a JSON file for the command line to write the UpgradeResult to; upgrade() ignores it. */
52
- report?: string;
53
- /** Defaults to process.cwd(). */
54
- root?: string;
55
- }
56
-
57
- /** One rewrite upgrade made, or would make on a dry run. */
58
- export interface UpgradeHit {
59
- file: string;
60
- line: number;
61
- from: string;
62
- to: string;
63
- }
64
-
65
- /** A use upgrade cannot rewrite on its own; a person picks the new name. */
66
- export interface UpgradeChoice {
67
- file: string;
68
- line: number;
69
- name: string;
70
- hint: string;
71
- }
72
-
73
- export interface UpgradeResult {
74
- hits: UpgradeHit[];
75
- choose: UpgradeChoice[];
76
- changedFiles: string[];
77
- }
78
-
79
- /** webority check: fresh generated files, a valid design map, and repo-checks with no count above its record. */
80
- export interface CheckOptions {
81
- /** Defaults to process.cwd(). */
82
- root?: string;
83
- }
84
-
85
- export interface CheckFinding {
86
- check: "generated" | "design-map" | "repo-checks";
87
- file: string | null;
88
- message: string;
89
- }
90
-
91
- export interface CheckResult {
92
- findings: CheckFinding[];
93
- }
94
-
95
- export declare function brand(options: BrandOptions): Promise<BrandResult>;
96
- export declare function upgrade(options: UpgradeOptions): Promise<UpgradeResult>;
97
- export declare function check(options?: CheckOptions): Promise<CheckResult>;
98
-
99
- /** The command line: parses argv (without node and the script), runs one command and maps its result to an exit code. */
100
- export declare function run(argv: string[]): Promise<ExitCode>;
19
+ export declare function run(argv: string[], commands?: Commands): Promise<ExitCode>;
package/dist/cli.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import{brand as e}from"./build.js";import{check as t}from"./check.js";import{upgrade as n}from"./upgrade/index.js";import{writeFileSync as r}from"node:fs";var i=`Usage: webority <command>
2
2
  brand [--brand <file>] [--library] [--out <dir>] [--web <scss>] [--phone <ts>] [--check]
3
3
  upgrade <version> [--dry-run] [--app <folder>] [--path <p>]... [--alias <pkg>=<library pkg>]... [--report <path>]
4
- check`,a=[`brand`,`upgrade`,`check`],o=[`generated`,`design-map`,`repo-checks`],s={"--brand":`brand`,"--out":`out`,"--web":`web`,"--phone":`phone`},c=class extends Error{};function l(e){return console.error(e),console.error(i),2}function u(e){if(!(e instanceof Error))throw e;return console.error(e.message),1}function d(e,t,n){let r=[],i={},a=new Set;for(let o=0;o<e.length;o+=1){let s=e[o];if(t.includes(s)){let t=e[o+1];if(t===void 0||t.startsWith(`--`))throw new c(`${s} needs a value.`);i[s]=[...i[s]??[],t],o+=1}else if(n.includes(s))a.add(s);else if(s.startsWith(`--`))throw new c(`Unknown flag ${s}.`);else r.push(s)}return{positionals:r,values:i,flags:a}}function f(e,t){if(e===`brand`){let{positionals:e,values:n,flags:r}=d(t,Object.keys(s),[`--library`,`--check`]);if(e.length>0)throw new c(`brand takes no argument, got ${e[0]}.`);let i={};for(let[e,t]of Object.entries(s))n[e]!==void 0&&(i[t]=n[e].at(-1));return r.has(`--library`)&&(i.library=!0),r.has(`--check`)&&(i.check=!0),{kind:`brand`,options:i}}if(e===`upgrade`){let{positionals:e,values:n,flags:r}=d(t,[`--path`,`--app`,`--alias`,`--report`],[`--dry-run`]);if(e.length===0)throw new c(`upgrade needs a <version>.`);if(e.length>1)throw new c(`Unexpected argument ${e[1]}.`);let i={version:e[0]};if(r.has(`--dry-run`)&&(i.dryRun=!0),n[`--alias`]!==void 0){i.aliases={};for(let e of n[`--alias`]){let[t,n]=e.split(`=`);if(!t||!n||e.split(`=`).length!==2)throw new c(`--alias takes <pkg>=<library pkg>, got ${e}.`);i.aliases[t]=n}}if(n[`--report`]!==void 0){if(n[`--report`].length>1)throw new c(`--report takes one path.`);i.report=n[`--report`][0]}if(n[`--app`]!==void 0){if(n[`--app`].length>1)throw new c(`--app takes one folder.`);if(n[`--path`]!==void 0)throw new c(`--app and --path cannot be combined.`);i.app=n[`--app`][0]}return n[`--path`]!==void 0&&(i.paths=n[`--path`]),{kind:`upgrade`,options:i}}if(t.length>0)throw new c(`check takes no arguments, got ${t[0]}.`);return{kind:`check`}}async function p(s,d={brand:e,upgrade:n,check:t}){if(s.includes(`--help`)||s.includes(`-h`))return console.log(i),0;if(s.length===0)return console.error(i),2;let[p,...m]=s;if(!a.includes(p))return l(`Unknown command ${p}. Commands: brand, upgrade, check.`);let h;try{h=f(p,m)}catch(e){if(e instanceof c)return l(e.message);throw e}if(h.kind===`brand`){let e;try{e=await d.brand(h.options)}catch(e){return u(e)}for(let t of e.written)console.log(`wrote ${t}`);if(!h.options.check)return 0;for(let t of e.stale)console.log(`stale ${t}`);return+(e.stale.length>0)}if(h.kind===`upgrade`){let{report:e,...t}=h.options,n;try{n=await d.upgrade(t)}catch(e){return u(e)}for(let e of n.hits)console.log(`${e.file}:${e.line} ${e.from} -> ${e.to}`);for(let e of n.choose)console.log(`choose ${e.file}:${e.line} ${e.name}: ${e.hint}`);if(e!==void 0)try{r(e,`${JSON.stringify({hits:n.hits,choose:n.choose,changedFiles:n.changedFiles},null,4)}\n`,`utf8`)}catch(e){return u(e)}return+(n.choose.length>0)}let g;try{g=await d.check()}catch(e){return u(e)}for(let e of o)g.findings.some(t=>t.check===e)||console.log(`ok ${e}`);for(let e of g.findings){let t=e.file?`${e.file}: `:``;console.log(`FAIL ${e.check}: ${t}${e.message}`)}return+(g.findings.length>0)}export{e as brand,t as check,p as run,n as upgrade};
4
+ check`,a=[`brand`,`upgrade`,`check`],o=[`generated`,`design-map`,`repo-checks`],s={"--brand":`brand`,"--out":`out`,"--web":`web`,"--phone":`phone`},c=class extends Error{};function l(e){return console.error(e),console.error(i),2}function u(e){if(!(e instanceof Error))throw e;return console.error(e.message),1}function d(e,t,n){let r=[],i={},a=new Set;for(let o=0;o<e.length;o+=1){let s=e[o]??``;if(t.includes(s)){let t=e[o+1];if(t===void 0||t.startsWith(`--`))throw new c(`${s} needs a value.`);i[s]=[...i[s]??[],t],o+=1}else if(n.includes(s))a.add(s);else if(s.startsWith(`--`))throw new c(`Unknown flag ${s}.`);else r.push(s)}return{positionals:r,values:i,flags:a}}function f(e,t){if(e===`brand`){let{positionals:e,values:n,flags:r}=d(t,Object.keys(s),[`--library`,`--check`]);if(e.length>0)throw new c(`brand takes no argument, got ${e[0]}.`);let i={};for(let[e,t]of Object.entries(s)){let r=n[e];r!==void 0&&(i[t]=r.at(-1))}return r.has(`--library`)&&(i.library=!0),r.has(`--check`)&&(i.check=!0),{kind:`brand`,options:i}}if(e===`upgrade`){let{positionals:e,values:n,flags:r}=d(t,[`--path`,`--app`,`--alias`,`--report`],[`--dry-run`]),[i,...a]=e;if(i===void 0)throw new c(`upgrade needs a <version>.`);if(a.length>0)throw new c(`Unexpected argument ${a[0]}.`);let o={version:i};r.has(`--dry-run`)&&(o.dryRun=!0);let s=n[`--alias`];if(s!==void 0){o.aliases={};for(let e of s){let[t,n]=e.split(`=`);if(!t||!n||e.split(`=`).length!==2)throw new c(`--alias takes <pkg>=<library pkg>, got ${e}.`);o.aliases[t]=n}}let l=n[`--report`];if(l!==void 0){if(l.length>1)throw new c(`--report takes one path.`);o.report=l[0]}let u=n[`--app`];if(u!==void 0){if(u.length>1)throw new c(`--app takes one folder.`);if(n[`--path`]!==void 0)throw new c(`--app and --path cannot be combined.`);o.app=u[0]}let f=n[`--path`];return f!==void 0&&(o.paths=f),{kind:`upgrade`,options:o}}if(t.length>0)throw new c(`check takes no arguments, got ${t[0]}.`);return{kind:`check`}}async function p(s,d={brand:e,upgrade:n,check:t}){if(s.includes(`--help`)||s.includes(`-h`))return console.log(i),0;if(s.length===0)return console.error(i),2;let[p=``,...m]=s;if(!a.includes(p))return l(`Unknown command ${p}. Commands: brand, upgrade, check.`);let h;try{h=f(p,m)}catch(e){if(e instanceof c)return l(e.message);throw e}if(h.kind===`brand`){let e;try{e=await d.brand(h.options)}catch(e){return u(e)}for(let t of e.written)console.log(`wrote ${t}`);if(!h.options.check)return 0;for(let t of e.stale)console.log(`stale ${t}`);return+(e.stale.length>0)}if(h.kind===`upgrade`){let{report:e,...t}=h.options,n;try{n=await d.upgrade(t)}catch(e){return u(e)}for(let e of n.hits)console.log(`${e.file}:${e.line} ${e.from} -> ${e.to}`);for(let e of n.choose)console.log(`choose ${e.file}:${e.line} ${e.name}: ${e.hint}`);if(e!==void 0)try{r(e,`${JSON.stringify({hits:n.hits,choose:n.choose,changedFiles:n.changedFiles},null,4)}\n`,`utf8`)}catch(e){return u(e)}return+(n.choose.length>0)}let g;try{g=await d.check()}catch(e){return u(e)}for(let e of o)g.findings.some(t=>t.check===e)||console.log(`ok ${e}`);for(let e of g.findings){let t=e.file?`${e.file}: `:``;console.log(`FAIL ${e.check}: ${t}${e.message}`)}return+(g.findings.length>0)}export{e as brand,t as check,p as run,n as upgrade};
@@ -0,0 +1,8 @@
1
+ import type { Model } from "../model/index.ts";
2
+ import type { ThemeTokensV2 } from "../outputs.ts";
3
+ export interface CanvasDocuments {
4
+ web: ThemeTokensV2;
5
+ web390: ThemeTokensV2;
6
+ phone: ThemeTokensV2;
7
+ }
8
+ export declare function emitCanvas(model: Model): CanvasDocuments;
@@ -1 +1 @@
1
- function e(e){return{web:t(e,`web`,1440),web390:t(e,`web`,390),phone:t(e,`phone`,null)}}function t(e,t,a){let o=n(e,`light`,t,a),s=n(e,`dark`,t,a),c={format:`webority-theme-tokens/2`,source:{package:e.source.package,version:e.source.version,brand:e.source.brand,platform:t,width:a},modes:[`light`,`dark`],colors:{light:{},dark:{}},space:{},radius:{},text:{},typography:{fontWeight:{},leading:{}},fonts:{},shadow:{},components:{}};for(let t of e.tokens.values())t.group===`color`?i(c,e,t,o,s):e.existsIn(t.path,o)&&r(c,e,t,o);return c}function n(e,t,n,r){let i=e.contexts.find(e=>e.mode===t&&e.platform===n&&e.width===r);if(!i){let e=r===null?``:` ${r}`;throw Error(`emitCanvas: the model has no ${t} ${n}${e} context`)}return i}function r(e,t,n,r){let{path:i,name:o}=n;switch(n.group){case`space`:e.space[o]=s(t,i,r);break;case`radius`:e.radius[o]=s(t,i,r);break;case`text`:e.text[o]={size:s(t,i,r)};break;case`weight`:e.typography.fontWeight[o]=c(t,i,r);break;case`leading`:e.typography.leading[o]=c(t,i,r);break;case`font`:e.fonts[o]=l(t,i,r);break;case`shadow`:e.shadow[o]=u(t,i,r);break;case`component`:a(e,t,i,r)}}function i(e,t,n,r,i){let a=t.existsIn(n.path,r);if(a!==t.existsIn(n.path,i))throw Error(`emitCanvas: ${n.path} is present in one mode only`);if(a&&(e.colors.light[n.name]=o(t,n.path,r),e.colors.dark[n.name]=o(t,n.path,i),n.brandOwn)){if(typeof n.description!=`string`||n.description===``)throw Error(`emitCanvas: ${n.path} is a brand colour and needs a description`);e.colorMeanings??={},e.colorMeanings[n.name]=n.description}}function a(e,t,n,r){let i=n.split(`.`);if(i.length!==3&&i.length!==4)throw Error(`emitCanvas: ${n} must be component.<name>.<prop> or component.<name>.<size>.<prop>`);let a=i[1],o=i[i.length-1];e.components[a]??={base:{},sizes:{}};let s=e.components[a].base;i.length===4&&(e.components[a].sizes[i[2]]??={},s=e.components[a].sizes[i[2]]),s[o]=f(t,n,r)}function o(e,t,n){let r=e.resolvedIn(t,n);if(typeof r!=`string`)throw Error(`emitCanvas: ${t} is not a colour string`);return r}function s(e,t,n){let r=e.resolvedIn(t,n);if(typeof r==`number`)return r;if(typeof r==`object`&&r&&r.unit===`px`&&typeof r.value==`number`)return r.value;throw Error(`emitCanvas: ${t} is not a px length`)}function c(e,t,n){let r=e.resolvedIn(t,n);if(typeof r!=`number`)throw Error(`emitCanvas: ${t} is not a number`);return r}function l(e,t,n){let r=e.resolvedIn(t,n);if(!Array.isArray(r)||r.length===0)throw Error(`emitCanvas: ${t} is not a font family list`);return{family:r[0],fallback:r.slice(1).join(`, `)}}function u(e,t,n){let r=e.resolvedIn(t,n);if(r===`none`)return null;if(typeof r!=`object`||!r||typeof r.color!=`string`)throw Error(`emitCanvas: ${t} is not a shadow`);let[i,a,o,s]=[`offsetX`,`offsetY`,`blur`,`spread`].map(e=>d(t,r[e])),c={x:i,y:a,blur:o,spread:s,color:r.color},l=e.tokens.get(t)?.extensions?.elevation;return Number.isFinite(l)&&(c.elevation=l),c}function d(e,t){let n=typeof t==`number`?t:t?.value;if(typeof n!=`number`)throw Error(`emitCanvas: ${e} has a shadow length that is not a number`);return n}function f(e,t,n){let r=e.resolvedIn(t,n);if(typeof r==`string`||typeof r==`number`)return String(r);if(typeof r==`object`&&r&&typeof r.unit==`string`)return`${r.value}${r.unit}`;throw Error(`emitCanvas: ${t} is not a component value`)}export{e as emitCanvas};
1
+ import{isObjectLike as e}from"../guards.js";import{present as t}from"../present.js";function n(e){return{web:r(e,`web`,1440),web390:r(e,`web`,390),phone:r(e,`phone`,null)}}function r(e,t,n){let r=i(e,`light`,t,n),s=i(e,`dark`,t,n),c={format:`webority-theme-tokens/2`,source:{package:e.source.package,version:e.source.version,brand:e.source.brand,platform:t,width:n},modes:[`light`,`dark`],colors:{light:{},dark:{}},space:{},radius:{},text:{},typography:{fontWeight:{},leading:{}},fonts:{},shadow:{},components:{}};for(let t of e.tokens.values())t.group===`color`?o(c,e,t,r,s):e.existsIn(t.path,r)&&a(c,e,t,r);return c}function i(e,t,n,r){let i=e.contexts.find(e=>e.mode===t&&e.platform===n&&e.width===r);if(!i){let e=r===null?``:` ${r}`;throw Error(`emitCanvas: the model has no ${t} ${n}${e} context`)}return i}function a(e,t,n,r){let{path:i,name:a}=n;switch(n.group){case`space`:e.space[a]=l(t,i,r);break;case`radius`:e.radius[a]=l(t,i,r);break;case`text`:e.text[a]={size:l(t,i,r)};break;case`weight`:e.typography.fontWeight[a]=u(t,i,r);break;case`leading`:e.typography.leading[a]=u(t,i,r);break;case`font`:e.fonts[a]=d(t,i,r);break;case`shadow`:e.shadow[a]=f(t,i,r);break;case`component`:s(e,t,i,r)}}function o(e,t,n,r,i){let a=t.existsIn(n.path,r);if(a!==t.existsIn(n.path,i))throw Error(`emitCanvas: ${n.path} is present in one mode only`);if(a&&(e.colors.light[n.name]=c(t,n.path,r),e.colors.dark[n.name]=c(t,n.path,i),n.brandOwn)){if(typeof n.description!=`string`||n.description===``)throw Error(`emitCanvas: ${n.path} is a brand colour and needs a description`);e.colorMeanings??={},e.colorMeanings[n.name]=n.description}}function s(e,n,r,i){let a=r.split(`.`);if(a.length!==3&&a.length!==4)throw Error(`emitCanvas: ${r} must be component.<name>.<prop> or component.<name>.<size>.<prop>`);let o=t(a[1],`the component name`),s=t(a[a.length-1],`the component property`),c=e.components[o];c===void 0&&(c={base:{},sizes:{}},e.components[o]=c);let l=c.base;if(a.length===4){let e=t(a[2],`the component size`),n=c.sizes[e];n===void 0&&(n={},c.sizes[e]=n),l=n}l[s]=m(n,r,i)}function c(e,t,n){let r=e.resolvedIn(t,n);if(typeof r!=`string`)throw Error(`emitCanvas: ${t} is not a colour string`);return r}function l(t,n,r){let i=t.resolvedIn(n,r);if(typeof i==`number`)return i;if(e(i)&&i.unit===`px`&&typeof i.value==`number`)return i.value;throw Error(`emitCanvas: ${n} is not a px length`)}function u(e,t,n){let r=e.resolvedIn(t,n);if(typeof r!=`number`)throw Error(`emitCanvas: ${t} is not a number`);return r}function d(e,t,n){let r=e.resolvedIn(t,n);if(!Array.isArray(r)||!r.every(e=>typeof e==`string`))throw Error(`emitCanvas: ${t} is not a font family list`);let[i,...a]=r;if(i===void 0)throw Error(`emitCanvas: ${t} is not a font family list`);return{family:i,fallback:a.join(`, `)}}function f(t,n,r){let i=t.resolvedIn(n,r);if(i===`none`)return null;if(!e(i)||typeof i.color!=`string`)throw Error(`emitCanvas: ${n} is not a shadow`);let a={x:p(n,i.offsetX),y:p(n,i.offsetY),blur:p(n,i.blur),spread:p(n,i.spread),color:i.color},o=t.tokens.get(n)?.extensions?.elevation;return typeof o==`number`&&Number.isFinite(o)&&(a.elevation=o),a}function p(t,n){let r=typeof n==`number`?n:e(n)?n.value:void 0;if(typeof r!=`number`)throw Error(`emitCanvas: ${t} has a shadow length that is not a number`);return r}function m(t,n,r){let i=t.resolvedIn(n,r);if(typeof i==`string`||typeof i==`number`)return String(i);if(e(i)&&typeof i.unit==`string`)return`${i.value}${i.unit}`;throw Error(`emitCanvas: ${n} is not a component value`)}export{n as emitCanvas};
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The email theme is derived from the brand's two web themes, at 1440 and 390. Email clients render alpha
3
+ * unreliably, so a translucent colour is composited over the card colour of its mode and every colour leaves as
4
+ * opaque #RRGGBB. Sizes and spacing are whole pixels from the token scale, and phone type sizes come from the 390
5
+ * theme. The role tables below are the one home of the email-to-token mapping.
6
+ */
7
+ import type { EmailColorRoleKey, EmailShapeKey, EmailSpaceKey, EmailThemeTokensV1, EmailTypeRoleKey, ThemeTokensV2 } from "../outputs.ts";
8
+ export declare const EMAIL_COLOR_ROLES: Readonly<Record<EmailColorRoleKey, string>>;
9
+ export declare const EMAIL_TYPE_ROLES: Readonly<Record<EmailTypeRoleKey, {
10
+ step: string;
11
+ leading: string;
12
+ }>>;
13
+ export declare const EMAIL_SPACE: Readonly<Record<EmailSpaceKey, string>>;
14
+ export declare const EMAIL_PADDING_ROLES: ReadonlySet<string>;
15
+ export declare const EMAIL_PADDING_PATTERN: RegExp;
16
+ export declare const EMAIL_SHAPE: Readonly<Record<EmailShapeKey, string>>;
17
+ export declare const EMAIL_BUTTON: Readonly<{
18
+ radius: string;
19
+ padding: string;
20
+ }>;
21
+ export interface EmailGroupEntry {
22
+ $value?: unknown;
23
+ $extensions?: Record<string, unknown>;
24
+ }
25
+ export interface EmailGroup {
26
+ color?: Record<string, EmailGroupEntry>;
27
+ type?: Record<string, EmailGroupEntry>;
28
+ space?: Record<string, EmailGroupEntry>;
29
+ shape?: Record<string, EmailGroupEntry>;
30
+ button?: Record<string, EmailGroupEntry>;
31
+ }
32
+ export declare function emitEmail(web: ThemeTokensV2, web390: ThemeTokensV2, email?: EmailGroup | null): EmailThemeTokensV1;
@@ -1,2 +1,2 @@
1
- var e=[`light`,`dark`],t=new Set([`serif`,`sans-serif`,`monospace`,`cursive`,`fantasy`,`system-ui`,`ui-serif`,`ui-sans-serif`,`ui-monospace`]),n=[[`body`,`ui`],[`display`,`display`],[`mono`,`mono`]],r=Object.freeze({page:`bg`,canvas:`bg`,card:`surface`,panel:`bg-subtle`,"panel-alt":`bg-muted`,surface:`surface`,line:`border`,"line-strong":`border-strong`,"accent-tint-line":`primary-wash-strong`,heading:`text`,strong:`text`,text:`text-body`,secondary:`text-muted`,muted:`text-subtle`,accent:`primary`,"on-accent":`on-primary`,link:`link`,success:`success-text`,warning:`warning-text`,danger:`danger-text`,"accent-tint":`primary-wash`,"success-tint":`success-soft`,"warning-tint":`warning-soft`,"danger-tint":`danger-soft`,"figure-panel":`primary-wash`}),i=Object.freeze({eyebrow:{step:`xs`,leading:`body`},h1:{step:`2xl`,leading:`heading`},lede:{step:`lg`,leading:`body`},body:{step:`base`,leading:`body`},"detail-value":{step:`sm`,leading:`body`},quote:{step:`base`,leading:`body`},"body-large":{step:`lg`,leading:`body`},title:{step:`2xl`,leading:`heading`},item:{step:`base`,leading:`body`},label:{step:`sm`,leading:`body`},note:{step:`sm`,leading:`body`},"note-small":{step:`xs`,leading:`body`},figure:{step:`3xl`,leading:`display`},"figure-hero":{step:`4xl`,leading:`display`},"figure-sub":{step:`sm`,leading:`body`},range:{step:`sm`,leading:`body`},sign:{step:`base`,leading:`body`},foot:{step:`xs`,leading:`body`},num:{step:`base`,leading:`body`},"code-otp":{step:`2xl`,leading:`heading`},"code-id":{step:`base`,leading:`body`},button:{step:`base`,leading:`body`}}),a=Object.freeze({section:`2xl`,"section-phone":`xl`,tight:`md`,item:`sm`,"heading-after":`xl`,"eyebrow-after":`xxs`,"title-after":`md`,"callout-lead-after":`xs`,"paragraph-gap":`sm`,"sign-gap":`2xl`,"sign-gap-phone":`lg`,"signature-gap":`md`,"step-gap":`lg`,"step-gutter":`3xl`,"step-gutter-phone":`2xl`,"step-number-space":`md`,"step-number-space-phone":`sm`,"column-top":`lg`,"column-gap":`lg`,"row-padding":`sm`,"panel-padding":`lg`,"panel-padding-wide":`lg`,"details-padding":`lg`,"details-padding-wide":`lg`,"hero-padding":`xl lg`,"hero-padding-wide":`2xl lg`,"columns-padding":`none lg lg lg`,"columns-padding-wide":`xs lg lg lg`}),o=new Set([`panel-padding`,`panel-padding-wide`,`details-padding`,`details-padding-wide`,`hero-padding`,`hero-padding-wide`,`columns-padding`,`columns-padding-wide`]),s=/^(0|\d+px)( (0|\d+px)){0,3}$/,c=Object.freeze({"panel-radius":`md`,"tint-radius":`md`,"code-radius":`md`,"details-radius":`md`}),l=Object.freeze({radius:`md`,padding:`md sm`});function u(e){return Math.floor(e+.5)}function d(e){if(typeof e!=`string`)return null;let t=/^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(e);if(t===null)return null;let n=t[1];n.length===3&&(n=[...n].map(e=>e+e).join(``));let r=e=>Number.parseInt(n.slice(e,e+2),16);return{r:r(0),g:r(2),b:r(4),a:n.length===8?r(6)/255:1}}function f(e,t,n){return`#${[e,t,n].map(e=>e.toString(16).padStart(2,`0`)).join(``)}`.toUpperCase()}function p(e,t){if(e.a===1)return f(e.r,e.g,e.b);if(t===null)return null;let n=(t,n)=>u(t*e.a+n*(1-e.a));return f(n(e.r,t.r),n(e.g,t.g),n(e.b,t.b))}function m(t,n){let i={},a={light:{},dark:{}};for(let o of e){let e=t.colors?.[o]?.surface;e===void 0&&n(`email role card needs meaning surface in ${o}`);let s=d(e);s!==null&&s.a<1&&n(`email card colour (meaning surface, ${o}) must be opaque`),i[o]=s;for(let[e,s]of Object.entries(r)){let r=t.colors?.[o]?.[s];if(r===void 0){n(`email role ${e} needs meaning ${s} in ${o}`);continue}let c=d(r);c===null?n(`email role ${e} (meaning ${s}, ${o}): cannot read colour ${r}`):a[o][e]=p(c,i[o])}}return Object.fromEntries(Object.keys(r).map(e=>[e,{light:a.light[e],dark:a.dark[e]}]))}function h(e,t,n){let r={};for(let[a,{step:o,leading:s}]of Object.entries(i)){let i=e.text?.[o],c=t.text?.[o],l=e.typography?.leading?.[s],d=t.typography?.leading?.[s];i===void 0&&n(`email type role ${a} needs text step ${o}`),c===void 0&&n(`email type role ${a} needs text step ${o} in the 390 theme`),l===void 0&&n(`email type role ${a} needs leading ${s}`),d===void 0&&n(`email type role ${a} needs leading ${s} in the 390 theme`),i!==void 0&&c!==void 0&&l!==void 0&&d!==void 0&&(r[a]={size:u(i.size),lineHeight:u(i.size*l),phoneSize:u(c.size),phoneLineHeight:u(c.size*d)})}return r}function g(e,t,n,r){let i=[],a=!0;for(let o of t.split(/\s+/))typeof e.space?.[o]==`number`?i.push(e.space[o]):(r(n(o)),a=!1);return a?i.map(e=>`${e}px`).join(` `):null}function _(e,t,n,r){return/\s/.test(n)?(r(`email space role ${t} is a gap and takes one space step, not ${n}`),null):typeof e.space?.[n]==`number`?e.space[n]:(r(`email space role ${t} needs space step ${n}`),null)}function v(e,t){let n={};for(let[r,i]of Object.entries(a)){let a=o.has(r)?g(e,i,e=>`email space role ${r} needs space step ${e}`,t):_(e,r,i,t);a!==null&&(n[r]=a)}return n}function y(e,t){let n={};for(let[r,i]of Object.entries(c)){let a=e.radius?.[i];typeof a==`number`?n[r]=a:t(`email shape role ${r} needs radius step ${i}`)}return n}function b(e,t){let n=e.radius?.[l.radius];return typeof n!=`number`&&t(`email button needs radius step ${l.radius}`),{radius:n,padding:g(e,l.padding,e=>`email button needs space step ${e}`,t)}}function x(e){let t=e.trim();return t.length>=2&&t[0]===t.at(-1)&&(t[0]===`"`||t[0]===`'`)?t.slice(1,-1).trim():t}function S(e){return t.has(e.toLowerCase())||e.startsWith(`-`)?e:e.includes(` `)?`'${e}'`:e}function C(e,t,n){let r=[t.family,...t.fallback.split(`,`)].map(x).filter(e=>e.length>0),i=r.find(e=>/["';<>]/.test(e));return i===void 0?r.map(S).join(`, `):(n(`email font ${e} holds an unsafe character: ${i}`),null)}function w(e,t){let r={};for(let[i,a]of n){let n=e.fonts?.[a];typeof n?.family!=`string`||typeof n?.fallback!=`string`?t(`email fonts need font ${a}`):r[i]=C(a,n,t)}return r}function T(e,t,n){return Number.isInteger(e?.value)&&e.value>=0&&e.unit===`px`?e.value:(n(`${t} needs a whole px value`),null)}function E(e,t,n){return typeof e==`string`&&s.test(e)?e:(n(`${t} needs CSS px padding`),null)}function D(e,t,n,r,i){let a=/^\{color\.([a-z0-9-]+)\}$/.exec(t)?.[1],o=a===void 0?t:e.colors?.[n]?.[a];if(o===void 0)return i(`email group role ${r} needs meaning ${a} in ${n}`),null;let s=d(o);if(s===null)return i(`email group role ${r} (${n}): cannot read colour ${o}`),null;let c=p(s,d(e.colors?.[n]?.surface));return c===null&&i(`email group role ${r} must be opaque in ${n}`),c}function O(e,t,n,s){let{colors:l,type:u,space:d,shape:f,button:p}=n,m=(e,t)=>s(`email group key email.${e}.${t} is not an email role`);for(let[n,i]of Object.entries(e.color??{}))Object.hasOwn(r,n)?l[n]={light:D(t,i.$value,`light`,n,s),dark:D(t,i.$extensions?.[`com.webority`]?.dark??i.$value,`dark`,n,s)}:m(`color`,n);for(let[t,n]of Object.entries(e.type??{})){if(!Object.hasOwn(i,t)){m(`type`,t);continue}let{size:e,lineHeight:r,phoneSize:a,phoneLineHeight:o,narrowSize:c,narrowLineHeight:l}=n.$value??{};[e,r,a,o].every(e=>Number.isInteger(e)&&e>0)||s(`email group role ${t} needs size, lineHeight, phoneSize and phoneLineHeight in whole px`),u[t]=c===void 0?{size:e,lineHeight:r,phoneSize:a,phoneLineHeight:o}:{size:e,lineHeight:r,phoneSize:a,phoneLineHeight:o,narrowSize:c,narrowLineHeight:l}}for(let[t,n]of Object.entries(e.space??{})){if(!Object.hasOwn(a,t)){m(`space`,t);continue}let e=`email group role ${t}`;d[t]=o.has(t)?E(n.$value,e,s):T(n.$value,e,s)}for(let[t,n]of Object.entries(e.shape??{}))Object.hasOwn(c,t)?f[t]=T(n.$value,`email group role ${t}`,s):m(`shape`,t);for(let[t,n]of Object.entries(e.button??{}))t===`radius`?p.radius=T(n.$value,`email group role button radius`,s):t===`padding`?p.padding=E(n.$value,`email group role button padding`,s):m(`button`,t)}function k(e,t,n=null){if(e?.format!==`webority-theme-tokens/2`||e.source?.platform!==`web`)throw Error(`emitEmail: web must be the web theme tokens at width 1440`);if(e.source.width!==1440)throw Error(`emitEmail: web must be the 1440 theme`);if(t?.format!==`webority-theme-tokens/2`||t.source?.platform!==`web`||t.source?.width!==390)throw Error(`emitEmail: web390 must be the web theme tokens at width 390`);let r=[],i=e=>r.push(`emitEmail: ${e}`),a=m(e,i),o=h(e,t,i),s=v(e,i),c=y(e,i),l=b(e,i),u=w(e,i);if(n!==null&&O(n,e,{colors:a,type:o,space:s,shape:c,button:l},i),r.length>0)throw Error(r.join(`
2
- `));return{format:`webority-email-theme/1`,source:{package:e.source.package,version:e.source.version,brand:e.source.brand},colors:a,fonts:u,type:o,space:s,shape:c,button:l}}export{l as EMAIL_BUTTON,r as EMAIL_COLOR_ROLES,s as EMAIL_PADDING_PATTERN,o as EMAIL_PADDING_ROLES,c as EMAIL_SHAPE,a as EMAIL_SPACE,i as EMAIL_TYPE_ROLES,k as emitEmail};
1
+ import{isObjectLike as e}from"../guards.js";import{present as t}from"../present.js";var n=[`light`,`dark`],r=new Set([`serif`,`sans-serif`,`monospace`,`cursive`,`fantasy`,`system-ui`,`ui-serif`,`ui-sans-serif`,`ui-monospace`]),i=[[`body`,`ui`],[`display`,`display`],[`mono`,`mono`]],a=Object.freeze({page:`bg`,canvas:`bg`,card:`surface`,panel:`bg-subtle`,"panel-alt":`bg-muted`,surface:`surface`,line:`border`,"line-strong":`border-strong`,"accent-tint-line":`primary-wash-strong`,heading:`text`,strong:`text`,text:`text-body`,secondary:`text-muted`,muted:`text-subtle`,accent:`primary`,"on-accent":`on-primary`,link:`link`,success:`success-text`,warning:`warning-text`,danger:`danger-text`,"accent-tint":`primary-wash`,"success-tint":`success-soft`,"warning-tint":`warning-soft`,"danger-tint":`danger-soft`,"figure-panel":`primary-wash`}),o=Object.freeze({eyebrow:{step:`xs`,leading:`body`},h1:{step:`2xl`,leading:`heading`},lede:{step:`lg`,leading:`body`},body:{step:`base`,leading:`body`},"detail-value":{step:`sm`,leading:`body`},quote:{step:`base`,leading:`body`},"body-large":{step:`lg`,leading:`body`},title:{step:`2xl`,leading:`heading`},item:{step:`base`,leading:`body`},label:{step:`sm`,leading:`body`},note:{step:`sm`,leading:`body`},"note-small":{step:`xs`,leading:`body`},figure:{step:`3xl`,leading:`display`},"figure-hero":{step:`4xl`,leading:`display`},"figure-sub":{step:`sm`,leading:`body`},range:{step:`sm`,leading:`body`},sign:{step:`base`,leading:`body`},foot:{step:`xs`,leading:`body`},num:{step:`base`,leading:`body`},"code-otp":{step:`2xl`,leading:`heading`},"code-id":{step:`base`,leading:`body`},button:{step:`base`,leading:`body`}}),s=Object.freeze({section:`2xl`,"section-phone":`xl`,tight:`md`,item:`sm`,"heading-after":`xl`,"eyebrow-after":`xxs`,"title-after":`md`,"callout-lead-after":`xs`,"paragraph-gap":`sm`,"sign-gap":`2xl`,"sign-gap-phone":`lg`,"signature-gap":`md`,"step-gap":`lg`,"step-gutter":`3xl`,"step-gutter-phone":`2xl`,"step-number-space":`md`,"step-number-space-phone":`sm`,"column-top":`lg`,"column-gap":`lg`,"row-padding":`sm`,"panel-padding":`lg`,"panel-padding-wide":`lg`,"details-padding":`lg`,"details-padding-wide":`lg`,"hero-padding":`xl lg`,"hero-padding-wide":`2xl lg`,"columns-padding":`none lg lg lg`,"columns-padding-wide":`xs lg lg lg`}),c=new Set([`panel-padding`,`panel-padding-wide`,`details-padding`,`details-padding-wide`,`hero-padding`,`hero-padding-wide`,`columns-padding`,`columns-padding-wide`]),l=/^(0|\d+px)( (0|\d+px)){0,3}$/,u=Object.freeze({"panel-radius":`md`,"tint-radius":`md`,"code-radius":`md`,"details-radius":`md`}),d=Object.freeze({radius:`md`,padding:`md sm`});function f(e){return Object.hasOwn(a,e)}function p(e){return Object.hasOwn(o,e)}function m(e){return Object.hasOwn(s,e)}function h(e){return Object.hasOwn(u,e)}var g=Object.keys(a).filter(f),_=Object.keys(o).filter(p),v=Object.keys(s).filter(m),y=Object.keys(u).filter(h),b=i.map(([e])=>e);function x(e){return typeof e==`number`&&Number.isInteger(e)&&e>0}function S(e){return Math.floor(e+.5)}function C(e){if(typeof e!=`string`)return null;let t=/^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(e);if(t===null)return null;let n=t[1]??``;n.length===3&&(n=[...n].map(e=>e+e).join(``));let r=e=>Number.parseInt(n.slice(e,e+2),16);return{r:r(0),g:r(2),b:r(4),a:n.length===8?r(6)/255:1}}function w(e,t,n){return`#${[e,t,n].map(e=>e.toString(16).padStart(2,`0`)).join(``)}`.toUpperCase()}function T(e,t){if(e.a===1)return w(e.r,e.g,e.b);if(t===null)return null;let n=(t,n)=>S(t*e.a+n*(1-e.a));return w(n(e.r,t.r),n(e.g,t.g),n(e.b,t.b))}function E(e,t){let r={light:null,dark:null},i={light:{},dark:{}};for(let o of n){let n=e.colors?.[o]?.surface;n===void 0&&t(`email role card needs meaning surface in ${o}`);let s=C(n);s!==null&&s.a<1&&t(`email card colour (meaning surface, ${o}) must be opaque`),r[o]=s;for(let n of g){let s=a[n],c=e.colors?.[o]?.[s];if(c===void 0){t(`email role ${n} needs meaning ${s} in ${o}`);continue}let l=C(c);l===null?t(`email role ${n} (meaning ${s}, ${o}): cannot read colour ${c}`):i[o][n]=T(l,r[o])}}let o={};for(let e of g)o[e]={light:i.light[e]??null,dark:i.dark[e]??null};return o}function D(e,t,n){let r={};for(let i of _){let{step:a,leading:s}=o[i],c=e.text?.[a],l=t.text?.[a],u=e.typography?.leading?.[s],d=t.typography?.leading?.[s];c===void 0&&n(`email type role ${i} needs text step ${a}`),l===void 0&&n(`email type role ${i} needs text step ${a} in the 390 theme`),u===void 0&&n(`email type role ${i} needs leading ${s}`),d===void 0&&n(`email type role ${i} needs leading ${s} in the 390 theme`),c!==void 0&&l!==void 0&&u!==void 0&&d!==void 0&&(r[i]={size:S(c.size),lineHeight:S(c.size*u),phoneSize:S(l.size),phoneLineHeight:S(l.size*d)})}return r}function O(e,t,n,r){let i=[],a=!0;for(let o of t.split(/\s+/)){let t=e.space?.[o];typeof t==`number`?i.push(t):(r(n(o)),a=!1)}return a?i.map(e=>`${e}px`).join(` `):null}function k(e,t,n,r){if(/\s/.test(n))return r(`email space role ${t} is a gap and takes one space step, not ${n}`),null;let i=e.space?.[n];return typeof i==`number`?i:(r(`email space role ${t} needs space step ${n}`),null)}function A(e,t){let n={};for(let r of v){let i=s[r],a=c.has(r)?O(e,i,e=>`email space role ${r} needs space step ${e}`,t):k(e,r,i,t);a!==null&&(n[r]=a)}return n}function j(e,t){let n={};for(let r of y){let i=u[r],a=e.radius?.[i];typeof a==`number`?n[r]=a:t(`email shape role ${r} needs radius step ${i}`)}return n}function M(e,t){let n=e.radius?.[d.radius];return typeof n!=`number`&&t(`email button needs radius step ${d.radius}`),{radius:n,padding:O(e,d.padding,e=>`email button needs space step ${e}`,t)}}function N(e){let t=e.trim();return t.length>=2&&t[0]===t.at(-1)&&(t[0]===`"`||t[0]===`'`)?t.slice(1,-1).trim():t}function P(e){return r.has(e.toLowerCase())||e.startsWith(`-`)?e:e.includes(` `)?`'${e}'`:e}function F(e,t,n){let r=[t.family,...t.fallback.split(`,`)].map(N).filter(e=>e.length>0),i=r.find(e=>/["';<>]/.test(e));return i===void 0?r.map(P).join(`, `):(n(`email font ${e} holds an unsafe character: ${i}`),null)}function I(e,t){let n={};for(let[r,a]of i){let i=e.fonts?.[a];typeof i?.family!=`string`||typeof i?.fallback!=`string`?t(`email fonts need font ${a}`):n[r]=F(a,i,t)}return n}function L(t,n,r){if(e(t)){let e=t.value;if(typeof e==`number`&&Number.isInteger(e)&&e>=0&&t.unit===`px`)return e}return r(`${n} needs a whole px value`),null}function R(e,t,n){return typeof e==`string`&&l.test(e)?e:(n(`${t} needs CSS px padding`),null)}function z(e,t,n,r,i){let a=typeof t==`string`?/^\{color\.([a-z0-9-]+)\}$/.exec(t)?.[1]:void 0,o=a===void 0?t:e.colors?.[n]?.[a];if(o===void 0)return i(`email group role ${r} needs meaning ${a} in ${n}`),null;let s=C(o);if(s===null)return i(`email group role ${r} (${n}): cannot read colour ${o}`),null;let c=T(s,C(e.colors?.[n]?.surface));return c===null&&i(`email group role ${r} must be opaque in ${n}`),c}function B(t){let n=t.$extensions?.[`com.webority`];return(e(n)?n.dark:void 0)??t.$value}function V(t,n,r){let{size:i,lineHeight:a,phoneSize:o,phoneLineHeight:s,narrowSize:c,narrowLineHeight:l}=e(n)?n:{};if(!x(i)||!x(a)||!x(o)||!x(s))return r(`email group role ${t} needs size, lineHeight, phoneSize and phoneLineHeight in whole px`),null;let u={size:i,lineHeight:a,phoneSize:o,phoneLineHeight:s};return c===void 0?u:typeof c!=`number`||typeof l!=`number`?(r(`email group role ${t} needs narrowSize and narrowLineHeight to be numbers`),null):{...u,narrowSize:c,narrowLineHeight:l}}function H(e,t,n,r){let{colors:i,type:a,space:o,shape:s,button:l}=n,u=(e,t)=>r(`email group key email.${e}.${t} is not an email role`);for(let[n,a]of Object.entries(e.color??{}))f(n)?i[n]={light:z(t,a.$value,`light`,n,r),dark:z(t,B(a),`dark`,n,r)}:u(`color`,n);for(let[t,n]of Object.entries(e.type??{})){if(!p(t)){u(`type`,t);continue}let e=V(t,n.$value,r);e!==null&&(a[t]=e)}for(let[t,n]of Object.entries(e.space??{})){if(!m(t)){u(`space`,t);continue}let e=`email group role ${t}`;o[t]=c.has(t)?R(n.$value,e,r):L(n.$value,e,r)}for(let[t,n]of Object.entries(e.shape??{}))h(t)?s[t]=L(n.$value,`email group role ${t}`,r):u(`shape`,t);for(let[t,n]of Object.entries(e.button??{}))t===`radius`?l.radius=L(n.$value,`email group role button radius`,r)??void 0:t===`padding`?l.padding=R(n.$value,`email group role button padding`,r):u(`button`,t)}function U(e,n,r){let i={};for(let a of n)i[a]=t(r(a)??void 0,`${e}.${a}`);if(!W(n,i))throw Error(`emitEmail: ${e} is incomplete`);return i}function W(e,t){return e.every(e=>t[e]!==void 0)}function G(e,n){let{colors:r,fonts:i,type:a,space:o,shape:s,button:c}=n;return{format:`webority-email-theme/1`,source:{package:e.source.package,version:e.source.version,brand:e.source.brand},colors:U(`colors`,g,e=>{let n=r[e];return n===void 0?void 0:{light:t(n.light??void 0,`colors.${e}.light`),dark:t(n.dark??void 0,`colors.${e}.dark`)}}),fonts:U(`fonts`,b,e=>i[e]),type:U(`type`,_,e=>a[e]),space:U(`space`,v,e=>o[e]),shape:U(`shape`,y,e=>s[e]),button:{radius:t(c.radius,`button.radius`),padding:t(c.padding??void 0,`button.padding`)}}}function K(e,t,n=null){if(e?.format!==`webority-theme-tokens/2`||e.source?.platform!==`web`)throw Error(`emitEmail: web must be the web theme tokens at width 1440`);if(e.source.width!==1440)throw Error(`emitEmail: web must be the 1440 theme`);if(t?.format!==`webority-theme-tokens/2`||t.source?.platform!==`web`||t.source?.width!==390)throw Error(`emitEmail: web390 must be the web theme tokens at width 390`);let r=[],i=e=>r.push(`emitEmail: ${e}`),a={colors:E(e,i),type:D(e,t,i),space:A(e,i),shape:j(e,i),button:M(e,i),fonts:I(e,i)};if(n!==null&&H(n,e,a,i),r.length>0)throw Error(r.join(`
2
+ `));return G(e,a)}export{d as EMAIL_BUTTON,a as EMAIL_COLOR_ROLES,l as EMAIL_PADDING_PATTERN,c as EMAIL_PADDING_ROLES,u as EMAIL_SHAPE,s as EMAIL_SPACE,o as EMAIL_TYPE_ROLES,K as emitEmail};
@@ -0,0 +1,3 @@
1
+ import type { Model } from "../model/index.ts";
2
+ import type { MeaningRegistry } from "../outputs.ts";
3
+ export declare function emitMeanings(model: Model): MeaningRegistry;