diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 05c06c0f..e801b340 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -20,6 +20,9 @@ dependencies: execa: specifier: ^8.0.1 version: 8.0.1 + pluralize: + specifier: ^8.0.0 + version: 8.0.0 strip-json-comments: specifier: ^5.0.1 version: 5.0.1 @@ -1067,6 +1070,11 @@ packages: engines: {node: '>=8.6'} dev: true + /pluralize@8.0.0: + resolution: {integrity: sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==} + engines: {node: '>=4'} + dev: false + /prelude-ls@1.2.1: resolution: {integrity: sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==} engines: {node: '>= 0.8.0'} diff --git a/src/commands/generate/index.ts b/src/commands/generate/index.ts index d0784176..05e57b7e 100644 --- a/src/commands/generate/index.ts +++ b/src/commands/generate/index.ts @@ -5,6 +5,7 @@ import { DBField, DBType, DrizzleColumnType, + GenerateOptions, ORMType, PrismaColumnType, } from "../../types.js"; @@ -26,7 +27,7 @@ import { scaffoldModel } from "./generators/model/index.js"; function provideInstructions() { consola.info( - "Quickly generate your Model (Drizzle schema + queries / mutations), Controllers (API Routes and TRPC Routes), and Views", + "Quickly generate your Model (Drizzle schema + queries / mutations), Controllers (API Routes and TRPC Routes), and Views" ); } @@ -92,7 +93,7 @@ async function askForFields(orm: ORMType, dbType: DBType, tableName: string) { const currentSchemas = getCurrentSchemas(); const baseFieldTypeChoices = Object.keys( - createOrmMappings()[orm][dbType].typeMappings, + createOrmMappings()[orm][dbType].typeMappings ) .filter((field) => field !== "id") .map((field) => { @@ -105,7 +106,7 @@ async function askForFields(orm: ORMType, dbType: DBType, tableName: string) { currentSchemas[0] === toCamelCase(tableName)); const fieldTypeChoices = removeReferenceOption ? baseFieldTypeChoices.filter( - (field) => field.name.toLowerCase() !== "references", + (field) => field.name.toLowerCase() !== "references" ) : baseFieldTypeChoices; @@ -203,7 +204,7 @@ export function preBuild() { return true; } -export async function buildSchema() { +export async function buildSchema(options?: GenerateOptions) { const ready = preBuild(); if (!ready) return; @@ -215,13 +216,20 @@ export async function buildSchema() { if (orm !== null) { provideInstructions(); - const resourceType = await askForResourceType(); - const tableName = await askForTable(); - const fields = await askForFields(orm, driver, tableName); - const indexedField = await askForIndex(fields); + const resourceType = options.resourceTypes || (await askForResourceType()); + const tableName = options.table || (await askForTable()); + const fields = + options.fields || (await askForFields(orm, driver, tableName)); + const indexedField = + typeof options.index === "string" + ? options.index || null + : await askForIndex(fields); let schema: Schema; if (resourceType.includes("model") && auth !== null) { - const belongsToUser = await askIfBelongsToUser(); + const belongsToUser = + typeof options.belongsToUser === "string" + ? options.belongsToUser === "yes" + : await askIfBelongsToUser(); schema = { tableName, fields, @@ -244,7 +252,7 @@ export async function buildSchema() { scaffoldViewsAndComponents(schema); } else { consola.warn( - "You need to have an ORM installed in order to use the scaffold command.", + "You need to have an ORM installed in order to use the scaffold command." ); addPackage(); } diff --git a/src/index.ts b/src/index.ts index daedeefa..5a6b0cb8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,24 @@ addCommonOptions(program.command("init")) program .command("generate") .description("Generate a new resource") + .option("-r, --resourceTypes ", "Resource types", (types) => { + if (!types) return undefined; + const parts = types.split(","); + if (parts.length === 0) return undefined; + return parts; + }) + .option("-t, --table ", "Table name") + .option("-f, --fields ", "Fields in JSON format", (fields) => { + try { + if (!fields) return undefined; + return JSON.parse(fields); + } catch (error) { + console.error("Error parsing fields: ", error); + process.exit(1); + } + }) + .option("-i, --index ", "Database index") + .option("-b, --belongsToUser ", "Belongs to user flag. yes/no", false) .action(buildSchema); addCommonOptions(program.command("add")) diff --git a/src/types.d.ts b/src/types.d.ts index b1c69f2f..f6a7b2d7 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -110,14 +110,14 @@ export type InitOptions = { includeExample?: "yes" | "no"; }; -// export type BuildOptions = { -// resources?: ("model" | "api_route" | "trpc_route" | "views_and_components")[]; -// table?: string; -// belongsToUser?: "yes" | "no"; -// index?: string; -// field?: DBField[]; -// migrate?: "yes" | "no"; -// }; +export type GenerateOptions = { + resourceTypes?: ("model" | "api_route" | "trpc_route" | "views_and_components")[]; + table?: string; + belongsToUser?: "yes" | "no"; + index?: string; + fields?: DBField[]; + migrate?: "yes" | "no"; +}; export type ScaffoldSchema = { tableName: string;