archivo del paquete→ .shtukbase/packages/keer-ui/actions/place.ts
actions/place.ts
45 líneas, 2.1 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // keer-ui `place`: copies the component library into the project (like shadcn: the code becomes the project's).
- // Runs by itself after install and update; by hand: `shtuk run keer-ui place [--force]`.
- // A file the project has not touched since the last place is replaced by the new version; a file the project
- // changed is kept and named (with --force it is replaced too). Files are never deleted.
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
- import { dirname, join } from 'node:path';
- import { LIBRARY, MANIFEST, type Manifest, config, files, readManifest, render, sha, version } from './kitlib.ts';
- const root = process.cwd();
- const force = process.argv.includes('--force');
- const { target, tokens } = config(root);
- const old = readManifest(root, target);
- const next: Manifest = { version: version(), tokens, files: {} };
- let wrote = 0, same = 0;
- const kept: string[] = [];
- for (const rel of files(LIBRARY)) {
- const text = render(rel, readFileSync(join(LIBRARY, rel), 'utf8'), target, tokens);
- const dest = join(root, target, rel);
- const h = sha(text);
- if (!existsSync(dest)) {
- mkdirSync(dirname(dest), { recursive: true });
- writeFileSync(dest, text);
- next.files[rel] = h;
- wrote++;
- continue;
- }
- const have = sha(readFileSync(dest, 'utf8'));
- if (have === h) { next.files[rel] = h; same++; continue; }
- const clean = old?.files[rel] === have;
- if (clean || force) {
- writeFileSync(dest, text);
- next.files[rel] = h;
- wrote++;
- } else {
- next.files[rel] = old?.files[rel] ?? h; // stays "changed in the project" until placed with --force
- kept.push(rel);
- }
- }
- mkdirSync(join(root, target), { recursive: true });
- writeFileSync(join(root, target, MANIFEST), `${JSON.stringify(next, null, 2)}\n`);
- console.log(`keer-ui ${next.version}: ${target}/ — written ${wrote}, unchanged ${same}, kept ${kept.length}`);
- for (const k of kept) console.log(` kept, changed in the project: ${target}/${k} (replace: shtuk run keer-ui place --force)`);
- if (!existsSync(join(root, tokens))) console.log(` note: tokens file ${tokens} is missing — install keer-style or set tokens: in ${'design/keer-ui.md'}`);