Skip to content

Commit e04be4b

Browse files
authored
templates: improve gen-templates script (#10015)
- Allow gen templates script to take template dir name arg - All `skipDockerCompose` and `skipConfig`
1 parent 7037983 commit e04be4b

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

packages/create-payload-app/src/utils/copy-recursive-sync.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ export function copyRecursiveSync(src: string, dest: string, ignoreRegex?: strin
1313
if (isDirectory) {
1414
fs.mkdirSync(dest, { recursive: true })
1515
fs.readdirSync(src).forEach((childItemName) => {
16-
if (ignoreRegex && ignoreRegex.some((regex) => new RegExp(regex).test(childItemName))) {
16+
if (
17+
ignoreRegex &&
18+
ignoreRegex.some((regex) => {
19+
return new RegExp(regex).test(childItemName)
20+
})
21+
) {
22+
console.log(`Ignoring ${childItemName} due to regex: ${ignoreRegex}`)
1723
return
1824
}
19-
copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName))
25+
copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName), ignoreRegex)
2026
})
2127
} else {
2228
fs.copyFileSync(src, dest)

scripts/generate-template-variations.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import chalk from 'chalk'
1515
import { execSync } from 'child_process'
1616
import { configurePayloadConfig } from 'create-payload-app/lib/configure-payload-config.js'
1717
import { copyRecursiveSync } from 'create-payload-app/utils/copy-recursive-sync.js'
18+
import minimist from 'minimist'
1819
import * as fs from 'node:fs/promises'
1920
import { fileURLToPath } from 'node:url'
2021
import path from 'path'
@@ -40,6 +41,11 @@ type TemplateVariations = {
4041
* @default false
4142
*/
4243
skipReadme?: boolean
44+
skipConfig?: boolean
45+
/**
46+
* @default false
47+
*/
48+
skipDockerCompose?: boolean
4349
configureConfig?: boolean
4450
generateLockfile?: boolean
4551
}
@@ -50,12 +56,13 @@ main().catch((error) => {
5056
})
5157

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

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

58-
const variations: TemplateVariations[] = [
65+
let variations: TemplateVariations[] = [
5966
{
6067
name: 'payload-vercel-postgres-template',
6168
dirname: 'with-vercel-postgres',
@@ -132,20 +139,22 @@ async function main() {
132139
generateLockfile: true,
133140
storage: 'localDisk',
134141
sharp: true,
142+
skipConfig: true, // Do not copy the payload.config.ts file from the base template
135143
// The blank template is used as a base for create-payload-app functionality,
136144
// so we do not configure the payload.config.ts file, which leaves the placeholder comments.
137145
configureConfig: false,
138146
},
139-
{
140-
name: 'payload-cloud-mongodb-template',
141-
dirname: 'with-payload-cloud',
142-
db: 'mongodb',
143-
generateLockfile: true,
144-
storage: 'payloadCloud',
145-
sharp: true,
146-
},
147147
]
148148

149+
// If template is set, only generate that template
150+
if (template) {
151+
const variation = variations.find((v) => v.dirname === template)
152+
if (!variation) {
153+
throw new Error(`Variation not found: ${template}`)
154+
}
155+
variations = [variation]
156+
}
157+
149158
for (const {
150159
name,
151160
base,
@@ -158,6 +167,8 @@ async function main() {
158167
sharp,
159168
configureConfig,
160169
skipReadme = false,
170+
skipConfig = false,
171+
skipDockerCompose = false,
161172
} of variations) {
162173
header(`Generating ${name}...`)
163174
const destDir = path.join(templatesDir, dirname)
@@ -167,21 +178,24 @@ async function main() {
167178
'.next',
168179
'.env$',
169180
'pnpm-lock.yaml',
170-
...(skipReadme ? ['README.md'] : ['']),
181+
...(skipReadme ? ['README.md'] : []),
182+
...(skipDockerCompose ? ['docker-compose.yml'] : []),
183+
...(skipConfig ? ['payload.config.ts'] : []),
171184
])
172185

173186
log(`Copied to ${destDir}`)
174187

175188
if (configureConfig !== false) {
176189
log('Configuring payload.config.ts')
177-
await configurePayloadConfig({
190+
const configureArgs = {
178191
dbType: db,
179192
packageJsonName: name,
180193
projectDirOrConfigPath: { projectDir: destDir },
181194
storageAdapter: storage,
182195
sharp,
183196
envNames,
184-
})
197+
}
198+
await configurePayloadConfig(configureArgs)
185199

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

0 commit comments

Comments
 (0)