restructure create-test-boilerplate.js code

This commit is contained in:
flx-sta 2024-09-17 18:52:24 -07:00
parent 91235ea1f5
commit 71616fc39d
1 changed files with 58 additions and 33 deletions

View File

@ -1,8 +1,3 @@
import fs from "fs";
import inquirer from "inquirer";
import path from "path";
import { fileURLToPath } from "url";
/** /**
* This script creates a test boilerplate file for a move or ability. * This script creates a test boilerplate file for a move or ability.
* @param {string} type - The type of test to create. Either "move", "ability", * @param {string} type - The type of test to create. Either "move", "ability",
@ -11,46 +6,72 @@ import { fileURLToPath } from "url";
* @example npm run create-test move tackle * @example npm run create-test move tackle
*/ */
import fs from "fs";
import inquirer from "inquirer";
import path from "path";
import { fileURLToPath } from "url";
// Get the directory name of the current module file // Get the directory name of the current module file
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
const choices = { const typeChoices = ["Move", "Ability", "Item", "EXIT"];
type: ["Move", "Ability", "Item", "Exit"],
};
async function runInteractive() { /**
* Prompts the user to select a type via list.
* @returns {Promise<{selectedOption: string}>} the selected type
*/
async function promptTestType() {
const typeAnswer = await inquirer.prompt([ const typeAnswer = await inquirer.prompt([
{ {
type: "list", type: "list",
name: "selectedOption", name: "selectedOption",
message: "What test type would you like to create:", message: "What type of test would you like to create:",
choices: choices.type, choices: typeChoices,
}, },
]); ]);
switch (typeAnswer.selectedOption) { if (typeAnswer.selectedOption === "EXIT") {
case "Exit": console.log("Exiting...");
console.log("Exiting..."); return process.exit();
return process.exit(); } else if (!typeChoices.includes(typeAnswer.selectedOption)) {
default: console.error('Please provide a valid type ("move", "ability", or "item")!');
if (!choices.type.includes(typeAnswer.selectedOption)) { return await promptTestType();
console.error('Please provide a valid type ("move", "ability", or "item")!');
} // else proceed
} }
return typeAnswer;
}
/**
* Prompts the user to provide a file name.
* @param {string} selectedType
* @returns {Promise<{userInput: string}>} the selected file name
*/
async function promptFileName(selectedType) {
const fileNameAnswer = await inquirer.prompt([ const fileNameAnswer = await inquirer.prompt([
{ {
type: "input", type: "input",
name: "userInput", name: "userInput",
message: `Please provide a file name for the ${typeAnswer.selectedOption}:`, message: `Please provide a file name for the ${selectedType} test:`,
}, },
]); ]);
if (!fileNameAnswer.userInput || fileNameAnswer.userInput.trim().length === 0) { if (!fileNameAnswer.userInput || fileNameAnswer.userInput.trim().length === 0) {
console.error("Please provide a valid file name!"); console.error("Please provide a valid file name!");
return process.exit(); return await promptFileName(selectedType);
} }
return fileNameAnswer;
}
/**
* Runs the interactive create-test "CLI"
* @returns {Promise<void>}
*/
async function runInteractive() {
const typeAnswer = await promptTestType();
const fileNameAnswer = await promptFileName(typeAnswer.selectedOption);
const type = typeAnswer.selectedOption.toLowerCase(); const type = typeAnswer.selectedOption.toLowerCase();
// Convert fileName from kebab-case or camelCase to snake_case // Convert fileName from kebab-case or camelCase to snake_case
const fileName = fileNameAnswer.userInput const fileName = fileNameAnswer.userInput
@ -63,18 +84,22 @@ async function runInteractive() {
// Determine the directory based on the type // Determine the directory based on the type
let dir; let dir;
let description; let description;
if (type === "move") { switch (type) {
dir = path.join(__dirname, "src", "test", "moves"); case "move":
description = `Moves - ${formattedName}`; dir = path.join(__dirname, "src", "test", "moves");
} else if (type === "ability") { description = `Moves - ${formattedName}`;
dir = path.join(__dirname, "src", "test", "abilities"); break;
description = `Abilities - ${formattedName}`; case "ability":
} else if (type === "item") { dir = path.join(__dirname, "src", "test", "abilities");
dir = path.join(__dirname, "src", "test", "items"); description = `Abilities - ${formattedName}`;
description = `Items - ${formattedName}`; break;
} else { case "item":
console.error('Invalid type. Please use "move", "ability", or "item".'); dir = path.join(__dirname, "src", "test", "items");
process.exit(1); description = `Items - ${formattedName}`;
break;
default:
console.error('Invalid type. Please use "move", "ability", or "item".');
process.exit(1);
} }
// Define the content template // Define the content template