Skip to content

Commit

Permalink
feat: add support for reading configuration at generation time (#27)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Kuhrt <jasonkuhrt@me.com>
  • Loading branch information
iddan and jasonkuhrt authored May 17, 2021
1 parent afa09c6 commit 275e03f
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 21 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@
"nexus": "^1.0.0"
},
"dependencies": {
"@endemolshinegroup/cosmiconfig-typescript-loader": "^3.0.2",
"@prisma/generator-helper": "^2.22.1",
"cosmiconfig": "^7.0.0",
"debug": "^4.3.1",
"endent": "^2.0.1",
"fs-jetpack": "^4.1.0",
Expand Down
6 changes: 4 additions & 2 deletions src/cli/nexus-prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ process.env.DEBUG_HIDE_DATE = 'true'

import { generatorHandler } from '@prisma/generator-helper'
import * as Path from 'path'

import { generateRuntimeAndEmit } from '../generator'
import { externalToInternalDmmf } from '../helpers/prismaExternalToInternalDMMF'
import { getConfiguration } from '../configuration'

// todo by default error in ci and warn in local
// enforceValidPeerDependencies({
Expand All @@ -23,10 +25,10 @@ generatorHandler({
}
},
// async required by interface
// eslint-disable-next-line
async onGenerate({ dmmf }) {
const internalDMMF = externalToInternalDmmf(dmmf)
console.log('created internal dmmf')
generateRuntimeAndEmit(internalDMMF)
const configuration = await getConfiguration()
generateRuntimeAndEmit(internalDMMF, configuration)
},
})
30 changes: 30 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { cosmiconfig } from 'cosmiconfig'
import TypeScriptLoader from '@endemolshinegroup/cosmiconfig-typescript-loader'
import { Configuration } from '../generator'

const CONFIGURATION_NAME = 'nexus-prisma'

export async function getConfiguration(): Promise<Configuration | null> {
const explorer = cosmiconfig(CONFIGURATION_NAME, {
searchPlaces: [
'nexus-prisma.ts',
'nexusPrisma.ts',
'nexus_prisma.ts',
'prisma/nexus-prisma.ts',
'prisma/nexusPrisma.ts',
'prisma/nexus_prisma.ts',
],
loaders: {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
'.ts': TypeScriptLoader,
},
packageProp: [],
})
const result = await explorer.search()
if (!result) {
return null
}
console.log(`Loaded configuration from ${result.filepath}`)
// TODO runtime validation with zod
return result.config as Configuration
}
1 change: 1 addition & 0 deletions src/configuration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './configuration'
6 changes: 4 additions & 2 deletions src/generator/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import * as Path from 'path'
import * as pkgup from 'pkg-up'
import { d } from '../helpers/debugNexusPrisma'
import * as ModelsGenerator from './models'
import { ModuleSpec } from './types'
import { ModuleSpec, Configuration } from './types'

const OUTPUT_SOURCE_DIR = getOutputSourceDir()

