Skip to content

Commit

Permalink
remove getBuiltKeystoneConfiguration from createSystem, move to scrip…
Browse files Browse the repository at this point in the history
…ts/utils
  • Loading branch information
dcousens committed Jun 25, 2024
1 parent f8fd744 commit 1c72c66
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 76 deletions.
9 changes: 4 additions & 5 deletions packages/core/src/lib/createSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ import { resolveDefaults } from './defaults'
import { createAdminMeta } from './create-admin-meta'
import { createGraphQLSchema } from './createGraphQLSchema'
import { createContext } from './context/createContext'
import { initialiseLists, type InitialisedList } from './core/initialise-lists'
import {
type InitialisedList,
initialiseLists,
} from './core/initialise-lists'

// 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 require(getBuiltKeystoneConfigurationPath(cwd)).default
}

function posixify (s: string) {
return s.split(path.sep).join('/')
}
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import nextBuild from 'next/dist/build'
import { generateAdminUI } from '../admin-ui/system'
import {
createSystem,
getBuiltKeystoneConfiguration
} from '../lib/createSystem'
import {
generateArtifacts,
Expand All @@ -13,16 +12,16 @@ import {
} from '../artifacts'
import { getEsbuildConfig } from '../lib/esbuild'
import type { Flags } from './cli'
import { importBuiltKeystoneConfiguration } from './utils'

export async function build (
cwd: string,
{ frozen, prisma, ui }: Pick<Flags, 'frozen' | 'prisma' | 'ui'>
) {
// TODO: should this happen if frozen?
await esbuild.build(getEsbuildConfig(cwd))

// TODO: this cannot be changed for now, circular dependency with getSystemPaths, getEsbuildConfig
const system = createSystem(getBuiltKeystoneConfiguration(cwd))

const system = createSystem(await importBuiltKeystoneConfiguration(cwd))
if (prisma) {
if (frozen) {
await validateArtifacts(cwd, system)
Expand Down
34 changes: 15 additions & 19 deletions packages/core/src/scripts/dev.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
import fsp from 'node:fs/promises'
import path from 'node:path'
import type { ListenOptions } from 'node:net'
import url from 'node:url'
import { createServer } from 'node:http'
import { type ListenOptions } from 'node:net'

import chalk from 'chalk'
import next from 'next'
import esbuild, { type BuildResult } from 'esbuild'
import express from 'express'
import next from 'next'
import { printSchema } from 'graphql'
import esbuild, { type BuildResult } from 'esbuild'
import { createDatabase } from '@prisma/internals'

import { generateAdminUI } from '../admin-ui/system'
import { withMigrate } from '../lib/migrations'
import { confirmPrompt } from '../lib/prompts'
import {
createSystem,
getBuiltKeystoneConfiguration,
} from '../lib/createSystem'
import { createSystem, } from '../lib/createSystem'
import { getEsbuildConfig } from '../lib/esbuild'
import { createExpressServer } from '../lib/createExpressServer'
import { createAdminUIMiddlewareWithNextApp } from '../lib/createAdminUIMiddleware'
import { runTelemetry } from '../lib/telemetry'
import {
getFormattedGraphQLSchema,
generateArtifacts,
generatePrismaClient,
generateTypes,
generatePrismaClient
getFormattedGraphQLSchema,
} from '../artifacts'
import {
type KeystoneConfig
} from '../types'
import { type KeystoneConfig } from '../types'
import { printPrismaSchema } from '../lib/core/prisma-schema-printer'
import { pkgDir } from '../pkg-dir'
import { ExitError } from './utils'
import {
type Flags
} from './cli'
ExitError,
importBuiltKeystoneConfiguration,
} from './utils'
import { type Flags } from './cli'

const devLoadingHTMLFilepath = path.join(pkgDir, 'static', 'dev-loading.html')

Expand Down Expand Up @@ -129,15 +125,15 @@ export async function dev (

if (exit) throw new ExitError(1)
}
// TODO: this cannot be changed for now, circular dependency with getSystemPaths, getEsbuildConfig

const app = server ? express() : null
const httpServer = app ? createServer(app) : null
let expressServer: express.Express | null = null
let hasAddedAdminUIMiddleware = false
const isReady = () => !server || (expressServer !== null && hasAddedAdminUIMiddleware)

const initKeystone = async () => {
const configWithExtendHttp = getBuiltKeystoneConfiguration(cwd)
const configWithExtendHttp = await importBuiltKeystoneConfiguration(cwd)
const {
system,
context,
Expand Down Expand Up @@ -307,7 +303,7 @@ export async function dev (
delete require.cache[resolved]
}

const newConfigWithHttp = getBuiltKeystoneConfiguration(cwd)
const newConfigWithHttp = await importBuiltKeystoneConfiguration(cwd)
const newSystem = createSystem(stripExtendHttpServer(newConfigWithHttp))

if (prisma) {
Expand Down Expand Up @@ -369,7 +365,7 @@ export async function dev (
})

if (app && httpServer) {
const config = getBuiltKeystoneConfiguration(cwd)
const config = await importBuiltKeystoneConfiguration(cwd)

app.use('/__keystone/dev/status', (req, res) => {
res.status(isReady() ? 200 : 501).end()
Expand Down
19 changes: 8 additions & 11 deletions packages/core/src/scripts/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import chalk from 'chalk'
import esbuild from 'esbuild'
import fse from 'fs-extra'

import {
createSystem,
getBuiltKeystoneConfiguration
} from '../lib/createSystem'
import { createSystem } from '../lib/createSystem'
import { getEsbuildConfig } from '../lib/esbuild'
import { withMigrate } from '../lib/migrations'
import {
Expand All @@ -23,7 +20,10 @@ import {
validateArtifacts,
} from '../artifacts'
import { type Flags } from './cli'
import { ExitError } from './utils'
import {
ExitError,
importBuiltKeystoneConfiguration,
} from './utils'

export async function spawnPrisma (cwd: string, system: {
config: {
Expand Down Expand Up @@ -58,9 +58,7 @@ export async function migrateCreate (
) {
await esbuild.build(getEsbuildConfig(cwd))

// TODO: this cannot be changed for now, circular dependency with getSystemPaths, getEsbuildConfig
const system = createSystem(getBuiltKeystoneConfiguration(cwd))

const system = createSystem(await importBuiltKeystoneConfiguration(cwd))
if (frozen) {
await validateArtifacts(cwd, system)
console.log('✨ GraphQL and Prisma schemas are up to date')
Expand Down Expand Up @@ -129,11 +127,10 @@ export async function migrateApply (
cwd: string,
{ frozen }: Pick<Flags, 'frozen'>
) {
// TODO: should this happen if frozen?
await esbuild.build(getEsbuildConfig(cwd))

// TODO: this cannot be changed for now, circular dependency with getSystemPaths, getEsbuildConfig
const system = createSystem(getBuiltKeystoneConfiguration(cwd))

const system = createSystem(await importBuiltKeystoneConfiguration(cwd))
if (frozen) {
await validateArtifacts(cwd, system)
console.log('✨ GraphQL and Prisma schemas are up to date')
Expand Down
27 changes: 7 additions & 20 deletions packages/core/src/scripts/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import fs from 'node:fs/promises'
import { spawn } from 'node:child_process'

import { createSystem } from '../lib/createSystem'
import { validateArtifacts } from '../artifacts'
import {
createSystem,
getBuiltKeystoneConfigurationPath,
getBuiltKeystoneConfiguration,
} from '../lib/createSystem'
import {
validateArtifacts,
} from '../artifacts'
import { ExitError } from './utils'
ExitError,
importBuiltKeystoneConfiguration,
} from './utils'

async function spawnPrisma3 (cwd: string, system: {
config: {
Expand All @@ -36,18 +32,9 @@ async function spawnPrisma3 (cwd: string, system: {
}

export async function prisma (cwd: string, args: string[], frozen: boolean) {
// TODO: this cannot be changed for now, circular dependency with getSystemPaths, getEsbuildConfig
const builtConfigPath = getBuiltKeystoneConfigurationPath(cwd)

// this is the compiled version of the configuration which was generated during the build step
if (!(await fs.stat(builtConfigPath).catch(() => null))) {
console.error('🚨 keystone build must be run before running keystone prisma')
throw new ExitError(1)
}
// TODO: should build unless --frozen?

// TODO: this cannot be changed for now, circular dependency with getSystemPaths, getEsbuildConfig
const config = getBuiltKeystoneConfiguration(cwd)
const system = createSystem(config)
const system = createSystem(await importBuiltKeystoneConfiguration(cwd))

await validateArtifacts(cwd, system)
console.log('✨ GraphQL and Prisma schemas are up to date')
Expand Down
21 changes: 4 additions & 17 deletions packages/core/src/scripts/start.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import fs from 'node:fs/promises'
import next from 'next'
import {
createSystem,
getBuiltKeystoneConfigurationPath,
getBuiltKeystoneConfiguration,
} from '../lib/createSystem'

import { createSystem } from '../lib/createSystem'
import { createExpressServer } from '../lib/createExpressServer'
import { createAdminUIMiddlewareWithNextApp } from '../lib/createAdminUIMiddleware'
import { withMigrate } from '../lib/migrations'
import { ExitError } from './utils'
import { importBuiltKeystoneConfiguration } from './utils'
import { type Flags } from './cli'

export async function start (
Expand All @@ -17,16 +13,7 @@ export async function start (
) {
console.log('✨ Starting Keystone')

// TODO: this cannot be changed for now, circular dependency with getSystemPaths, getEsbuildConfig
const builtConfigPath = getBuiltKeystoneConfigurationPath(cwd)

// this is the compiled version of the configuration which was generated during the build step
if (!(await fs.stat(builtConfigPath).catch(() => null))) {
console.error('🚨 keystone build must be run before running keystone start')
throw new ExitError(1)
}

const system = createSystem(getBuiltKeystoneConfiguration(cwd))
const system = createSystem(await importBuiltKeystoneConfiguration(cwd))
const paths = system.getPaths(cwd)

if (withMigrations) {
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { getBuiltKeystoneConfigurationPath } from '../lib/createSystem'

export class ExitError extends Error {
code: number
constructor (code: number) {
super(`The process exited with Error ${code}`)
this.code = code
}
}

// TODO: this cannot be changed for now, circular dependency with getSystemPaths, getEsbuildConfig
export async function importBuiltKeystoneConfiguration (cwd: string) {
try {
return require(getBuiltKeystoneConfigurationPath(cwd)).default
} catch (e) {
console.error('🚨 keystone build has not been run')
throw new ExitError(1)
}
}

0 comments on commit 1c72c66

Please sign in to comment.