Skip to content

Commit

Permalink
fix: endent via dedent introduces unexpected newlines on Windows (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored May 21, 2021
1 parent 0e94cea commit 2447f56
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 82 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"dependencies": {
"@prisma/generator-helper": "^2.22.1",
"debug": "^4.3.1",
"endent": "^2.0.1",
"dindist": "^1.0.2",
"fs-jetpack": "^4.1.0",
"graphql-scalars": "^1.9.3",
"kleur": "^4.1.4",
Expand Down
28 changes: 14 additions & 14 deletions src/generator/helpers/JSDocTemplates.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DMMF } from '@prisma/client/runtime'
import endent from 'endent'
import dedent from 'dindist'
import { upperFirst } from 'lodash'

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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DMMF } from '@prisma/generator-helper'
import endent from 'endent'
import dedent from 'dindist'
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DMMF } from '@prisma/client/runtime'
import endent from 'endent'
import dedent from 'dindist'
import { chain, lowerFirst } from 'lodash'
import * as Nexus from 'nexus'
import { NexusEnumTypeConfig, NexusListDef, NexusNonNullDef, NexusNullDef } from 'nexus/dist/core'
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DMMF } from '@prisma/client/runtime'
import endent from 'endent'
import dedent from 'dindist'
import ono from 'ono'
import { detectProjectPackageManager, renderRunBin } from '../lib/packageManager'
import { d } from './debugNexusPrisma'
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import endent from 'endent'
import dedent from 'dindist'
import * as Semver from 'semver'
import { PackageJson } from 'type-fest'
import { d } from '../helpers/debugNexusPrisma'
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as PrismaSDK from '@prisma/sdk'
import endent from 'endent'
import dedent from 'dindist'
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
14 changes: 7 additions & 7 deletions tests/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debug from 'debug'
import endent from 'endent'
import dedent from 'dindist'
import * as Execa from 'execa'
import { gql } from 'graphql-request'
import stripAnsi from 'strip-ansi'
Expand Down Expand Up @@ -114,7 +114,7 @@ it('When bundled custom scalars are used the project type checks and generates e
{
filePath: `prisma/schema.prisma`,
content: createPrismaSchema({
content: endent`
content: dedent`
model Foo {
id String @id
someJsonField Json
Expand All @@ -139,7 +139,7 @@ it('When bundled custom scalars are used the project type checks and generates e
},
{
filePath: `prisma/seed.ts`,
content: endent/*ts*/ `
content: dedent/*ts*/ `
import { PrismaClient } from '@prisma/client'
main()
Expand Down Expand Up @@ -168,7 +168,7 @@ it('When bundled custom scalars are used the project type checks and generates e
},
{
filePath: `src/schema.ts`,
content: endent/*ts*/ `
content: dedent/*ts*/ `
require('dotenv').config()
import { makeSchema, objectType, enumType, queryType } from 'nexus'
Expand Down Expand Up @@ -227,7 +227,7 @@ it('When bundled custom scalars are used the project type checks and generates e
},
{
filePath: `src/context.ts`,
content: endent/*ts*/ `
content: dedent/*ts*/ `
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
Expand All @@ -248,7 +248,7 @@ it('When bundled custom scalars are used the project type checks and generates e
},
{
filePath: `src/server.ts`,
content: endent/*ts*/ `
content: dedent/*ts*/ `
require('dotenv').config()
import { ApolloServer } from 'apollo-server'
Expand All @@ -267,7 +267,7 @@ it('When bundled custom scalars are used the project type checks and generates e
},
{
filePath: `.env`,
content: endent`
content: dedent`
DB_URL="postgres://bcnfshogmxsukp:e31b6ddc8b9d85f8964b6671e4b578c58f0d13e15f637513207d44268eabc950@ec2-54-196-33-23.compute-1.amazonaws.com:5432/d17vadgam0dtao?schema=${
process.env.E2E_DB_SCHEMA ?? 'local'
}"
Expand Down
Loading

0 comments on commit 2447f56

Please sign in to comment.