feat(cpa): handle next.js app with and without src dir
This commit is contained in:
@@ -7,12 +7,8 @@ import fs from 'fs'
|
||||
import path from 'path'
|
||||
import shelljs from 'shelljs'
|
||||
import tempy from 'tempy'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { promisify } from 'util'
|
||||
|
||||
const filename = fileURLToPath(import.meta.url)
|
||||
const dirname = path.dirname(filename)
|
||||
|
||||
const readFile = promisify(fs.readFile)
|
||||
const writeFile = promisify(fs.writeFile)
|
||||
|
||||
@@ -29,13 +25,6 @@ describe('create-payload-app', () => {
|
||||
shelljs.exec('pnpm build:create-payload-app')
|
||||
})
|
||||
|
||||
describe('Next.js app template files', () => {
|
||||
it('should exist in dist', () => {
|
||||
const distPath = path.resolve(dirname, '../../packages/create-payload-app/dist/app/(payload)')
|
||||
expect(fs.existsSync(distPath)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe.each(Object.keys(nextCreateCommands))(`--init-next with %s`, (nextCmdKey) => {
|
||||
const projectDir = tempy.directory()
|
||||
beforeEach(async () => {
|
||||
@@ -74,19 +63,51 @@ describe('create-payload-app', () => {
|
||||
it('should install payload app in Next.js project', async () => {
|
||||
expect(fs.existsSync(projectDir)).toBe(true)
|
||||
|
||||
const result = await initNext({
|
||||
const firstResult = await initNext({
|
||||
'--debug': true,
|
||||
projectDir,
|
||||
nextConfigPath: path.resolve(projectDir, 'next.config.mjs'),
|
||||
useDistFiles: true, // create-payload-app/dist/app/(payload)
|
||||
packageManager: 'pnpm',
|
||||
})
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
// Will fail because we detect top-level layout.tsx file
|
||||
expect(firstResult.success).toEqual(false)
|
||||
|
||||
const payloadFilesPath = path.resolve(result.userAppDir, '(payload)')
|
||||
// Move all files from app to top-level directory named `(app)`
|
||||
if (firstResult.success === false && 'nextAppDir' in firstResult) {
|
||||
fs.mkdirSync(path.resolve(firstResult.nextAppDir, '(app)'))
|
||||
fs.readdirSync(path.resolve(firstResult.nextAppDir)).forEach((file) => {
|
||||
if (file === '(app)') return
|
||||
fs.renameSync(
|
||||
path.resolve(firstResult.nextAppDir, file),
|
||||
path.resolve(firstResult.nextAppDir, '(app)', file),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// Rerun after moving files
|
||||
const result = await initNext({
|
||||
'--debug': true,
|
||||
projectDir,
|
||||
nextConfigPath: path.resolve(projectDir, 'next.config.mjs'),
|
||||
useDistFiles: true, // create-payload-app/dist/app/(payload)
|
||||
packageManager: 'pnpm',
|
||||
})
|
||||
|
||||
expect(result.success).toEqual(true)
|
||||
expect(result.nextAppDir).toEqual(
|
||||
path.resolve(projectDir, result.isSrcDir ? 'src/app' : 'app'),
|
||||
)
|
||||
|
||||
const payloadFilesPath = path.resolve(result.nextAppDir, '(payload)')
|
||||
// shelljs.exec(`tree ${projectDir}`)
|
||||
expect(fs.existsSync(payloadFilesPath)).toBe(true)
|
||||
|
||||
const payloadConfig = path.resolve(projectDir, 'payload.config.ts')
|
||||
const payloadConfig = path.resolve(
|
||||
projectDir,
|
||||
result.isSrcDir ? 'src/payload.config.ts' : 'payload.config.ts',
|
||||
)
|
||||
expect(fs.existsSync(payloadConfig)).toBe(true)
|
||||
|
||||
const tsConfigPath = path.resolve(projectDir, 'tsconfig.json')
|
||||
@@ -95,7 +116,7 @@ describe('create-payload-app', () => {
|
||||
compilerOptions?: CompilerOptions
|
||||
}
|
||||
expect(userTsConfig.compilerOptions.paths?.['@payload-config']).toStrictEqual([
|
||||
'./payload.config.ts',
|
||||
`./${result.isSrcDir ? 'src/' : ''}payload.config.ts`,
|
||||
])
|
||||
|
||||
// TODO: Start the Next.js app and check if it runs
|
||||
|
||||
Reference in New Issue
Block a user