nuxt-crud-cli
Version:
CLI tool to generate API resources for Nuxt CRUD
121 lines (105 loc) • 3.59 kB
text/typescript
import fs from 'fs';
import path from 'path';
import { pluralize, camelCase } from '../utils/string.js';
/**
* Generate controller file
*/
export async function generateController(
resourceDir: string,
resourceName: string,
resourcePascalName: string
): Promise<void> {
const pluralResourceName = pluralize(resourceName);
const controllerName = `${resourcePascalName}Controller`;
const controllerPath = path.join(resourceDir, `${controllerName}.ts`);
const controllerContent = `import { use${resourcePascalName}Collection } from "~~/server/api/v1/${pluralResourceName}/resources/${resourceName}Collection";
import { use${resourcePascalName}Resource } from "~~/server/api/v1/${pluralResourceName}/resources/${resourceName}Resource";
/**
* ${resourcePascalName} Controller Composable
* Handles business logic for ${resourceName} operations
*/
export const use${resourcePascalName}Controller = () => {
/**
* Get all ${pluralResourceName}
*/
const index = async (params: any = {}) => {
// In a real app, this would fetch from a database with pagination/filtering
const ${pluralResourceName} = [
{ id: 1, name: 'Example ${resourcePascalName} 1' },
{ id: 2, name: 'Example ${resourcePascalName} 2' },
];
// Apply filters
let filtered${pluralResourceName} = ${pluralResourceName};
// Example filter implementation
if (params.search) {
const searchLower = params.search.toLowerCase();
filtered${pluralResourceName} = filtered${pluralResourceName}.filter(${resourceName} =>
${resourceName}.name.toLowerCase().includes(searchLower)
);
}
return {
data: use${resourcePascalName}Collection({ data: filtered${pluralResourceName} }).toArray(),
meta: {
total: filtered${pluralResourceName}.length,
page: params.page || 1,
limit: params.limit || 10
}
};
};
/**
* Get a specific ${resourceName} by ID
*/
const show = async (id: string) => {
// In a real app, this would fetch from a database
const ${resourceName} = { id: parseInt(id), name: \`${resourcePascalName} \${id}\` };
return {
data: use${resourcePascalName}Resource(${resourceName}).toArray(),
};
};
/**
* Create a new ${resourceName}
*/
const store = async (${resourceName}Data: any) => {
// In a real app, this would create in a database
const new${resourcePascalName} = {
id: 3, // Would be generated by DB
...${resourceName}Data
};
return {
message: '${resourcePascalName} created successfully',
data: use${resourcePascalName}Resource(new${resourcePascalName}).toArray(),
};
};
/**
* Update a specific ${resourceName}
*/
const update = async (id: string, ${resourceName}Data: any) => {
// In a real app, this would update in a database
const updated${resourcePascalName} = {
id: parseInt(id),
name: ${resourceName}Data.name || \`${resourcePascalName} \${id}\`,
};
return {
message: '${resourcePascalName} updated successfully',
data: use${resourcePascalName}Resource(updated${resourcePascalName}).toArray(),
};
};
/**
* Delete a specific ${resourceName}
*/
const destroy = async (id: string) => {
// In a real app, this would delete from a database
return {
message: \`${resourcePascalName} \${id} deleted successfully\`,
};
};
return {
index,
show,
store,
update,
destroy
};
};`;
fs.writeFileSync(controllerPath, controllerContent);
}