From 992299ad64d5ca6d118a3d59e1b4643fae897b0d Mon Sep 17 00:00:00 2001 From: Eliezer Steinbock <3090527+elie222@users.noreply.github.com> Date: Fri, 1 Dec 2023 13:26:48 -0500 Subject: [PATCH 1/3] Add generate options via CLI back --- pnpm-lock.yaml | 8 ++++++++ src/commands/generate/index.ts | 18 ++++++++++++------ src/index.ts | 18 ++++++++++++++++++ src/types.d.ts | 16 ++++++++-------- 4 files changed, 46 insertions(+), 14 deletions(-) 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..3d399252 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"; @@ -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,18 @@ 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 + : await askForIndex(fields); let schema: Schema; if (resourceType.includes("model") && auth !== null) { - const belongsToUser = await askIfBelongsToUser(); + const belongsToUser = + options.belongsToUser === "yes" || (await askIfBelongsToUser()); schema = { tableName, fields, 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; From 74d41d1e99f73d968d32f052df7db6e5aaf0b393 Mon Sep 17 00:00:00 2001 From: Eliezer Steinbock <3090527+elie222@users.noreply.github.com> Date: Fri, 1 Dec 2023 15:20:02 -0500 Subject: [PATCH 2/3] fix empty index --- src/commands/generate/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/commands/generate/index.ts b/src/commands/generate/index.ts index 3d399252..a2abea3b 100644 --- a/src/commands/generate/index.ts +++ b/src/commands/generate/index.ts @@ -27,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" ); } @@ -93,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) => { @@ -106,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; @@ -222,7 +222,7 @@ export async function buildSchema(options?: GenerateOptions) { options.fields || (await askForFields(orm, driver, tableName)); const indexedField = typeof options.index === "string" - ? options.index + ? options.index || null : await askForIndex(fields); let schema: Schema; if (resourceType.includes("model") && auth !== null) { @@ -250,7 +250,7 @@ export async function buildSchema(options?: GenerateOptions) { 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(); } From 225139887717b6f5621541e7c74a78e29be3f4e5 Mon Sep 17 00:00:00 2001 From: Eliezer Steinbock <3090527+elie222@users.noreply.github.com> Date: Sun, 3 Dec 2023 12:16:20 -0500 Subject: [PATCH 3/3] fix belongs to user via cli --- src/commands/generate/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/commands/generate/index.ts b/src/commands/generate/index.ts index a2abea3b..05e57b7e 100644 --- a/src/commands/generate/index.ts +++ b/src/commands/generate/index.ts @@ -227,7 +227,9 @@ export async function buildSchema(options?: GenerateOptions) { let schema: Schema; if (resourceType.includes("model") && auth !== null) { const belongsToUser = - options.belongsToUser === "yes" || (await askIfBelongsToUser()); + typeof options.belongsToUser === "string" + ? options.belongsToUser === "yes" + : await askIfBelongsToUser(); schema = { tableName, fields,