This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
nuxt.ts
74 lines (62 loc) · 1.88 KB
/
nuxt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { existsSync, promises as fsp } from 'node:fs'
import { resolve } from 'node:path'
import { defu } from 'defu'
import * as _kit from '@nuxt/kit'
import { useTestContext } from './context'
// @ts-ignore type cast
// eslint-disable-next-line
const kit: typeof _kit = _kit.default || _kit
const isNuxtApp = (dir: string) => {
return existsSync(dir) && (
existsSync(resolve(dir, 'pages')) ||
existsSync(resolve(dir, 'nuxt.config.js')) ||
existsSync(resolve(dir, 'nuxt.config.mjs')) ||
existsSync(resolve(dir, 'nuxt.config.cjs')) ||
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()
if (!ctx.options.dev) {
const randomId = Math.random().toString(36).slice(2, 8)
const buildDir = resolve(ctx.options.rootDir, '.nuxt', randomId)
ctx.options.nuxtConfig = defu(ctx.options.nuxtConfig, {
buildDir,
nitro: {
output: {
dir: resolve(buildDir, 'output')
}
}
})
}
ctx.nuxt = await kit.loadNuxt({
cwd: ctx.options.rootDir,
dev: ctx.options.dev,
overrides: ctx.options.nuxtConfig,
configFile: ctx.options.configFile
})
await fsp.mkdir(ctx.nuxt.options.buildDir, { recursive: true })
}
export async function buildFixture () {
const ctx = useTestContext()
// Hide build info for test
const prevLevel = kit.logger.level
kit.logger.level = ctx.options.logLevel
await kit.buildNuxt(ctx.nuxt!)
kit.logger.level = prevLevel
}