chore: importConfig and importWithoutClientFiles (#5701)
This commit is contained in:
4
packages/payload/node.d.ts
vendored
Normal file
4
packages/payload/node.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
export { getFileByPath } from './dist/uploads/getFileByPath.js';
|
||||
export { importConfig } from './dist/utilities/importConfig.js';
|
||||
export { importWithoutClientFiles } from './dist/utilities/importWithoutClientFiles.js';
|
||||
//# sourceMappingURL=node.d.ts.map
|
||||
5
packages/payload/node.js
Normal file
5
packages/payload/node.js
Normal file
@@ -0,0 +1,5 @@
|
||||
export { getFileByPath } from './dist/uploads/getFileByPath.js';
|
||||
export { importConfig } from './dist/utilities/importConfig.js';
|
||||
export { importWithoutClientFiles } from './dist/utilities/importWithoutClientFiles.js';
|
||||
|
||||
//# sourceMappingURL=node.js.map
|
||||
3
packages/payload/src/exports/node.ts
Normal file
3
packages/payload/src/exports/node.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { getFileByPath } from '../uploads/getFileByPath.js'
|
||||
export { importConfig } from '../utilities/importConfig.js'
|
||||
export { importWithoutClientFiles } from '../utilities/importWithoutClientFiles.js'
|
||||
@@ -1 +0,0 @@
|
||||
export { getFileByPath } from '../uploads/getFileByPath.js'
|
||||
@@ -29,17 +29,17 @@ export { getCollectionIDFieldTypes } from '../utilities/getCollectionIDFieldType
|
||||
export { getIDType } from '../utilities/getIDType.js'
|
||||
export { getObjectDotNotation } from '../utilities/getObjectDotNotation.js'
|
||||
export { default as getUniqueListBy } from '../utilities/getUniqueListBy.js'
|
||||
|
||||
export { isEntityHidden } from '../utilities/isEntityHidden.js'
|
||||
|
||||
export { isNumber } from '../utilities/isNumber.js'
|
||||
|
||||
export { isPlainObject } from '../utilities/isPlainObject.js'
|
||||
export { isPlainFunction, isReactComponent } from '../utilities/isReactComponent.js'
|
||||
|
||||
export { isPlainFunction, isReactComponent } from '../utilities/isReactComponent.js'
|
||||
export { isValidID } from '../utilities/isValidID.js'
|
||||
export { default as isolateObjectProperty } from '../utilities/isolateObjectProperty.js'
|
||||
export { mapAsync } from '../utilities/mapAsync.js'
|
||||
|
||||
export { mapAsync } from '../utilities/mapAsync.js'
|
||||
export { mergeListSearchAndWhere } from '../utilities/mergeListSearchAndWhere.js'
|
||||
export { setsAreEqual } from '../utilities/setsAreEqual.js'
|
||||
export { default as toKebabCase } from '../utilities/toKebabCase.js'
|
||||
|
||||
41
packages/payload/src/utilities/importConfig.ts
Normal file
41
packages/payload/src/utilities/importConfig.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import path from 'node:path'
|
||||
|
||||
import type { SanitizedConfig } from '../config/types.js'
|
||||
|
||||
import { importWithoutClientFiles } from './importWithoutClientFiles.js'
|
||||
|
||||
/**
|
||||
* Resolve and load Payload config from either a relative or absolute path
|
||||
*/
|
||||
export const importConfig = async (configPath: string) => {
|
||||
const isAbsolutePath = path.isAbsolute(configPath)
|
||||
if (isAbsolutePath) {
|
||||
const config = await importWithoutClientFiles<{ default: Promise<SanitizedConfig> }>(configPath)
|
||||
return await config.default
|
||||
}
|
||||
|
||||
const callerDir = path.dirname(getCallerInfo()[1].getFileName()).replace('file://', '')
|
||||
const fullConfigPath = path.resolve(callerDir, configPath)
|
||||
|
||||
const config = await importWithoutClientFiles<{ default: Promise<SanitizedConfig> }>(
|
||||
fullConfigPath,
|
||||
)
|
||||
return await config.default
|
||||
}
|
||||
|
||||
const getCallerInfo = () => {
|
||||
const _prepareStackTrace = Error.prepareStackTrace
|
||||
try {
|
||||
let result = []
|
||||
Error.prepareStackTrace = (_, callSites) => {
|
||||
const callSitesWithoutCurrent = callSites.slice(1)
|
||||
result = callSitesWithoutCurrent
|
||||
return callSitesWithoutCurrent
|
||||
}
|
||||
|
||||
new Error().stack
|
||||
return result
|
||||
} finally {
|
||||
Error.prepareStackTrace = _prepareStackTrace
|
||||
}
|
||||
}
|
||||
14
packages/payload/src/utilities/importWithoutClientFiles.ts
Normal file
14
packages/payload/src/utilities/importWithoutClientFiles.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { register } from 'node:module'
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url'
|
||||
import path from 'path'
|
||||
|
||||
const filename = fileURLToPath(import.meta.url)
|
||||
const dirname = path.dirname(filename)
|
||||
|
||||
export const importWithoutClientFiles = async <T = unknown>(filePath: string) => {
|
||||
const url = pathToFileURL(filePath).toString()
|
||||
|
||||
register(path.resolve(dirname, '../../dist/bin/register/index.js'), url)
|
||||
const result = await import(filePath)
|
||||
return result as T
|
||||
}
|
||||
2
packages/payload/uploads.d.ts
vendored
2
packages/payload/uploads.d.ts
vendored
@@ -1,2 +0,0 @@
|
||||
export { getFileByPath } from './dist/uploads/getFileByPath.js'
|
||||
//# sourceMappingURL=uploads.d.ts.map
|
||||
@@ -1,3 +0,0 @@
|
||||
export { getFileByPath } from './dist/uploads/getFileByPath.js'
|
||||
|
||||
//# sourceMappingURL=uploads.js.map
|
||||
@@ -1,5 +1,5 @@
|
||||
import path from 'path'
|
||||
import { getFileByPath } from 'payload/uploads'
|
||||
import { getFileByPath } from 'payload/node'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import path from 'path'
|
||||
import { getFileByPath } from 'payload/uploads'
|
||||
import { getFileByPath } from 'payload/node'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Payload } from 'payload'
|
||||
|
||||
import path from 'path'
|
||||
import { getFileByPath } from 'payload/uploads'
|
||||
import { getFileByPath } from 'payload/node'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
import { devUser } from '../credentials.js'
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Payload } from 'payload'
|
||||
|
||||
import path from 'path'
|
||||
import { getFileByPath } from 'payload/uploads'
|
||||
import { getFileByPath } from 'payload/node'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { Page } from '@playwright/test'
|
||||
|
||||
import { expect, test } from '@playwright/test'
|
||||
import path from 'path'
|
||||
import { getFileByPath } from 'payload/uploads'
|
||||
import { getFileByPath } from 'payload/node'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
import type { Page as PayloadPage } from './payload-types.js'
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Payload } from 'payload'
|
||||
|
||||
import path from 'path'
|
||||
import { getFileByPath } from 'payload/uploads'
|
||||
import { getFileByPath } from 'payload/node'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
import { initPayloadInt } from '../helpers/initPayloadInt.js'
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { Payload } from 'payload'
|
||||
import type { PayloadRequest } from 'payload/types'
|
||||
|
||||
import path from 'path'
|
||||
import { getFileByPath } from 'payload/uploads'
|
||||
import { getFileByPath } from 'payload/node'
|
||||
|
||||
import { mediaSlug } from '../shared.js'
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import path from 'path'
|
||||
import { getFileByPath } from 'payload/uploads'
|
||||
import { getFileByPath } from 'payload/node'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
|
||||
|
||||
@@ -4,7 +4,7 @@ import { File as FileBuffer } from 'buffer'
|
||||
import NodeFormData from 'form-data'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { getFileByPath } from 'payload/uploads'
|
||||
import { getFileByPath } from 'payload/node'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { promisify } from 'util'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user