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.
* @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
*/
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
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const choices = {
type: ["Move", "Ability", "Item", "Exit"],
};
const typeChoices = ["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([
{
type: "list",
name: "selectedOption",
message: "What test type would you like to create:",
choices: choices.type,
message: "What type of test would you like to create:",
choices: typeChoices,
},
]);
switch (typeAnswer.selectedOption) {
case "Exit":
console.log("Exiting...");
return process.exit();
default:
if (!choices.type.includes(typeAnswer.selectedOption)) {
console.error('Please provide a valid type ("move", "ability", or "item")!');
} // else proceed
if (typeAnswer.selectedOption === "EXIT") {
console.log("Exiting...");
return process.exit();
} else if (!typeChoices.includes(typeAnswer.selectedOption)) {
console.error('Please provide a valid type ("move", "ability", or "item")!');
return await promptTestType();
}
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([
{
type: "input",
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) {
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();
// Convert fileName from kebab-case or camelCase to snake_case
const fileName = fileNameAnswer.userInput
@ -63,18 +84,22 @@ async function runInteractive() {
// Determine the directory based on the type
let dir;
let description;
if (type === "move") {
dir = path.join(__dirname, "src", "test", "moves");
description = `Moves - ${formattedName}`;
} else if (type === "ability") {
dir = path.join(__dirname, "src", "test", "abilities");
description = `Abilities - ${formattedName}`;
} else if (type === "item") {
dir = path.join(__dirname, "src", "test", "items");
description = `Items - ${formattedName}`;
} else {
console.error('Invalid type. Please use "move", "ability", or "item".');
process.exit(1);
switch (type) {
case "move":
dir = path.join(__dirname, "src", "test", "moves");
description = `Moves - ${formattedName}`;
break;
case "ability":
dir = path.join(__dirname, "src", "test", "abilities");
description = `Abilities - ${formattedName}`;
break;
case "item":
dir = path.join(__dirname, "src", "test", "items");
description = `Items - ${formattedName}`;
break;
default:
console.error('Invalid type. Please use "move", "ability", or "item".');
process.exit(1);
}
// Define the content template