-
Notifications
You must be signed in to change notification settings - Fork 0
/
createPost.js
72 lines (58 loc) · 1.75 KB
/
createPost.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const File = require("fs/promises");
const fs = require("fs");
/**
* @param {string} title
*/
const main = async (_title) => {
const title = _title.join("-");
console.log(`attempt to create file name: ${title}`);
if (title.length < 1) throw new Error("파일 이름을 입력하지 않았습니다.");
const t = TITLE(title.replaceAll("-", " "));
const FRONTMATTER = `---\n${TITLE(
title.replaceAll("-", " ")
)}\ndate: ${getKoreanDateTime()}\n${COVER_URL()}\n---\n<br />\n<br />\n<br />`;
const TEMP_DIR = `./content/blog/temp/`;
const FULL_PATH = TEMP_DIR + title + ".md";
const isDirExists = fs.existsSync(TEMP_DIR);
const isFileExists = fs.existsSync(FULL_PATH);
if (isFileExists) {
throw Error("이미 같은 이름의 파일이 " + TEMP_DIR + "에 존재합니다.");
}
if (isDirExists) {
await File.writeFile(FULL_PATH, FRONTMATTER);
} else {
await File.mkdir(TEMP_DIR);
await File.writeFile(FULL_PATH, FRONTMATTER);
}
};
main(process.argv.splice(2));
/**
*
* @returns {string}
*/
function getKoreanDateTime() {
const now = new Date();
const year = now.getFullYear().toString();
const month =
now.getMonth() < 9 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1;
const date = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
const hours = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
const minutes =
now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
const seconds =
now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();
return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
}
/**
* @param {string} title
* @returns {string}
*/
function TITLE(title) {
return `title: ${title}`;
}
/**
* @returns {string}
*/
function COVER_URL() {
return `coverURL: `;
}