-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(admin): admin improvements #524
Merged
Merged
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d2f1e99
wip: components playground
antfu e66d3b9
fix: switch to nuxt module for components preview
antfu 6551c10
refactor(admin): modulize preview
antfu 63db4af
refactor(admin): folder structure
antfu 113bcd6
chore: typo
antfu 90a3468
feat(admin): embed windicss-analysis
antfu cbfb219
chore: update notes
antfu 3c2ff4a
feat: config tab
antfu 08e806a
feat: preview navigate to editor
antfu 0917efb
fix(admin): reuse utils instance for windi analysis
antfu e3df45b
chore: update deps
antfu 49ef13e
feat(admin): entry for windi analyzer
antfu eb509c8
Merge branch 'dev' into feat/admin
antfu cb7eae8
chore: clean up
antfu 5e3c89b
Apply suggestions from code review
antfu 1822c7a
:rotating_light: (lint) no-console fix
Tahul a74c1a4
Merge branch 'dev' into feat/admin
antfu 0407a30
chore: cleanup vite fix
antfu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is nice to see we can edit nuxt.config from there too. I think the point of that config editor is to edit Should only need to update that reference and/or to add a new route for it. 😄 Maybe we could support both, wdyt @atinux ? |
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will most likely come in a second step, but I think we need these functions to go a little bit deeper concerning components detection.
I like the idea of having a distinction between "user-level" components and "project-level" ones.
But I think we want users to discover all the components they have access to from that UI, and so I think we should try to refer to
.nuxt/components/index.js
(this is generated once the Nuxt server starts) to get a list of the current project components.We could show the editor in read-only mode for these components that are outside the user-level scope, and ask to the user if they want to duplicate it in their own
components/
directory once he tries to edit it?I know that @pi0 told me
.nuxt
will be removed in Nuxt 3, so maybe we could postpone that task an open an issue about this instead of searching for a fix in this branch.WDYT @atinux @pi0 ? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use components:extend hook also to read list of discovered components and filter based on level property