-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateJsonData.js
55 lines (43 loc) · 1.51 KB
/
GenerateJsonData.js
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
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { join, dirname } from 'path'
import { Low, JSONFile } from 'lowdb'
import { fileURLToPath } from 'url'
import fs from 'fs';
import path from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const post_dir = 'src/routes/posts';
// Use JSON file for storage
const file = join(path.join(__dirname, post_dir), 'db.json')
const adapter = new JSONFile(file)
const db = new Low(adapter)
// Read data from JSON file, this will set db.data content
await db.read()
// If file.json doesn't exist, db.data will be null
// Set default data
// db.data = db.data || { posts: [] } // Node < v15.x
db.data ||= { posts: {}}; // Node >= 15.x
let posts = db.data.posts;
// Create and query items using plain JS
// db.data.posts.push('hello world')
// file name should be the same as slug
fs.readdirSync(path.join(__dirname, post_dir))
.filter(e => e.endsWith('.md') || e.endsWith('.svx'))
.map(fullname => {
let stat = fs.statSync(path.join(__dirname, post_dir, fullname));
return ({slug: path.parse(fullname).name, mtime: stat.mtime, birthtime: stat.birthtime})
})
.forEach(e => {
if (e.slug in posts){
posts[e.slug].mtime = e.mtime;
} else {
posts[e.slug] = e;
}
});
// const firstPost = db.data.posts[0]
// Alternatively, you can also use this syntax if you prefer
// const { posts } = db.data
// posts.push('hello world')
// Finally write db.data content to file
await db.write()
console.log("---Posts data updated.");