feat: add application productivity foundations
This commit is contained in:
@@ -30,6 +30,46 @@ export interface GeneratedFile {
|
||||
content: string;
|
||||
}
|
||||
|
||||
export function scaffoldResource(name: string): GeneratedFile[] {
|
||||
const clean = name.replace(/[^A-Za-z0-9_-]/g, "").toLowerCase();
|
||||
if (!clean) throw new TypeError("resource name is required");
|
||||
const singular = clean.replace(/s$/, "");
|
||||
const pascal = toPascalCase(singular);
|
||||
return [
|
||||
{
|
||||
path: `schemas/${singular}.ts`,
|
||||
content: `import { v } from "@wrnexus/validation";\n\nexport default v.object({\n name: v.string().min(1, "Required"),\n});\n`,
|
||||
},
|
||||
{
|
||||
path: `resources/${clean}.ts`,
|
||||
content: `import { getDb } from "@wrnexus/db";\nimport { defineResource } from "@wrnexus/helpers";\n\nexport default defineResource({\n name: "${clean}",\n db: getDb,\n table: "${clean}",\n owner: "owner_id",\n fields: ["name"],\n permissions: { read: "${singular}:read", write: "${singular}:write" },\n});\n`,
|
||||
},
|
||||
{
|
||||
path: `api/${clean}.ts`,
|
||||
content: `import resource from "../resources/${clean}.ts";\nexport const GET = resource.list;\nexport const POST = resource.create;\n`,
|
||||
},
|
||||
{
|
||||
path: `api/${clean}/[id].ts`,
|
||||
content: `import resource from "../../resources/${clean}.ts";\nexport const GET = resource.get;\nexport const PUT = resource.update;\nexport const DELETE = resource.remove;\n`,
|
||||
},
|
||||
{
|
||||
path: `pages/${clean}.wrn`,
|
||||
content: `page ${pascal}List {\n view {\n <h1>${pascal}</h1>\n <p>Generated resource page. Connect it to /api/${clean}.</p>\n }\n}\n`,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
export function runGenerateResource(appRoot: string, name: string | undefined): void {
|
||||
if (!name) throw new TypeError("Usage: wrnexus generate resource <name>");
|
||||
for (const file of scaffoldResource(name)) {
|
||||
const target = join(resolve(appRoot), "app", file.path);
|
||||
if (existsSync(target)) continue;
|
||||
mkdirSync(dirname(target), { recursive: true });
|
||||
writeFileSync(target, file.content, "utf8");
|
||||
console.log(`✓ Generated app/${file.path}`);
|
||||
}
|
||||
}
|
||||
|
||||
function toPascalCase(name: string): string {
|
||||
return name
|
||||
.split(/[^A-Za-z0-9]+/)
|
||||
|
||||
Reference in New Issue
Block a user