-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.js
More file actions
102 lines (88 loc) · 2.41 KB
/
new.js
File metadata and controls
102 lines (88 loc) · 2.41 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const inquirer = require("inquirer");
const slugify = require("slugify");
const fs = require("fs").promises;
const path = require("path");
const config = require("../_data/site")();
inquirer.registerPrompt("datetime", require("inquirer-datepicker-prompt"));
async function run() {
const [postHours, postMinutes] = config.publishTime.split(":");
const dateInitial = new Date();
dateInitial.setHours(postHours);
dateInitial.setMinutes(postMinutes);
const answers = await inquirer.prompt([
{
type: "list",
name: "type",
message: "What do you want generate?",
choices: ["Post", "Demo"],
},
{
type: "input",
name: "title",
message: "What is the title?",
},
{
type: "datetime",
name: "date",
message: "What is the publish date/time?",
format: ["yyyy", "-", "mm", "-", "dd", " ", "HH", ":", "MM"],
initial: dateInitial,
when: (answers) => {
return answers.type === "Post";
},
},
]);
if (answers.type === "Demo") {
let file = await fs.readFile(
path.join(__dirname, "templates", "demo.js"),
"utf-8"
);
file = file.replace("Default title", answers.title);
const filename = slug(answers.title);
await fs.writeFile(
path.join(__dirname, "..", "_data", "demos", `${filename}.js`),
file
);
console.log(
`New demo generated at: ${path.join(
__dirname,
"..",
"_data",
"demos",
`${filename}.js`
)}`
);
} else if (answers.type === "Post") {
let file = await fs.readFile(
path.join(__dirname, "templates", "post.md"),
"utf-8"
);
file = file.replace("Default title", answers.title);
const formattedDate = `${answers.date.getFullYear()}-${
answers.date.getMonth() + 1
}-${answers.date.getDate()} ${answers.date.getHours()}:${answers.date.getMinutes()}:00`;
file = file.replace("1970-01-01 00:00:00", formattedDate);
const filename = slug(answers.title);
await fs.writeFile(
path.join(__dirname, "..", "posts", `${filename}.md`),
file
);
console.log(
`New post generated at: ${path.join(
__dirname,
"..",
"posts",
`${filename}.md`
)}`
);
}
}
function slug(input) {
const options = {
replacement: "-",
remove: /[&,+()$~%.'":*!?<>{}]/g,
lower: true,
};
return slugify(input, options);
}
run();