@slabui/cli 0.1.0 → 0.1.2

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @slabui/cli
2
2
 
3
+ ## 0.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Versão alinhada com o `slabui`, que não pode usar a `0.1.0` nem a `0.1.1` (já usadas no npm antes de as versões serem zeradas). Sem mudanças no código.
8
+
3
9
  ## 0.1.0
4
10
 
5
11
  ### Minor Changes
package/dist/cli.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import e from"node:path";import{Command as t,Option as n}from"commander";import r from"postcss";import{access as i,lstat as a,mkdir as o,readFile as s,realpath as c,rm as l,writeFile as u}from"node:fs/promises";import{createHash as d,randomBytes as f}from"node:crypto";import{z as p}from"zod";import{homedir as m,hostname as h}from"node:os";import{createPathsMatcher as g,parseTsconfig as _}from"get-tsconfig";import{applyEdits as v,modify as y,parse as b}from"jsonc-parser";import x from"semver";import{spawn as S}from"node:child_process";import*as C from"@clack/prompts";import{createTwoFilesPatch as ee}from"diff";import w from"minisearch";import{createServer as te}from"node:http";import{merge as ne}from"node-diff3";var T=`0.1.0`,re=`The SlabUI CLI: add, update and compare items from versioned component registries`;const ie=/^[a-z0-9][a-z0-9-]*$/i;function ae(e){return ie.test(e)}function oe(e,t=!1){if(!ae(e))throw Error(`Invalid theme name: "${e}"`);return`:root[data-slabui-theme="${e}"]${t?`.dark`:``}`}const se=/^\s*(oklch|oklab|lch|lab|hsla?|rgba?|hwb|color)\(|^\s*#[0-9a-f]{3,8}\s*$/i;async function ce(e,t,{base:n,overwrite:i=!1,shouldRemove:a,theme:o}={}){let s=r.parse(e),c=[],l=[],u=e=>o?e:le(e),d=u(t),f=n?u(n):void 0;for(let e of[`theme`,`light`,`dark`]){let t=d[e]??{},n=f?.[e]??{},u=/* @__PURE__ */ new Set([...Object.keys(t),...Object.keys(n)]);if(u.size===0)continue;let p=fe(s,e,o);for(let d of u){let u=t[d],f=n[d],m=p?.nodes?.find(e=>e.type===`decl`&&e.prop===`--${d}`),h=m?.value,g=t=>c.push({group:e,name:d,action:t});if(u===void 0){if(h===void 0)continue;a&&await a(e,d)?(m?.remove(),g(`removed`)):(l.push(`--${d} (${e}) was removed in the new version; kept in the CSS.`),g(`kept`))}else if(h===void 0){if(f!==void 0){g(`kept`);continue}p??=pe(s,e,o),p.raws.semicolon=!0,p.raws.after=`
2
+ import e from"node:path";import{Command as t,Option as n}from"commander";import r from"postcss";import{access as i,lstat as a,mkdir as o,readFile as s,realpath as c,rm as l,writeFile as u}from"node:fs/promises";import{createHash as d,randomBytes as f}from"node:crypto";import{z as p}from"zod";import{homedir as m,hostname as h}from"node:os";import{createPathsMatcher as g,parseTsconfig as _}from"get-tsconfig";import{applyEdits as v,modify as y,parse as b}from"jsonc-parser";import x from"semver";import{spawn as S}from"node:child_process";import*as C from"@clack/prompts";import{createTwoFilesPatch as ee}from"diff";import w from"minisearch";import{createServer as te}from"node:http";import{merge as ne}from"node-diff3";var T=`0.1.2`,re=`The SlabUI CLI: add, update and compare items from versioned component registries`;const ie=/^[a-z0-9][a-z0-9-]*$/i;function ae(e){return ie.test(e)}function oe(e,t=!1){if(!ae(e))throw Error(`Invalid theme name: "${e}"`);return`:root[data-slabui-theme="${e}"]${t?`.dark`:``}`}const se=/^\s*(oklch|oklab|lch|lab|hsla?|rgba?|hwb|color)\(|^\s*#[0-9a-f]{3,8}\s*$/i;async function ce(e,t,{base:n,overwrite:i=!1,shouldRemove:a,theme:o}={}){let s=r.parse(e),c=[],l=[],u=e=>o?e:le(e),d=u(t),f=n?u(n):void 0;for(let e of[`theme`,`light`,`dark`]){let t=d[e]??{},n=f?.[e]??{},u=/* @__PURE__ */ new Set([...Object.keys(t),...Object.keys(n)]);if(u.size===0)continue;let p=fe(s,e,o);for(let d of u){let u=t[d],f=n[d],m=p?.nodes?.find(e=>e.type===`decl`&&e.prop===`--${d}`),h=m?.value,g=t=>c.push({group:e,name:d,action:t});if(u===void 0){if(h===void 0)continue;a&&await a(e,d)?(m?.remove(),g(`removed`)):(l.push(`--${d} (${e}) was removed in the new version; kept in the CSS.`),g(`kept`))}else if(h===void 0){if(f!==void 0){g(`kept`);continue}p??=pe(s,e,o),p.raws.semicolon=!0,p.raws.after=`
3
3
  `,p.append(r.decl({prop:`--${d}`,value:u,raws:{before:`
4
4
  `}})),g(`added`)}else he(h,u)?g(`unchanged`):i||f!==void 0&&he(h,f)?(m.value=u,g(`updated`)):(l.push(`--${d} (${e}) has a custom value in the CSS (${h}); kept instead of ${u}.`),g(`kept`))}}return!o&&Object.keys(d.dark??{}).length>0&&me(s),{css:s.toString(),changes:c,warnings:l}}function le(e){let t={...e.theme};for(let n of[e.light,e.dark])for(let[e,r]of Object.entries(n??{}))se.test(r)&&t[`color-${e}`]===void 0&&(t[`color-${e}`]=`var(--${e})`);return{...e,...Object.keys(t).length>0&&{theme:t}}}function ue(e,t){return t?oe(t,e===`dark`):e===`light`?`:root`:`.dark`}const de=e=>e.replace(/["'\s]/g,``);function fe(e,t,n){return e.nodes.find(e=>t===`theme`?e.type===`atrule`&&e.name===`theme`&&e.params.trim()===`inline`:e.type===`rule`&&de(e.selector)===de(ue(t,n)))}function pe(e,t,n){let i=t===`theme`?r.atRule({name:`theme`,params:`inline`}):r.rule({selector:ue(t,n)});return i.raws={before:e.nodes.length>0?`
5
5
 
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import{createHash as e,randomBytes as t}from"node:crypto";import{z as n}from"zod";import{homedir as r,hostname as i}from"node:os";import a from"node:path";import{access as o,lstat as s,mkdir as c,readFile as l,realpath as u,rm as d,writeFile as f}from"node:fs/promises";import{createPathsMatcher as p,parseTsconfig as m}from"get-tsconfig";import"jsonc-parser";import h from"semver";import{spawn as g}from"node:child_process";import _ from"postcss";import{createTwoFilesPatch as v}from"diff";import{merge as y}from"node-diff3";import{createServer as b}from"node:http";import x from"minisearch";const S=`slabui.json`,C={namespace:`@slabui`,url:`https://slabui.com/r`},w=`https://slabui.com`,T=`${w}/schema.json`,E={command:`x-slabui-command`,optOut:`x-slabui-telemetry`},D=[`ui`,`component`,`lib`,`hook`,`util`,`style`,`file`,`theme`,`block`,`example`],O=[`free`,`pro`],k=`SLABUI_TOKEN`,ee=`whoami.json`,te=[`claude`,`cursor`,`vscode`,`zed`,`codex`];function A(e=process.env,t=process.platform,n=r()){let i=t===`win32`?a.join(e.APPDATA??a.join(n,`AppData/Roaming`),`slabui`):a.join(e.XDG_CONFIG_HOME||a.join(n,`.config`),`slabui`);return a.join(i,`credentials.json`)}async function ne(e=A()){try{let{token:t}=JSON.parse(await l(e,`utf8`));return typeof t==`string`&&t?t:void 0}catch{return}}async function re(e,t=A()){await c(a.dirname(t),{recursive:!0}),await f(t,`${JSON.stringify({token:e})}\n`,{mode:384})}async function ie({registryUrl:e,token:t,fetch:n=globalThis.fetch,headers:r={}}){let i=`${e.replace(/\/+$/,``)}/whoami.json`,a=await n(i,{headers:{...r,authorization:`Bearer ${t.trim()}`},signal:AbortSignal.timeout(15e3)});if(a.status===401)return null;if(!a.ok)throw Error(`Error ${a.status} while fetching ${i}.`);let o=await a.json();return{...o,access:o.access??[]}}function ae(){return t(24).toString(`base64url`)}function oe({registryUrl:e,callback:t,state:n,client:r,host:a=i()}){let o=new URL(`/cli/authorize`,new URL(e).origin);return o.searchParams.set(`callback`,t),o.searchParams.set(`state`,n),o.searchParams.set(`client`,r),o.searchParams.set(`host`,a.replace(/\.local$/,``).slice(0,60)),o.toString()}var j=class extends Error{name=`SlabUIError`};const M=n.string().regex(/^[a-z0-9]+(-[a-z0-9]+)*$/,`name must be kebab-case`),se=n.string().regex(/^@[a-z0-9-]+$/,`namespace must look like @name`),N=n.string().regex(/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/,`version must be X.Y.Z`),ce=n.string().regex(/^sha256-[A-Za-z0-9+/]+={0,2}$/,`hash must be sha256-<base64>`),P=n.enum(D),le=n.enum(O),ue=n.string().regex(/^(@[a-z0-9-]+\/)?[a-z0-9]+(-[a-z0-9]+)*(@\S+)?$/,`dependency must look like [@ns/]name[@version]`),de=n.object({registry:n.array(ue).default([]),npm:n.array(n.string().min(1)).default([]),npmDev:n.array(n.string().min(1)).default([])}),fe=[e=>e.kind!==`file`||e.target!==void 0,{message:`a file with kind "file" needs a target`,path:[`target`]}],pe=[e=>e.files.length>0||e.cssVars!==void 0,{message:`the item needs files or cssVars`,path:[`files`]}],me=n.object({path:n.string().min(1),kind:P,target:n.string().min(1).optional()}).refine(...fe),he=n.object({path:n.string().min(1),kind:P,target:n.string().min(1).optional(),content:n.string()}).refine(...fe),F=n.record(n.string(),n.string()),ge=n.object({theme:F.optional(),light:F.optional(),dark:F.optional()}),_e={name:M,kind:P,title:n.string().min(1).optional(),description:n.string().min(1),dependencies:de.prefault({}),cssVars:ge.optional()},ve=n.object({$schema:n.string().optional(),schemaVersion:n.literal(1),..._e,version:N,files:n.array(he).default([])}).refine(...pe);n.object({..._e,categories:n.array(n.string().min(1)).optional(),keywords:n.array(n.string().min(1)).optional(),docs:n.url().optional(),access:le.optional(),inferDependencies:n.boolean().optional(),files:n.array(me).default([])}).refine(...pe);const ye=n.string().regex(/^(?!.*(^|\/)\.\.?(\/|$))[A-Za-z0-9._-]+(\/[A-Za-z0-9._-]+)*\.json$/,`path must be a relative path to a .json file`),be=n.object({hash:ce,path:ye}),xe=n.object({kind:P,title:n.string().min(1).optional(),description:n.string().min(1),categories:n.array(n.string().min(1)).optional(),keywords:n.array(n.string().min(1)).optional(),docs:n.url().optional(),access:le.optional(),dependencies:de.prefault({}),tokens:n.array(n.string().min(1)).optional(),latest:N,versions:n.record(N,be)}).refine(e=>e.latest in e.versions,{message:`latest must exist in versions`,path:[`latest`]}),Se=n.object({$schema:n.string().optional(),schemaVersion:n.literal(2),namespace:se.optional(),homepage:n.url().optional(),mcp:n.url().optional(),license:n.url().optional(),items:n.record(M,xe)});var Ce=`0.1.0`;let we;function Te(e){return e.SLABUI_TELEMETRY_DISABLED===`1`||e.DO_NOT_TRACK===`1`}function Ee(e,t=we){let n={"user-agent":`slabui/${Ce}`};return Te(e)?n[E.optOut]=`off`:t&&(n[E.command]=t),n}const De=/* @__PURE__ */ new Map;function I(e,{fetch:t=globalThis.fetch,env:r=process.env,command:i,savedToken:a=()=>ne()}={}){let o=e.url.replace(/\/+$/,``),s={...Ee(r,i),...ke(e.headers,r)},c;async function l(t){let n=Object.keys(s).some(e=>e.toLowerCase()===`authorization`);if(!e.isDefault||n)return{};let i=(e.token===void 0?void 0:Ae(e.token,`"token" in slabui.json`,r))||r.SLABUI_TOKEN||await a();if(!i)throw new j(`"${t}" is a Pro item. Run "slabui login" or set ${k} to install it.`);return{authorization:`Bearer ${i}`}}async function u(n,{extraHeaders:r={},pro:i}={}){let a;try{a=await t(n,{headers:{...s,...r},signal:AbortSignal.timeout(3e4)})}catch{throw new j(`Could not reach ${n}.`)}if(i!==void 0&&e.isDefault&&a.status===401)throw new j(`The token was refused while fetching the Pro item "${i}". Run "slabui login" again or check ${k}.`);if(i!==void 0&&e.isDefault&&a.status===403)throw new j(`Your token does not give access to the Pro item "${i}". Check your SlabUI Pro subscription.`);if(a.status===401||a.status===403)throw new j(`Access denied to ${n} (${a.status}).`);if(a.status===404)throw new j(`Not found: ${n}.`);if(!a.ok)throw new j(`Error ${a.status} while fetching ${n}.`);return new Uint8Array(await a.arrayBuffer())}function d(e,t,r,i){let a;try{a=JSON.parse(new TextDecoder().decode(t))}catch{throw new j(`${e} is not valid JSON.`)}i?.(e,a);let o=r.safeParse(a);if(!o.success)throw new j(`${e} does not match the expected format:\n${n.prettifyError(o.error)}`);return o.data}async function f(){let t=`${o}/registry.json`,n=d(t,await u(t),Se,Oe);return De.set(e.namespace,n),n}function p(){return c??=f().catch(e=>{throw c=void 0,e}),c}return{index:p,async item(t,n){let r=(await p()).items[t];if(r===void 0)throw new j(`Item "${t}" does not exist in the "${e.namespace}" registry.`);let i=r.versions[n];if(i===void 0)throw new j(`Version ${n} of "${t}" does not exist in the "${e.namespace}" registry.`);let a=`${o}/${i.path}`,s=r.access===`pro`?await u(a,{extraHeaders:await l(t),pro:t}):await u(a);if(je(s)!==i.hash)throw new j(`${a} does not match the hash in the index. The file may have been changed on the server.`);let c=d(a,s,ve);if(c.name!==t||c.version!==n)throw new j(`${a} contains ${c.name}@${c.version}, not ${t}@${n}.`);return c}}}function Oe(e,t,n=`The registry needs to be published again in the current format.`){let r=t&&typeof t==`object`&&`schemaVersion`in t?t.schemaVersion:void 0;if(typeof r==`number`&&r!==2)throw r>2?new j(`${e} uses format ${r}, which this version of slabui does not support. Update it with \`pnpm add -D slabui@latest\`.`):new j(`${e} uses format ${r}, which is no longer supported. ${n}`)}function ke(e,t=process.env){return Object.fromEntries(Object.entries(e).map(([e,n])=>[e,Ae(n,`the "${e}" header`,t)]))}function Ae(e,t,n){return e.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g,(e,r)=>{let i=n[r];if(i===void 0)throw new j(`Environment variable ${r}, used in ${t}, is not set.`);return i})}function je(t){return`sha256-${e(`sha256`).update(t).digest(`base64`)}`}const Me=n.object({version:N,files:n.array(n.string().min(1))}),Ne=n.object({$schema:n.string().optional(),token:n.string().min(1).optional(),css:n.string().min(1).optional(),aliases:n.object({components:n.string().min(1),ui:n.string().min(1),lib:n.string().min(1),hooks:n.string().min(1),utils:n.string().min(1),styles:n.string().min(1)}),items:n.record(M,Me).default({})}),Pe=[`tsconfig.json`,`jsconfig.json`],Fe=[`app/globals.css`,`src/app/globals.css`,`styles/globals.css`,`src/styles/globals.css`,`app/global.css`,`src/index.css`,`src/styles.css`];async function L(e){return o(e).then(()=>!0,()=>!1)}async function R(e){for(let t of Pe){let n=a.join(e,t);if(await L(n))try{return{path:n,config:m(n)}}catch(e){throw new j(`Could not read ${t}: ${e.message}`)}}throw new j(`No tsconfig.json or jsconfig.json in ${e}. slabui uses its "paths" to resolve the aliases.`)}function z(e){let t=Object.keys(e.config.compilerOptions?.paths??{}).filter(e=>e.endsWith(`/*`));return(t.find(e=>e===`@/*`)??t.find(e=>e===`~/*`)??t[0])?.slice(0,-1)}function B(e,t,n){if(n===void 0)return;let r=z(e),i=r!==void 0&&n.startsWith(r)?V(e,n):void 0;return i===void 0?n:a.relative(t,i).split(a.sep).join(`/`)}function V(e,t){return p(e)?.(t)[0]}async function Ie(e){for(let t of Fe)if(await L(a.join(e,t)))return t}async function H(e){let t=a.join(e,S),r=await l(t,`utf8`).catch(()=>void 0);if(r===void 0)throw new j(`${S} not found in ${e}. Run "slabui init" first.`);let i;try{i=JSON.parse(r)}catch{throw new j(`${S} is not valid JSON.`)}let o=Ne.safeParse(i);if(!o.success)throw new j(`${S} has errors:\n${n.prettifyError(o.error)}`);return o.data}async function U(e,t){await f(a.join(e,S),`${JSON.stringify(t,null,2)}\n`)}function Le(e){return e.startsWith(`./`)||e.startsWith(`../`)}async function Re(e){let t=z(await R(e));if(t===void 0)throw new j(`The tsconfig has no alias in "paths" (e.g. "@/*": ["./*"]). slabui uses it to place files and write imports.`);return{aliases:{components:`${t}components`,ui:`${t}components/ui`,lib:`${t}lib`,hooks:`${t}hooks`,utils:`${t}utils`,styles:`${t}styles`},...await ze(e,`${t}styles`)}}async function ze(e,t){let n=await Ie(e);return n===void 0?{css:`${t}/globals.css`,cssDetected:!1}:{css:n,cssDetected:!0}}async function Be({root:e,aliases:t,css:n,force:r=!1}){if(!r&&await L(a.join(e,`slabui.json`)))throw new j(`${S} already exists. Use --force to create it again.`);let i=await R(e);for(let[e,n]of Object.entries(t))if(!Le(n)&&V(i,n)===void 0)throw new j(`Alias "${n}" (${e}) does not match any "paths" in the tsconfig.`);let o={$schema:T,...n&&{css:n},aliases:t,items:{}};return await U(e,o),o}async function W(e,t){let n=e=>new j(`Path "${t}" refused: ${e}.`);if(t===``)throw n(`it is empty`);if(t.includes(`\0`))throw n(`it contains a null character`);if(t.includes(`\\`))throw n(`use / as the separator`);if(a.posix.isAbsolute(t)||a.win32.isAbsolute(t))throw n(`it is absolute`);let r=a.resolve(e),i=a.resolve(r,t);if(i===r)throw n(`it points to the project root`);if(!Ve(r,i))throw n(`it leaves the project root`);let o=await He(i);if(o===i&&(await s(i)).isSymbolicLink())throw n(`the target is a symbolic link`);let c=await u(r),l=await u(o).catch(()=>{throw n(`it goes through a broken symbolic link`)});if(!Ve(c,l))throw n(`it goes through a symbolic link outside the project`);return a.join(l,a.relative(o,i))}function Ve(e,t){let n=a.relative(e,t);return n===``||n!==`..`&&!n.startsWith(`..${a.sep}`)&&!a.isAbsolute(n)}async function He(e){let t=e;for(;;)try{return await s(t),t}catch(e){if(e.code!==`ENOENT`)throw e;t=a.dirname(t)}}const G=[`.ts`,`.tsx`,`.js`,`.jsx`,`.mts`,`.cts`,`.mjs`],Ue=/(\bfrom\s+|\bimport\s+|\bimport\s*\(\s*)(["'])([^"'\n]+)\2/g,We=/(@(?:import|reference)\s+)(["'])([^"'\n]+)\2/g;function Ge(e){return a.posix.extname(e)===`.css`}function Ke(e,t){return e.replace(We,(e,n,r,i)=>{let a=t(i);return a===void 0?e:`${n}${r}${a}${r}`})}function qe(e){return G.includes(a.posix.extname(e))}function Je(e,t){return e.replace(Ue,(e,n,r,i)=>{let a=t(i);return a===void 0?e:`${n}${r}${a}${r}`})}function Ye(e,t){return[e,...G.map(t=>`${e}${t}`),...G.map(t=>`${e}/index${t}`)].find(e=>t.has(e))}function Xe(e){let t=a.posix.extname(e);return G.includes(t)?e.slice(0,-t.length):e}function K(e){let t=e;if(t.startsWith(`@`)){let n=t.indexOf(`/`);if(n===-1)throw new j(`"${e}" has no item name after the namespace.`);if(t.slice(0,n)!==C.namespace)throw new j(`"${e}" is not from the SlabUI registry. Only SlabUI items can be installed.`);t=t.slice(n+1)}let n=t.indexOf(`@`),r=n===-1?t:t.slice(0,n),i=n===-1?void 0:t.slice(n+1);if(!M.safeParse(r).success)throw new j(`"${r}" is not a valid item name (kebab-case).`);if(i!==void 0&&(!i||h.validRange(i)===null))throw new j(`"${i}" is not a valid version or range.`);return{name:r,...i!==void 0&&{range:i}}}function q(e){return e.name}function J(e,t=process.env){return{namespace:C.namespace,url:t.SLABUI_REGISTRY_URL||C.url,headers:{},isDefault:!0,...e!==void 0&&{token:e}}}function Y(e,t){if(e.range===void 0)return t.latest;let n=Object.keys(t.versions),r=h.maxSatisfying(n,e.range);if(r===null)throw new j(`No version of "${q(e)}" satisfies "${e.range}". Available: ${h.sort(n).join(`, `)}.`);return r}const Ze={ui:`ui`,component:`components`,lib:`lib`,hook:`hooks`,util:`utils`,style:`styles`};function Qe(e,t){if(e.target!==void 0){let n=e.target;return et((n.startsWith(z(t.tsconfig)??`\0`)?V(t.tsconfig,n):void 0)??a.resolve(t.root,n),t)}if(!(e.kind in Ze))throw new j(`Files with kind "${e.kind}" cannot be installed by add yet (${e.path}).`);let n=t.aliases[Ze[e.kind]],r=e.kind===`style`&&e.path.split(`/`).slice(1).join(`/`)||a.posix.basename(e.path),i=Le(n)?a.resolve(t.root,n,r):V(t.tsconfig,`${n}/${r}`);if(i===void 0)throw new j(`Alias "${n}" does not match any "paths" in the tsconfig.`);return et(i,t)}function $e(e,t,n){let r=z(n.tsconfig),i=r===void 0?void 0:V(n.tsconfig,`${r}__slabui__`)?.slice(0,-10),o=a.resolve(n.root,e);if(r!==void 0&&i!==void 0&&o.startsWith(i))return`${r}${Xe(a.relative(i,o).split(a.sep).join(`/`))}`;let s=a.posix.relative(a.posix.dirname(t),e);return s.startsWith(`.`)||(s=`./${s}`),Xe(s)}function et(e,t){return a.relative(t.root,e).split(a.sep).join(`/`)}function X(e){return e.kind===`block`||e.kind===`example`}function Z(e,t,n){return X(e)?at(e,n):new Map(e.files.map(e=>[e.path,Qe(e,t)]))}function tt(e,t){return[e,...t.dependencies.registry.map(e=>q(K(e)))]}async function nt(e,{config:t,context:n,destinations:r,clientFor:i}){for(let a of e)for(let e of a.dependencies.registry){let a=K(e),o=q(a),s=t.items[o];if(r.has(o)||s===void 0)continue;let c=i(J(t.token));r.set(o,Z(await c.item(a.name,s.version),n))}}function rt(e,{key:t,item:n,destination:r,destinations:i,context:a,warnings:o}){let s=Ge(e.path);if(!qe(e.path)&&!s)return e.content;let c=tt(t,n),l=e=>{if(e===`slabui:css`){if(a.css===void 0){o.push(`"${t}": ${r} uses the global CSS, but "css" is not set in slabui.json.`);return}return it(a.css,r)}if(!e.startsWith(`@/registry/`))return;let n=e.slice(11);for(let e of c){let t=i.get(e),o=t&&Ye(n,new Set(t.keys()));if(o){let e=t.get(o);return s?it(e,r):$e(e,r,a)}}o.push(`"${t}": could not rewrite the import ${e} in ${r}.`)};return s?Ke(e.content,l):Je(e.content,l)}function it(e,t){let n=a.posix.relative(a.posix.dirname(t),e);return n.startsWith(`.`)?n:`./${n}`}function at(e,t){let n=e.files.map(e=>a.posix.dirname(e.path).split(`/`)),r=[];for(let[e,t]of(n[0]??[]).entries())if(n.every(n=>n[e]===t))r.push(t);else break;let i=r.join(`/`),o=a.posix.normalize(t.split(a.sep).join(`/`));return new Map(e.files.map(e=>[e.path,a.posix.join(o,a.posix.relative(i,e.path))]))}const ot=[[`pnpm-lock.yaml`,`pnpm`],[`bun.lock`,`bun`],[`bun.lockb`,`bun`],[`yarn.lock`,`yarn`],[`package-lock.json`,`npm`]];function st(e){let t=e.indexOf(`@`,+!!e.startsWith(`@`));return t===-1?{name:e}:{name:e.slice(0,t),range:e.slice(t+1)||void 0}}async function ct(e){let t=(await lt(e))?.packageManager?.split(`@`)[0];if(t&&[`pnpm`,`npm`,`yarn`,`bun`].includes(t))return t;let n=a.resolve(e);for(;;){for(let[e,t]of ot)if(await L(a.join(n,e)))return t;let e=a.dirname(n);if(e===n)return`npm`;n=e}}async function lt(e){let t=await l(a.join(e,`package.json`),`utf8`).catch(()=>void 0);return t===void 0?void 0:JSON.parse(t)}async function ut(e,{npm:t,npmDev:n}){let r=await lt(e)??{},i={...r.devDependencies,...r.dependencies},a={install:[],installDev:[],conflicts:[]},o=(e,t)=>{let{name:n,range:r}=st(e),o=i[n];if(o===void 0){t.includes(e)||t.push(e);return}let s=h.minVersion(o);r&&s&&!h.satisfies(s,r)&&a.conflicts.push({spec:e,installed:o})};for(let e of new Set(t))o(e,a.install);for(let e of new Set(n))o(e,a.installDev);return a}function dt(e,t,n){return[e,[e===`npm`?`install`:`add`,...n?[e===`bun`?`--dev`:`-D`]:[],...t]]}async function ft(e,t,n,r){if(n.length===0)return;let[i,a]=dt(t,n,r);await new Promise((t,n)=>{let r=g(i,a,{cwd:e,stdio:`inherit`});r.on(`error`,e=>n(new j(`Could not run ${i}: ${e.message}`))),r.on(`exit`,e=>e===0?t():n(new j(`${i} ${a.join(` `)} exited with code ${e}.`)))})}async function pt(e,t,{confirm:n,skipInstall:r=!1,warnings:i}){let a=await ut(e,t);for(let{spec:e,installed:t}of a.conflicts)n&&await n(`The project has ${t}, but ${e} is required. Update it?`)?a.install.push(e):i.push(`Kept ${st(e).name}@${t}; the item requires ${e}.`);let o=[...a.install,...a.installDev];if(o.length===0)return[];if(r)return i.push(`Packages not installed (--skip-install): ${o.join(` `)}`),[];let s=await ct(e);return await ft(e,s,a.install,!1),await ft(e,s,a.installDev,!0),o}async function mt({refs:e,config:t,createClient:n=e=>I(e)}){let r=/* @__PURE__ */ new Map,i=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map,o=[],s=/* @__PURE__ */ new Set,c=[];function l(e){let t=r.get(e.namespace);return t===void 0&&(t=n(e),r.set(e.namespace,t)),t}async function u(e,n){let r=q(e),d=i.get(r)??[];d.push({range:e.range,by:n}),i.set(r,d);let f=t.items[r];if(f!==void 0){let e=d.filter(({range:e})=>e&&!h.satisfies(f.version,e));if(e.length===0)s.add(r);else for(let{range:t,by:n}of e)c.push(`"${r}" is installed at ${f.version}, but ${n?`"${n}"`:`the request`} requires ${t}. Run "slabui update ${r}".`);return}let p=a.get(r);if(p&&d.every(({range:e})=>ht(p,e)))return;let m=J(t.token),g=l(m),_=(await g.index()).items[e.name];if(_===void 0)throw new j(`Item "${e.name}" does not exist in the "${m.namespace}" registry.`);let v=Object.keys(_.versions),y=d.flatMap(({range:e})=>e?[e]:[]),b=y.length===0?_.latest:h.rsort(v.filter(e=>y.every(t=>h.satisfies(e,t))))[0]??null;if(b===null)throw new j(`No version of "${r}" satisfies every request: ${d.map(({range:e,by:t})=>`${e??`latest`} (${t??`requested`})`).join(`, `)}. Available: ${h.sort(v).join(`, `)}.`);let x=await g.item(e.name,b);a.set(r,{key:r,registry:m,item:x});for(let e of x.dependencies.registry)await u(K(e),r);o.includes(r)||o.push(r)}for(let t of e)await u(K(t));return{items:o.map(e=>a.get(e)),installed:[...s],warnings:c}}function ht(e,t){return t===void 0||h.satisfies(e.item.version,t)}const gt=/^[a-z0-9][a-z0-9-]*$/i;function _t(e){return gt.test(e)}function vt(e,t=!1){if(!_t(e))throw Error(`Invalid theme name: "${e}"`);return`:root[data-slabui-theme="${e}"]${t?`.dark`:``}`}const yt=/^\s*(oklch|oklab|lch|lab|hsla?|rgba?|hwb|color)\(|^\s*#[0-9a-f]{3,8}\s*$/i;async function bt(e,t,{base:n,overwrite:r=!1,shouldRemove:i,theme:a}={}){let o=_.parse(e),s=[],c=[],l=e=>a?e:xt(e),u=l(t),d=n?l(n):void 0;for(let e of[`theme`,`light`,`dark`]){let t=u[e]??{},n=d?.[e]??{},l=/* @__PURE__ */ new Set([...Object.keys(t),...Object.keys(n)]);if(l.size===0)continue;let f=wt(o,e,a);for(let u of l){let l=t[u],d=n[u],p=f?.nodes?.find(e=>e.type===`decl`&&e.prop===`--${u}`),m=p?.value,h=t=>s.push({group:e,name:u,action:t});if(l===void 0){if(m===void 0)continue;i&&await i(e,u)?(p?.remove(),h(`removed`)):(c.push(`--${u} (${e}) was removed in the new version; kept in the CSS.`),h(`kept`))}else if(m===void 0){if(d!==void 0){h(`kept`);continue}f??=Tt(o,e,a),f.raws.semicolon=!0,f.raws.after=`
1
+ import{createHash as e,randomBytes as t}from"node:crypto";import{z as n}from"zod";import{homedir as r,hostname as i}from"node:os";import a from"node:path";import{access as o,lstat as s,mkdir as c,readFile as l,realpath as u,rm as d,writeFile as f}from"node:fs/promises";import{createPathsMatcher as p,parseTsconfig as m}from"get-tsconfig";import"jsonc-parser";import h from"semver";import{spawn as g}from"node:child_process";import _ from"postcss";import{createTwoFilesPatch as v}from"diff";import{merge as y}from"node-diff3";import{createServer as b}from"node:http";import x from"minisearch";const S=`slabui.json`,C={namespace:`@slabui`,url:`https://slabui.com/r`},w=`https://slabui.com`,T=`${w}/schema.json`,E={command:`x-slabui-command`,optOut:`x-slabui-telemetry`},D=[`ui`,`component`,`lib`,`hook`,`util`,`style`,`file`,`theme`,`block`,`example`],O=[`free`,`pro`],k=`SLABUI_TOKEN`,ee=`whoami.json`,te=[`claude`,`cursor`,`vscode`,`zed`,`codex`];function A(e=process.env,t=process.platform,n=r()){let i=t===`win32`?a.join(e.APPDATA??a.join(n,`AppData/Roaming`),`slabui`):a.join(e.XDG_CONFIG_HOME||a.join(n,`.config`),`slabui`);return a.join(i,`credentials.json`)}async function ne(e=A()){try{let{token:t}=JSON.parse(await l(e,`utf8`));return typeof t==`string`&&t?t:void 0}catch{return}}async function re(e,t=A()){await c(a.dirname(t),{recursive:!0}),await f(t,`${JSON.stringify({token:e})}\n`,{mode:384})}async function ie({registryUrl:e,token:t,fetch:n=globalThis.fetch,headers:r={}}){let i=`${e.replace(/\/+$/,``)}/whoami.json`,a=await n(i,{headers:{...r,authorization:`Bearer ${t.trim()}`},signal:AbortSignal.timeout(15e3)});if(a.status===401)return null;if(!a.ok)throw Error(`Error ${a.status} while fetching ${i}.`);let o=await a.json();return{...o,access:o.access??[]}}function ae(){return t(24).toString(`base64url`)}function oe({registryUrl:e,callback:t,state:n,client:r,host:a=i()}){let o=new URL(`/cli/authorize`,new URL(e).origin);return o.searchParams.set(`callback`,t),o.searchParams.set(`state`,n),o.searchParams.set(`client`,r),o.searchParams.set(`host`,a.replace(/\.local$/,``).slice(0,60)),o.toString()}var j=class extends Error{name=`SlabUIError`};const M=n.string().regex(/^[a-z0-9]+(-[a-z0-9]+)*$/,`name must be kebab-case`),se=n.string().regex(/^@[a-z0-9-]+$/,`namespace must look like @name`),N=n.string().regex(/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/,`version must be X.Y.Z`),ce=n.string().regex(/^sha256-[A-Za-z0-9+/]+={0,2}$/,`hash must be sha256-<base64>`),P=n.enum(D),le=n.enum(O),ue=n.string().regex(/^(@[a-z0-9-]+\/)?[a-z0-9]+(-[a-z0-9]+)*(@\S+)?$/,`dependency must look like [@ns/]name[@version]`),de=n.object({registry:n.array(ue).default([]),npm:n.array(n.string().min(1)).default([]),npmDev:n.array(n.string().min(1)).default([])}),fe=[e=>e.kind!==`file`||e.target!==void 0,{message:`a file with kind "file" needs a target`,path:[`target`]}],pe=[e=>e.files.length>0||e.cssVars!==void 0,{message:`the item needs files or cssVars`,path:[`files`]}],me=n.object({path:n.string().min(1),kind:P,target:n.string().min(1).optional()}).refine(...fe),he=n.object({path:n.string().min(1),kind:P,target:n.string().min(1).optional(),content:n.string()}).refine(...fe),F=n.record(n.string(),n.string()),ge=n.object({theme:F.optional(),light:F.optional(),dark:F.optional()}),_e={name:M,kind:P,title:n.string().min(1).optional(),description:n.string().min(1),dependencies:de.prefault({}),cssVars:ge.optional()},ve=n.object({$schema:n.string().optional(),schemaVersion:n.literal(1),..._e,version:N,files:n.array(he).default([])}).refine(...pe);n.object({..._e,categories:n.array(n.string().min(1)).optional(),keywords:n.array(n.string().min(1)).optional(),docs:n.url().optional(),access:le.optional(),inferDependencies:n.boolean().optional(),files:n.array(me).default([])}).refine(...pe);const ye=n.string().regex(/^(?!.*(^|\/)\.\.?(\/|$))[A-Za-z0-9._-]+(\/[A-Za-z0-9._-]+)*\.json$/,`path must be a relative path to a .json file`),be=n.object({hash:ce,path:ye}),xe=n.object({kind:P,title:n.string().min(1).optional(),description:n.string().min(1),categories:n.array(n.string().min(1)).optional(),keywords:n.array(n.string().min(1)).optional(),docs:n.url().optional(),access:le.optional(),dependencies:de.prefault({}),tokens:n.array(n.string().min(1)).optional(),latest:N,versions:n.record(N,be)}).refine(e=>e.latest in e.versions,{message:`latest must exist in versions`,path:[`latest`]}),Se=n.object({$schema:n.string().optional(),schemaVersion:n.literal(2),namespace:se.optional(),homepage:n.url().optional(),mcp:n.url().optional(),license:n.url().optional(),items:n.record(M,xe)});var Ce=`0.1.2`;let we;function Te(e){return e.SLABUI_TELEMETRY_DISABLED===`1`||e.DO_NOT_TRACK===`1`}function Ee(e,t=we){let n={"user-agent":`slabui/${Ce}`};return Te(e)?n[E.optOut]=`off`:t&&(n[E.command]=t),n}const De=/* @__PURE__ */ new Map;function I(e,{fetch:t=globalThis.fetch,env:r=process.env,command:i,savedToken:a=()=>ne()}={}){let o=e.url.replace(/\/+$/,``),s={...Ee(r,i),...ke(e.headers,r)},c;async function l(t){let n=Object.keys(s).some(e=>e.toLowerCase()===`authorization`);if(!e.isDefault||n)return{};let i=(e.token===void 0?void 0:Ae(e.token,`"token" in slabui.json`,r))||r.SLABUI_TOKEN||await a();if(!i)throw new j(`"${t}" is a Pro item. Run "slabui login" or set ${k} to install it.`);return{authorization:`Bearer ${i}`}}async function u(n,{extraHeaders:r={},pro:i}={}){let a;try{a=await t(n,{headers:{...s,...r},signal:AbortSignal.timeout(3e4)})}catch{throw new j(`Could not reach ${n}.`)}if(i!==void 0&&e.isDefault&&a.status===401)throw new j(`The token was refused while fetching the Pro item "${i}". Run "slabui login" again or check ${k}.`);if(i!==void 0&&e.isDefault&&a.status===403)throw new j(`Your token does not give access to the Pro item "${i}". Check your SlabUI Pro subscription.`);if(a.status===401||a.status===403)throw new j(`Access denied to ${n} (${a.status}).`);if(a.status===404)throw new j(`Not found: ${n}.`);if(!a.ok)throw new j(`Error ${a.status} while fetching ${n}.`);return new Uint8Array(await a.arrayBuffer())}function d(e,t,r,i){let a;try{a=JSON.parse(new TextDecoder().decode(t))}catch{throw new j(`${e} is not valid JSON.`)}i?.(e,a);let o=r.safeParse(a);if(!o.success)throw new j(`${e} does not match the expected format:\n${n.prettifyError(o.error)}`);return o.data}async function f(){let t=`${o}/registry.json`,n=d(t,await u(t),Se,Oe);return De.set(e.namespace,n),n}function p(){return c??=f().catch(e=>{throw c=void 0,e}),c}return{index:p,async item(t,n){let r=(await p()).items[t];if(r===void 0)throw new j(`Item "${t}" does not exist in the "${e.namespace}" registry.`);let i=r.versions[n];if(i===void 0)throw new j(`Version ${n} of "${t}" does not exist in the "${e.namespace}" registry.`);let a=`${o}/${i.path}`,s=r.access===`pro`?await u(a,{extraHeaders:await l(t),pro:t}):await u(a);if(je(s)!==i.hash)throw new j(`${a} does not match the hash in the index. The file may have been changed on the server.`);let c=d(a,s,ve);if(c.name!==t||c.version!==n)throw new j(`${a} contains ${c.name}@${c.version}, not ${t}@${n}.`);return c}}}function Oe(e,t,n=`The registry needs to be published again in the current format.`){let r=t&&typeof t==`object`&&`schemaVersion`in t?t.schemaVersion:void 0;if(typeof r==`number`&&r!==2)throw r>2?new j(`${e} uses format ${r}, which this version of slabui does not support. Update it with \`pnpm add -D slabui@latest\`.`):new j(`${e} uses format ${r}, which is no longer supported. ${n}`)}function ke(e,t=process.env){return Object.fromEntries(Object.entries(e).map(([e,n])=>[e,Ae(n,`the "${e}" header`,t)]))}function Ae(e,t,n){return e.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g,(e,r)=>{let i=n[r];if(i===void 0)throw new j(`Environment variable ${r}, used in ${t}, is not set.`);return i})}function je(t){return`sha256-${e(`sha256`).update(t).digest(`base64`)}`}const Me=n.object({version:N,files:n.array(n.string().min(1))}),Ne=n.object({$schema:n.string().optional(),token:n.string().min(1).optional(),css:n.string().min(1).optional(),aliases:n.object({components:n.string().min(1),ui:n.string().min(1),lib:n.string().min(1),hooks:n.string().min(1),utils:n.string().min(1),styles:n.string().min(1)}),items:n.record(M,Me).default({})}),Pe=[`tsconfig.json`,`jsconfig.json`],Fe=[`app/globals.css`,`src/app/globals.css`,`styles/globals.css`,`src/styles/globals.css`,`app/global.css`,`src/index.css`,`src/styles.css`];async function L(e){return o(e).then(()=>!0,()=>!1)}async function R(e){for(let t of Pe){let n=a.join(e,t);if(await L(n))try{return{path:n,config:m(n)}}catch(e){throw new j(`Could not read ${t}: ${e.message}`)}}throw new j(`No tsconfig.json or jsconfig.json in ${e}. slabui uses its "paths" to resolve the aliases.`)}function z(e){let t=Object.keys(e.config.compilerOptions?.paths??{}).filter(e=>e.endsWith(`/*`));return(t.find(e=>e===`@/*`)??t.find(e=>e===`~/*`)??t[0])?.slice(0,-1)}function B(e,t,n){if(n===void 0)return;let r=z(e),i=r!==void 0&&n.startsWith(r)?V(e,n):void 0;return i===void 0?n:a.relative(t,i).split(a.sep).join(`/`)}function V(e,t){return p(e)?.(t)[0]}async function Ie(e){for(let t of Fe)if(await L(a.join(e,t)))return t}async function H(e){let t=a.join(e,S),r=await l(t,`utf8`).catch(()=>void 0);if(r===void 0)throw new j(`${S} not found in ${e}. Run "slabui init" first.`);let i;try{i=JSON.parse(r)}catch{throw new j(`${S} is not valid JSON.`)}let o=Ne.safeParse(i);if(!o.success)throw new j(`${S} has errors:\n${n.prettifyError(o.error)}`);return o.data}async function U(e,t){await f(a.join(e,S),`${JSON.stringify(t,null,2)}\n`)}function Le(e){return e.startsWith(`./`)||e.startsWith(`../`)}async function Re(e){let t=z(await R(e));if(t===void 0)throw new j(`The tsconfig has no alias in "paths" (e.g. "@/*": ["./*"]). slabui uses it to place files and write imports.`);return{aliases:{components:`${t}components`,ui:`${t}components/ui`,lib:`${t}lib`,hooks:`${t}hooks`,utils:`${t}utils`,styles:`${t}styles`},...await ze(e,`${t}styles`)}}async function ze(e,t){let n=await Ie(e);return n===void 0?{css:`${t}/globals.css`,cssDetected:!1}:{css:n,cssDetected:!0}}async function Be({root:e,aliases:t,css:n,force:r=!1}){if(!r&&await L(a.join(e,`slabui.json`)))throw new j(`${S} already exists. Use --force to create it again.`);let i=await R(e);for(let[e,n]of Object.entries(t))if(!Le(n)&&V(i,n)===void 0)throw new j(`Alias "${n}" (${e}) does not match any "paths" in the tsconfig.`);let o={$schema:T,...n&&{css:n},aliases:t,items:{}};return await U(e,o),o}async function W(e,t){let n=e=>new j(`Path "${t}" refused: ${e}.`);if(t===``)throw n(`it is empty`);if(t.includes(`\0`))throw n(`it contains a null character`);if(t.includes(`\\`))throw n(`use / as the separator`);if(a.posix.isAbsolute(t)||a.win32.isAbsolute(t))throw n(`it is absolute`);let r=a.resolve(e),i=a.resolve(r,t);if(i===r)throw n(`it points to the project root`);if(!Ve(r,i))throw n(`it leaves the project root`);let o=await He(i);if(o===i&&(await s(i)).isSymbolicLink())throw n(`the target is a symbolic link`);let c=await u(r),l=await u(o).catch(()=>{throw n(`it goes through a broken symbolic link`)});if(!Ve(c,l))throw n(`it goes through a symbolic link outside the project`);return a.join(l,a.relative(o,i))}function Ve(e,t){let n=a.relative(e,t);return n===``||n!==`..`&&!n.startsWith(`..${a.sep}`)&&!a.isAbsolute(n)}async function He(e){let t=e;for(;;)try{return await s(t),t}catch(e){if(e.code!==`ENOENT`)throw e;t=a.dirname(t)}}const G=[`.ts`,`.tsx`,`.js`,`.jsx`,`.mts`,`.cts`,`.mjs`],Ue=/(\bfrom\s+|\bimport\s+|\bimport\s*\(\s*)(["'])([^"'\n]+)\2/g,We=/(@(?:import|reference)\s+)(["'])([^"'\n]+)\2/g;function Ge(e){return a.posix.extname(e)===`.css`}function Ke(e,t){return e.replace(We,(e,n,r,i)=>{let a=t(i);return a===void 0?e:`${n}${r}${a}${r}`})}function qe(e){return G.includes(a.posix.extname(e))}function Je(e,t){return e.replace(Ue,(e,n,r,i)=>{let a=t(i);return a===void 0?e:`${n}${r}${a}${r}`})}function Ye(e,t){return[e,...G.map(t=>`${e}${t}`),...G.map(t=>`${e}/index${t}`)].find(e=>t.has(e))}function Xe(e){let t=a.posix.extname(e);return G.includes(t)?e.slice(0,-t.length):e}function K(e){let t=e;if(t.startsWith(`@`)){let n=t.indexOf(`/`);if(n===-1)throw new j(`"${e}" has no item name after the namespace.`);if(t.slice(0,n)!==C.namespace)throw new j(`"${e}" is not from the SlabUI registry. Only SlabUI items can be installed.`);t=t.slice(n+1)}let n=t.indexOf(`@`),r=n===-1?t:t.slice(0,n),i=n===-1?void 0:t.slice(n+1);if(!M.safeParse(r).success)throw new j(`"${r}" is not a valid item name (kebab-case).`);if(i!==void 0&&(!i||h.validRange(i)===null))throw new j(`"${i}" is not a valid version or range.`);return{name:r,...i!==void 0&&{range:i}}}function q(e){return e.name}function J(e,t=process.env){return{namespace:C.namespace,url:t.SLABUI_REGISTRY_URL||C.url,headers:{},isDefault:!0,...e!==void 0&&{token:e}}}function Y(e,t){if(e.range===void 0)return t.latest;let n=Object.keys(t.versions),r=h.maxSatisfying(n,e.range);if(r===null)throw new j(`No version of "${q(e)}" satisfies "${e.range}". Available: ${h.sort(n).join(`, `)}.`);return r}const Ze={ui:`ui`,component:`components`,lib:`lib`,hook:`hooks`,util:`utils`,style:`styles`};function Qe(e,t){if(e.target!==void 0){let n=e.target;return et((n.startsWith(z(t.tsconfig)??`\0`)?V(t.tsconfig,n):void 0)??a.resolve(t.root,n),t)}if(!(e.kind in Ze))throw new j(`Files with kind "${e.kind}" cannot be installed by add yet (${e.path}).`);let n=t.aliases[Ze[e.kind]],r=e.kind===`style`&&e.path.split(`/`).slice(1).join(`/`)||a.posix.basename(e.path),i=Le(n)?a.resolve(t.root,n,r):V(t.tsconfig,`${n}/${r}`);if(i===void 0)throw new j(`Alias "${n}" does not match any "paths" in the tsconfig.`);return et(i,t)}function $e(e,t,n){let r=z(n.tsconfig),i=r===void 0?void 0:V(n.tsconfig,`${r}__slabui__`)?.slice(0,-10),o=a.resolve(n.root,e);if(r!==void 0&&i!==void 0&&o.startsWith(i))return`${r}${Xe(a.relative(i,o).split(a.sep).join(`/`))}`;let s=a.posix.relative(a.posix.dirname(t),e);return s.startsWith(`.`)||(s=`./${s}`),Xe(s)}function et(e,t){return a.relative(t.root,e).split(a.sep).join(`/`)}function X(e){return e.kind===`block`||e.kind===`example`}function Z(e,t,n){return X(e)?at(e,n):new Map(e.files.map(e=>[e.path,Qe(e,t)]))}function tt(e,t){return[e,...t.dependencies.registry.map(e=>q(K(e)))]}async function nt(e,{config:t,context:n,destinations:r,clientFor:i}){for(let a of e)for(let e of a.dependencies.registry){let a=K(e),o=q(a),s=t.items[o];if(r.has(o)||s===void 0)continue;let c=i(J(t.token));r.set(o,Z(await c.item(a.name,s.version),n))}}function rt(e,{key:t,item:n,destination:r,destinations:i,context:a,warnings:o}){let s=Ge(e.path);if(!qe(e.path)&&!s)return e.content;let c=tt(t,n),l=e=>{if(e===`slabui:css`){if(a.css===void 0){o.push(`"${t}": ${r} uses the global CSS, but "css" is not set in slabui.json.`);return}return it(a.css,r)}if(!e.startsWith(`@/registry/`))return;let n=e.slice(11);for(let e of c){let t=i.get(e),o=t&&Ye(n,new Set(t.keys()));if(o){let e=t.get(o);return s?it(e,r):$e(e,r,a)}}o.push(`"${t}": could not rewrite the import ${e} in ${r}.`)};return s?Ke(e.content,l):Je(e.content,l)}function it(e,t){let n=a.posix.relative(a.posix.dirname(t),e);return n.startsWith(`.`)?n:`./${n}`}function at(e,t){let n=e.files.map(e=>a.posix.dirname(e.path).split(`/`)),r=[];for(let[e,t]of(n[0]??[]).entries())if(n.every(n=>n[e]===t))r.push(t);else break;let i=r.join(`/`),o=a.posix.normalize(t.split(a.sep).join(`/`));return new Map(e.files.map(e=>[e.path,a.posix.join(o,a.posix.relative(i,e.path))]))}const ot=[[`pnpm-lock.yaml`,`pnpm`],[`bun.lock`,`bun`],[`bun.lockb`,`bun`],[`yarn.lock`,`yarn`],[`package-lock.json`,`npm`]];function st(e){let t=e.indexOf(`@`,+!!e.startsWith(`@`));return t===-1?{name:e}:{name:e.slice(0,t),range:e.slice(t+1)||void 0}}async function ct(e){let t=(await lt(e))?.packageManager?.split(`@`)[0];if(t&&[`pnpm`,`npm`,`yarn`,`bun`].includes(t))return t;let n=a.resolve(e);for(;;){for(let[e,t]of ot)if(await L(a.join(n,e)))return t;let e=a.dirname(n);if(e===n)return`npm`;n=e}}async function lt(e){let t=await l(a.join(e,`package.json`),`utf8`).catch(()=>void 0);return t===void 0?void 0:JSON.parse(t)}async function ut(e,{npm:t,npmDev:n}){let r=await lt(e)??{},i={...r.devDependencies,...r.dependencies},a={install:[],installDev:[],conflicts:[]},o=(e,t)=>{let{name:n,range:r}=st(e),o=i[n];if(o===void 0){t.includes(e)||t.push(e);return}let s=h.minVersion(o);r&&s&&!h.satisfies(s,r)&&a.conflicts.push({spec:e,installed:o})};for(let e of new Set(t))o(e,a.install);for(let e of new Set(n))o(e,a.installDev);return a}function dt(e,t,n){return[e,[e===`npm`?`install`:`add`,...n?[e===`bun`?`--dev`:`-D`]:[],...t]]}async function ft(e,t,n,r){if(n.length===0)return;let[i,a]=dt(t,n,r);await new Promise((t,n)=>{let r=g(i,a,{cwd:e,stdio:`inherit`});r.on(`error`,e=>n(new j(`Could not run ${i}: ${e.message}`))),r.on(`exit`,e=>e===0?t():n(new j(`${i} ${a.join(` `)} exited with code ${e}.`)))})}async function pt(e,t,{confirm:n,skipInstall:r=!1,warnings:i}){let a=await ut(e,t);for(let{spec:e,installed:t}of a.conflicts)n&&await n(`The project has ${t}, but ${e} is required. Update it?`)?a.install.push(e):i.push(`Kept ${st(e).name}@${t}; the item requires ${e}.`);let o=[...a.install,...a.installDev];if(o.length===0)return[];if(r)return i.push(`Packages not installed (--skip-install): ${o.join(` `)}`),[];let s=await ct(e);return await ft(e,s,a.install,!1),await ft(e,s,a.installDev,!0),o}async function mt({refs:e,config:t,createClient:n=e=>I(e)}){let r=/* @__PURE__ */ new Map,i=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map,o=[],s=/* @__PURE__ */ new Set,c=[];function l(e){let t=r.get(e.namespace);return t===void 0&&(t=n(e),r.set(e.namespace,t)),t}async function u(e,n){let r=q(e),d=i.get(r)??[];d.push({range:e.range,by:n}),i.set(r,d);let f=t.items[r];if(f!==void 0){let e=d.filter(({range:e})=>e&&!h.satisfies(f.version,e));if(e.length===0)s.add(r);else for(let{range:t,by:n}of e)c.push(`"${r}" is installed at ${f.version}, but ${n?`"${n}"`:`the request`} requires ${t}. Run "slabui update ${r}".`);return}let p=a.get(r);if(p&&d.every(({range:e})=>ht(p,e)))return;let m=J(t.token),g=l(m),_=(await g.index()).items[e.name];if(_===void 0)throw new j(`Item "${e.name}" does not exist in the "${m.namespace}" registry.`);let v=Object.keys(_.versions),y=d.flatMap(({range:e})=>e?[e]:[]),b=y.length===0?_.latest:h.rsort(v.filter(e=>y.every(t=>h.satisfies(e,t))))[0]??null;if(b===null)throw new j(`No version of "${r}" satisfies every request: ${d.map(({range:e,by:t})=>`${e??`latest`} (${t??`requested`})`).join(`, `)}. Available: ${h.sort(v).join(`, `)}.`);let x=await g.item(e.name,b);a.set(r,{key:r,registry:m,item:x});for(let e of x.dependencies.registry)await u(K(e),r);o.includes(r)||o.push(r)}for(let t of e)await u(K(t));return{items:o.map(e=>a.get(e)),installed:[...s],warnings:c}}function ht(e,t){return t===void 0||h.satisfies(e.item.version,t)}const gt=/^[a-z0-9][a-z0-9-]*$/i;function _t(e){return gt.test(e)}function vt(e,t=!1){if(!_t(e))throw Error(`Invalid theme name: "${e}"`);return`:root[data-slabui-theme="${e}"]${t?`.dark`:``}`}const yt=/^\s*(oklch|oklab|lch|lab|hsla?|rgba?|hwb|color)\(|^\s*#[0-9a-f]{3,8}\s*$/i;async function bt(e,t,{base:n,overwrite:r=!1,shouldRemove:i,theme:a}={}){let o=_.parse(e),s=[],c=[],l=e=>a?e:xt(e),u=l(t),d=n?l(n):void 0;for(let e of[`theme`,`light`,`dark`]){let t=u[e]??{},n=d?.[e]??{},l=/* @__PURE__ */ new Set([...Object.keys(t),...Object.keys(n)]);if(l.size===0)continue;let f=wt(o,e,a);for(let u of l){let l=t[u],d=n[u],p=f?.nodes?.find(e=>e.type===`decl`&&e.prop===`--${u}`),m=p?.value,h=t=>s.push({group:e,name:u,action:t});if(l===void 0){if(m===void 0)continue;i&&await i(e,u)?(p?.remove(),h(`removed`)):(c.push(`--${u} (${e}) was removed in the new version; kept in the CSS.`),h(`kept`))}else if(m===void 0){if(d!==void 0){h(`kept`);continue}f??=Tt(o,e,a),f.raws.semicolon=!0,f.raws.after=`
2
2
  `,f.append(_.decl({prop:`--${u}`,value:l,raws:{before:`
3
3
  `}})),h(`added`)}else Q(m,l)?h(`unchanged`):r||d!==void 0&&Q(m,d)?(p.value=l,h(`updated`)):(c.push(`--${u} (${e}) has a custom value in the CSS (${m}); kept instead of ${l}.`),h(`kept`))}}return!a&&Object.keys(u.dark??{}).length>0&&Et(o),{css:o.toString(),changes:s,warnings:c}}function xt(e){let t={...e.theme};for(let n of[e.light,e.dark])for(let[e,r]of Object.entries(n??{}))yt.test(r)&&t[`color-${e}`]===void 0&&(t[`color-${e}`]=`var(--${e})`);return{...e,...Object.keys(t).length>0&&{theme:t}}}function St(e,t){return t?vt(t,e===`dark`):e===`light`?`:root`:`.dark`}const Ct=e=>e.replace(/["'\s]/g,``);function wt(e,t,n){return e.nodes.find(e=>t===`theme`?e.type===`atrule`&&e.name===`theme`&&e.params.trim()===`inline`:e.type===`rule`&&Ct(e.selector)===Ct(St(t,n)))}function Tt(e,t,n){let r=t===`theme`?_.atRule({name:`theme`,params:`inline`}):_.rule({selector:St(t,n)});return r.raws={before:e.nodes.length>0?`
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slabui/cli",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "The SlabUI CLI: add, update and compare items from versioned component registries",
5
5
  "keywords": [
6
6
  "slabui",