Skip to content

Commit

Permalink
feat: ask for validator when a component needs it
Browse files Browse the repository at this point in the history
  • Loading branch information
BayBreezy committed Dec 21, 2024
1 parent 52507cf commit 57981da
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { compareUIConfig } from "../utils/compareUIConfig";
import { addModuleToConfig, getNuxtConfig, getUIConfig, updateConfig } from "../utils/config";
import { fileExists } from "../utils/fileExists";
import { installPackages } from "../utils/installPackages";
import { installValidator } from "../utils/installValidator";
import { printFancyBoxMessage } from "../utils/printFancyBoxMessage";
import { promptUserForComponents } from "../utils/promptForComponents";
import { writeFile } from "../utils/writeFile";
Expand Down Expand Up @@ -281,6 +282,21 @@ export const add = new Command()
}
}

// check if any of the components has the `askValidator` property set to true
let shouldAskValidator = false;
// Check if any component has askValidator set to true
for (const component of found) {
if (component.askValidator) {
shouldAskValidator = true;
break;
}
}

if (shouldAskValidator) {
// Ask the user for their choice of validator
await installValidator(uiConfig.packageManager);
}

printFancyBoxMessage(
"All Done!",
{ title: "Components Added" },
Expand Down
35 changes: 35 additions & 0 deletions src/utils/installValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import prompts from "prompts";

import { installPackages } from "./installPackages";

export const installValidator = async (packageManager: string) => {
// Depending on the selected validator, install the corresponding packages
const validatorPackages = {
yup: ["yup", "@vee-validate/yup"],
zod: ["zod", "@vee-validate/zod"],
joi: ["joi", "@vee-validate/joi"],
valibot: ["valibot", "@vee-validate/valibot"],
};

const { validator }: { validator: keyof typeof validatorPackages } = await prompts({
type: "select",
name: "validator",
message: "Choose the validator package to use with Vee Validate: ",
choices: [
{ title: "Yup", value: "yup" },
{ title: "Zod", value: "zod" },
{ title: "Joi", value: "joi" },
{ title: "Valibot", value: "valibot" },
],
});
if (!validator) {
console.log("No validator package selected");
return;
}
console.log(`Selected ${validator} as the validator package`);

if (validatorPackages[validator]) {
await installPackages(packageManager, validatorPackages[validator]);
}
return validator;
};

0 comments on commit 57981da

Please sign in to comment.