Skip to content

Commit

Permalink
templates: improve gen-templates script (#10015)
Browse files Browse the repository at this point in the history
- Allow gen templates script to take template dir name arg
- All `skipDockerCompose` and `skipConfig`
  • Loading branch information
denolfe authored Dec 17, 2024
1 parent 7037983 commit e04be4b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
10 changes: 8 additions & 2 deletions packages/create-payload-app/src/utils/copy-recursive-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ export function copyRecursiveSync(src: string, dest: string, ignoreRegex?: strin
if (isDirectory) {
fs.mkdirSync(dest, { recursive: true })
fs.readdirSync(src).forEach((childItemName) => {
if (ignoreRegex && ignoreRegex.some((regex) => new RegExp(regex).test(childItemName))) {
if (
ignoreRegex &&
ignoreRegex.some((regex) => {
return new RegExp(regex).test(childItemName)
})
) {
console.log(`Ignoring ${childItemName} due to regex: ${ignoreRegex}`)
return
}
copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName))
copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName), ignoreRegex)
})
} else {
fs.copyFileSync(src, dest)
Expand Down
40 changes: 27 additions & 13 deletions scripts/generate-template-variations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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'
import minimist from 'minimist'
import * as fs from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import path from 'path'
Expand All @@ -40,6 +41,11 @@ type TemplateVariations = {
* @default false
*/
skipReadme?: boolean
skipConfig?: boolean
/**
* @default false
*/
skipDockerCompose?: boolean
configureConfig?: boolean
generateLockfile?: boolean
}
Expand All @@ -50,12 +56,13 @@ main().catch((error) => {
})

async function main() {
const args = minimist(process.argv.slice(2))
const template = args['template'] // template directory name
const templatesDir = path.resolve(dirname, '../templates')

// WARNING: This will need to be updated when this merges into main
const templateRepoUrlBase = `https://github.com/payloadcms/payload/tree/main/templates`

const variations: TemplateVariations[] = [
let variations: TemplateVariations[] = [
{
name: 'payload-vercel-postgres-template',
dirname: 'with-vercel-postgres',
Expand Down Expand Up @@ -132,20 +139,22 @@ async function main() {
generateLockfile: true,
storage: 'localDisk',
sharp: true,
skipConfig: true, // Do not copy the payload.config.ts file from the base template
// 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,
},
{
name: 'payload-cloud-mongodb-template',
dirname: 'with-payload-cloud',
db: 'mongodb',
generateLockfile: true,
storage: 'payloadCloud',
sharp: true,
},
]

// If template is set, only generate that template
if (template) {
const variation = variations.find((v) => v.dirname === template)
if (!variation) {
throw new Error(`Variation not found: ${template}`)
}
variations = [variation]
}

for (const {
name,
base,
Expand All @@ -158,6 +167,8 @@ async function main() {
sharp,
configureConfig,
skipReadme = false,
skipConfig = false,
skipDockerCompose = false,
} of variations) {
header(`Generating ${name}...`)
const destDir = path.join(templatesDir, dirname)
Expand All @@ -167,21 +178,24 @@ async function main() {
'.next',
'.env$',
'pnpm-lock.yaml',
...(skipReadme ? ['README.md'] : ['']),
...(skipReadme ? ['README.md'] : []),
...(skipDockerCompose ? ['docker-compose.yml'] : []),
...(skipConfig ? ['payload.config.ts'] : []),
])

log(`Copied to ${destDir}`)

if (configureConfig !== false) {
log('Configuring payload.config.ts')
await configurePayloadConfig({
const configureArgs = {
dbType: db,
packageJsonName: name,
projectDirOrConfigPath: { projectDir: destDir },
storageAdapter: storage,
sharp,
envNames,
})
}
await configurePayloadConfig(configureArgs)

log('Configuring .env.example')
// Replace DATABASE_URI with the correct env name if set
Expand Down

0 comments on commit e04be4b

Please sign in to comment.