pokerogue/scripts/create-test/create-test.js
Bertie690 3478e09923
[Dev] Break up test:create script; add help message, file name CLI argument support (#6793)
* [Dev] Broke up `test:create` script, added CLI args file name suppoert

* Moved `HELP_FLAGS` constant; fixed help msg indentation

* ran biome

* Fix floting promise err

* Added REUSE info

* Typo fix

* comment out reward boilerplate

* Removed redundant comments

---------

Co-authored-by: fabske0 <192151969+fabske0@users.noreply.github.com>
2025-11-28 21:26:49 +00:00

87 lines
2.3 KiB
JavaScript

/*
* SPDX-FileCopyrightText: 2024-2025 Pagefault Games
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
/*
* This script creates a test boilerplate file in the appropriate
* directory based on the type selected.
* Usage: `pnpm test:create`
*/
import fs from "node:fs";
import { join } from "node:path";
import chalk from "chalk";
import { writeFileSafe } from "../helpers/file.js";
import { toKebabCase, toTitleCase } from "../helpers/strings.js";
import { getFileName, getTestType } from "./cli.js";
import { getBoilerplatePath, getTestFileFullPath } from "./dirs.js";
import { HELP_FLAGS, showHelpText } from "./help-message.js";
/**
* @import {testType} from "./constants.js"
*/
//#region Constants
const version = "2.1.0";
const __dirname = import.meta.dirname;
const projectRoot = join(__dirname, "..", "..");
//#endregion
//#region Main
/**
* Run the interactive `test:create` CLI.
* @returns {Promise<void>}
*/
async function runInteractive() {
console.group(chalk.grey(`🧪 Create Test - v${version}\n`));
const args = process.argv.slice(2);
if (HELP_FLAGS.some(h => args.includes(h))) {
return showHelpText();
}
const testType = await getTestType(args[0]);
if (process.exitCode || !testType) {
return;
}
const fileNameAnswer = await getFileName(testType, args[1]);
if (process.exitCode || !fileNameAnswer) {
return;
}
try {
doCreateFile(testType, fileNameAnswer);
} catch (err) {
console.error(chalk.red("✗ Error: ", err));
}
console.groupEnd();
}
/**
* Helper function to create the test file.
* @param {testType} testType - The type of test to create
* @param {string} fileNameAnswer - The name of the file to create
* @returns {void}
*/
function doCreateFile(testType, fileNameAnswer) {
// Convert file name to kebab-case, formatting the description in Title Case
const fileName = toKebabCase(fileNameAnswer);
const formattedName = toTitleCase(fileNameAnswer);
const description = `${testType} - ${formattedName}`;
const content = fs.readFileSync(getBoilerplatePath(testType), "utf8").replace("{{description}}", description);
const filePath = getTestFileFullPath(testType, fileName);
writeFileSafe(filePath, content, "utf8");
console.log(chalk.green.bold(`✔ File created at: ${filePath.replace(`${projectRoot}/`, "")}\n`));
}
//#endregion
await runInteractive();