-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.mjs
250 lines (226 loc) Β· 5.97 KB
/
cli.mjs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env node
"use strict";
import fs from "fs";
import ora from "ora";
import { black, bold, dim, green, yellow, bgGreen } from "kleur/colors";
import path from "path";
import prompts from "prompts";
import url from "url";
import slugify from "slugify";
// TODO move to JSON so this can be consumed everywhere
const TAG_SUGGESTIONS = {
languages: [
"HTML",
"CSS",
"JavaScript",
"TypeScript",
"Markdown",
"MDX",
"PHP",
"Python",
"SQL",
"C",
"C++",
],
frontend: ["React", "React Native", "VueJS", "Svelte", "SolidJS"],
backend: ["NodeJS", "ExpressJS", "Flask", "SQLite", "GraphQL", "SQL"],
fullstack: ["Astro", "NextJS", "Gatsby", "Laravel"],
libraries: [
"Framer-Motion",
"Radix UI",
"Redux",
"Styled Components",
"TensorflowJS",
"Three JS",
"P5JS",
"image-q",
],
preprocessors: ["TailwindCSS", "SASS"],
software: ["AudioStellar", "GIMP", "Orca", "Processing", "Pure Data", "uxn"],
protocols: ["WebSockets", "OSC"],
adjectives: [
"Accessibility",
"Agile methodology",
"Animation",
"Art",
"Artificial Intelligence",
"Design",
"Contributions",
"Open Source",
"SSR",
],
};
function isEmptyDir(dirPath = "") {
return !fs.existsSync(dirPath) || fs.readdirSync(dirPath).length === 0;
}
function onCancel() {
ora().info(dim("Cancelled post template, see you later!"));
process.exit(1);
}
const POST_TEMPLATES = [
{
layout: "~layouts/BlogPostLayout.astro",
label: "Blog Post",
type: "blog",
},
{
layout: "~layouts/ProjectsLayout.astro",
label: "Project",
type: "projects",
},
];
async function main() {
console.log(bold("Welcome to the helper cli for posts!"));
const options = await prompts(
[
{
type: "select",
name: "post",
message: "What would you like to create?",
choices: POST_TEMPLATES.map(({ type }) => type),
},
],
{
onCancel,
}
);
if (options.post === undefined) {
process.exit(1);
}
const { label, type } = POST_TEMPLATES[options.post];
console.log(bold(`\nCreating ${label}:\n`));
// TODO Add github URL for posts, add language badges for posts
const post = await prompts(
[
{
type: "date",
name: "date",
message: "Select a date for your post",
mask: "YYYY-MM-DD",
initial: new Date(),
},
{
type: "text",
name: "title",
message: `What should you name your ${label}?`,
initial: `My ${label} Title`,
validate(value) {
return !!value;
},
},
{
type: "text",
name: "description",
message: (prev) => `What would describe "${prev}"?`,
initial: `My ${label}'s description`,
},
type === "project" && {
type: "text",
name: "url",
message: "The URL for the project",
initial: "https://",
validate(value) {
return value.startsWith("https://")
}
},
type === "project" && {
type: "text",
name: "repo",
message: "The URL for the repo",
initial: "https://github.com/",
validate(value) {
return value.startsWith("https://")
}
},
{
type: "confirm",
name: "assets",
message: `Does this ${label} include any images?`,
},
{
type: "autocompleteMultiselect",
name: "tags",
message: `Please select your tags for this ${label}`,
choices: Object.values(TAG_SUGGESTIONS)
.flat()
.map((title) => ({
title,
value: title,
})),
},
{
type: "confirm",
name: "draft",
initial: true,
message: `Is this ${label} a draft?`,
},
].filter(option => !!option),
{
onCancel,
}
);
const date = post.date;
const dateSlug = `${date.getFullYear()}/${(date.getMonth() + 1)
.toString()
.padStart(2, "0")}/${date.getDate().toString().padStart(2, "0")}`;
const postDir = path.join(
path.dirname(url.fileURLToPath(import.meta.url)),
`/src/pages/${type}/${dateSlug}`
);
if (!fs.existsSync(postDir)) {
console.log(bold(green(`\nCreating path for ${label} at ${postDir}`)));
fs.mkdirSync(postDir, { recursive: true });
} else {
console.log(bold(green(`\nAdding ${label} to ${postDir}`)));
}
const postFilename = slugify(post.title).toLocaleLowerCase();
const assetsBaseURL = `/assets/posts/${type}/${dateSlug}/${postFilename}`;
const assetsDir = path.join(
path.dirname(import.meta.url),
`/public${assetsBaseURL}`
);
const postPath = path.join(postDir, postFilename);
if (!isEmptyDir(postPath)) {
console.log(bold(yellow("\nPost already exists!\n")));
const action = await prompts(
[
{
type: "confirm",
name: "replace",
message: "Replace folder?",
},
],
{ onCancel }
);
if (action.replace) fs.unlinkSync(postPath);
}
fs.mkdirSync(postDir, { recursive: true });
if (post.assets) {
fs.mkdirSync(assetsDir, { recursive: true });
}
const postImageSrc = post.assets && `${assetsDir}/${postFilename}.jpeg`;
fs.writeFileSync(
path.join(postDir, `${postFilename}.mdx`),
`---
layout: "${POST_TEMPLATES[options.post].layout}"
draft: ${post.draft}
date: "${date.toDateString()}"
description: "${post.description ?? ""}"
${postImageSrc ? "" : "# "}image: "${postImageSrc || ""}"
${postImageSrc ? "" : "# "}alt: "A screenshot showcasing the website"
${type === "projects" ? 'website: ""' : ""}
tags: [${post.tags.map((tag) => `"${tag}"`).join(", ")}]
title: "${post.title}"
---
import OptimizedImage from "~components/Base/OptimizedImage.astro"
import SmartLink from "~components/Base/SmartLink.astro"
export const components = { a: SmartLink, img: OptimizedImage };
<div id="content">
### About
</div>`
);
console.log(
bold(bgGreen(black(`\n"${post.title}" ${label} was created!\n`)))
);
}
main();