factuges_2025/scripts/create-package.ts
2025-04-27 22:47:47 +02:00

66 lines
2.1 KiB
TypeScript

import fs from "fs";
import inquirer from "inquirer";
import path from "path";
function capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
async function main() {
let rawName = process.argv[2];
if (!rawName) {
const answers = await inquirer.prompt([
{
type: "input",
name: "packageName",
message: "Nombre del nuevo package:",
validate: (input) => (input ? true : "El nombre no puede estar vacío"),
},
]);
rawName = answers.packageName;
}
const name = rawName.toLowerCase();
const capitalized = capitalize(name);
const packagePath = path.resolve(__dirname, "../packages", name);
const clientPath = path.join(packagePath, "client");
const serverPath = path.join(packagePath, "server");
const templatePath = path.resolve(__dirname, "./templates");
// Crear carpetas
fs.mkdirSync(clientPath, { recursive: true });
fs.mkdirSync(serverPath, { recursive: true });
// Función de reemplazo de contenido
const renderTemplate = (content: string) =>
content
.replace(/__PACKAGE_NAME__/g, name)
.replace(/__PACKAGE_NAME_CAPITALIZED__/g, capitalized);
// Copiar plantillas desde carpeta 'templates/client'
const copyFromTemplate = (srcDir: string, destDir: string) => {
const files = fs.readdirSync(srcDir);
for (const file of files) {
const filePath = path.join(srcDir, file);
const content = fs.readFileSync(filePath, "utf-8");
const outputName = file.replace(/__PACKAGE_NAME__/g, capitalized);
const outputPath = path.join(destDir, outputName);
fs.writeFileSync(outputPath, renderTemplate(content));
}
};
copyFromTemplate(path.join(templatePath, "client"), clientPath);
copyFromTemplate(path.join(templatePath, "server"), serverPath);
// package.json
const pkgJsonTemplate = fs.readFileSync(path.join(templatePath, "package.json"), "utf-8");
fs.writeFileSync(path.join(packagePath, "package.json"), renderTemplate(pkgJsonTemplate));
console.log(`✅ Package '${name}' creado correctamente en packages/${name}`);
}
main();