Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: gentime setting for output directory #166

Merged
merged 27 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e42836b
ops: trigger a ci run
jasonkuhrt Dec 6, 2021
fbef87d
docs: fix doc typo
jasonkuhrt Dec 6, 2021
e078ec2
drop support for pre-3.x prisma
jasonkuhrt Dec 6, 2021
fe4c97d
refactor
jasonkuhrt Dec 6, 2021
002eb27
pc versions list
jasonkuhrt Dec 6, 2021
bb2660b
drop support for pre-3.5 prisma
jasonkuhrt Dec 6, 2021
d80447e
refactor: resolver "constraints"
jasonkuhrt Dec 8, 2021
14152af
lint
jasonkuhrt Dec 8, 2021
793836d
more deterministic test snapshots
jasonkuhrt Dec 8, 2021
b32f73c
feat: output gentime setting
jasonkuhrt Dec 8, 2021
e28c829
Merge branch 'main' into feat/output-gentime-setting
jasonkuhrt Dec 8, 2021
2923b80
Merge branch 'main' into feat/output-gentime-setting
jasonkuhrt Dec 8, 2021
6fc7dcc
format
jasonkuhrt Dec 8, 2021
870c840
split settings docs into own pages
jasonkuhrt Dec 13, 2021
a97faa7
improve doc and config schema
jasonkuhrt Dec 13, 2021
945fad8
fixes
jasonkuhrt Dec 13, 2021
dcc23f8
better fix, why did it work before :/
jasonkuhrt Dec 13, 2021
351752d
remove debug logs
jasonkuhrt Dec 14, 2021
5f3ce60
test
jasonkuhrt Dec 14, 2021
699bc83
add test showing config via psl file works
jasonkuhrt Dec 14, 2021
f08854e
fix tests
jasonkuhrt Dec 15, 2021
0c86054
remove unused code
jasonkuhrt Dec 15, 2021
b690624
update docs to mention gen block based config
jasonkuhrt Dec 15, 2021
255ea4c
output warning when using psl config
jasonkuhrt Dec 15, 2021
11c5e83
fix lint
jasonkuhrt Dec 15, 2021
f02391b
fix rendering
jasonkuhrt Dec 15, 2021
1c2a861
fix jsdoc
jasonkuhrt Dec 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
"./generator": {
"require": "./dist-cjs/entrypoints/generator.js",
"import": "./dist-esm/entrypoints/generator.js"
}
},
"./_/*": {
"require": "./dist-cjs/*.js",
"import": "./dist-esm/*.js"
jasonkuhrt marked this conversation as resolved.
Show resolved Hide resolved
}
},
"types": "./dist-cjs/entrypoints/main.d.ts",
"typesVersions": {
Expand Down
14 changes: 13 additions & 1 deletion src/cli/nexus-prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ generatorHandler({
},
// async required by interface
// eslint-disable-next-line
async onGenerate({ dmmf, otherGenerators }) {
async onGenerate({ dmmf, otherGenerators, generator }) {
const prismaClientGenerator = otherGenerators.find((g) => g.provider.value === 'prisma-client-js')

// TODO test showing this pretty error in action
Expand All @@ -39,6 +39,18 @@ generatorHandler({
)
}

// TODO If the user is using nexus-prisma.config.ts then that should be the preferred source for this configuration because it is type safe. Give them a warning so they are aware.
if (generator.isCustomOutput) {
if (!generator.output) {
throw new Error(`Failed to read the custom output path.`)
}
Gentime.changeSettings({
output: {
directory: generator.output.value,
},
})
}

// WARNING: Make sure this logic comes before `loadUserGentimeSettings` below
// otherwise we will overwrite the user's choice for this setting if they have set it.
Gentime.settings.change({
Expand Down
170 changes: 96 additions & 74 deletions src/generator/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,80 +10,6 @@ import { ModuleSpec } from './types'
const OUTPUT_SOURCE_DIR_ESM = getOutputSourceDir({ esm: true })
const OUTPUT_SOURCE_DIR_CJS = getOutputSourceDir({ esm: false })

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

d('start generateRuntime')

if (process.env.NP_DEBUG) {
fs.write('dmmf.json', dmmf)
}

const declarationSourceFile = ModelsGenerator.TS.createModuleSpec(dmmf, settings)

// ESM

const esmSourceFiles: ModuleSpec[] = [
ModelsGenerator.JS.createModuleSpec({
gentimeSettings: settings,
esm: true,
dmmf,
}),
declarationSourceFile,
]

fs.remove(OUTPUT_SOURCE_DIR_ESM)

esmSourceFiles.forEach((sf) => {
const filePath = Path.join(OUTPUT_SOURCE_DIR_ESM, sf.fileName)
fs.remove(filePath)
fs.write(filePath, sf.content)
d(`did write ${filePath}`)
})

// CJS

fs.remove(OUTPUT_SOURCE_DIR_CJS)

const cjsSourceFiles: ModuleSpec[] = [
ModelsGenerator.JS.createModuleSpec({
gentimeSettings: settings,
esm: false,
dmmf,
}),
declarationSourceFile,
]

cjsSourceFiles.forEach((sf) => {
const filePath = Path.join(OUTPUT_SOURCE_DIR_CJS, sf.fileName)
fs.remove(filePath)
fs.write(filePath, sf.content)
d(`did write ${filePath}`)
})

d(`done writing all emitted files`)
}

/** Transform the given DMMF into JS source code with accompanying TS declarations. */
export function generateRuntime(dmmf: DMMF.Document, settings: Gentime.Settings): ModuleSpec[] {
const sourceFiles: ModuleSpec[] = [
ModelsGenerator.JS.createModuleSpec({
gentimeSettings: settings,
esm: true,
dmmf,
}),
ModelsGenerator.JS.createModuleSpec({
gentimeSettings: settings,
esm: false,
dmmf,
}),
ModelsGenerator.TS.createModuleSpec(dmmf, settings),
]

return sourceFiles
}

/**
* Find the output source directory. When using the Yalc workflow some additional hacking around is required.
*
Expand Down Expand Up @@ -119,3 +45,99 @@ function getOutputSourceDir(params: { esm: boolean }): string {

return outputSourceDir
}

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

d('start generateRuntime')

if (process.env.NP_DEBUG) {
fs.write('dmmf.json', dmmf)
}

const declarationSourceFile = ModelsGenerator.TS.createModuleSpec(dmmf, settings)

if (settings.data.output) {
jasonkuhrt marked this conversation as resolved.
Show resolved Hide resolved
const outputDir = settings.data.output.directory

const sourceFiles = [
ModelsGenerator.JS.createModuleSpec({
gentimeSettings: settings,
esm: settings.data.output?.moduleSystem === 'esm',
dmmf,
}),
declarationSourceFile,
]

fs.remove(outputDir)

sourceFiles.forEach((sf) => {
const filePath = Path.join(outputDir, sf.fileName)
fs.remove(filePath)
fs.write(filePath, sf.content)
d(`did write ${filePath}`)
})
} else {
// ESM

const esmSourceFiles = [
ModelsGenerator.JS.createModuleSpec({
gentimeSettings: settings,
esm: true,
dmmf,
}),
declarationSourceFile,
]

fs.remove(OUTPUT_SOURCE_DIR_ESM)

esmSourceFiles.forEach((sf) => {
const filePath = Path.join(OUTPUT_SOURCE_DIR_ESM, sf.fileName)
fs.remove(filePath)
fs.write(filePath, sf.content)
d(`did write ${filePath}`)
})

// CJS

const cjsSourceFiles = [
ModelsGenerator.JS.createModuleSpec({
gentimeSettings: settings,
esm: false,
dmmf,
}),
declarationSourceFile,
]

fs.remove(OUTPUT_SOURCE_DIR_CJS)

cjsSourceFiles.forEach((sf) => {
const filePath = Path.join(OUTPUT_SOURCE_DIR_CJS, sf.fileName)
fs.remove(filePath)
fs.write(filePath, sf.content)
d(`did write ${filePath}`)
})
}

d(`done writing all emitted files`)
}

/** Transform the given DMMF into JS source code with accompanying TS declarations. */
export function generateRuntime(dmmf: DMMF.Document, settings: Gentime.Settings): ModuleSpec[] {
const sourceFiles: ModuleSpec[] = [
ModelsGenerator.JS.createModuleSpec({
gentimeSettings: settings,
esm: true,
dmmf,
}),
ModelsGenerator.JS.createModuleSpec({
gentimeSettings: settings,
esm: false,
dmmf,
}),
ModelsGenerator.TS.createModuleSpec(dmmf, settings),
]

return sourceFiles
}
48 changes: 47 additions & 1 deletion src/generator/gentime/settingsSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,60 @@ export namespace Gentime {
* @default '@prisma/client'
*/
prismaClientImportId?: string
/**
* Where to output the Nexus Prisma runtime and if it should use ESM or CJS module system.
*
* By default Nexus Prisma runtime is output into its installed node_modules location and supports both ESM and CJS.
*
* When you explicitly configure this setting Nexus Prisma only supports outputting ESM OR CJS, not both.
*
* Passing `string` is shorthand for `{ directory: string }`
*/
output?:
jasonkuhrt marked this conversation as resolved.
Show resolved Hide resolved
| string
| {
/**
* The directory to output the Nexus Prisma runtime into.
*
* If a relative path is given then it is considered relative to the Prisma Schema file.
*
* By default Nexus Prisma runtime is output into its installed node_modules location.
*/
directory: string
/**
* The module system to use for the output modules.
*
* @default 'cjs'
*/
moduleSystem?: 'esm' | 'cjs'
}
}

export type SettingsData = Setset.InferDataFromInput<SettingsInput>
export type SettingsData = Omit<Setset.InferDataFromInput<SettingsInput>, 'output'> & {
output?: {
jasonkuhrt marked this conversation as resolved.
Show resolved Hide resolved
directory: string
moduleSystem: 'esm' | 'cjs'
}
}

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

export const settings = Setset.create<SettingsInput, SettingsData>({
fields: {
output: {
shorthand: (directory) => ({ directory }),
fields: {
directory: {
fixup: (directory) => ({
// TODO if relative, make absolute, from PSL file
jasonkuhrt marked this conversation as resolved.
Show resolved Hide resolved
value: directory,
}),
},
moduleSystem: {
initial: () => 'cjs',
},
},
},
projectIdIntToGraphQL: {
initial: () => 'Int',
},
Expand Down
2 changes: 1 addition & 1 deletion src/generator/helpers/JSDocTemplates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DMMF } from '@prisma/client/runtime'
import dedent from 'dindist'
import { PrismaDocumentation } from '../../lib/prisma-documnetation'
import { PrismaDocumentation } from '../../lib/prisma-documentation'
import { Gentime } from '../gentime/settingsSingleton'

type JSDoc = string
Expand Down
83 changes: 0 additions & 83 deletions src/generator/helpers/constraints.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/generator/models/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Gentime } from '../gentime/settingsSingleton'
import { jsDocForEnum, jsDocForField, jsDocForModel } from '../helpers/JSDocTemplates'
import { ModuleSpec } from '../types'

export function createModuleSpec(dmmf: DMMF.Document, settings: Gentime.Settings): ModuleSpec {
export const createModuleSpec = (dmmf: DMMF.Document, settings: Gentime.Settings): ModuleSpec => {
return {
fileName: 'index.d.ts',
content: dedent`
Expand Down
Loading