109 lines
2.8 KiB
TypeScript
109 lines
2.8 KiB
TypeScript
|
|
import fs from "fs";
|
||
|
|
import inquirer from "inquirer";
|
||
|
|
import path from "path";
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
let rawName = process.argv[2];
|
||
|
|
|
||
|
|
// Preguntar si no se pasó argumento
|
||
|
|
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 = name.charAt(0).toUpperCase() + name.slice(1);
|
||
|
|
const basePath = path.resolve(__dirname, "../packages", name);
|
||
|
|
const clientPath = path.join(basePath, "client");
|
||
|
|
const serverPath = path.join(basePath, "server");
|
||
|
|
|
||
|
|
// Plantillas
|
||
|
|
const manifestTemplate = () => `
|
||
|
|
import { IPackageClient } from '@libs/package';
|
||
|
|
import ${capitalized}Page from './${capitalized}Page';
|
||
|
|
|
||
|
|
export const ${capitalized}Package: IPackageClient = {
|
||
|
|
metadata: {
|
||
|
|
name: '${name}',
|
||
|
|
route: '/${name}',
|
||
|
|
version: '1.0.0',
|
||
|
|
description: '${capitalized} package'
|
||
|
|
},
|
||
|
|
component: ${capitalized}Page
|
||
|
|
};
|
||
|
|
`;
|
||
|
|
|
||
|
|
const pageTemplate = () => `
|
||
|
|
export default function ${capitalized}Page() {
|
||
|
|
return <div>${capitalized} Package Page</div>;
|
||
|
|
}
|
||
|
|
`;
|
||
|
|
|
||
|
|
const serverIndexTemplate = () => `
|
||
|
|
import { IPackageServer } from '@libs/package';
|
||
|
|
import { ${name}Controller } from './controller';
|
||
|
|
|
||
|
|
export const ${capitalized}Package: IPackageServer = {
|
||
|
|
metadata: {
|
||
|
|
name: '${name}',
|
||
|
|
version: '1.0.0',
|
||
|
|
dependencies: []
|
||
|
|
},
|
||
|
|
init(app) {
|
||
|
|
app.get('/${name}', ${name}Controller);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
`;
|
||
|
|
|
||
|
|
const controllerTemplate = () => `
|
||
|
|
export function ${name}Controller(req, res) {
|
||
|
|
res.send('${capitalized} package response');
|
||
|
|
}
|
||
|
|
`;
|
||
|
|
|
||
|
|
const tsconfigReact = () =>
|
||
|
|
JSON.stringify(
|
||
|
|
{
|
||
|
|
extends: "@repo/tsconfig/react",
|
||
|
|
include: ["."],
|
||
|
|
},
|
||
|
|
null,
|
||
|
|
2
|
||
|
|
);
|
||
|
|
|
||
|
|
const tsconfigNode = () =>
|
||
|
|
JSON.stringify(
|
||
|
|
{
|
||
|
|
extends: "@repo/tsconfig/node",
|
||
|
|
include: ["."],
|
||
|
|
},
|
||
|
|
null,
|
||
|
|
2
|
||
|
|
);
|
||
|
|
|
||
|
|
// --- Crear carpetas ---
|
||
|
|
fs.mkdirSync(clientPath, { recursive: true });
|
||
|
|
fs.mkdirSync(serverPath, { recursive: true });
|
||
|
|
|
||
|
|
// --- Crear archivos cliente ---
|
||
|
|
fs.writeFileSync(path.join(clientPath, `${capitalized}Page.tsx`), pageTemplate());
|
||
|
|
fs.writeFileSync(path.join(clientPath, `manifest.ts`), manifestTemplate());
|
||
|
|
fs.writeFileSync(path.join(clientPath, `tsconfig.json`), tsconfigReact());
|
||
|
|
|
||
|
|
// --- Crear archivos servidor ---
|
||
|
|
fs.writeFileSync(path.join(serverPath, `index.ts`), serverIndexTemplate());
|
||
|
|
fs.writeFileSync(path.join(serverPath, `controller.ts`), controllerTemplate());
|
||
|
|
fs.writeFileSync(path.join(serverPath, `tsconfig.json`), tsconfigNode());
|
||
|
|
|
||
|
|
console.log(`✅ Package '${name}' creado exitosamente en /packages/${name}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
main();
|