From 1c72c6684913e6584642fe023e7be1329eae628b Mon Sep 17 00:00:00 2001 From: Daniel Cousens <413395+dcousens@users.noreply.github.com> Date: Tue, 25 Jun 2024 12:26:31 +1000 Subject: [PATCH] remove getBuiltKeystoneConfiguration from createSystem, move to scripts/utils --- packages/core/src/lib/createSystem.ts | 9 ++++--- packages/core/src/scripts/build.ts | 7 +++--- packages/core/src/scripts/dev.ts | 34 ++++++++++++--------------- packages/core/src/scripts/migrate.ts | 19 +++++++-------- packages/core/src/scripts/prisma.ts | 27 ++++++--------------- packages/core/src/scripts/start.ts | 21 ++++------------- packages/core/src/scripts/utils.ts | 12 ++++++++++ 7 files changed, 53 insertions(+), 76 deletions(-) diff --git a/packages/core/src/lib/createSystem.ts b/packages/core/src/lib/createSystem.ts index 31f8f3137e5..6dcfae09eff 100644 --- a/packages/core/src/lib/createSystem.ts +++ b/packages/core/src/lib/createSystem.ts @@ -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('/') } diff --git a/packages/core/src/scripts/build.ts b/packages/core/src/scripts/build.ts index 53b77770c03..4c45c1ae1e0 100644 --- a/packages/core/src/scripts/build.ts +++ b/packages/core/src/scripts/build.ts @@ -3,7 +3,6 @@ import nextBuild from 'next/dist/build' import { generateAdminUI } from '../admin-ui/system' import { createSystem, - getBuiltKeystoneConfiguration } from '../lib/createSystem' import { generateArtifacts, @@ -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 ) { + // 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) diff --git a/packages/core/src/scripts/dev.ts b/packages/core/src/scripts/dev.ts index 8e96fa33166..b735999ebf5 100644 --- a/packages/core/src/scripts/dev.ts +++ b/packages/core/src/scripts/dev.ts @@ -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') @@ -129,7 +125,7 @@ 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 @@ -137,7 +133,7 @@ export async function dev ( const isReady = () => !server || (expressServer !== null && hasAddedAdminUIMiddleware) const initKeystone = async () => { - const configWithExtendHttp = getBuiltKeystoneConfiguration(cwd) + const configWithExtendHttp = await importBuiltKeystoneConfiguration(cwd) const { system, context, @@ -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) { @@ -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() diff --git a/packages/core/src/scripts/migrate.ts b/packages/core/src/scripts/migrate.ts index e1eb149333f..2447e9c5227 100644 --- a/packages/core/src/scripts/migrate.ts +++ b/packages/core/src/scripts/migrate.ts @@ -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 { @@ -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: { @@ -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') @@ -129,11 +127,10 @@ export async function migrateApply ( cwd: string, { frozen }: Pick ) { + // 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') diff --git a/packages/core/src/scripts/prisma.ts b/packages/core/src/scripts/prisma.ts index 6d572e3d6d9..a200eba238b 100644 --- a/packages/core/src/scripts/prisma.ts +++ b/packages/core/src/scripts/prisma.ts @@ -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: { @@ -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') diff --git a/packages/core/src/scripts/start.ts b/packages/core/src/scripts/start.ts index 395d3f9de07..0c534f59afe 100644 --- a/packages/core/src/scripts/start.ts +++ b/packages/core/src/scripts/start.ts @@ -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 ( @@ -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) { diff --git a/packages/core/src/scripts/utils.ts b/packages/core/src/scripts/utils.ts index 798208db3c5..840c6c420c2 100644 --- a/packages/core/src/scripts/utils.ts +++ b/packages/core/src/scripts/utils.ts @@ -1,3 +1,5 @@ +import { getBuiltKeystoneConfigurationPath } from '../lib/createSystem' + export class ExitError extends Error { code: number constructor (code: number) { @@ -5,3 +7,13 @@ export class ExitError extends Error { 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) + } +}