Skip to content

Commit

Permalink
chore: move autogen to reload, add flag for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
denolfe committed Jun 7, 2024
1 parent 704a003 commit 6d9b462
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
21 changes: 10 additions & 11 deletions packages/next/src/utilities/getPayloadHMR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,21 @@ export const getPayloadHMR = async (options: InitOptions): Promise<Payload> => {
if (cached.payload) {
const config = await options.config // TODO: check if we can move this inside the cached.reload === true condition

// Generate types on startup
if (config.typescript.autoGenerate !== false) {
const configHash = quickHash(config)

if (cached.hashedConfig !== configHash) {
cached.payload.logger.info(`Config has changed, regenerating types... (${configHash})`)
cached.hashedConfig = configHash
await generateTypes(config)
}
}

if (cached.reload === true) {
let resolve

cached.reload = new Promise((res) => (resolve = res))

// Generate types on startup
if (config.typescript.autoGenerate !== false) {
const configHash = quickHash(config)

if (cached.hashedConfig !== configHash) {
cached.hashedConfig = configHash
void generateTypes(config, { log: false })
}
}

await reload(config, cached.payload)

resolve()
Expand Down
11 changes: 8 additions & 3 deletions packages/payload/src/bin/generateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import type { SanitizedConfig } from '../config/types.js'
import { configToJSONSchema } from '../utilities/configToJSONSchema.js'
import Logger from '../utilities/logger.js'

export async function generateTypes(config: SanitizedConfig): Promise<void> {
export async function generateTypes(
config: SanitizedConfig,
options?: { log: boolean },
): Promise<void> {
const logger = Logger()
const outputFile = process.env.PAYLOAD_TS_OUTPUT_PATH || config.typescript.outputFile

logger.info('Compiling TS types for Collections and Globals...')
const shouldLog = options?.log ?? true

if (shouldLog) logger.info('Compiling TS types for Collections and Globals...')

const jsonSchema = configToJSONSchema(config, config.db.defaultIDType)

Expand All @@ -37,5 +42,5 @@ export async function generateTypes(config: SanitizedConfig): Promise<void> {
}
}
fs.writeFileSync(outputFile, compiled)
logger.info(`Types written to ${outputFile}`)
if (shouldLog) logger.info(`Types written to ${outputFile}`)
}
2 changes: 1 addition & 1 deletion packages/payload/src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ export default joi.object({
sharp: joi.any(),
telemetry: joi.boolean(),
typescript: joi.object({
autoGenerate: joi.boolean(),
declare: joi.alternatives().try(joi.boolean(), joi.object({ ignoreTSError: joi.boolean() })),
generateTypesOnStartup: joi.boolean(),
outputFile: joi.string(),
}),
upload: joi.object(),
Expand Down
8 changes: 4 additions & 4 deletions test/fields/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ export const collectionSlugs: CollectionConfig[] = [
type: 'checkbox',
defaultValue: true,
},
{
name: 'asdf',
type: 'text',
},
// {
// name: 'asdf',
// type: 'text',
// },
],
},
ArrayFields,
Expand Down
2 changes: 1 addition & 1 deletion test/fields/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ export interface LexicalLocalizedField {
export interface User {
id: string;
canViewConditionalField?: boolean | null;
asdf?: string | null;
updatedAt: string;
createdAt: string;
email: string;
Expand Down Expand Up @@ -1424,5 +1423,6 @@ export interface LexicalBlocksRadioButtonsBlock {


declare module 'payload' {
// @ts-ignore
export interface GeneratedTypes extends Config {}
}

0 comments on commit 6d9b462

Please sign in to comment.