templates: improve gen-templates script (#10015)

- Allow gen templates script to take template dir name arg
- All `skipDockerCompose` and `skipConfig`
This commit is contained in:
Elliot DeNolf
2024-12-17 10:00:17 -05:00
committed by GitHub
parent 7037983de0
commit e04be4bf62
2 changed files with 35 additions and 15 deletions

View File

@@ -13,10 +13,16 @@ export function copyRecursiveSync(src: string, dest: string, ignoreRegex?: strin
if (isDirectory) { if (isDirectory) {
fs.mkdirSync(dest, { recursive: true }) fs.mkdirSync(dest, { recursive: true })
fs.readdirSync(src).forEach((childItemName) => { 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 return
} }
copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName)) copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName), ignoreRegex)
}) })
} else { } else {
fs.copyFileSync(src, dest) fs.copyFileSync(src, dest)

View File

@@ -15,6 +15,7 @@ import chalk from 'chalk'
import { execSync } from 'child_process' import { execSync } from 'child_process'
import { configurePayloadConfig } from 'create-payload-app/lib/configure-payload-config.js' import { configurePayloadConfig } from 'create-payload-app/lib/configure-payload-config.js'
import { copyRecursiveSync } from 'create-payload-app/utils/copy-recursive-sync.js' import { copyRecursiveSync } from 'create-payload-app/utils/copy-recursive-sync.js'
import minimist from 'minimist'
import * as fs from 'node:fs/promises' import * as fs from 'node:fs/promises'
import { fileURLToPath } from 'node:url' import { fileURLToPath } from 'node:url'
import path from 'path' import path from 'path'
@@ -40,6 +41,11 @@ type TemplateVariations = {
* @default false * @default false
*/ */
skipReadme?: boolean skipReadme?: boolean
skipConfig?: boolean
/**
* @default false
*/
skipDockerCompose?: boolean
configureConfig?: boolean configureConfig?: boolean
generateLockfile?: boolean generateLockfile?: boolean
} }
@@ -50,12 +56,13 @@ main().catch((error) => {
}) })
async function main() { async function main() {
const args = minimist(process.argv.slice(2))
const template = args['template'] // template directory name
const templatesDir = path.resolve(dirname, '../templates') 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 templateRepoUrlBase = `https://github.com/payloadcms/payload/tree/main/templates`
const variations: TemplateVariations[] = [ let variations: TemplateVariations[] = [
{ {
name: 'payload-vercel-postgres-template', name: 'payload-vercel-postgres-template',
dirname: 'with-vercel-postgres', dirname: 'with-vercel-postgres',
@@ -132,20 +139,22 @@ async function main() {
generateLockfile: true, generateLockfile: true,
storage: 'localDisk', storage: 'localDisk',
sharp: true, 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, // 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. // so we do not configure the payload.config.ts file, which leaves the placeholder comments.
configureConfig: false, 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 { for (const {
name, name,
base, base,
@@ -158,6 +167,8 @@ async function main() {
sharp, sharp,
configureConfig, configureConfig,
skipReadme = false, skipReadme = false,
skipConfig = false,
skipDockerCompose = false,
} of variations) { } of variations) {
header(`Generating ${name}...`) header(`Generating ${name}...`)
const destDir = path.join(templatesDir, dirname) const destDir = path.join(templatesDir, dirname)
@@ -167,21 +178,24 @@ async function main() {
'.next', '.next',
'.env$', '.env$',
'pnpm-lock.yaml', 'pnpm-lock.yaml',
...(skipReadme ? ['README.md'] : ['']), ...(skipReadme ? ['README.md'] : []),
...(skipDockerCompose ? ['docker-compose.yml'] : []),
...(skipConfig ? ['payload.config.ts'] : []),
]) ])
log(`Copied to ${destDir}`) log(`Copied to ${destDir}`)
if (configureConfig !== false) { if (configureConfig !== false) {
log('Configuring payload.config.ts') log('Configuring payload.config.ts')
await configurePayloadConfig({ const configureArgs = {
dbType: db, dbType: db,
packageJsonName: name, packageJsonName: name,
projectDirOrConfigPath: { projectDir: destDir }, projectDirOrConfigPath: { projectDir: destDir },
storageAdapter: storage, storageAdapter: storage,
sharp, sharp,
envNames, envNames,
}) }
await configurePayloadConfig(configureArgs)
log('Configuring .env.example') log('Configuring .env.example')
// Replace DATABASE_URI with the correct env name if set // Replace DATABASE_URI with the correct env name if set