Skip to content

Commit

Permalink
Remove deprecated @keystone-6/core/system from package (#9085)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens authored Apr 8, 2024
1 parent b405f1b commit 70ffa67
Show file tree
Hide file tree
Showing 37 changed files with 1,658 additions and 1,816 deletions.
5 changes: 5 additions & 0 deletions .changeset/less-config-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': major
---

Removes `AdminUIConfig`, `DatabaseConfig`, `GraphQLConfig` and `ServerConfig` from `@keystone-6/core/types`, and some unused types - instead use `KeystoneConfig['ui']`, `KeystoneConfig['db']` and so on
5 changes: 5 additions & 0 deletions .changeset/no-system-export.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': major
---

Removes `@keystone-6/core/system` from exports
1 change: 0 additions & 1 deletion examples/custom-output-paths/my-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ type ResolvedPostCreateInput = {
content?: import('./node_modules/.myprisma/client').Prisma.PostCreateInput['content']
publishDate?: import('./node_modules/.myprisma/client').Prisma.PostCreateInput['publishDate']
}

type ResolvedPostUpdateInput = {
id?: undefined
title?: import('./node_modules/.myprisma/client').Prisma.PostUpdateInput['title']
Expand Down
3 changes: 0 additions & 3 deletions examples/extend-graphql-schema-nexus/keystone-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ type ResolvedPostCreateInput = {
publishDate?: import('./node_modules/.myprisma/client').Prisma.PostCreateInput['publishDate']
author?: import('./node_modules/.myprisma/client').Prisma.PostCreateInput['author']
}

type ResolvedPostUpdateInput = {
id?: undefined
title?: import('./node_modules/.myprisma/client').Prisma.PostUpdateInput['title']
Expand All @@ -229,13 +228,11 @@ type ResolvedPostUpdateInput = {
publishDate?: import('./node_modules/.myprisma/client').Prisma.PostUpdateInput['publishDate']
author?: import('./node_modules/.myprisma/client').Prisma.PostUpdateInput['author']
}

type ResolvedAuthorCreateInput = {
id?: import('./node_modules/.myprisma/client').Prisma.AuthorCreateInput['id']
name?: import('./node_modules/.myprisma/client').Prisma.AuthorCreateInput['name']
posts?: import('./node_modules/.myprisma/client').Prisma.AuthorCreateInput['posts']
}

type ResolvedAuthorUpdateInput = {
id?: undefined
name?: import('./node_modules/.myprisma/client').Prisma.AuthorUpdateInput['name']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { type Context } from '.keystone/types'
import * as PrismaModule from '.myprisma/client'

// Making sure multiple prisma clients are not created during hot reloading
export const keystoneContext: Context =
(globalThis as any).keystoneContext || getContext(config, PrismaModule)
export const keystoneContext: Context = (globalThis as any).keystoneContext ?? getContext(config, PrismaModule)

if (process.env.NODE_ENV !== 'production') (globalThis as any).keystoneContext = keystoneContext
if (process.env.NODE_ENV !== 'production') {
(globalThis as any).keystoneContext = keystoneContext
}
5 changes: 0 additions & 5 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
"module": "./access/dist/keystone-6-core-access.esm.js",
"default": "./access/dist/keystone-6-core-access.cjs.js"
},
"./system": {
"module": "./system/dist/keystone-6-core-system.esm.js",
"default": "./system/dist/keystone-6-core-system.cjs.js"
},
"./context": {
"module": "./context/dist/keystone-6-core-context.esm.js",
"default": "./context/dist/keystone-6-core-context.cjs.js"
Expand Down Expand Up @@ -265,7 +261,6 @@
"entrypoints": [
"index.ts",
"access.ts",
"system.ts",
"___internal-do-not-use-will-break-in-patch/artifacts.ts",
"___internal-do-not-use-will-break-in-patch/admin-ui/pages/*/index.tsx",
"___internal-do-not-use-will-break-in-patch/admin-ui/id-field-view.tsx",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
export {
validatePrismaAndGraphQLSchemas,
generateTypescriptTypesAndPrisma,
generatePrismaAndGraphQLSchemas,
getCommittedArtifacts,
createExpressServer,
} from '../lib/createExpressServer'
export {
createSystem,
} from '../lib/createSystem'
export {
pushPrismaSchemaToDatabase,
} from '../lib/migrations'
export {
generateArtifacts,
getArtifacts,
} from '../artifacts'
4 changes: 2 additions & 2 deletions packages/core/src/admin-ui/system/generateAdminUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { type GraphQLSchema } from 'graphql'
import { type Entry, walk as _walk } from '@nodelib/fs.walk'
import {
type AdminFileToWrite,
type KeystoneConfig
type __ResolvedKeystoneConfig
} from '../../types'
import { writeAdminFiles } from '../templates'
import { type AdminMetaRootVal } from '../../lib/create-admin-meta'
Expand Down Expand Up @@ -66,7 +66,7 @@ export async function writeAdminFile (file: AdminFileToWrite, projectAdminPath:
const pageExtensions = new Set(['.js', '.jsx', '.ts', '.tsx'])

export async function generateAdminUI (
config: KeystoneConfig,
config: __ResolvedKeystoneConfig,
graphQLSchema: GraphQLSchema,
adminMeta: AdminMetaRootVal,
projectAdminPath: string,
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/admin-ui/templates/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as Path from 'path'
import type { GraphQLSchema } from 'graphql'
import type { KeystoneConfig, AdminFileToWrite } from '../../types'
import {
type AdminFileToWrite,
type __ResolvedKeystoneConfig
} from '../../types'
import type { AdminMetaRootVal } from '../../lib/create-admin-meta'
import { appTemplate } from './app'
import { homeTemplate } from './home'
Expand All @@ -13,7 +16,7 @@ import { nextConfigTemplate } from './next-config'
const pkgDir = Path.dirname(require.resolve('@keystone-6/core/package.json'))

export const writeAdminFiles = (
config: KeystoneConfig,
config: __ResolvedKeystoneConfig,
graphQLSchema: GraphQLSchema,
adminMeta: AdminMetaRootVal,
configFileExists: boolean
Expand Down
161 changes: 35 additions & 126 deletions packages/core/src/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { createRequire } from 'node:module'
import { printSchema, GraphQLSchema } from 'graphql'
import { getGenerators, formatSchema } from '@prisma/internals'
import type { KeystoneConfig } from './types'
import { printGeneratedTypes } from './lib/typescript-schema-printer'
import { ExitError } from './scripts/utils'
import { type __ResolvedKeystoneConfig } from './types'
import { initialiseLists } from './lib/core/initialise-lists'
import {
type System,
getSystemPaths
} from './lib/createSystem'
import { printPrismaSchema } from './lib/core/prisma-schema-printer'
import { initConfig } from './system'
import { printGeneratedTypes } from './lib/typescript-schema-printer'

export function getFormattedGraphQLSchema (schema: string) {
return (
Expand All @@ -19,41 +21,6 @@ export function getFormattedGraphQLSchema (schema: string) {
)
}

export async function getCommittedArtifacts (config: KeystoneConfig, graphQLSchema: GraphQLSchema) {
const lists = initialiseLists(config)
const prismaSchema = printPrismaSchema(config, lists)
return {
graphql: getFormattedGraphQLSchema(printSchema(graphQLSchema)),
prisma: await formatPrismaSchema(prismaSchema),
}
}

let hasEnsuredBinariesExist = false
async function ensurePrismaBinariesExist () {
// ensureBinariesExist does a bunch of slightly expensive things
// so if we can avoid running it a bunch in tests, that's ideal
if (hasEnsuredBinariesExist) return
// we're resolving @prisma/engines from @prisma/internals
// because we don't want to depend on @prisma/engines
// since its version includes a commit hash from https://github.com/prisma/prisma-engines
// and we just want to use whatever version @prisma/internals is using
// also note we use an exact version of @prisma/internals
// so if @prisma/internals suddenly stops depending on @prisma/engines
// that won't break a released version of Keystone
// also, we're not just directly importing @prisma/engines
// since stricter package managers(e.g. pnpm, Yarn Berry)
// don't allow importing packages that aren't explicitly depended on
const requireFromPrismaSdk = createRequire(require.resolve('@prisma/internals'))
const prismaEngines = requireFromPrismaSdk('@prisma/engines')
await prismaEngines.ensureBinariesExist()
hasEnsuredBinariesExist = true
}

async function formatPrismaSchema (schema: string) {
await ensurePrismaBinariesExist()
return formatSchema({ schema })
}

async function readFileButReturnNothingIfDoesNotExist (path: string) {
try {
return await fs.readFile(path, 'utf8')
Expand All @@ -65,60 +32,9 @@ async function readFileButReturnNothingIfDoesNotExist (path: string) {
}
}

// TODO: this cannot be changed for now, circular dependency with getSystemPaths, getEsbuildConfig
export function getBuiltKeystoneConfigurationPath (cwd: string) {
return path.join(cwd, '.keystone/config.js')
}

export function getBuiltKeystoneConfiguration (cwd: string) {
return initConfig(require(getBuiltKeystoneConfigurationPath(cwd)).default)
}

function posixify (s: string) {
return s.split(path.sep).join('/')
}

export function getSystemPaths (cwd: string, config: KeystoneConfig) {
const prismaClientPath = config.db.prismaClientPath === '@prisma/client'
? null
: config.db.prismaClientPath
? path.join(cwd, config.db.prismaClientPath)
: null

const builtTypesPath = config.types?.path
? path.join(cwd, config.types.path) // TODO: enforce initConfig before getSystemPaths
: path.join(cwd, 'node_modules/.keystone/types.ts')

const builtPrismaPath = config.db?.prismaSchemaPath
? path.join(cwd, config.db.prismaSchemaPath) // TODO: enforce initConfig before getSystemPaths
: path.join(cwd, 'schema.prisma')

const relativePrismaPath = prismaClientPath
? `./${posixify(path.relative(path.dirname(builtTypesPath), prismaClientPath))}`
: '@prisma/client'

const builtGraphqlPath = config.graphql?.schemaPath
? path.join(cwd, config.graphql.schemaPath) // TODO: enforce initConfig before getSystemPaths
: path.join(cwd, 'schema.graphql')

return {
config: getBuiltKeystoneConfigurationPath(cwd),
admin: path.join(cwd, '.keystone/admin'),
prisma: prismaClientPath ?? '@prisma/client',
types: {
relativePrismaPath,
},
schema: {
types: builtTypesPath,
prisma: builtPrismaPath,
graphql: builtGraphqlPath,
},
}
}

export async function validatePrismaAndGraphQLSchemas (
async function validatePrismaAndGraphQLSchemas (
cwd: string,
config: KeystoneConfig,
config: __ResolvedKeystoneConfig,
graphQLSchema: GraphQLSchema
) {
const paths = getSystemPaths(cwd, config)
Expand Down Expand Up @@ -150,51 +66,40 @@ export async function validatePrismaAndGraphQLSchemas (
throw new ExitError(1)
}

export async function generatePrismaAndGraphQLSchemas (
cwd: string,
config: KeystoneConfig,
graphQLSchema: GraphQLSchema
) {
const paths = getSystemPaths(cwd, config)
const artifacts = await getCommittedArtifacts(config, graphQLSchema)
async function getCommittedArtifacts (config: __ResolvedKeystoneConfig, graphQLSchema: GraphQLSchema) {
const lists = initialiseLists(config)
const prismaSchema = printPrismaSchema(config, lists)
return {
graphql: getFormattedGraphQLSchema(printSchema(graphQLSchema)),
prisma: await formatSchema({ schema: prismaSchema }),
}
}

export async function getArtifacts (system: System) {
return await getCommittedArtifacts(system.config, system.graphQLSchema)
}

export async function generateArtifacts (cwd: string, system: System) {
const paths = getSystemPaths(cwd, system.config)
const artifacts = await getCommittedArtifacts(system.config, system.graphQLSchema)

await fs.writeFile(paths.schema.graphql, artifacts.graphql)
await fs.writeFile(paths.schema.prisma, artifacts.prisma)
return artifacts
}

export async function generateTypescriptTypes (
cwd: string,
config: KeystoneConfig,
graphQLSchema: GraphQLSchema
) {
const lists = initialiseLists(config)
const paths = getSystemPaths(cwd, config)
const schema = printGeneratedTypes(paths.types.relativePrismaPath, graphQLSchema, lists)

export async function generateTypes (cwd: string, system: System) {
const paths = getSystemPaths(cwd, system.config)
const schema = printGeneratedTypes(paths.types.relativePrismaPath, system.graphQLSchema, system.lists)
await fs.mkdir(path.dirname(paths.schema.types), { recursive: true })
await fs.writeFile(paths.schema.types, schema)
}

export async function generateTypescriptTypesAndPrisma (
cwd: string,
config: KeystoneConfig,
graphQLSchema: GraphQLSchema
) {
const paths = getSystemPaths(cwd, config)
const dataProxy = config.db.url.startsWith('prisma:')
if (dataProxy === true) {
console.log('✨ Generating Prisma Client (data proxy)')
}
await Promise.all([
generatePrismaClient(paths.schema.prisma, dataProxy),
generateTypescriptTypes(cwd, config, graphQLSchema),
])
}

async function generatePrismaClient (prismaSchemaPath: string, dataProxy: boolean) {
export async function generatePrismaClient (cwd: string, system: System) {
const paths = getSystemPaths(cwd, system.config)
const dataProxy = system.config.db.url.startsWith('prisma:')
const generators = await getGenerators({
schemaPath: prismaSchemaPath,
schemaPath: paths.schema.prisma,
dataProxy,
})

Expand All @@ -216,3 +121,7 @@ async function generatePrismaClient (prismaSchemaPath: string, dataProxy: boolea
})
)
}

export async function validateArtifacts (cwd: string, system: System) {
return await validatePrismaAndGraphQLSchemas(cwd, system.config, system.graphQLSchema)
}
3 changes: 1 addition & 2 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import {
type KeystoneConfig,
type KeystoneContext
} from './types'
import { initConfig } from './system'
import { createSystem } from './lib/createSystem'

export function getContext<TypeInfo extends BaseKeystoneTypeInfo> (
config: KeystoneConfig<TypeInfo>,
PrismaModule: unknown
): KeystoneContext<TypeInfo> {
const system = createSystem(initConfig(config))
const system = createSystem(config)
const { context } = system.getKeystone(PrismaModule)
return context
}
7 changes: 5 additions & 2 deletions packages/core/src/lib/assets/createFilesContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { randomBytes } from 'node:crypto'

import type { KeystoneConfig, FilesContext } from '../../types'
import {
type FilesContext,
type __ResolvedKeystoneConfig,
} from '../../types'
import { localFileAssetsAPI } from './local'
import { s3FileAssetsAPI } from './s3'
import type { FileAdapter } from './types'
Expand All @@ -18,7 +21,7 @@ function defaultTransformName (path: string) {
return `${urlSafeName}-${id}`
}

export function createFilesContext (config: KeystoneConfig): FilesContext {
export function createFilesContext (config: __ResolvedKeystoneConfig): FilesContext {
const adaptersMap = new Map<string, FileAdapter>()

for (const [storageKey, storageConfig] of Object.entries(config.storage || {})) {
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/lib/assets/createImagesContext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { randomBytes } from 'node:crypto'
import imageSize from 'image-size'

import type { KeystoneConfig, ImagesContext } from '../../types'
import {
type ImagesContext,
type __ResolvedKeystoneConfig,
} from '../../types'
import type { ImageAdapter } from './types'
import { localImageAssetsAPI } from './local'
import { s3ImageAssetsAPI } from './s3'
Expand Down Expand Up @@ -30,7 +33,7 @@ async function getImageMetadataFromBuffer (buffer: Buffer) {
return { width, height, filesize: buffer.length, extension }
}

export function createImagesContext (config: KeystoneConfig): ImagesContext {
export function createImagesContext (config: __ResolvedKeystoneConfig): ImagesContext {
const imageAssetsAPIs = new Map<string, ImageAdapter>()
for (const [storageKey, storageConfig] of Object.entries(config.storage || {})) {
if (storageConfig.type === 'image') {
Expand Down
Loading

0 comments on commit 70ffa67

Please sign in to comment.