|
| 1 | +/** |
| 2 | + * `webjs generate <type> <name>` — code generators following CONVENTIONS.md. |
| 3 | + * |
| 4 | + * Generates files with sensible defaults matching the module architecture: |
| 5 | + * webjs generate page contact → app/contact/page.ts |
| 6 | + * webjs generate module posts → modules/posts/{actions,queries,components,utils,types.ts} |
| 7 | + * webjs generate action posts/create → modules/posts/actions/create.server.ts |
| 8 | + * webjs generate query posts/list → modules/posts/queries/list.server.ts |
| 9 | + * webjs generate component my-widget → components/my-widget.ts |
| 10 | + * webjs generate route api/webhooks → app/api/webhooks/route.ts |
| 11 | + */ |
| 12 | + |
| 13 | +import { mkdir, writeFile } from 'node:fs/promises'; |
| 14 | +import { join, dirname } from 'node:path'; |
| 15 | +import { existsSync } from 'node:fs'; |
| 16 | + |
| 17 | +const USAGE = `Usage: webjs generate <type> <name> |
| 18 | +
|
| 19 | +Types: |
| 20 | + page <path> app/<path>/page.ts |
| 21 | + module <name> modules/<name>/{actions,queries,components,utils,types.ts} |
| 22 | + action <module/name> modules/<module>/actions/<name>.server.ts |
| 23 | + query <module/name> modules/<module>/queries/<name>.server.ts |
| 24 | + component <tag-name> components/<tag-name>.ts |
| 25 | + route <path> app/<path>/route.ts |
| 26 | +
|
| 27 | +Examples: |
| 28 | + webjs generate page contact |
| 29 | + webjs generate module posts |
| 30 | + webjs generate action posts/create |
| 31 | + webjs generate query posts/list |
| 32 | + webjs generate component my-widget |
| 33 | + webjs generate route api/webhooks`; |
| 34 | + |
| 35 | +/** |
| 36 | + * @param {string[]} args |
| 37 | + * @param {string} cwd |
| 38 | + */ |
| 39 | +export async function generate(args, cwd) { |
| 40 | + const [type, name] = args; |
| 41 | + if (!type || !name) { console.error(USAGE); process.exit(1); } |
| 42 | + |
| 43 | + switch (type) { |
| 44 | + case 'page': return genPage(name, cwd); |
| 45 | + case 'module': return genModule(name, cwd); |
| 46 | + case 'action': return genAction(name, cwd); |
| 47 | + case 'query': return genQuery(name, cwd); |
| 48 | + case 'component': return genComponent(name, cwd); |
| 49 | + case 'route': return genRoute(name, cwd); |
| 50 | + default: |
| 51 | + console.error(`Unknown type: ${type}\n${USAGE}`); |
| 52 | + process.exit(1); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +async function write(file, content) { |
| 57 | + await mkdir(dirname(file), { recursive: true }); |
| 58 | + if (existsSync(file)) { |
| 59 | + console.error(` ✗ ${file} already exists — skipping`); |
| 60 | + return; |
| 61 | + } |
| 62 | + await writeFile(file, content); |
| 63 | + console.log(` ✓ ${file}`); |
| 64 | +} |
| 65 | + |
| 66 | +function toPascal(s) { |
| 67 | + return s.replace(/(^|[-_/])(\w)/g, (_, __, c) => c.toUpperCase()); |
| 68 | +} |
| 69 | + |
| 70 | +function toCamel(s) { |
| 71 | + const p = toPascal(s); |
| 72 | + return p[0].toLowerCase() + p.slice(1); |
| 73 | +} |
| 74 | + |
| 75 | +async function genPage(path, cwd) { |
| 76 | + const file = join(cwd, 'app', path, 'page.ts'); |
| 77 | + const name = toPascal(path.split('/').pop() || path); |
| 78 | + console.log(`Generating page: /${path}`); |
| 79 | + await write(file, `import { html } from 'webjs'; |
| 80 | +
|
| 81 | +export const metadata = { title: '${name}' }; |
| 82 | +
|
| 83 | +export default function ${name}Page() { |
| 84 | + return html\` |
| 85 | + <h1>${name}</h1> |
| 86 | + <p>Edit <code>app/${path}/page.ts</code></p> |
| 87 | + \`; |
| 88 | +} |
| 89 | +`); |
| 90 | +} |
| 91 | + |
| 92 | +async function genModule(name, cwd) { |
| 93 | + const base = join(cwd, 'modules', name); |
| 94 | + console.log(`Generating module: ${name}`); |
| 95 | + await mkdir(join(base, 'actions'), { recursive: true }); |
| 96 | + await mkdir(join(base, 'queries'), { recursive: true }); |
| 97 | + await mkdir(join(base, 'components'), { recursive: true }); |
| 98 | + await mkdir(join(base, 'utils'), { recursive: true }); |
| 99 | + |
| 100 | + await write(join(base, 'types.ts'), `/** |
| 101 | + * Shared types for the ${name} module. |
| 102 | + */ |
| 103 | +
|
| 104 | +export interface ActionResult<T> { |
| 105 | + success: true; data: T; |
| 106 | +} | { |
| 107 | + success: false; error: string; status: number; |
| 108 | +} |
| 109 | +`); |
| 110 | + |
| 111 | + console.log(` ✓ modules/${name}/actions/`); |
| 112 | + console.log(` ✓ modules/${name}/queries/`); |
| 113 | + console.log(` ✓ modules/${name}/components/`); |
| 114 | + console.log(` ✓ modules/${name}/utils/`); |
| 115 | +} |
| 116 | + |
| 117 | +async function genAction(path, cwd) { |
| 118 | + const parts = path.split('/'); |
| 119 | + if (parts.length < 2) { |
| 120 | + console.error('Usage: webjs generate action <module>/<name>\n e.g. webjs generate action posts/create'); |
| 121 | + process.exit(1); |
| 122 | + } |
| 123 | + const mod = parts[0]; |
| 124 | + const name = parts.slice(1).join('-'); |
| 125 | + const fnName = toCamel(name); |
| 126 | + const file = join(cwd, 'modules', mod, 'actions', `${name}.server.ts`); |
| 127 | + console.log(`Generating action: ${mod}/${name}`); |
| 128 | + await write(file, `'use server'; |
| 129 | +
|
| 130 | +// import { prisma } from '../../../lib/prisma.ts'; |
| 131 | +
|
| 132 | +export async function ${fnName}(input: unknown) { |
| 133 | + // TODO: implement |
| 134 | + return { success: true, data: null }; |
| 135 | +} |
| 136 | +`); |
| 137 | +} |
| 138 | + |
| 139 | +async function genQuery(path, cwd) { |
| 140 | + const parts = path.split('/'); |
| 141 | + if (parts.length < 2) { |
| 142 | + console.error('Usage: webjs generate query <module>/<name>\n e.g. webjs generate query posts/list'); |
| 143 | + process.exit(1); |
| 144 | + } |
| 145 | + const mod = parts[0]; |
| 146 | + const name = parts.slice(1).join('-'); |
| 147 | + const fnName = toCamel(name); |
| 148 | + const file = join(cwd, 'modules', mod, 'queries', `${name}.server.ts`); |
| 149 | + console.log(`Generating query: ${mod}/${name}`); |
| 150 | + await write(file, `'use server'; |
| 151 | +
|
| 152 | +// import { prisma } from '../../../lib/prisma.ts'; |
| 153 | +
|
| 154 | +export async function ${fnName}() { |
| 155 | + // TODO: implement |
| 156 | + return []; |
| 157 | +} |
| 158 | +`); |
| 159 | +} |
| 160 | + |
| 161 | +async function genComponent(tagName, cwd) { |
| 162 | + if (!tagName.includes('-')) { |
| 163 | + console.error(`Component tag name must contain a hyphen: ${tagName}`); |
| 164 | + process.exit(1); |
| 165 | + } |
| 166 | + const className = toPascal(tagName); |
| 167 | + const file = join(cwd, 'components', `${tagName}.ts`); |
| 168 | + console.log(`Generating component: <${tagName}>`); |
| 169 | + await write(file, `import { WebComponent, html, css } from 'webjs'; |
| 170 | +
|
| 171 | +export class ${className} extends WebComponent { |
| 172 | + static tag = '${tagName}'; |
| 173 | + static styles = css\` |
| 174 | + :host { display: block; } |
| 175 | + \`; |
| 176 | +
|
| 177 | + render() { |
| 178 | + return html\`<p>${tagName} works</p>\`; |
| 179 | + } |
| 180 | +} |
| 181 | +${className}.register(import.meta.url); |
| 182 | +`); |
| 183 | +} |
| 184 | + |
| 185 | +async function genRoute(path, cwd) { |
| 186 | + const file = join(cwd, 'app', path, 'route.ts'); |
| 187 | + console.log(`Generating route: /${path}`); |
| 188 | + await write(file, `/** |
| 189 | + * ${path} route handler. |
| 190 | + * |
| 191 | + * Convention: routes are thin wrappers over typed server actions. |
| 192 | + * Business logic lives in modules/, not here. |
| 193 | + */ |
| 194 | +
|
| 195 | +export async function GET(req: Request) { |
| 196 | + return Response.json({ status: 'ok' }); |
| 197 | +} |
| 198 | +
|
| 199 | +export async function POST(req: Request) { |
| 200 | + const body = await req.json(); |
| 201 | + // import { myAction } from '.../.server.ts'; |
| 202 | + // return Response.json(await myAction(body)); |
| 203 | + return Response.json({ received: body }); |
| 204 | +} |
| 205 | +`); |
| 206 | +} |
0 commit comments