Adds the ability to create a project using an existing in the Payload repo example through `create-payload-app`: For example: `pnpx create-payload-app --example custom-server` - creates a project from the [custom-server](https://github.com/payloadcms/payload/tree/main/examples/custom-server) example. This is much easier and faster then downloading the whole repo and copying the example to another folder. Note that we don't configure the payload config with the storage / DB adapter there because examples can be very specific.
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import type { ProjectExample } from '../types.js'
|
|
|
|
import { error, info } from '../utils/log.js'
|
|
|
|
export async function getExamples({ branch }: { branch: string }): Promise<ProjectExample[]> {
|
|
const url = `https://api.github.com/repos/payloadcms/payload/contents/examples?ref=${branch}`
|
|
|
|
const response = await fetch(url)
|
|
|
|
const examplesResponseList: { name: string; path: string }[] = await response.json()
|
|
|
|
const examples: ProjectExample[] = examplesResponseList.map((example) => ({
|
|
name: example.name,
|
|
url: `https://github.com/payloadcms/payload/examples/${example.name}#${branch}`,
|
|
}))
|
|
|
|
return examples
|
|
}
|
|
|
|
export async function parseExample({
|
|
name,
|
|
branch,
|
|
}: {
|
|
branch: string
|
|
name: string
|
|
}): Promise<false | ProjectExample> {
|
|
const examples = await getExamples({ branch })
|
|
|
|
const example = examples.find((e) => e.name === name)
|
|
|
|
if (!example) {
|
|
error(`'${name}' is not a valid example name.`)
|
|
info(`Valid examples: ${examples.map((e) => e.name).join(', ')}`)
|
|
return false
|
|
}
|
|
|
|
return example
|
|
}
|