-
Notifications
You must be signed in to change notification settings - Fork 154
/
config.ts
49 lines (43 loc) · 1.13 KB
/
config.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
import { parse } from 'path'
import fs from 'fs-extra'
import { createError, Middleware, useBody } from 'h3'
import { FileData } from '../../type'
import { r } from '../utils'
export default <Middleware>async function configHandler(req) {
const root = r()
let path = [r('nuxt.config.ts'), r('nuxt.config.js')].find(i => fs.existsSync(i))
const exist = Boolean(path)
path = path || r('nuxt.config.ts')
if (req.method === 'GET') {
// Get config file
return {
path: path.replace(root, ''),
exist,
extension: parse(path).ext,
raw: exist ? await fs.readFile(path, 'utf-8') : ''
}
}
// Update config
if (req.method === 'PUT') {
const { raw } = await useBody<FileData>(req)
if (raw == null) {
return createError({
statusCode: 400,
statusMessage: '"raw" key is required'
})
}
try {
await fs.writeFile(path, raw)
return { ok: true }
} catch (err) {
return createError({
statusCode: 404,
statusMessage: 'File not found'
})
}
}
return createError({
statusCode: 400,
statusMessage: 'Bad Request'
})
}