-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.ts
163 lines (153 loc) · 5.19 KB
/
cli.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Command } from "commander";
import * as fs from "@std/fs";
import * as path from "@std/path";
import { storeArticle } from "./article_generator.ts";
import manifest from "./deno.json" with { type: "json" };
type CLI = (args: string[]) => void;
export function createCli(postsFolder: string): CLI {
const program = new Command();
program
.name(path.basename(Deno.cwd()))
.version(manifest.version)
.description("Smallblog CLI, handle your articles from the terminal.");
program
.command("create")
.alias("c")
.description("Create a new blog post as draft")
.argument("<name>", "The id of the post (no space allowed)")
.option("-P, --published", "Create a published post")
.option("-t, --title <title>", "The title of the post")
.option("-p", "--preview <preview>", "The preview of the post")
.option("-d", "--description <description>", "The description of the post")
.option("-a, --authors <authors...>", "The author of the post")
.option(
"-D, --date <date>",
"The date of the post (YYYY-MM-DD, default: current date)",
)
.option("--tags <tags...>", "The tags of the post")
.option("-s, --section <section>", "The section of the post")
.option("-c, --content <content>", "The content of the post")
.action((fileId, options) => {
console.log("Creating post...");
try {
const underscoreToAdd = options.published
? false
: !fileId.startsWith("_");
const filename = (underscoreToAdd ? "_" : "") + fileId + ".md";
const folder = postsFolder;
const params = {
title: options.title as string,
content: options.content as string,
description: options.description as string,
preview: options.preview as string,
tags: (options.tags as string[]) || undefined,
authors: (options.authors as string[]) || undefined,
date: options.date as string,
section: options.section as string,
};
storeArticle(folder, filename, params);
console.log("Post created.");
} catch (e) {
console.error("Error while creating post:", e);
Deno.exit(1);
}
});
program
.command("publish")
.alias("p")
.description("Publish a draft post")
.argument("<name>", "The id of the post (no space allowed)")
.action((fileId) => {
console.log("Publishing post...");
try {
fs.moveSync(
path.join(
postsFolder,
(!fileId.startsWith("_") ? "_" + fileId : fileId) + ".md",
),
path.join(
postsFolder,
(!fileId.startsWith("_") ? fileId : fileId.substring(1)) + ".md",
),
);
console.log("Post published.");
} catch (e) {
console.error("Error while publishing post:", e);
Deno.exit(1);
}
});
program
.command("list")
.alias("ls")
.description("List all blog posts")
.option("-D, --drafts", "List only draft posts")
.option("-P", "--published", "List only published posts")
.option("-a, --all", "List all posts (default)")
.action((options) => {
console.log("Posts:");
if (options.all || options.published || !options.drafts) {
console.log("");
console.log("Published:");
for (
const file of fs.expandGlobSync(path.join(postsFolder, "[!_]*.md"))
) {
console.log(" - ", file.name.replace(".md", ""));
}
}
if (options.all || options.drafts || !options.published) {
console.log("");
console.log("Drafts:");
for (const file of fs.expandGlobSync(path.join(postsFolder, "_*.md"))) {
console.log(" - ", file.name.replace(".md", "").replace("_", ""));
}
}
console.log("");
});
program
.command("archive")
.alias("a")
.description("Archive a blog post as a draft")
.argument("<name>", "The id of the post (no space allowed)")
.action((fileId) => {
console.log("Archiving post...");
try {
fs.moveSync(
path.join(postsFolder, fileId + ".md"),
path.join(
postsFolder,
(fileId.startsWith("_") ? fileId : "_" + fileId) + ".md",
),
);
console.log("Post archived.");
} catch (e) {
console.error("Error while archiving post:", e);
Deno.exit(1);
}
});
program
.command("remove")
.alias("rm")
.description(
"Remove a blog post. You can only delete a draft post. If you want to delete a published post, use `smallblog archive` first. This does NOT delete the images or videos of the post, You should delete them manually.",
)
.argument("<name>", "The id of the post (no space allowed)")
.action((fileId) => {
console.log("Removing post...");
try {
Deno.removeSync(
path.join(
postsFolder,
(!fileId.startsWith("_") ? "_" + fileId : fileId) + ".md",
),
);
console.log("Post removed.");
} catch {
console.log("Post not found.");
Deno.exit(1);
}
});
function run(args: string[]) {
program.parse(args, { from: "user" });
}
return run;
}