diff --git a/.changeset/cyan-laws-raise.md b/.changeset/cyan-laws-raise.md new file mode 100644 index 000000000..79a2c5716 --- /dev/null +++ b/.changeset/cyan-laws-raise.md @@ -0,0 +1,5 @@ +--- +"shadcn-svelte": minor +--- + +feat: Added a `utils` option for `update` diff --git a/packages/cli/src/commands/update.ts b/packages/cli/src/commands/update.ts index 97331901a..17f80e149 100644 --- a/packages/cli/src/commands/update.ts +++ b/packages/cli/src/commands/update.ts @@ -17,6 +17,7 @@ import { getRegistryIndex, resolveTree } from "../utils/registry"; +import { UTILS } from "../utils/templates"; import { transformImport } from "../utils/transformer"; const updateOptionsSchema = z.object({ @@ -93,10 +94,18 @@ export const update = new Command() const component = registryIndex.find( (comp) => comp.name === file.name ); - // is a valid shadcn component - component && existingComponents.push(component); + if (component) { + // is a valid shadcn component + existingComponents.push(component); + } } } + // add `utils` option to the end + existingComponents.push({ + name: "utils", + type: "components:ui", + files: [] + }); let selectedComponents: typeof registryIndex = options.all ? existingComponents @@ -123,21 +132,40 @@ export const update = new Command() ); } + const spinner = ora( + `Updating ${selectedComponents.length} component(s) and dependencies...` + ).start(); + if (selectedComponents.length === 0) { - logger.info("No components selected. Nothing to update."); + spinner.info("No components selected. Nothing to update."); process.exitCode = 0; return; } + // `update utils` - update the utils.ts file + if (selectedComponents.find((item) => item.name === "utils")) { + const utilsPath = config.resolvedPaths.utils + ".ts"; + + if (!existsSync(utilsPath)) { + spinner.fail( + `utils at ${logger.highlight( + utilsPath + )} does not exist.` + ); + process.exitCode = 1; + return; + } + + // utils.ts is not in the registry, it is a template, so we'll just overwrite it + await fs.writeFile(utilsPath, UTILS); + } + const tree = await resolveTree( registryIndex, selectedComponents.map((com) => com.name) ); const payload = await fetchTree(config.style, tree); - const spinner = ora( - `Updating ${selectedComponents.length} component(s) and dependencies...` - ).start(); for (const item of payload) { spinner.text = `Updating ${item.name}...`; const targetDir = await getItemTargetPath(config, item);