Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(templates): add generated templates [no-lint] #6604

Merged
merged 10 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ jobs:
echo "templates: ${{ steps.filter.outputs.templates }}"

lint:
if: github.event_name == 'pull_request'
if: >
github.event_name == 'pull_request' && !contains(github.event.pull_request.title, 'no-lint') && !contains(github.event.pull_request.title, 'skip-lint')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -500,6 +501,36 @@ jobs:
yarn build
yarn generate:types

generated-templates:
needs: build
runs-on: ubuntu-latest

steps:
# https://github.com/actions/virtual-environments/issues/1187
- name: tune linux network
run: sudo ethtool -K eth0 tx off rx off

- name: Setup Node@${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}

- name: Install pnpm
uses: pnpm/action-setup@v3
with:
version: ${{ env.PNPM_VERSION }}
run_install: false

- name: Restore build
uses: actions/cache@v4
timeout-minutes: 10
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}

- name: Build all generated templates
run: pnpm tsx ./scripts/build-generated-templates.ts

all-green:
name: All Green
if: always()
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"reinstall": "pnpm clean:all && pnpm install",
"release:alpha": "tsx ./scripts/release.ts --bump prerelease --tag alpha",
"release:beta": "tsx ./scripts/release.ts --bump prerelease --tag beta",
"script:gen-templates": "tsx ./scripts/generate-template-variations.ts",
"script:list-published": "tsx scripts/lib/getPackageRegistryVersions.ts",
"script:pack": "tsx scripts/pack-all-to-dest.ts",
"pretest": "pnpm build",
Expand Down Expand Up @@ -112,6 +113,7 @@
"comment-json": "^4.2.3",
"concat-stream": "^2.0.0",
"copyfiles": "2.4.1",
"create-payload-app": "workspace:*",
"cross-env": "7.0.3",
"dotenv": "8.6.0",
"drizzle-kit": "0.20.14-1f2c838",
Expand Down
14 changes: 14 additions & 0 deletions packages/create-payload-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,24 @@
"license": "MIT",
"type": "module",
"exports": {
"./types": {
"import": "./src/types.ts",
"require": "./src/types.ts"
},
"./commands": {
"import": "./src/lib/init-next.ts",
"require": "./src/lib/init-next.ts",
"types": "./src/lib/init-next.ts"
},
"./lib/*": {
"import": "./src/lib/*",
"require": "./src/lib/*",
"types": "./src/lib/*"
},
"./utils/*": {
"import": "./src/utils/*",
"require": "./src/utils/*",
"types": "./src/utils/*"
}
},
"bin": {
Expand Down
129 changes: 98 additions & 31 deletions packages/create-payload-app/src/lib/configure-payload-config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import fse from 'fs-extra'
import globby from 'globby'
import { fileURLToPath } from 'node:url'
import path from 'path'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)

import type { DbDetails } from '../types.js'
import type { DbType, StorageAdapterType } from '../types.js'

import { warning } from '../utils/log.js'
import { dbReplacements } from './packages.js'
import { configReplacements, dbReplacements, storageReplacements } from './replacements.js'

