UNPKG

1.37 kBJavaScriptView Raw
1//
2
3const Client = require("@minodisk/medkit");
4const createClient = require("./client");
5const { md2html } = require("@minodisk/medmd");
6const { toPost, toText } = require("./format");
7const { readFile, writeFile, glob } = require("./utils");
8
9module.exports = (patterns, options) => {
10 return new Promise(async (resolve, reject) => {
11 await syncPosts(createClient(options.parent), patterns);
12 resolve();
13 });
14};
15
16const syncPosts = (client, patterns) => {
17 return new Promise(async (resolve, reject) => {
18 for (const pattern of patterns) {
19 const paths = await glob(pattern);
20 if (paths.length === 0) {
21 reject(`no matched file for the pattern: ${pattern}`);
22 return;
23 }
24 for (const path of paths) {
25 await syncPost(client, path);
26 }
27 }
28 resolve();
29 });
30};
31
32const syncPost = (client, path) => {
33 return new Promise(async (resolve, reject) => {
34 const text = await readFile(path);
35 const post = toPost(text);
36 const html = await md2html(post.body);
37 const { id, ...options } = post.meta;
38 if (id == null) {
39 const postId = await client.createPost(html, options);
40 post.meta.id = postId;
41 const newText = toText(post);
42 await writeFile(path, newText);
43 } else {
44 await client.updatePost(id, html, options);
45 }
46 await client.close();
47 resolve();
48 });
49};