chore: --init-next improvements and tests

This commit is contained in:
Elliot DeNolf
2024-02-28 14:16:05 -05:00
parent b9dec2f714
commit cdc1e024f1
4 changed files with 74 additions and 13 deletions

View File

@@ -6,22 +6,34 @@ import type { CliArgs } from '../types'
import { copyRecursiveSync } from '../utils/copy-recursive-sync' import { copyRecursiveSync } from '../utils/copy-recursive-sync'
export async function initNext(args: CliArgs): Promise<void> { export async function initNext(
const { '--debug': debug } = args args: Pick<CliArgs, '--debug'> & { nextDir?: string; useDistFiles?: boolean },
const projectDir = process.cwd() ): Promise<{ success: boolean }> {
const { '--debug': debug, nextDir, useDistFiles } = args
let projectDir = process.cwd()
if (nextDir) {
projectDir = path.resolve(projectDir, nextDir)
console.log(`Overriding project directory to ${projectDir}`)
}
const foundConfig = (await globby('next.config.*js', { cwd: process.cwd() }))?.[0] if (!fs.existsSync(projectDir)) {
console.log(`Could not find specified project directory at ${projectDir}`)
return { success: false }
}
const foundConfig = (await globby('next.config.*js', { cwd: projectDir }))?.[0]
const nextConfigPath = path.resolve(projectDir, foundConfig) const nextConfigPath = path.resolve(projectDir, foundConfig)
if (!fs.existsSync(nextConfigPath)) { if (!fs.existsSync(nextConfigPath)) {
console.log( console.log(
`No next.config.js found at ${nextConfigPath}. Ensure you are in a Next.js project directory.`, `No next.config.js found at ${nextConfigPath}. Ensure you are in a Next.js project directory.`,
) )
process.exit(1) return { success: false }
} else { } else {
if (debug) console.log(`Found Next config at ${nextConfigPath}`) if (debug) console.log(`Found Next config at ${nextConfigPath}`)
} }
const templateFilesPath = __dirname.endsWith('dist') const templateFilesPath =
__dirname.endsWith('dist') || useDistFiles
? path.resolve(__dirname, '../..', 'dist/app') ? path.resolve(__dirname, '../..', 'dist/app')
: path.resolve(__dirname, '../../../next/src/app') : path.resolve(__dirname, '../../../next/src/app')
@@ -29,7 +41,7 @@ export async function initNext(args: CliArgs): Promise<void> {
if (!fs.existsSync(templateFilesPath)) { if (!fs.existsSync(templateFilesPath)) {
console.log(`Could not find template source files from ${templateFilesPath}`) console.log(`Could not find template source files from ${templateFilesPath}`)
process.exit(1) return { success: false }
} else { } else {
console.log('Found template source files') console.log('Found template source files')
} }
@@ -37,11 +49,11 @@ export async function initNext(args: CliArgs): Promise<void> {
const userAppDir = path.resolve(projectDir, 'src/app') const userAppDir = path.resolve(projectDir, 'src/app')
if (!fs.existsSync(userAppDir)) { if (!fs.existsSync(userAppDir)) {
console.log(`Could not find user app directory at ${userAppDir}`) console.log(`Could not find user app directory at ${userAppDir}`)
process.exit(1) return { success: false }
} else { } else {
if (debug) console.log(`Found user app directory: ${userAppDir}`) if (debug) console.log(`Found user app directory: ${userAppDir}`)
} }
copyRecursiveSync(templateFilesPath, userAppDir, debug) copyRecursiveSync(templateFilesPath, userAppDir, debug)
process.exit(0) return { success: true }
} }

View File

@@ -60,8 +60,8 @@ export class Main {
} }
if (this.args['--init-next']) { if (this.args['--init-next']) {
await initNext(this.args) const result = await initNext(this.args)
process.exit(0) process.exit(result.success ? 0 : 1)
} }
const templateArg = this.args['--template'] const templateArg = this.args['--template']

1
test/create-payload-app/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
test-app

View File

@@ -0,0 +1,48 @@
import fs from 'fs'
import path from 'path'
import shelljs from 'shelljs'
import { initNext } from '../../packages/create-payload-app/src/lib/init-next'
describe('create-payload-app', () => {
describe('--init-next', () => {
const nextDir = path.resolve(__dirname, 'test-app')
beforeAll(() => {
if (fs.existsSync(nextDir)) {
fs.rmdirSync(nextDir, { recursive: true })
}
// Create dir for Next.js project
if (!fs.existsSync(nextDir)) {
fs.mkdirSync(nextDir)
}
// Create a new Next.js project with default options
shelljs.exec(
'pnpm create next-app@latest . --typescript --eslint --no-tailwind --app --import-alias="@/*" --src-dir',
{ cwd: nextDir },
)
})
afterAll(() => {
if (fs.existsSync(nextDir)) {
fs.rmdirSync(nextDir, { recursive: true })
}
})
it('should install payload app in Next.js project', async () => {
expect(fs.existsSync(nextDir)).toBe(true)
const result = await initNext({
'--debug': true,
nextDir,
useDistFiles: true, // create-payload-app must be built
})
expect(result.success).toBe(true)
const payloadFilesPath = path.resolve(nextDir, 'src/app/(payload)')
expect(fs.existsSync(payloadFilesPath)).toBe(true)
})
})
})