-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin): admin improvements (#524)
* wip: components playground * fix: switch to nuxt module for components preview * refactor(admin): modulize preview * refactor(admin): folder structure * chore: typo * feat(admin): embed windicss-analysis * chore: update notes * feat: config tab * feat: preview navigate to editor * fix(admin): reuse utils instance for windi analysis * chore: update deps * feat(admin): entry for windi analyzer * chore: clean up * Apply suggestions from code review Co-authored-by: Sébastien Chopin <seb@nuxtjs.com> * 🚨 (lint) no-console fix * chore: cleanup vite fix Co-authored-by: Sébastien Chopin <seb@nuxtjs.com> Co-authored-by: Yaël GUILLOUX <yael.guilloux@gmail.com>
- Loading branch information
1 parent
98c3603
commit dc0caa0
Showing
22 changed files
with
575 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { promises as fs } from 'fs' | ||
import { join, extname } from 'path' | ||
import { createError, Middleware, useBody } from 'h3' | ||
import dirTree from 'directory-tree' | ||
import { FileData, File } from '../../type' | ||
import { normalizeFiles, r } from '../utils' | ||
|
||
export default <Middleware>async function componentsHandler(req) { | ||
const url = req.url | ||
|
||
if (req.method === 'GET') { | ||
// List all files in components/ | ||
if (url === '/') { | ||
const tree = dirTree(r('components')) | ||
return normalizeFiles(tree.children, r('components')) | ||
} | ||
// Read a single content file | ||
try { | ||
const path = join(r('components'), url) | ||
const file = await fs.readFile(path, 'utf-8') | ||
|
||
return <File>{ | ||
path: path.replace(r('components'), ''), | ||
extension: extname(path), | ||
raw: file | ||
} | ||
} catch (err) { | ||
return createError({ | ||
statusCode: 404, | ||
statusMessage: 'File not found' | ||
}) | ||
} | ||
} | ||
|
||
// Update changes | ||
if (req.method === 'PUT') { | ||
const { raw } = await useBody<FileData>(req) | ||
if (raw == null) { | ||
return createError({ | ||
statusCode: 400, | ||
statusMessage: '"raw" key is required' | ||
}) | ||
} | ||
|
||
const path = join(r('components'), url) | ||
|
||
try { | ||
// @ts-ignore | ||
// await fs.stat(path, 'utf-8') | ||
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' | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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' | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ref } from 'vue3' | ||
import { useApi } from '../plugins/api' | ||
import { File } from '../../type' | ||
import { getRoutePath, navigateToFile } from './preview' | ||
|
||
const api = useApi() | ||
|
||
export const files = ref<File[]>([]) | ||
export const currentFile = ref(null) | ||
|
||
export const openFile = async (file: File) => { | ||
if (currentFile.value?.path === file.path) return | ||
|
||
navigateToFile(file.path) | ||
currentFile.value = await api.get(`/content${file.path}`) | ||
} | ||
|
||
export async function fetchFiles() { | ||
files.value = await api.get('/content') | ||
} | ||
|
||
export function findFileFromRoute(routePath: string, fileList: File[] = files.value) { | ||
for (const file of fileList) { | ||
if (getRoutePath(file.path) === routePath) { | ||
return file | ||
} | ||
if (file.children) { | ||
const result = findFileFromRoute(routePath, file.children) | ||
if (result) return result | ||
} | ||
} | ||
} | ||
|
||
export function onPreviewNavigated(routePath: string) { | ||
const file = findFileFromRoute(routePath) | ||
if (file) openFile(file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.