Skip to content

Commit

Permalink
chore: update and apply biome (#37)
Browse files Browse the repository at this point in the history
* chore: update biome config

* chore: enable no unused import

* chore: apply biome

* chore: format more

---------

Co-authored-by: farreldarian <[email protected]>
  • Loading branch information
fdarian and farreldarian authored Mar 9, 2024
1 parent bdd1ecd commit 0842c0f
Show file tree
Hide file tree
Showing 38 changed files with 132 additions and 120 deletions.
10 changes: 8 additions & 2 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
{
"$schema": "https://biomejs.dev/schemas/1.5.2/schema.json",
"$schema": "https://biomejs.dev/schemas/1.6.0/schema.json",
"files": {
"ignore": ["**/dist", "packages/usages/prisma/**"]
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
"recommended": true,
"correctness": {
"noUnusedImports": "warn"
}
}
},
"javascript": {
Expand Down
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"workspaces": [
"packages/*"
],
"devDependencies": {
"@biomejs/biome": "^1.6.0"
}
"workspaces": ["packages/*"],
"devDependencies": {
"@biomejs/biome": "^1.6.0"
}
}
37 changes: 21 additions & 16 deletions packages/generator/src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import { execSync } from 'node:child_process'
import fs from 'node:fs'
import path from 'node:path'
import {
DMMF,
type DMMF,
type GeneratorOptions,
generatorHandler,
GeneratorOptions,
} from '@prisma/generator-helper'
import { execSync } from 'child_process'
import { map, reduce } from 'fp-ts/lib/Array'
import { pipe } from 'fp-ts/lib/function'
import fs from 'fs'
import { isEmpty } from 'lodash'
import path from 'path'
import { GENERATOR_NAME } from './constants'
import { generateEnumDeclaration } from './lib/adapter/declarations/generateEnumDeclaration'
import { generateSchemaDeclaration } from './lib/adapter/declarations/generateSchemaDeclaration'
import { generateTableRelationsDeclaration } from './lib/adapter/declarations/generateTableRelationsDeclaration'
import {
type ModelModule,
createModelModule,
ModelModule,
} from './lib/adapter/modules/createModelModule'
import { isRelationalQueryEnabled } from './lib/config'
import { Context } from './lib/context'
import type { Context } from './lib/context'
import { logger } from './lib/logger'
import { getEnumModuleName } from './lib/prisma-helpers/enums'
import { isRelationField } from './lib/prisma-helpers/field'
import { ImportValue, namedImport, NamedImport } from './lib/syntaxes/imports'
import { createModule, Module } from './lib/syntaxes/module'
import {
type ImportValue,
type NamedImport,
namedImport,
} from './lib/syntaxes/imports'
import { type Module, createModule } from './lib/syntaxes/module'
import { setGeneratorContext } from './shared/generator-context'

const { version } = require('../package.json')
Expand Down Expand Up @@ -60,13 +64,13 @@ generatorHandler({
fs.existsSync(basePath) && fs.rmSync(basePath, { recursive: true })
fs.mkdirSync(basePath, { recursive: true })

adapter.extraModules?.forEach((module) => {
for (const module of adapter.extraModules ?? []) {
const moduleCreation = logger.createTask()
writeModule(basePath, module)
moduleCreation.end(`◟ ${module.name}.ts`)
})
}

options.dmmf.datamodel.enums.forEach((prismaEnum) => {
for (const prismaEnum of options.dmmf.datamodel.enums ?? []) {
const enumCreation = logger.createTask()

const enumModule = createModule({
Expand All @@ -76,8 +80,7 @@ generatorHandler({
writeModule(basePath, enumModule)

enumCreation.end(`◟ ${enumModule.name}.ts`)
return enumModule
})
}

const modelModules = options.dmmf.datamodel.models.map((model) => {
const modelCreation = logger.createTask()
Expand Down Expand Up @@ -143,7 +146,7 @@ generatorHandler({
writeModule(basePath, schemaModule)
}

const formatter = options.generator.config['formatter']
const formatter = options.generator.config.formatter
if (formatter === 'prettier') {
execSync(`prettier --write ${basePath}`, { stdio: 'inherit' })
}
Expand Down Expand Up @@ -173,7 +176,9 @@ export function reduceImports(imports: ImportValue[]) {
if (command.type !== 'namedImport') return accum

const imports = new Set(accum.get(command.module))
command.names.forEach((name) => imports.add(name))
for (const name of command.names) {
imports.add(name)
}

return accum.set(command.module, imports)
}),
Expand Down
11 changes: 7 additions & 4 deletions packages/generator/src/lib/adapter/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { PrismaEnumField, PrismaScalarField } from '../prisma-helpers/field'
import { ImportValue } from '../syntaxes/imports'
import { Module } from '../syntaxes/module'
import { FieldFunc } from './fields/createField'
import type {
PrismaEnumField,
PrismaScalarField,
} from '../prisma-helpers/field'
import type { ImportValue } from '../syntaxes/imports'
import type { Module } from '../syntaxes/module'
import type { FieldFunc } from './fields/createField'

type ParsableField = PrismaScalarField | PrismaEnumField

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DMMF } from '@prisma/generator-helper'
import type { DMMF } from '@prisma/generator-helper'
import { getDbName } from '~/lib/prisma-helpers/getDbName'
import { getEnumVarName } from '../../prisma-helpers/enums'
import { Adapter } from '../types'
import type { Adapter } from '../types'

export function generateEnumDeclaration(
adapter: Adapter,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { camelCase } from 'lodash'
import { Module } from '~/lib/syntaxes/module'
import type { Module } from '~/lib/syntaxes/module'
import { wildcardImport } from '../../syntaxes/imports'

export function generateSchemaDeclaration(models: Module[]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DMMF } from '@prisma/generator-helper'
import type { DMMF } from '@prisma/generator-helper'
import { or } from 'fp-ts/lib/Refinement'
import { pipe } from 'fp-ts/lib/function'
import { isKind } from '~/lib/prisma-helpers/field'
import { getDbName } from '~/lib/prisma-helpers/getDbName'
import { getModelVarName } from '~/lib/prisma-helpers/model'
import { Adapter } from '../types'
import type { Adapter } from '../types'

export function generateTableDeclaration(adapter: Adapter, model: DMMF.Model) {
const fields = model.fields
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { DMMF } from '@prisma/generator-helper'
import type { DMMF } from '@prisma/generator-helper'
import { map } from 'fp-ts/lib/Array'
import { pipe } from 'fp-ts/lib/function'
import { camelCase, kebabCase } from 'lodash'
import pluralize from 'pluralize'
import { ModelModule } from '~/lib/adapter/model-module'
import {
PrismaRelationField,
type PrismaRelationField,
isRelationField,
} from '~/lib/prisma-helpers/field'
import { getDbName } from '~/lib/prisma-helpers/getDbName'
import { getModelVarName } from '~/lib/prisma-helpers/model'
import { namedImport } from '../../syntaxes/imports'
import type { ModelModule } from '../modules/createModelModule'

type GenerateTableRelationsInput = {
fields: PrismaRelationField[]
Expand Down Expand Up @@ -40,7 +40,7 @@ export function generateTableRelationsDeclaration(
}

function getRelationField(ctx: GenerateTableRelationsInput) {
return function (field: PrismaRelationField) {
return (field: PrismaRelationField) => {
const { implicit, opts, referenceModelVarName } = !field.isList
? getOneToOneOrManyRelation(field, ctx)
: opposingIsList(field, ctx)
Expand Down
10 changes: 6 additions & 4 deletions packages/generator/src/lib/adapter/fields/createField.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DMMF } from '@prisma/generator-helper'
import { ImportValue, namedImport } from '~/lib/syntaxes/imports'
import { MakeRequired, ModifyType, Prettify } from '~/lib/types/utils'
import type { DMMF } from '@prisma/generator-helper'
import { type ImportValue, namedImport } from '~/lib/syntaxes/imports'
import type { MakeRequired, ModifyType, Prettify } from '~/lib/types/utils'

export type DefineImport = {
module: string
Expand Down Expand Up @@ -111,7 +111,9 @@ function getCustomDefault(field: DMMF.Field) {
imports: namedImport([type], module),
code: `.$defaultFn(() => ${type}())`,
}
} else if (splits2.length !== 3)
}

if (splits2.length !== 3)
throw new Error(`Invalid default definition: ${field.documentation}`)

const [type, code] = splits2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DMMF } from '@prisma/generator-helper'
import { Context } from '../../context'
import type { DMMF } from '@prisma/generator-helper'
import type { Context } from '../../context'
import { getModelModuleName } from '../../prisma-helpers/model'
import { createModule } from '../../syntaxes/module'
import { generateTableDeclaration } from '../declarations/generateTableDeclaration'
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/lib/adapter/providers/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getDbName } from '~/lib/prisma-helpers/getDbName'
import { namedImport } from '~/lib/syntaxes/imports'
import { createAdapter } from '../adapter'
import {
CreateFieldInput,
type CreateFieldInput,
createField as baseCreateField,
hasDefault,
isDefaultFunc,
Expand Down
6 changes: 3 additions & 3 deletions packages/generator/src/lib/adapter/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mysqlAdapter } from './providers/mysql'
import { postgresAdapter } from './providers/postgres'
import { sqliteAdapter } from './providers/sqlite'
import type { mysqlAdapter } from './providers/mysql'
import type { postgresAdapter } from './providers/postgres'
import type { sqliteAdapter } from './providers/sqlite'

export type Adapter =
| typeof postgresAdapter
Expand Down
4 changes: 2 additions & 2 deletions packages/generator/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Dictionary } from '@prisma/sdk'
import type { Dictionary } from '@prisma/sdk'

export type Config = Dictionary<string | string[] | undefined>

export function isRelationalQueryEnabled(config: Config) {
const value = config['relationalQuery']
const value = config.relationalQuery
if (value === 'false') return false
return true
}
Expand Down
6 changes: 3 additions & 3 deletions packages/generator/src/lib/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DMMF } from '@prisma/generator-helper'
import { Adapter } from './adapter/types'
import { Config } from './config'
import type { DMMF } from '@prisma/generator-helper'
import type { Adapter } from './adapter/types'
import type { Config } from './config'

export type Context = {
adapter: Adapter
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GeneratorOptions } from '@prisma/generator-helper'
import type { GeneratorOptions } from '@prisma/generator-helper'
import { logger as baseLogger } from '@prisma/sdk'
import { GENERATOR_NAME } from 'src/constants'

Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/lib/prisma-helpers/enums.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DMMF } from '@prisma/generator-helper'
import type { DMMF } from '@prisma/generator-helper'
import { camelCase, kebabCase, memoize } from 'lodash'

export const getEnumVarName = memoize((prismaEnum: DMMF.DatamodelEnum) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/lib/prisma-helpers/field.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DMMF } from '@prisma/generator-helper'
import type { DMMF } from '@prisma/generator-helper'

export type PrismaFieldType =
| 'BigInt'
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/lib/prisma-helpers/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DMMF } from '@prisma/generator-helper'
import type { DMMF } from '@prisma/generator-helper'
import { camelCase, kebabCase, memoize } from 'lodash'
import pluralize from 'pluralize'

Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/lib/syntaxes/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { flatMap } from 'fp-ts/lib/Array'
import { pipe } from 'fp-ts/lib/function'
import { reduceImports } from '../../generator'
import { ImportValue } from './imports'
import type { ImportValue } from './imports'

export type Module = ReturnType<typeof createModule>

Expand Down
4 changes: 2 additions & 2 deletions packages/generator/src/lib/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export type Prettify<T> = {
} & {}

export type MakeRequired<
shake extends Record<string, any>,
shake extends Record<string, unknown>,
key extends keyof shake,
> = Omit<shake, key> & Required<Pick<shake, key>>

export type ModifyType<
shape extends Record<string, any>,
shape extends Record<string, unknown>,
key extends keyof shape,
modified,
> = Omit<shape, key> & Record<key, modified>
6 changes: 3 additions & 3 deletions packages/generator/src/shared/generator-context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import fs from 'fs'
import { GeneratorOptions } from '@prisma/generator-helper'
import fs from 'node:fs'
import path from 'node:path'
import type { GeneratorOptions } from '@prisma/generator-helper'
import { getModuleResolution } from '~/lib/config'

type GeneratorContext = {
Expand Down
48 changes: 24 additions & 24 deletions packages/generator/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"lib": ["esnext"],
"strict": true,
"strictPropertyInitialization": false,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"removeComments": true,
"sourceMap": true,
"baseUrl": ".",
"moduleResolution": "Node",
"outDir": "./dist",
"rootDir": "./src",
"newLine": "lf",
"paths": {
"~/*": ["src/*"]
}
},
"include": ["src/**/*"],
"exclude": ["**/node_modules"]
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"lib": ["esnext"],
"strict": true,
"strictPropertyInitialization": false,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"removeComments": true,
"sourceMap": true,
"baseUrl": ".",
"moduleResolution": "Node",
"outDir": "./dist",
"rootDir": "./src",
"newLine": "lf",
"paths": {
"~/*": ["src/*"]
}
},
"include": ["src/**/*"],
"exclude": ["**/node_modules"]
}
2 changes: 1 addition & 1 deletion packages/usage/scripts/cloneMysql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {}
export type {}

const BASE_PATH = './prisma/schema.prisma'
const TARGET_PATH = './prisma/mysql/schema.prisma'
Expand Down
2 changes: 1 addition & 1 deletion packages/usage/scripts/cloneSqlite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {}
export type {}

const BASE_PATH = './prisma/schema.prisma'
const TARGET_PATH = './prisma/sqlite/schema.prisma'
Expand Down
2 changes: 1 addition & 1 deletion packages/usage/src/lib/mysql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { drizzle } from 'drizzle-orm/mysql2'
import mysql from 'mysql2/promise'
import { schema } from 'prisma/mysql/drizzle/schema'
import { object, parse, string, url } from 'valibot'
import { url, object, parse, string } from 'valibot'

const env = parse(
object({
Expand Down
Loading

0 comments on commit 0842c0f

Please sign in to comment.