-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnew.ts
executable file
·140 lines (126 loc) · 2.95 KB
/
new.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env esno
import slugify from "@sindresorhus/slugify";
import fs from "fs";
import inquirer, { PromptModule } from "inquirer";
import path from "path";
import makeTitleCase from "title";
import config from "./gatsby-config";
import { SiteSiteMetadata as Meta } from "./src/types";
import * as DateTime from "./src/utils/datetime";
const meta = config.siteMetadata as Meta;
interface PromptAttributes {
readonly title: string;
readonly author: string;
readonly language: string;
readonly excerpt: string;
readonly type: string[] | string;
readonly categories: string;
readonly date: string;
readonly cover: string | null;
}
const isValidCheckBoxAnswer = (answers: string[]): boolean | string => {
if (answers.length < 1) return "Choose either type!";
if (answers.length > 1) return "Choose only one type!";
return true;
};
const newPost = async () => {
let prompt: PromptModule & PromptAttributes;
try {
prompt = await inquirer.prompt([
{
type: "input",
name: "title",
message: "Title: ",
},
{
type: "input",
name: "author",
message: "Author: ",
default: meta?.author?.name,
},
{
type: "input",
name: "language",
message: "Language (en, fi, sv): ",
default: meta?.language,
},
{
type: "input",
name: "excerpt",
message: "Summary: ",
default: "To be written later...",
},
{
type: "checkbox",
name: "type",
message: "Is this a post or a page?",
choices: [
{
name: "post",
checked: true,
},
{
name: "page",
},
],
validate: isValidCheckBoxAnswer,
},
{
type: "input",
name: "categories",
message: "Post categories (separated by comma): ",
default: "",
},
{
type: "input",
name: "date",
message: "Publication date (YYYY-MM-DD): ",
default: DateTime.toISOString(),
},
{
type: "input",
name: "cover",
message: "URL of the cover image: ",
default: "null",
validate: (input: string) => {
if (input === "null") return true;
try {
new URL(input);
} catch {
return "Enter a valid URL";
}
return true;
},
},
]);
} catch (err) {
return console.error(err instanceof Error ? err.message : String(err));
}
const { title, type, excerpt, categories, author, cover, date, language } =
prompt;
const postType = Array.isArray(type) ? type[0] : type;
const postCategories = categories
.split(",")
.map((c) => c.trim())
.join(", ");
const targetFile = path.join(
__dirname,
`content/${postType === "post" ? "blog/" : ""}${slugify(title)}.md`,
);
const frontMatterBlock = `---
title: ${makeTitleCase(title)}
author: ${author}
lang: ${language}
excerpt: ${excerpt}
type: ${postType}
categories: [${postCategories}]
date: ${date}
hero: ${cover}
---`.trim();
fs.writeFileSync(targetFile, frontMatterBlock);
console.log(`
Saved new post to ${targetFile} with following content:
${frontMatterBlock}
`);
};
newPost();