chore: importConfig and importWithoutClientFiles (#5701)

This commit is contained in:
Elliot DeNolf
2024-04-05 16:49:20 -04:00
committed by GitHub
parent 136545d1fd
commit 2412134073
18 changed files with 79 additions and 18 deletions

4
packages/payload/node.d.ts vendored Normal file
View 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
View 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

View File

@@ -0,0 +1,3 @@
export { getFileByPath } from '../uploads/getFileByPath.js'
export { importConfig } from '../utilities/importConfig.js'
export { importWithoutClientFiles } from '../utilities/importWithoutClientFiles.js'

View File

@@ -1 +0,0 @@
export { getFileByPath } from '../uploads/getFileByPath.js'

View File

@@ -29,17 +29,17 @@ export { getCollectionIDFieldTypes } from '../utilities/getCollectionIDFieldType
export { getIDType } from '../utilities/getIDType.js' export { getIDType } from '../utilities/getIDType.js'
export { getObjectDotNotation } from '../utilities/getObjectDotNotation.js' export { getObjectDotNotation } from '../utilities/getObjectDotNotation.js'
export { default as getUniqueListBy } from '../utilities/getUniqueListBy.js' export { default as getUniqueListBy } from '../utilities/getUniqueListBy.js'
export { isEntityHidden } from '../utilities/isEntityHidden.js' export { isEntityHidden } from '../utilities/isEntityHidden.js'
export { isNumber } from '../utilities/isNumber.js' export { isNumber } from '../utilities/isNumber.js'
export { isPlainObject } from '../utilities/isPlainObject.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 { isValidID } from '../utilities/isValidID.js'
export { default as isolateObjectProperty } from '../utilities/isolateObjectProperty.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 { mergeListSearchAndWhere } from '../utilities/mergeListSearchAndWhere.js'
export { setsAreEqual } from '../utilities/setsAreEqual.js' export { setsAreEqual } from '../utilities/setsAreEqual.js'
export { default as toKebabCase } from '../utilities/toKebabCase.js' export { default as toKebabCase } from '../utilities/toKebabCase.js'

View 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
}
}

View 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
}

View File

@@ -1,2 +0,0 @@
export { getFileByPath } from './dist/uploads/getFileByPath.js'
//# sourceMappingURL=uploads.d.ts.map

View File

@@ -1,3 +0,0 @@
export { getFileByPath } from './dist/uploads/getFileByPath.js'
//# sourceMappingURL=uploads.js.map

View File

@@ -1,5 +1,5 @@
import path from 'path' import path from 'path'
import { getFileByPath } from 'payload/uploads' import { getFileByPath } from 'payload/node'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js' import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'

View File

@@ -1,5 +1,5 @@
import path from 'path' import path from 'path'
import { getFileByPath } from 'payload/uploads' import { getFileByPath } from 'payload/node'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js' import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'

View File

@@ -1,7 +1,7 @@
import type { Payload } from 'payload' import type { Payload } from 'payload'
import path from 'path' import path from 'path'
import { getFileByPath } from 'payload/uploads' import { getFileByPath } from 'payload/node'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import { devUser } from '../credentials.js' import { devUser } from '../credentials.js'

View File

@@ -1,7 +1,7 @@
import type { Payload } from 'payload' import type { Payload } from 'payload'
import path from 'path' import path from 'path'
import { getFileByPath } from 'payload/uploads' import { getFileByPath } from 'payload/node'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import type { NextRESTClient } from '../helpers/NextRESTClient.js' import type { NextRESTClient } from '../helpers/NextRESTClient.js'

View File

@@ -2,7 +2,7 @@ import type { Page } from '@playwright/test'
import { expect, test } from '@playwright/test' import { expect, test } from '@playwright/test'
import path from 'path' import path from 'path'
import { getFileByPath } from 'payload/uploads' import { getFileByPath } from 'payload/node'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import type { Page as PayloadPage } from './payload-types.js' import type { Page as PayloadPage } from './payload-types.js'

View File

@@ -1,7 +1,7 @@
import type { Payload } from 'payload' import type { Payload } from 'payload'
import path from 'path' import path from 'path'
import { getFileByPath } from 'payload/uploads' import { getFileByPath } from 'payload/node'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import { initPayloadInt } from '../helpers/initPayloadInt.js' import { initPayloadInt } from '../helpers/initPayloadInt.js'

View File

@@ -2,7 +2,7 @@ import type { Payload } from 'payload'
import type { PayloadRequest } from 'payload/types' import type { PayloadRequest } from 'payload/types'
import path from 'path' import path from 'path'
import { getFileByPath } from 'payload/uploads' import { getFileByPath } from 'payload/node'
import { mediaSlug } from '../shared.js' import { mediaSlug } from '../shared.js'

View File

@@ -1,5 +1,5 @@
import path from 'path' import path from 'path'
import { getFileByPath } from 'payload/uploads' import { getFileByPath } from 'payload/node'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js' import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'

View File

@@ -4,7 +4,7 @@ import { File as FileBuffer } from 'buffer'
import NodeFormData from 'form-data' import NodeFormData from 'form-data'
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import { getFileByPath } from 'payload/uploads' import { getFileByPath } from 'payload/node'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import { promisify } from 'util' import { promisify } from 'util'