Skip to content

Commit

Permalink
feat: add support for update utils command (#578)
Browse files Browse the repository at this point in the history
Co-authored-by: AdrianGonz97 <[email protected]>
  • Loading branch information
Skylli202 and AdrianGonz97 authored Jan 2, 2024
1 parent db84fdb commit dcceae8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-laws-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn-svelte": minor
---

feat: Added a `utils` option for `update`
40 changes: 34 additions & 6 deletions packages/cli/src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getRegistryIndex,
resolveTree
} from "../utils/registry";
import { UTILS } from "../utils/templates";
import { transformImport } from "../utils/transformer";

const updateOptionsSchema = z.object({
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit dcceae8

Please sign in to comment.