feat(templates): add vercel postgres template (#7841)

- docs: add db-vercel-postgres info
- feat: update template variations
This commit is contained in:
Elliot DeNolf
2024-08-23 15:17:26 -04:00
committed by GitHub
parent 83022f6d55
commit 825d8b83d1
41 changed files with 1164 additions and 31 deletions

View File

@@ -11,6 +11,7 @@
import type { DbType, StorageAdapterType } from 'packages/create-payload-app/src/types.js'
import chalk from 'chalk'
import { execSync } from 'child_process'
import { configurePayloadConfig } from 'create-payload-app/lib/configure-payload-config.js'
import { copyRecursiveSync } from 'create-payload-app/utils/copy-recursive-sync.js'
@@ -51,7 +52,7 @@ async function main() {
{
name: 'payload-vercel-postgres-template',
dirname: 'with-vercel-postgres',
db: 'postgres',
db: 'vercelPostgres',
storage: 'vercelBlobStorage',
sharp: false,
vercelDeployButtonLink:
@@ -68,6 +69,13 @@ async function main() {
dbUri: 'POSTGRES_URL',
},
},
{
name: 'payload-postgres-template',
dirname: 'with-postgres',
db: 'postgres',
storage: 'localDisk',
sharp: true,
},
{
name: 'payload-vercel-mongodb-template',
dirname: 'with-vercel-mongodb',
@@ -94,8 +102,8 @@ async function main() {
db: 'mongodb',
storage: 'localDisk',
sharp: true,
// The blank template will later be used by CPA as a base. That's when we want to
// configure the config - not here
// The blank template is used as a base for create-payload-app functionality,
// so we do not configure the payload.config.ts file, which leaves the placeholder comments.
configureConfig: false,
},
{
@@ -117,13 +125,13 @@ async function main() {
sharp,
configureConfig,
} of variations) {
console.log(`Generating ${name}...`)
header(`Generating ${name}...`)
const destDir = path.join(templatesDir, dirname)
copyRecursiveSync(path.join(templatesDir, '_template'), destDir)
console.log(`Generated ${name} in ${destDir}`)
log(`Copied to ${destDir}`)
if (configureConfig !== false) {
// Configure payload config
log('Configuring payload.config.ts')
await configurePayloadConfig({
dbType: db,
packageJsonName: name,
@@ -132,6 +140,13 @@ async function main() {
sharp,
envNames,
})
log('Configuring .env.example')
// Replace DATABASE_URI with the correct env name if set
await writeEnvExample({
destDir,
envNames,
})
}
await generateReadme({
@@ -153,7 +168,7 @@ async function main() {
if ((await fs.stat(migrationDestDir).catch(() => null)) === null) {
await fs.mkdir(migrationDestDir, { recursive: true })
}
console.log(`Copying migrations from ${migrationSrcDir} to ${migrationDestDir}`)
log(`Copying migrations from ${migrationSrcDir} to ${migrationDestDir}`)
copyRecursiveSync(migrationSrcDir, migrationDestDir)
}
@@ -161,13 +176,13 @@ async function main() {
// TODO: Sharp?
console.log(`Done configuring payload config for ${destDir}/src/payload.config.ts`)
log(`Done configuring payload config for ${destDir}/src/payload.config.ts`)
}
// TODO: Run prettier manually on the generated files, husky blows up
console.log('Running prettier on generated files...')
log('Running prettier on generated files...')
execSync(`pnpm prettier --write templates "*.{js,jsx,ts,tsx}"`)
console.log('Template generation complete!')
log('Template generation complete!')
}
async function generateReadme({
@@ -199,5 +214,35 @@ ${description}
const readmePath = path.join(destDir, 'README.md')
await fs.writeFile(readmePath, readmeContent)
console.log(`Generated README.md in ${readmePath}`)
log('Generated README.md')
}
async function writeEnvExample({
destDir,
envNames,
}: {
destDir: string
envNames?: TemplateVariations['envNames']
}) {
const envExamplePath = path.join(destDir, '.env.example')
const envFileContents = await fs.readFile(envExamplePath, 'utf8')
const fileContents = envFileContents
.split('\n')
.filter((e) => e)
.map((l) =>
envNames?.dbUri && l.startsWith('DATABASE_URI')
? l.replace('DATABASE_URI', envNames.dbUri)
: l,
)
.join('\n')
await fs.writeFile(envExamplePath, fileContents)
}
function header(message: string) {
console.log(chalk.bold.green(`\n${message}\n`))
}
function log(message: string) {
console.log(chalk.dim(message))
}