Skip to content

Commit

Permalink
fix: endent introduces unexpected newlines
Browse files Browse the repository at this point in the history
closes #50
  • Loading branch information
jasonkuhrt committed May 20, 2021
1 parent 0e94cea commit 5da65e9
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 92 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -87,7 +87,6 @@
"dependencies": {
"@prisma/generator-helper": "^2.22.1",
"debug": "^4.3.1",
"endent": "^2.0.1",
"fs-jetpack": "^4.1.0",
"graphql-scalars": "^1.9.3",
"kleur": "^4.1.4",
Expand All @@ -96,6 +95,7 @@
"pluralize": "^8.0.0",
"semver": "^7.3.5",
"setset": "^0.0.7",
"ts-dedent": "^2.1.1",
"tslib": "^2.2.0"
},
"nodemonConfig": {
Expand Down
28 changes: 14 additions & 14 deletions src/generator/helpers/JSDocTemplates.ts
@@ -1,6 +1,6 @@
import { DMMF } from '@prisma/client/runtime'
import endent from 'endent'
import { upperFirst } from 'lodash'
import dedent from 'ts-dedent'

type JSDoc = string

Expand All @@ -15,7 +15,7 @@ type FieldModelParams = {

export function jsDocForEnum(enum_: DMMF.DatamodelEnum): JSDoc {
const enumDoc = enum_.documentation ? `* ${enum_.documentation}` : enumMissingDoc(enum_)
return endent`
return dedent`
/**
${enumIntro(enum_)}
*
Expand All @@ -27,13 +27,13 @@ export function jsDocForEnum(enum_: DMMF.DatamodelEnum): JSDoc {
}

function enumIntro(enum_: DMMF.DatamodelEnum): string {
return endent`
return dedent`
* Nexus \`enumType\` configuration based on the \`${enum_.name}\` enum in your Prisma schema.
`
}

function enumExample(enum_: DMMF.DatamodelEnum): string {
return endent`
return dedent`
* @example
*
* import { enumType } from 'nexus'
Expand All @@ -44,7 +44,7 @@ function enumExample(enum_: DMMF.DatamodelEnum): string {
}

function enumMissingDoc(enum_: DMMF.DatamodelEnum): string {
return endent`
return dedent`
${missingDocsIntro('enum')}
*
* \`\`\`prisma
Expand All @@ -64,7 +64,7 @@ function enumMissingDoc(enum_: DMMF.DatamodelEnum): string {

export function jsDocForModel(model: DMMF.Model): JSDoc {
const modelDoc = model.documentation ? `* ${model.documentation}` : modelMissingDoc(model)
return endent`
return dedent`
/**
${modelIntro(model)}
*
Expand All @@ -76,15 +76,15 @@ export function jsDocForModel(model: DMMF.Model): JSDoc {
}

function modelIntro(model: DMMF.Model): string {
return endent`
return dedent`
* Nexus \`objectType\` related configuration based on the \`${model.name}\` model in your Prisma schema.
`
}

function modelMissingDoc(model: DMMF.Model): string {
// TODO once https://stackoverflow.com/questions/61893953/how-to-escape-symbol-in-jsdoc-for-vscode
// is resolved then we can write better examples below like: id String @id
return endent`
return dedent`
${missingDocsIntro('model')}
*
* \`\`\`prisma
Expand All @@ -99,7 +99,7 @@ function modelMissingDoc(model: DMMF.Model): string {
}

function modelExample(model: DMMF.Model): string {
return endent`
return dedent`
* @example
*
* import { objectType } from 'nexus'
Expand All @@ -121,7 +121,7 @@ function modelExample(model: DMMF.Model): string {

export function jsDocForField({ field, model }: FieldModelParams): JSDoc {
const fieldDocs = field.documentation ? `* ${field.documentation}` : fieldMissingDoc({ field, model })
return endent`
return dedent`
/**
${fieldIntro({ field, model })}
*
Expand All @@ -133,13 +133,13 @@ export function jsDocForField({ field, model }: FieldModelParams): JSDoc {
}

function fieldIntro({ model, field }: FieldModelParams): string {
return endent`
return dedent`
* Nexus \`t.field\` related configuration based on the \`${model.name}.${field.name}\` field in your Prisma schema.
`
}

function fieldMissingDoc({ model, field }: FieldModelParams): string {
return endent`
return dedent`
${missingDocsIntro('model')}
* \`\`\`prisma
* model ${model.name} {
Expand All @@ -153,7 +153,7 @@ function fieldMissingDoc({ model, field }: FieldModelParams): string {
}

function fieldExample({ model, field }: FieldModelParams): string {
return endent`
return dedent`
* @example
*
* import { objectType } from 'nexus'
Expand All @@ -174,7 +174,7 @@ function fieldExample({ model, field }: FieldModelParams): string {
*/

function missingDocsIntro(kind: 'model' | 'enum' | 'field'): string {
return endent`
return dedent`
* ### ️⚠️ Missing Documentation for this ${upperFirst(kind)}
*
* Automatically ✨ enrich this JSDoc with information about your enum
Expand Down
32 changes: 16 additions & 16 deletions src/generator/models/declaration.ts
@@ -1,5 +1,5 @@
import { DMMF } from '@prisma/generator-helper'
import endent from 'endent'
import dedent from 'ts-dedent'
import { LiteralUnion } from 'type-fest'
import { StandardGraphQLScalarType, StandardgraphQLScalarTypes } from '../../helpers/graphql'
import { PrismaScalarType } from '../../helpers/prisma'
Expand All @@ -11,17 +11,17 @@ import { ModuleSpec } from '../types'
export function createModuleSpec(dmmf: DMMF.Document, settings: Gentime.Settings): ModuleSpec {
return {
fileName: 'index.d.ts',
content: endent`
${renderTypeScriptDeclarationForDocumentModels(dmmf, settings)}
`,
content: dedent`
${renderTypeScriptDeclarationForDocumentModels(dmmf, settings)}
`,
}
}

const NO_ENUMS_DEFINED_COMMENT = endent`
const NO_ENUMS_DEFINED_COMMENT = dedent`
// N/A –– You have not defined any models in your Prisma schema file.
`

const NO_MODELS_DEFINED_COMMENT = endent`
const NO_MODELS_DEFINED_COMMENT = dedent`
// N/A –– You have not defined any enums in your Prisma schema file.
`

Expand All @@ -32,7 +32,7 @@ export function renderTypeScriptDeclarationForDocumentModels(
const models = dmmf.datamodel.models
const enums = dmmf.datamodel.enums

return endent`
return dedent`
import * as Nexus from 'nexus'
import * as NexusCore from 'nexus/dist/core'
Expand Down Expand Up @@ -88,7 +88,7 @@ export function renderTypeScriptDeclarationForDocumentModels(
: models
.map((model) => {
const jsdoc = settings.data.docPropagation.JSDoc ? jsDocForModel(model) + '\n' : ''
return endent`
return dedent`
${jsdoc}export const ${model.name}: $Types.${model.name}
`
})
Expand All @@ -110,7 +110,7 @@ export function renderTypeScriptDeclarationForDocumentModels(
: enums
.map((enum_) => {
const jsdoc = settings.data.docPropagation.JSDoc ? jsDocForEnum(enum_) + '\n' : ''
return endent`
return dedent`
${jsdoc}export const ${enum_.name}: $Types.${enum_.name}
`
})
Expand All @@ -124,7 +124,7 @@ function renderTypeScriptDeclarationForEnum(enum_: DMMF.DatamodelEnum, settings:
const description = `${
enum_.documentation && settings.data.docPropagation.GraphQLDocs ? `'${enum_.documentation}'` : 'undefined'
}`
return endent`
return dedent`
${jsdoc}interface ${enum_.name} {
name: '${enum_.name}'
description: ${description}
Expand All @@ -138,7 +138,7 @@ function renderTypeScriptDeclarationForModel(model: DMMF.Model, settings: Gentim
const description = `${
model.documentation && settings.data.docPropagation.GraphQLDocs ? `'${model.documentation}'` : 'undefined'
}`
return endent`
return dedent`
${jsdoc}interface ${model.name} {
$name: '${model.name}'
$description: ${description}
Expand Down Expand Up @@ -166,7 +166,7 @@ function renderTypeScriptDeclarationForField({
const description = `${
field.documentation && settings.data.docPropagation.GraphQLDocs ? `string` : `undefined`
}`
return endent`
return dedent`
${jsdoc}${field.name}: {
/**
* The name of this field.
Expand Down Expand Up @@ -195,19 +195,19 @@ function renderNexusType2(field: DMMF.Field, settings: Gentime.Settings): string
const graphqlType = fieldTypeToGraphQLType(field, settings.data)

if (field.isList && field.isRequired) {
return endent`
return dedent`
NexusCore.ListDef<${graphqlType}> | NexusCore.NexusNonNullDef<${graphqlType}>
`
} else if (field.isList && !field.isRequired) {
return endent`
return dedent`
NexusCore.ListDef<${graphqlType}> | NexusCore.NexusNullDef<${graphqlType}>
`
} else if (field.isRequired) {
return endent`
return dedent`
NexusCore.NexusNonNullDef<'${graphqlType}'>
`
} else {
return endent`
return dedent`
NexusCore.NexusNullDef<'${graphqlType}'>
`
}
Expand Down
4 changes: 2 additions & 2 deletions src/generator/models/javascript.ts
@@ -1,8 +1,8 @@
import { DMMF } from '@prisma/client/runtime'
import endent from 'endent'
import { chain, lowerFirst } from 'lodash'
import * as Nexus from 'nexus'
import { NexusEnumTypeConfig, NexusListDef, NexusNonNullDef, NexusNullDef } from 'nexus/dist/core'
import dedent from 'ts-dedent'
import { MaybePromise, RecordUnknown, Resolver } from '../../helpers/utils'
import { Gentime } from '../gentime/settingsSingleton'
import {
Expand Down Expand Up @@ -49,7 +49,7 @@ export type Settings = {
export function createModuleSpec(gentimeSettings: Gentime.Settings): ModuleSpec {
return {
fileName: 'index.js',
content: endent`
content: dedent`
const { getPrismaClientDmmf } = require('../helpers/prisma')
const ModelsGenerator = require('../generator/models')
const { Runtime } = require('../generator/runtime/settingsSingleton')
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/prisma.ts
@@ -1,6 +1,6 @@
import { DMMF } from '@prisma/client/runtime'
import endent from 'endent'
import ono from 'ono'
import dedent from 'ts-dedent'
import { detectProjectPackageManager, renderRunBin } from '../lib/packageManager'
import { d } from './debugNexusPrisma'
import { GITHUB_NEW_DISCUSSION_LINK } from './errorMessages'
Expand All @@ -17,14 +17,14 @@ export const getPrismaClientDmmf = (importId = '@prisma/client'): DMMF.Document
maybeDmmf = require(importId).dmmf
} catch (error) {
// prettier-ignore
throw ono(error, endent`
throw ono(error, dedent`
Failed to get Prisma Client DMMF. An error occured while trying to import it from ${kleur.yellow(importId)}.
`)
}

if (maybeDmmf === undefined) {
// prettier-ignore
throw new Error(endent`
throw new Error(dedent`
Failed to get Prisma Client DMMF. It was imported from ${kleur.yellow(importId)} but was \`undefined\`.
This usually means that you need to run Prisma Client generation. Please run ${renderRunBin(detectProjectPackageManager(), `prisma generate`)}.
If that does not solve your problem, you can get community help by opening a discussion at ${kleur.yellow(GITHUB_NEW_DISCUSSION_LINK)}.
Expand All @@ -37,7 +37,7 @@ export const getPrismaClientDmmf = (importId = '@prisma/client'): DMMF.Document
const expectedFields = ['datamodel', 'schema', 'mappings'] as const

if (expectedFields.find((fieldName) => dmmf[fieldName] && typeof dmmf[fieldName] !== 'object')) {
throw new Error(endent`
throw new Error(dedent`
The DMMF imported from ${importId} appears to be invalid. Missing one/all of expected fields:
`)
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/peerDepValidator.ts
@@ -1,5 +1,5 @@
import endent from 'endent'
import * as Semver from 'semver'
import dedent from 'ts-dedent'
import { PackageJson } from 'type-fest'
import { d } from '../helpers/debugNexusPrisma'
import { detectProjectPackageManager, renderAddDeps } from './packageManager'
Expand Down Expand Up @@ -95,7 +95,7 @@ export function validatePeerDependencyRangeSatisfied({
kind: 'peer_dep_not_installed',
message: renderError(
// prettier-ignore
endent`
dedent`
${kleur.green(peerDependencyName)} is a peer dependency required by ${renderPackageJsonField(requireer,'name')}. But you have not installed it into this project yet. Please run \`${kleur.green(renderAddDeps(detectProjectPackageManager(),[peerDependencyName]))}\`.
`
),
Expand Down
8 changes: 4 additions & 4 deletions tests/__helpers__/testers.ts
@@ -1,5 +1,5 @@
import * as PrismaSDK from '@prisma/sdk'
import endent from 'endent'
import dedent from 'dedent'
import execa from 'execa'
import * as fs from 'fs-jetpack'
import { DocumentNode, execute, printSchema } from 'graphql'
Expand Down Expand Up @@ -120,7 +120,7 @@ export function createPrismaSchema({
clientOutput?: string
nexusPrisma?: boolean
}): string {
return endent`
return dedent`
datasource db {
provider = "${datasourceProvider.provider}"
url = ${datasourceProvider.url}
Expand All @@ -132,7 +132,7 @@ export function createPrismaSchema({
${
nexusPrisma
? endent`
? dedent`
generator nexusPrisma {
provider = "nexus-prisma"
}
Expand Down Expand Up @@ -216,7 +216,7 @@ function prepareGraphQLSDLForSnapshot(sdl: string): string {

function stripNexusQueryOk(sdl: string): string {
return sdl.replace(
endent`
dedent`
type Query {
ok: Boolean!
}
Expand Down

0 comments on commit 5da65e9

Please sign in to comment.