файл пакета→ .shtukbase/packages/shadcn/actions/place.ts
actions/place.ts
69 строк, 3.4 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // shadcn `place`: lays shadcn/ui components into the project — the code becomes the project's, as with the shadcn CLI.
- // Runs by itself after install and update; by hand: `shtuk run shadcn place [name ...] [--force]`.
- // Which components: `components:` of /design/shadcn.md, the ones placed before (.shadcn.json) and the names given;
- // their registry dependencies come too. With them: cn() at the `utils` alias and the theme at `tailwind.css` of
- // components.json (written with defaults when there is none).
- // A file the project has not touched since the last place is replaced by the new version; a file the project changed
- // (or that was there before the package) is kept and named; --force replaces it too. Nothing is ever deleted.
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
- import { dirname, join } from 'node:path';
- import {
- BASE_DEPS, CONFIG, MANIFEST, PAGE, REGISTRY, type Manifest, closure, components, index, page, readManifest, render,
- sha, targets, version,
- } from './shadcnlib.ts';
- const root = process.cwd();
- const force = process.argv.includes('--force');
- const asked = process.argv.slice(2).filter((a) => !a.startsWith('--'));
- let created = false;
- if (!existsSync(join(root, CONFIG))) {
- writeFileSync(join(root, CONFIG), readFileSync(join(REGISTRY, 'components.json'), 'utf8'));
- created = true;
- }
- const { cfg, error } = components(root);
- if (!cfg) {
- console.log(`shadcn: ${error} — fix components.json and run again`);
- process.exit(1);
- }
- const items = index();
- const old = readManifest(root);
- const { all, unknown } = closure([...page(root).components, ...(old?.components ?? []), ...asked], items);
- const t = targets(cfg, root);
- const plan: [string, string][] = all.map((n) => [`${t.ui}/${items[n].file}`, render(readFileSync(join(REGISTRY, 'ui', items[n].file), 'utf8'), cfg)]);
- plan.push([t.utils, readFileSync(join(REGISTRY, 'lib', 'utils.ts'), 'utf8')]);
- plan.push([t.theme, readFileSync(join(REGISTRY, 'styles', 'theme.css'), 'utf8')]);
- const next: Manifest = { version: version(), components: all, files: {} };
- let wrote = 0, same = 0;
- const kept: string[] = [];
- for (const [rel, text] of plan) {
- const dest = join(root, 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; }
- if (old?.files[rel] === have || force) {
- writeFileSync(dest, text);
- next.files[rel] = h;
- wrote++;
- } else {
- if (old?.files[rel]) next.files[rel] = old.files[rel]; // stays "changed in the project" until placed with --force
- kept.push(rel);
- }
- }
- writeFileSync(join(root, MANIFEST), `${JSON.stringify(next, null, 2)}\n`);
- const deps = [...new Set([...BASE_DEPS, ...all.flatMap((n) => items[n].dependencies)])].sort();
- console.log(`shadcn ${next.version}: ${all.length} components in ${t.ui}/ — written ${wrote}, unchanged ${same}, kept ${kept.length}`);
- if (created) console.log(` created ${CONFIG} (defaults: new-york, neutral, theme in ${t.theme})`);
- for (const k of kept) console.log(` kept, edited in the project: ${k} (replace: shtuk run shadcn place --force)`);
- for (const u of unknown) console.log(` not in the registry: ${u} (the list is on the page /${PAGE})`);
- console.log(` the project needs from npm: ${deps.join(', ')}`);