/** Generate the Nexus Prisma runtime files and emit them into a "hole" in the internal package source tree. */
export function generateRuntimeAndEmit(dmmf: DMMF.Document): void {
export function generateRuntimeAndEmit(dmmf: DMMF.Document, configuration: Configuration | null): void {
d('start generateRuntime with configuration %j', configuration)

d('start generateRuntime')

if (process.env.NP_DEBUG) {
Expand Down
1 change: 1 addition & 0 deletions src/generator/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { generateRuntimeAndEmit } from './generate'
export { Configuration } from './types'
3 changes: 3 additions & 0 deletions src/generator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ export type ModuleSpec = {
fileName: string
content: string
}

// eslint-disable-next-line @typescript-eslint/ban-types
export type Configuration = {}
11 changes: 8 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import {} from '@prisma/generator-helper'
import endent from 'endent'
import { PrismaClient } from '@prisma/client'
import * as Nexus from 'nexus'
import { d } from './helpers/debugNexusPrisma'
import { settings, SettingsInput } from './settings'
import { SettingsInput, change } from './settings'

throw new Error(endent`
Nexus Prisma is currently only available as a Prisma geneator.
Nexus Prisma is currently only available as a Prisma generator.
`)

export function plugin(settingsInput?: SettingsInput): Nexus.core.NexusPlugin {
return Nexus.plugin({
name: 'nexus-prisma',
onInstall() {
d('nexus plugin onInstall')
const settingsData = settings.change(settingsInput ?? {}).data
const settingsData = change({
...settingsInput,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
prisma: new PrismaClient(),
}).data
settingsData
},
})
Expand Down
19 changes: 8 additions & 11 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaClient } from '@prisma/client'
import type { PrismaClient } from '@prisma/client'
import * as Setset from 'setset'

export type SettingsInput = {
Expand All @@ -7,15 +7,12 @@ export type SettingsInput = {

export type SettingsData = Setset.InferDataFromInput<SettingsInput>

export type Settings = Setset.Manager<SettingsInput, SettingsData>

export const settings = Setset.create<SettingsInput, SettingsData>({
fields: {
prisma: {
initial() {
// Any type expected here because Prisma Client not generated
// Would have a type in a user's project, following prisma generate
// eslint-disable-next-line
return new PrismaClient()
},
},
},
fields: {},
})

export const change = (input: Setset.UserInput<SettingsInput>): Settings => {
return settings.change(input)
}
42 changes: 39 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@
exec-sh "^0.3.2"
minimist "^1.2.0"

"@endemolshinegroup/cosmiconfig-typescript-loader@^3.0.2":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@endemolshinegroup/cosmiconfig-typescript-loader/-/cosmiconfig-typescript-loader-3.0.2.tgz#eea4635828dde372838b0909693ebd9aafeec22d"
integrity sha512-QRVtqJuS1mcT56oHpVegkKBlgtWjXw/gHNWO3eL9oyB5Sc7HBoc2OLG/nYpVfT/Jejvo3NUrD0Udk7XgoyDKkA==
dependencies:
lodash.get "^4"
make-error "^1"
ts-node "^9"
tslib "^2"

"@eslint/eslintrc@^0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-0.4.0.tgz#99cc0a0584d72f1df38b900fb062ba995f395547"
Expand Down Expand Up @@ -1014,6 +1024,11 @@
dependencies:
"@types/node" "*"

"@types/parse-json@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==

"@types/pluralize@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/pluralize/-/pluralize-0.0.29.tgz#6ffa33ed1fc8813c469b859681d09707eb40d03c"
Expand Down Expand Up @@ -1954,6 +1969,17 @@ core-util-is@1.0.2, core-util-is@~1.0.0:
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=

cosmiconfig@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-7.0.0.tgz#ef9b44d773959cae63ddecd122de23853b60f8d3"
integrity sha512-pondGvTuVYDk++upghXJabWzL6Kxu6f26ljFw64Swq9v6sQPUL3EUlVDV56diOjpCayKihL6hVe8exIACU4XcA==
dependencies:
"@types/parse-json" "^4.0.0"
import-fresh "^3.2.1"
parse-json "^5.0.0"
path-type "^4.0.0"
yaml "^1.10.0"

crc-32@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/crc-32/-/crc-32-1.2.0.tgz#cb2db6e29b88508e32d9dd0ec1693e7b41a18208"
Expand Down Expand Up @@ -4228,6 +4254,11 @@ lodash.flatten@^4.4.0:
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"
integrity sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=

lodash.get@^4:
version "4.4.2"
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=

lodash.isplainobject@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
Expand Down Expand Up @@ -4287,7 +4318,7 @@ make-dir@3.1.0, make-dir@^3.0.0, make-dir@^3.0.2:
dependencies:
semver "^6.0.0"

make-error@1.x, make-error@^1.1.1:
make-error@1.x, make-error@^1, make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
Expand Down Expand Up @@ -5975,7 +6006,7 @@ ts-jest@26.5.6:
semver "7.x"
yargs-parser "20.x"

ts-node@^9.1.1:
ts-node@^9, ts-node@^9.1.1:
version "9.1.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.1.1.tgz#51a9a450a3e959401bda5f004a72d54b936d376d"
integrity sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg==
Expand All @@ -5992,7 +6023,7 @@ tslib@^1.8.1, tslib@^1.9.3:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.0.0, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.2.0, tslib@~2.2.0:
tslib@^2, tslib@^2.0.0, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.2.0, tslib@~2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.2.0.tgz#fb2c475977e35e241311ede2693cee1ec6698f5c"
integrity sha512-gS9GVHRU+RGn5KQM2rllAlR3dU6m7AcpJKdtH8gFvQiC4Otgk98XnmMU+nZenHt/+VhnBPWwgrJsyrdcw6i23w==
Expand Down Expand Up @@ -6378,6 +6409,11 @@ yallist@^4.0.0:
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==

yaml@^1.10.0:
version "1.10.2"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b"
integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==

yargs-parser@20.x:
version "20.2.7"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.7.tgz#61df85c113edfb5a7a4e36eb8aa60ef423cbc90a"
Expand Down

0 comments on commit 275e03f

Please sign in to comment.