@valaxyjs/devtools 0.19.5 → 0.19.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,10 +2,38 @@ import type { ResolvedConfig, ViteDevServer } from 'vite'
2
2
  import bodyParser from 'body-parser'
3
3
 
4
4
  import matter from 'gray-matter'
5
+ import { JSON_SCHEMA } from 'js-yaml'
5
6
  import fs from 'fs-extra'
6
7
 
7
8
  const prefix = '/valaxy-devtools-api'
8
9
 
10
+ /**
11
+ * migration
12
+ * @param path
13
+ * @param frontmatter
14
+ */
15
+ export async function migration(path: string, frontmatter: { [key: string]: string }) {
16
+ if (fs.existsSync(path)) {
17
+ const rawMd = await fs.readFile(path, 'utf-8')
18
+ const matterFile = matter(rawMd, { schema: JSON_SCHEMA } as any)
19
+ let mod = false
20
+ for (const key in frontmatter) {
21
+ if (key in matterFile.data) {
22
+ matterFile.data[frontmatter[key]] = matterFile.data[key]
23
+ delete matterFile.data[key]
24
+ mod = true
25
+ }
26
+ }
27
+ if (mod) {
28
+ const newMd = matter.stringify(matterFile.content, matterFile.data)
29
+ await fs.writeFile(path, newMd)
30
+ }
31
+ }
32
+ else {
33
+ // console.error(`post not exist:${path}`)
34
+ }
35
+ }
36
+
9
37
  export function registerApi(server: ViteDevServer, _viteConfig: ResolvedConfig) {
10
38
  const app = server.middlewares
11
39
 
@@ -27,9 +55,28 @@ export function registerApi(server: ViteDevServer, _viteConfig: ResolvedConfig)
27
55
  await fs.writeFile(path, newMd)
28
56
  }
29
57
  }
58
+ })
30
59
 
31
- // console.log('update', url, frontmatter)
60
+ app.use(`${prefix}/migration`, async (req, _res) => {
61
+ // update
62
+ if (req.method === 'POST') {
63
+ const { pageData, frontmatter } = await (req as any).body
64
+ // filePath
65
+ const worker: Promise<void>[] = []
32
66
 
33
- // console.log('frontmatter', req.url, res)
67
+ for (const item of pageData) {
68
+ const path = item
69
+ worker.push(migration(path, frontmatter))
70
+ }
71
+ // worker.push(migration(`${userRoot.root}/pages${item}.md`, frontmatter))
72
+ // for (const item of pageData)
73
+ // worker.push(migration(item, frontmatter))
74
+
75
+ Promise.all(worker).then(() => {
76
+ _res.end('ok')
77
+ }).catch((_) => {
78
+ _res.end('migration error')
79
+ })
80
+ }
34
81
  })
35
82
  }
package/src/node/types.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export interface ValaxyDevtoolsOptions {
2
+ userRoot?: string
2
3
  base?: string
3
4
  }