payload icon indicating copy to clipboard operation
payload copied to clipboard

Deprecated useImport, Unable to import the config file

Open ainsleyclark opened this issue 1 year ago • 4 comments

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

ainsleyclark avatar Oct 05 '24 19:10 ainsleyclark

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

Can you expand upon this?

denolfe avatar Oct 28 '24 18:10 denolfe

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.

ainsleyclark avatar Oct 30 '24 13:10 ainsleyclark

This issue has been marked as stale due to lack of activity.

To keep this issue open, please indicate that it is still relevant in a comment below.

github-actions[bot] avatar Dec 13 '24 05:12 github-actions[bot]

Still open.

ainsleyclark avatar Dec 13 '24 08:12 ainsleyclark