|
| 1 | +#!/usr/bin/env node |
| 2 | +import { writeFile } from 'node:fs/promises'; |
| 3 | +import type * as M from 'custom-elements-manifest/schema'; |
| 4 | + |
| 5 | +import manifest from '../elements/custom-elements.json' with { type: 'json' }; |
| 6 | + |
| 7 | +function isCustomElementDeclaration(decl: M.Declaration): decl is M.CustomElementDeclaration { |
| 8 | + return !!(decl as M.CustomElementDeclaration).customElement; |
| 9 | +} |
| 10 | + |
| 11 | +const RE = /^https:\/\/patternflyelements\.com\/components\/(?<slug>[\w-]+)\//; |
| 12 | + |
| 13 | +const copy = structuredClone(manifest) as M.Package; |
| 14 | + |
| 15 | +const getSlug = (demo: M.Demo) => |
| 16 | + demo.url.match(RE)?.groups?.slug ?? ''; |
| 17 | + |
| 18 | +const isMainDemo = (demo: M.Demo): boolean => |
| 19 | + demo.url.endsWith(`${getSlug(demo)}/`) |
| 20 | +|| demo.url.endsWith('demo/'); |
| 21 | + |
| 22 | +// replace all canonical demos with / |
| 23 | +// e.g. |
| 24 | +// from: https://patternflyelements.com/components/button/demo/button/ |
| 25 | +// to: https://patternflyelements.com/components/button/demo/ |
| 26 | +// This is a stopgap. the ideal would be to either generate the canonical demo from the cem, aka knobs |
| 27 | +// or to include it in the jsdoc in an @example tag |
| 28 | +// or to rearrange the elements/*/demo/*.html file structure |
| 29 | +await writeFile('custom-elements.json', JSON.stringify({ |
| 30 | + ...manifest, |
| 31 | + modules: copy.modules.map(module => ({ |
| 32 | + ...module, |
| 33 | + declarations: module.declarations?.map(decl => ({ |
| 34 | + ...decl, |
| 35 | + demos: |
| 36 | + !isCustomElementDeclaration(decl) ? decl |
| 37 | + : decl.demos?.map(demo => isMainDemo(demo) ? ({ |
| 38 | + ...demo, |
| 39 | + url: `https://patternflyelements.com/components/${getSlug(demo)}/demo/`, |
| 40 | + }) : demo) |
| 41 | + .sort((a, b) => isMainDemo(a) ? -1 : isMainDemo(b) ? 1 : 0), |
| 42 | + })), |
| 43 | + })), |
| 44 | +}, null, 2), 'utf-8'); |
0 commit comments