chore: logging for init-next
This commit is contained in:
@@ -5,31 +5,39 @@ import path from 'path'
|
|||||||
import type { CliArgs } from '../types'
|
import type { CliArgs } from '../types'
|
||||||
|
|
||||||
import { copyRecursiveSync } from '../utils/copy-recursive-sync'
|
import { copyRecursiveSync } from '../utils/copy-recursive-sync'
|
||||||
|
import { error, info, debug as origDebug, success } from '../utils/log'
|
||||||
|
|
||||||
export async function initNext(
|
export async function initNext(
|
||||||
args: Pick<CliArgs, '--debug'> & { nextDir?: string; useDistFiles?: boolean },
|
args: Pick<CliArgs, '--debug'> & { nextDir?: string; useDistFiles?: boolean },
|
||||||
): Promise<{ success: boolean }> {
|
): Promise<{ success: boolean }> {
|
||||||
const { '--debug': debug, nextDir, useDistFiles } = args
|
const { '--debug': debug, nextDir, useDistFiles } = args
|
||||||
|
|
||||||
|
info('Initializing Payload app in Next.js project', 1)
|
||||||
|
|
||||||
|
const logDebug = (message: string) => {
|
||||||
|
if (debug) origDebug(message)
|
||||||
|
}
|
||||||
|
|
||||||
let projectDir = process.cwd()
|
let projectDir = process.cwd()
|
||||||
if (nextDir) {
|
if (nextDir) {
|
||||||
projectDir = path.resolve(projectDir, nextDir)
|
projectDir = path.resolve(projectDir, nextDir)
|
||||||
console.log(`Overriding project directory to ${projectDir}`)
|
if (debug) logDebug(`Overriding project directory to ${projectDir}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fs.existsSync(projectDir)) {
|
if (!fs.existsSync(projectDir)) {
|
||||||
console.log(`Could not find specified project directory at ${projectDir}`)
|
error(`Could not find specified project directory at ${projectDir}`)
|
||||||
return { success: false }
|
return { success: false }
|
||||||
}
|
}
|
||||||
|
|
||||||
const foundConfig = (await globby('next.config.*js', { cwd: projectDir }))?.[0]
|
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(
|
error(
|
||||||
`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.`,
|
||||||
)
|
)
|
||||||
return { success: false }
|
return { success: false }
|
||||||
} else {
|
} else {
|
||||||
if (debug) console.log(`Found Next config at ${nextConfigPath}`)
|
if (debug) logDebug(`Found Next config at ${nextConfigPath}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const templateFilesPath =
|
const templateFilesPath =
|
||||||
@@ -37,23 +45,25 @@ export async function initNext(
|
|||||||
? path.resolve(__dirname, '../..', 'dist/app')
|
? path.resolve(__dirname, '../..', 'dist/app')
|
||||||
: path.resolve(__dirname, '../../../next/src/app')
|
: path.resolve(__dirname, '../../../next/src/app')
|
||||||
|
|
||||||
console.log(`Using template files from: ${templateFilesPath}`)
|
if (debug) logDebug(`Using template files from: ${templateFilesPath}`)
|
||||||
|
|
||||||
if (!fs.existsSync(templateFilesPath)) {
|
if (!fs.existsSync(templateFilesPath)) {
|
||||||
console.log(`Could not find template source files from ${templateFilesPath}`)
|
error(`Could not find template source files from ${templateFilesPath}`)
|
||||||
return { success: false }
|
return { success: false }
|
||||||
} else {
|
} else {
|
||||||
console.log('Found template source files')
|
if (debug) logDebug('Found template source files')
|
||||||
}
|
}
|
||||||
|
|
||||||
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}`)
|
error(`Could not find user app directory at ${userAppDir}`)
|
||||||
return { success: false }
|
return { success: false }
|
||||||
} else {
|
} else {
|
||||||
if (debug) console.log(`Found user app directory: ${userAppDir}`)
|
logDebug(`Found user app directory: ${userAppDir}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logDebug(`Copying template files from ${templateFilesPath} to ${userAppDir}`)
|
||||||
copyRecursiveSync(templateFilesPath, userAppDir, debug)
|
copyRecursiveSync(templateFilesPath, userAppDir, debug)
|
||||||
|
success('Successfully initialized.')
|
||||||
return { success: true }
|
return { success: true }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,11 +11,9 @@ export function copyRecursiveSync(src: string, dest: string, debug?: boolean) {
|
|||||||
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 (debug) console.log(`Copying: ${src}/${childItemName} -> ${dest}/${childItemName}`)
|
|
||||||
copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName))
|
copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName))
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
if (debug) console.log(`Copying: ${src} -> ${dest}`)
|
|
||||||
fs.copyFileSync(src, dest)
|
fs.copyFileSync(src, dest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,18 @@ export const warning = (message: string): void => {
|
|||||||
console.log(chalk.yellow('? ') + chalk.bold(message))
|
console.log(chalk.yellow('? ') + chalk.bold(message))
|
||||||
}
|
}
|
||||||
|
|
||||||
export const info = (message: string): void => {
|
export const info = (message: string, paddingTop?: number): void => {
|
||||||
console.log(`${chalk.yellow(figures.info)} ${chalk.bold(message)}`)
|
console.log(
|
||||||
|
`${'\n'.repeat(paddingTop || 0)}${chalk.green(figures.pointerSmall)} ${chalk.bold(message)}`,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const error = (message: string): void => {
|
export const error = (message: string): void => {
|
||||||
console.log(`${chalk.red(figures.cross)} ${chalk.bold(message)}`)
|
console.log(`${chalk.red(figures.cross)} ${chalk.bold(message)}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const debug = (message: string): void => {
|
||||||
|
console.log(
|
||||||
|
`${chalk.gray(figures.pointerSmall)} ${chalk.bgGray('[DEBUG]')} ${chalk.gray(message)}`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user