tspace-spear
Version:
tspace-spear is a lightweight, high-performance API framework for Node.js that leverages the native HTTP server and supports uWebSockets.js (C++) for maximum speed and efficiency.
78 lines (69 loc) ⢠1.93 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createController = createController;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const shared_1 = require("../shared");
function createController(root, name) {
if (!name) {
console.log("Missing controller path, try spear g controller dog");
process.exit(1);
}
const resolvedPath = path_1.default.resolve(process.cwd(), root, 'modules', (0, shared_1.toPlural)(name));
const fileName = `${(0, shared_1.toSingular)(name)}.controller.ts`;
const target = path_1.default.join(resolvedPath, fileName);
fs_1.default.mkdirSync(resolvedPath, { recursive: true });
const className = (0, shared_1.capitalize)((0, shared_1.toSingular)(name)) + "Controller";
fs_1.default.writeFileSync(target, `
import {
type T,
Controller,
Get,
Post,
Put,
Delete
} from "tspace-spear";
@Controller("/${name}")
class ${className} {
@Get("/")
async index() {
return {};
}
@Get("/:id")
async show({
params
}: T.Context<{ params: { id: number } }>) {
return { id: params.id };
}
@Post("/")
async create({
body
}: T.Context<{ body: {} }>) {
return { body };
}
@Put("/:id")
async update({
params,
body
}: T.Context<{ params: { id: number }; body: {} }>) {
return { id: params.id, body };
}
@Delete("/:id")
async remove({
params
}: T.Context<{ params: { id: number } }>) {
return { id: params.id };
}
}
export { ${className} };
export default ${className};
`);
console.log(`
CREATE ${target}
ā Successfully generated controller "${name}"
`);
}
//# sourceMappingURL=index.js.map