2022-04-15 15:19:05 +00:00
|
|
|
import { existsSync, promises as fsp } from 'node:fs'
|
|
|
|
import { resolve } from 'node:path'
|
2023-03-23 00:27:13 +00:00
|
|
|
import { defu } from 'defu'
|
2022-02-11 13:22:58 +00:00
|
|
|
import * as _kit from '@nuxt/kit'
|
|
|
|
import { useTestContext } from './context'
|
|
|
|
|
|
|
|
// @ts-ignore type cast
|
2023-03-11 21:16:01 +00:00
|
|
|
// eslint-disable-next-line
|
2022-02-11 13:22:58 +00:00
|
|
|
const kit: typeof _kit = _kit.default || _kit
|
|
|
|
|
|
|
|
const isNuxtApp = (dir: string) => {
|
|
|
|
return existsSync(dir) && (
|
|
|
|
existsSync(resolve(dir, 'pages')) ||
|
|
|
|
existsSync(resolve(dir, 'nuxt.config.js')) ||
|
2022-11-10 08:43:32 +00:00
|
|
|
existsSync(resolve(dir, 'nuxt.config.mjs')) ||
|
|
|
|
existsSync(resolve(dir, 'nuxt.config.cjs')) ||
|
2022-02-11 13:22:58 +00:00
|
|
|
existsSync(resolve(dir, 'nuxt.config.ts'))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const resolveRootDir = () => {
|
|
|
|
const { options } = useTestContext()
|
|
|
|
|
|
|
|
const dirs = [
|
|
|
|
options.rootDir,
|
|
|
|
resolve(options.testDir, options.fixture),
|
|
|
|
process.cwd()
|
|
|
|
]
|
|
|
|
|
|
|
|
for (const dir of dirs) {
|
|
|
|
if (dir && isNuxtApp(dir)) {
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Invalid nuxt app. (Please explicitly set `options.rootDir` pointing to a valid nuxt app)')
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function loadFixture () {
|
|
|
|
const ctx = useTestContext()
|
|
|
|
|
|
|
|
ctx.options.rootDir = resolveRootDir()
|
|
|
|
|
2022-04-07 19:15:30 +00:00
|
|
|
if (!ctx.options.dev) {
|
|
|
|
const randomId = Math.random().toString(36).slice(2, 8)
|
|
|
|
const buildDir = resolve(ctx.options.rootDir, '.nuxt', randomId)
|
2023-03-23 00:27:13 +00:00
|
|
|
ctx.options.nuxtConfig = defu(ctx.options.nuxtConfig, {
|
2022-04-07 19:15:30 +00:00
|
|
|
buildDir,
|
|
|
|
nitro: {
|
|
|
|
output: {
|
|
|
|
dir: resolve(buildDir, 'output')
|
|
|
|
}
|
2022-02-11 13:22:58 +00:00
|
|
|
}
|
2022-04-07 19:15:30 +00:00
|
|
|
})
|
|
|
|
}
|
2022-02-11 13:22:58 +00:00
|
|
|
|
|
|
|
ctx.nuxt = await kit.loadNuxt({
|
2022-03-17 20:10:12 +00:00
|
|
|
cwd: ctx.options.rootDir,
|
2022-03-17 21:31:06 +00:00
|
|
|
dev: ctx.options.dev,
|
2022-03-17 20:10:12 +00:00
|
|
|
overrides: ctx.options.nuxtConfig,
|
2022-02-11 13:22:58 +00:00
|
|
|
configFile: ctx.options.configFile
|
|
|
|
})
|
|
|
|
|
|
|
|
await fsp.mkdir(ctx.nuxt.options.buildDir, { recursive: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function buildFixture () {
|
|
|
|
const ctx = useTestContext()
|
2022-12-02 09:42:23 +00:00
|
|
|
// Hide build info for test
|
|
|
|
const prevLevel = kit.logger.level
|
|
|
|
kit.logger.level = ctx.options.logLevel
|
2022-08-26 15:47:29 +00:00
|
|
|
await kit.buildNuxt(ctx.nuxt!)
|
2022-12-02 09:42:23 +00:00
|
|
|
kit.logger.level = prevLevel
|
2022-02-11 13:22:58 +00:00
|
|
|
}
|