make create-test interactive

To be more intuitive
This commit is contained in:
flx-sta 2024-09-17 15:57:09 -07:00
parent e278f37ede
commit 91235ea1f5

View File

@ -1,6 +1,7 @@
import fs from 'fs'; import fs from "fs";
import path from 'path'; import inquirer from "inquirer";
import { fileURLToPath } from 'url'; 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.
@ -14,59 +15,70 @@ import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
// Get the arguments from the command line const choices = {
const args = process.argv.slice(2); type: ["Move", "Ability", "Item", "Exit"],
const type = args[0]; // "move" or "ability" };
let fileName = args[1]; // The file name
if (!type || !fileName) { async function runInteractive() {
console.error('Please provide a type ("move", "ability", or "item") and a file name.'); const typeAnswer = await inquirer.prompt([
process.exit(1); {
} type: "list",
name: "selectedOption",
message: "What test type would you like to create:",
choices: choices.type,
},
]);
// Convert fileName from kebab-case or camelCase to snake_case switch (typeAnswer.selectedOption) {
fileName = fileName case "Exit":
.replace(/-+/g, '_') // Convert kebab-case (dashes) to underscores console.log("Exiting...");
.replace(/([a-z])([A-Z])/g, '$1_$2') // Convert camelCase to snake_case return process.exit();
.toLowerCase(); // Ensure all lowercase default:
if (!choices.type.includes(typeAnswer.selectedOption)) {
console.error('Please provide a valid type ("move", "ability", or "item")!');
} // else proceed
}
const fileNameAnswer = await inquirer.prompt([
{
type: "input",
name: "userInput",
message: `Please provide a file name for the ${typeAnswer.selectedOption}:`,
},
]);
// Format the description for the test case if (!fileNameAnswer.userInput || fileNameAnswer.userInput.trim().length === 0) {
const formattedName = fileName console.error("Please provide a valid file name!");
.replace(/_/g, ' ') return process.exit();
.replace(/\b\w/g, char => char.toUpperCase()); }
// Determine the directory based on the type const type = typeAnswer.selectedOption.toLowerCase();
let dir; // Convert fileName from kebab-case or camelCase to snake_case
let description; const fileName = fileNameAnswer.userInput
if (type === 'move') { .replace(/-+/g, "_") // Convert kebab-case (dashes) to underscores
dir = path.join(__dirname, 'src', 'test', 'moves'); .replace(/([a-z])([A-Z])/g, "$1_$2") // Convert camelCase to snake_case
description = `Moves - ${formattedName}`; .toLowerCase(); // Ensure all lowercase
} else if (type === 'ability') { // Format the description for the test case
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);
}
// Ensure the directory exists const formattedName = fileName.replace(/_/g, " ").replace(/\b\w/g, (char) => char.toUpperCase());
if (!fs.existsSync(dir)) { // Determine the directory based on the type
fs.mkdirSync(dir, { recursive: true }); 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);
}
// Create the file with the given name // Define the content template
const filePath = path.join(dir, `${fileName}.test.ts`); const content = `import { Abilities } from "#enums/abilities";
if (fs.existsSync(filePath)) {
console.error(`File "${fileName}.test.ts" already exists.`);
process.exit(1);
}
// Define the content template
const content = `import { Abilities } from "#enums/abilities";
import { Moves } from "#enums/moves"; import { Moves } from "#enums/moves";
import { Species } from "#enums/species"; import { Species } from "#enums/species";
import GameManager from "#test/utils/gameManager"; import GameManager from "#test/utils/gameManager";
@ -104,7 +116,23 @@ describe("${description}", () => {
}); });
`; `;
// Write the template content to the file // Ensure the directory exists
fs.writeFileSync(filePath, content, 'utf8'); if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
console.log(`File created at: ${filePath}`); // Create the file with the given name
const filePath = path.join(dir, `${fileName}.test.ts`);
if (fs.existsSync(filePath)) {
console.error(`File "${fileName}.test.ts" already exists.`);
process.exit(1);
}
// Write the template content to the file
fs.writeFileSync(filePath, content, "utf8");
console.log(`File created at: ${filePath}`);
}
runInteractive();