包文件→ .shtukbase/packages/shtukbase-ui-react/actions/kitlib.ts
actions/kitlib.ts
90 行, 4.4 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // Shared parts of the shtukbase-ui-react action and checks. The working directory is the project root.
- // The library lives in .shtukbase/packages/shtukbase-ui-react/library; `place` copies it into the project folder named by
- // `target:` of /design/shtukbase-ui-react.md (default src/sb-ui) and writes <target>/.shtukbase-ui-react.json: the hash of
- // every file it laid.
- import { createHash } from 'node:crypto';
- import { existsSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs';
- import { dirname, join, relative } from 'node:path';
- import { readPage } from '../../collections/checks/lib/pages.ts';
- export const CONFIG = 'design/shtukbase-ui-react.md';
- export const MANIFEST = '.shtukbase-ui-react.json';
- export const LIBRARY = join(import.meta.dir, '..', 'library');
- /** Where the HTML library lays its components and its joined CSS (the design: one look, written once, in its CSS). */
- export const HTML_ROOT = 'design/shtukbase-ui-html';
- /** Level of a component -> the folder of the library it lives in. */
- export const LEVEL_DIRS: Record<string, string> = { primitive: 'primitives', composite: 'composites', block: 'blocks', shell: 'shell', page: 'pages' };
- export type Manifest = { version: string; css: string; files: Record<string, string> };
- export function sha(text: string): string {
- return createHash('sha256').update(text).digest('hex');
- }
- /** target and css paths (relative to the project root) from the config page, with defaults. */
- export function config(root = process.cwd()): { target: string; css: string } {
- const page = join(root, CONFIG);
- const fm = existsSync(page) ? readPage(page).fm : {};
- const clean = (v: unknown, d: string) => (typeof v === 'string' && v.trim() ? v.trim().replace(/^\/+|\/+$/g, '') : d);
- return { target: clean(fm.target, 'src/sb-ui'), css: clean(fm.css, `${HTML_ROOT}/ui.css`) };
- }
- /** Every file under a folder, relative paths with forward slashes. */
- export function files(dir: string, rel = ''): string[] {
- if (!existsSync(join(dir, rel))) return [];
- const out: string[] = [];
- for (const name of readdirSync(join(dir, rel)).sort()) {
- const r = rel ? `${rel}/${name}` : name;
- if (statSync(join(dir, r)).isDirectory()) out.push(...files(dir, r));
- else out.push(r);
- }
- return out;
- }
- /** The text of a library file as it lies in the project: the CSS import points at the project's `ui.css`. */
- export function render(rel: string, text: string, target: string, css: string): string {
- if (!text.includes('{{css}}')) return text;
- let path = relative(dirname(join(target, rel)), css).split('\\').join('/');
- if (!path.startsWith('.')) path = `./${path}`;
- return text.split('{{css}}').join(path);
- }
- export function readManifest(root: string, target: string): Manifest | null {
- const p = join(root, target, MANIFEST);
- if (!existsSync(p)) return null;
- try {
- return JSON.parse(readFileSync(p, 'utf8')) as Manifest;
- } catch {
- return null;
- }
- }
- /** The package version from its package.md (for the manifest). */
- export function version(): string {
- const p = join(import.meta.dir, '..', 'package.md');
- return existsSync(p) ? String(readPage(p).fm.version ?? '') : '';
- }
- /** The component files of a folder: `<level dir>/<id>.tsx`, not states, tests, stories; lib/, hooks/, styles/ are not components. */
- export function componentFiles(dir: string): string[] {
- return files(dir).filter((rel) => /^(primitives|composites|blocks|shell|pages)\/[^/]+\.tsx$/.test(rel) && !/\.(states|test|spec|stories)\.tsx$/.test(rel));
- }
- /**
- * Lets `design-components sync` find the placed components: adds the target folder to `sources:` in the header of
- * /design/components.md (a one-line list there: `sources: []`, `sources: [a, b]`). Returns what happened, for the log.
- */
- export function registerSource(root: string, target: string): 'added' | 'present' | 'no-page' | 'by-hand' {
- const path = join(root, 'design/components.md');
- if (!existsSync(path)) return 'no-page';
- const text = readFileSync(path, 'utf8');
- const head = /^---\n[\s\S]*?\n---/.exec(text);
- if (!head) return 'by-hand';
- const m = /^sources:[ \t]*\[([^\]\n]*)\][ \t]*$/m.exec(head[0]);
- if (!m) return 'by-hand';
- const items = m[1].split(',').map((x) => x.trim().replace(/^["']|["']$/g, '').replace(/^\/+|\/+$/g, '')).filter(Boolean);
- if (items.includes(target)) return 'present';
- const line = `sources: [${[...items, target].join(', ')}]`;
- writeFileSync(path, text.replace(head[0], head[0].replace(m[0], line)));
- return 'added';
- }