/** Update payload config with necessary imports and adapters */
export async function configurePayloadConfig(args: {
dbDetails: DbDetails | undefined
dbType?: DbType
envNames?: {
dbUri: string
}
packageJsonName?: string
projectDirOrConfigPath: { payloadConfigPath: string } | { projectDir: string }
storageAdapter?: StorageAdapterType
sharp?: boolean
}): Promise<void> {
if (!args.dbDetails) {
if (!args.dbType) {
return
}

Expand All @@ -28,7 +31,7 @@ export async function configurePayloadConfig(args: {
try {
const packageObj = await fse.readJson(packageJsonPath)

const dbPackage = dbReplacements[args.dbDetails.type]
const dbPackage = dbReplacements[args.dbType]

// Delete all other db adapters
Object.values(dbReplacements).forEach((p) => {
Expand All @@ -40,6 +43,24 @@ export async function configurePayloadConfig(args: {
// Set version of db adapter to match payload version
packageObj.dependencies[dbPackage.packageName] = packageObj.dependencies['payload']

if (args.storageAdapter) {
const storagePackage = storageReplacements[args.storageAdapter]

if (storagePackage?.packageName) {
// Set version of storage adapter to match payload version
packageObj.dependencies[storagePackage.packageName] = packageObj.dependencies['payload']
}
}

// Sharp provided by default, only remove if explicitly set to false
if (args.sharp === false) {
delete packageObj.dependencies['sharp']
}

if (args.packageJsonName) {
packageObj.name = args.packageJsonName
}

await fse.writeJson(packageJsonPath, packageObj, { spaces: 2 })
} catch (err: unknown) {
warning(`Unable to configure Payload in package.json`)
Expand All @@ -66,35 +87,54 @@ export async function configurePayloadConfig(args: {
}

const configContent = fse.readFileSync(payloadConfigPath, 'utf-8')
const configLines = configContent.split('\n')
let configLines = configContent.split('\n')

const dbReplacement = dbReplacements[args.dbDetails.type]
// DB Replacement
const dbReplacement = dbReplacements[args.dbType]

let dbConfigStartLineIndex: number | undefined
let dbConfigEndLineIndex: number | undefined
configLines = replaceInConfigLines({
replacement: dbReplacement.configReplacement(args.envNames?.dbUri),
startMatch: `// database-adapter-config-start`,
endMatch: `// database-adapter-config-end`,
lines: configLines,
})

configLines.forEach((l, i) => {
if (l.includes('// database-adapter-import')) {
configLines[i] = dbReplacement.importReplacement
}
configLines = replaceInConfigLines({
replacement: [dbReplacement.importReplacement],
startMatch: '// database-adapter-import',
lines: configLines,
})

if (l.includes('// database-adapter-config-start')) {
dbConfigStartLineIndex = i
}
if (l.includes('// database-adapter-config-end')) {
dbConfigEndLineIndex = i
// Storage Adapter Replacement
if (args.storageAdapter) {
const replacement = storageReplacements[args.storageAdapter]
configLines = replaceInConfigLines({
replacement: replacement.configReplacement,
startMatch: '// storage-adapter-placeholder',
lines: configLines,
})

if (replacement?.importReplacement) {
configLines = replaceInConfigLines({
replacement: [replacement.importReplacement],
startMatch: '// storage-adapter-import-placeholder',
lines: configLines,
})
}
})
}

if (!dbConfigStartLineIndex || !dbConfigEndLineIndex) {
warning('Unable to update payload.config.ts with database adapter import')
} else {
// Replaces lines between `// database-adapter-config-start` and `// database-adapter-config-end`
configLines.splice(
dbConfigStartLineIndex,
dbConfigEndLineIndex - dbConfigStartLineIndex + 1,
...dbReplacement.configReplacement,
)
// Sharp Replacement (provided by default, only remove if explicitly set to false)
if (args.sharp === false) {
configLines = replaceInConfigLines({
replacement: [],
startMatch: 'sharp,',
lines: configLines,
})
configLines = replaceInConfigLines({
replacement: [],
startMatch: "import sharp from 'sharp'",
lines: configLines,
})
}

fse.writeFileSync(payloadConfigPath, configLines.join('\n'))
Expand All @@ -104,3 +144,30 @@ export async function configurePayloadConfig(args: {
)
}
}

function replaceInConfigLines({
replacement,
startMatch,
endMatch,
lines,
}: {
replacement: string[]
startMatch: string
/** Optional endMatch to replace multiple lines */
endMatch?: string
lines: string[]
}) {
if (!replacement) {
return lines
}

const startIndex = lines.findIndex((l) => l.includes(startMatch))
const endIndex = endMatch ? lines.findIndex((l) => l.includes(endMatch)) : startIndex

if (startIndex === -1 || endIndex === -1) {
return lines
}

lines.splice(startIndex, endIndex - startIndex + 1, ...replacement)
return lines
}
4 changes: 2 additions & 2 deletions packages/create-payload-app/src/lib/create-project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fse from 'fs-extra'
import path from 'path'
import type { CliArgs, DbType, ProjectTemplate } from '../types.js'
import { createProject } from './create-project.js'
import { dbReplacements } from './packages.js'
import { dbReplacements } from './replacements.js'
import { getValidTemplates } from './templates.js'
import globby from 'globby'

Expand Down Expand Up @@ -124,7 +124,7 @@ describe('createProject', () => {

expect(content).not.toContain('// database-adapter-config-start')
expect(content).not.toContain('// database-adapter-config-end')
expect(content).toContain(dbReplacement.configReplacement.join('\n'))
expect(content).toContain(dbReplacement.configReplacement().join('\n'))
})
})
})
Expand Down
5 changes: 4 additions & 1 deletion packages/create-payload-app/src/lib/create-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export async function createProject(args: {

await updatePackageJSON({ projectDir, projectName })
spinner.message('Configuring Payload...')
await configurePayloadConfig({ dbDetails, projectDirOrConfigPath: { projectDir } })
await configurePayloadConfig({
dbType: dbDetails?.type,
projectDirOrConfigPath: { projectDir },
})

// Remove yarn.lock file. This is only desired in Payload Cloud.
const lockPath = path.resolve(projectDir, 'yarn.lock')
Expand Down
35 changes: 0 additions & 35 deletions packages/create-payload-app/src/lib/packages.ts

This file was deleted.

Loading