-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnew_post.js
53 lines (45 loc) · 1.32 KB
/
new_post.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
const { exec } = require('child_process');
const { program } = require('commander');
program.option('-t, --title <title>', 'Post title');
(() => {
console.log("creating a new post...");
try {
program.parse();
const options = program.opts();
if (!options.title) {
console.log("New post title required");
process.exit(1);
}
const date = new Date();
const month = date.getMonth() + 1;
const twoDigitMonth = month.toString().length < 2 ? `0${month.toString()}` : month;
const day = date.getDate();
const twoDigitDay = day.toString().length < 2 ? `0${day.toString()}` : day;
const year = date.getFullYear();
const titleForFile = options.title.toLowerCase();
const titleSplit = titleForFile.split(' ').join('-');
const markdownTitle = `${year}-${twoDigitMonth}-${twoDigitDay}-${titleSplit}.md`
exec(`
cat <<EOF >./content/news/${year}/${markdownTitle}
---
title: ${options.title}
date: ${year}-${twoDigitMonth}-${twoDigitDay}
draft: true
---
`, (err, stdout, stderr) => {
if(err) {
console.log(err);
process.exit(1);
}
if(stderr) {
console.log(stderr);
process.exit(1);
}
console.log("New post created");
process.exit(0);
})
} catch (error) {
console.log(error);
process.exit(1);
}
})();