payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
28.89k stars 1.79k forks source link

Deprecated useImport, Unable to import the config file #8573

Open ainsleyclark opened 1 month ago

ainsleyclark commented 1 month ago

Link to reproduction

https://github.com/ainsleydev/webkit/tree/main/packages/payload-helper

Environment Info

Payload: Beta 111 Node: v21.7.3

Describe the Bug

As of https://github.com/payloadcms/payload/releases/tag/v3.0.0-beta.79 I believe importConfig has been deprecated in favour of importing the file like so: import config from "./payload.config.js"

This change poses a problem for plugins that are CLI tools for Payload. I've tried various methods, including using require, but I'm still unable to import the config file via a plugin using a file path.

Reproduction Steps

Example script is here:

import * as fs from 'node:fs';
import { configToJSONSchema, getPayload } from 'payload';
import { findConfig, importConfig } from 'payload/node';

/**
 * Creates JSON schema types of Payloads Collections & Globals
 */
export async function generateTypes(outFile: string): Promise<void> {
    console.log('Compiling JSON types for Collections and Globals...');

    let configPath = '';
    try {
        configPath = findConfig();
    } catch (e) {
        console.log(`Error finding config: ${e}`);
        return;
    }

    // Set the environment variable to generate Golang types.
    process.env.GEN_GOLANG = 'true';

    const config = await importConfig(configPath);
    const outputFile = (process.env.PAYLOAD_TS_OUTPUT_PATH || config.typescript.outputFile).replace(
        '.ts',
        '.json',
    );

    const payload = await getPayload({
        config,
        disableDBConnect: true,
        disableOnInit: true,
        // @ts-ignore
        local: true,
        secret: '--unused--',
    });

    const jsonSchema = configToJSONSchema(payload.config, payload.db.defaultIDType);
    const prettyJSON = JSON.stringify(jsonSchema, null, 4);

    fs.writeFileSync(outputFile, prettyJSON);

    console.log(`JSON types written to: ${outputFile}`);

    delete process.env.GEN_GOLANG;
}

Adapters and Plugins

N/A

denolfe commented 1 month ago

This change poses a problem for plugins that are CLI tools for Payload.

Can you expand upon this?

ainsleyclark commented 1 month ago

If there is any occasion where a plugin may wish to import configuration externally to use as an executable, I'm not sure there's a way to do it?

For instance, the code snippet above is compiled using tsx, but now there's no way to obtain the Payload config.