2023-01-10 11:48:40 -05:00
import * as dotenv from 'dotenv'
2022-07-13 11:00:10 -07:00
import express from 'express'
2023-05-31 15:51:49 -04:00
import fs from 'fs'
2023-01-10 11:48:40 -05:00
import path from 'path'
2022-07-14 16:51:41 -04:00
import { v4 as uuid } from 'uuid'
2023-05-10 19:47:22 +07:00
2023-09-01 14:45:41 -04:00
import payload from '../packages/payload/src'
2023-10-03 12:34:48 -04:00
import { prettySyncLoggerDestination } from '../packages/payload/src/utilities/logger'
2023-10-02 11:40:08 -04:00
import { startLivePreviewDemo } from './live-preview/startLivePreviewDemo'
2022-07-13 11:00:10 -07:00
2023-05-10 19:47:22 +07:00
dotenv . config ( )
2023-09-01 17:39:44 +02:00
2023-10-06 03:38:39 -04:00
const [ testSuiteDir ] = process . argv . slice ( 4 )
2023-01-10 11:48:40 -05:00
2023-10-03 12:34:48 -04:00
/**
* The default logger's options did not allow for forcing sync logging
* Using these options, to force both pretty print and sync logging
*/
const prettySyncLogger = {
loggerDestination : prettySyncLoggerDestination ,
loggerOptions : { } ,
}
2023-01-10 11:48:40 -05:00
if ( ! testSuiteDir ) {
console . error ( 'ERROR: You must provide an argument for "testSuiteDir"' )
process . exit ( 1 )
}
const configPath = path . resolve ( __dirname , testSuiteDir , 'config.ts' )
if ( ! fs . existsSync ( configPath ) ) {
console . error ( 'ERROR: You must pass a valid directory under test/ that contains a config.ts' )
process . exit ( 1 )
}
2026-01-09 16:16:47 +02:00
// empty
2023-01-10 11:48:40 -05:00
process . env . PAYLOAD_CONFIG_PATH = configPath
2023-09-27 16:28:59 -04:00
// Default to true unless explicitly set to false
if ( process . env . PAYLOAD_DROP_DATABASE === 'false' ) {
process . env . PAYLOAD_DROP_DATABASE = 'false'
} else {
process . env . PAYLOAD_DROP_DATABASE = 'true'
}
2023-01-10 11:48:40 -05:00
2023-07-21 14:13:18 -04:00
if ( process . argv . includes ( '--no-auto-login' ) && process . env . NODE_ENV !== 'production' ) {
process . env . PAYLOAD_PUBLIC_DISABLE_AUTO_LOGIN = 'true'
}
2022-07-13 11:00:10 -07:00
const expressApp = express ( )
2023-01-11 09:32:44 -05:00
2023-01-11 15:58:58 -05:00
const startDev = async ( ) = > {
2023-01-11 17:03:10 -05:00
await payload . init ( {
2022-07-13 11:00:10 -07:00
email : {
fromAddress : 'hello@payloadcms.com' ,
2023-10-06 03:38:39 -04:00
fromName : 'Payload' ,
logMockCredentials : false ,
2022-07-13 11:00:10 -07:00
} ,
2023-10-06 03:38:39 -04:00
express : expressApp ,
secret : uuid ( ) ,
2023-10-03 12:34:48 -04:00
. . . prettySyncLogger ,
2023-09-28 15:02:59 -04:00
onInit : async ( payload ) = > {
2023-01-19 16:06:50 -05:00
payload . logger . info ( 'Payload Dev Server Initialized' )
2022-07-13 11:00:10 -07:00
} ,
} )
2022-09-12 16:38:02 -07:00
// Redirect root to Admin panel
expressApp . get ( '/' , ( _ , res ) = > {
res . redirect ( '/admin' )
} )
2022-07-13 11:00:10 -07:00
const externalRouter = express . Router ( )
externalRouter . use ( payload . authenticate )
2023-10-02 11:40:08 -04:00
if ( testSuiteDir === 'live-preview' ) {
await startLivePreviewDemo ( {
payload ,
} )
}
2022-07-13 11:00:10 -07:00
expressApp . listen ( 3000 , async ( ) = > {
2023-05-08 19:23:09 +02:00
payload . logger . info ( ` Admin URL on http://localhost:3000 ${ payload . getAdminURL ( ) } ` )
payload . logger . info ( ` API URL on http://localhost:3000 ${ payload . getAPIURL ( ) } ` )
2022-07-13 11:00:10 -07:00
} )
}
2023-10-06 17:16:13 -04:00
// eslint-disable-next-line @typescript-eslint/no-floating-promises
2023-01-11 15:58:58 -05:00
startDev ( )