Skip to content

Commit

Permalink
feat: add cli for automation (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
nozomuikuta authored Nov 25, 2023
1 parent 556953a commit a9abe71
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 47 deletions.
20 changes: 2 additions & 18 deletions .vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineConfig } from "vitepress";
import blogSidebar from "./sidebar.blog.json";

export default defineConfig({
srcDir: "src",
Expand Down Expand Up @@ -212,24 +213,7 @@ export default defineConfig({
{ text: "Ecosystem", link: "/docs/learn/ecosystem" },
{ text: "References", link: "/docs/learn/references" },
],
"/blog/": [
{
text: "Oxlint General Availability",
link: "/blog/2023-11-08-announcing-oxlint",
},
{
text: "Announcing Oxc",
link: "/blog/2023-11-07-announcing-oxc",
},
{
text: "A research on JavaScript linters",
link: "/blog/2022-08-08-linter-research",
},
{
text: "High Performance JavaScript Toolchain",
link: "/blog/2022-02-10-js-tooling-research",
},
],
"/blog/": blogSidebar,
},
},
});
18 changes: 18 additions & 0 deletions .vitepress/sidebar.blog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"text": "2023-11-08<br>Oxlint General Availability",
"link": "/blog/2023-11-08-announcing-oxlint"
},
{
"text": "2023-11-07<br>Announcing Oxc",
"link": "/blog/2023-11-07-announcing-oxc"
},
{
"text": "2022-08-08<br>A research on JavaScript linters",
"link": "/blog/2022-08-08-linter-research"
},
{
"text": "2022-02-10<br>High Performance JavaScript Toolchain",
"link": "/blog/2022-02-10-js-tooling-research"
}
]
116 changes: 116 additions & 0 deletions cli/commands/create-blog-post.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import * as fs from "node:fs/promises";
import { defineCommand } from "citty";
import consola from "consola";

export default defineCommand({
meta: {
name: "create-blog-post",
description: "create blog post",
},
args: {
date: {
type: "positional",
description: "publish date of the post",
required: true,
},
title: {
type: "positional",
description: "title of the post",
required: true,
},
slug: {
type: "string",
description: "slug of the post",
required: false,
},
author: {
type: "string",
description: "author ID of the post",
required: false,
},
},
async run({ args }) {
const { date, title, slug, author } = args;
const isValidDate =
/^(?:20\d{2})\-(?:0?[1-9]|1[012])\-(?:0?[1-9]|1\d|2\d|3[0-1])$/.test(
date,
);

if (!isValidDate) {
consola.error(`date is invalid: ${date}`);
}

await writeMarkdownFile({ date, title, slug, author });
await writeSidebarFile({ date, title, slug });

consola.success(`blog post created`);
},
});

async function writeMarkdownFile({
date,
title,
slug,
author,
}: {
date: string;
title: string;
slug: string;
author: string;
}) {
const filePath = `${process.cwd()}/src${getBlogPostUrl({
date,
title,
slug,
})}.md`;
const content = `
---
title: ${title}
outline: deep
authors:
- ${author || "# TODO: add author ID"}
---
<AppBlogPostHeader />
`.trimStart();

await fs.writeFile(filePath, content);
}

async function writeSidebarFile({
date,
title,
slug,
}: {
date: string;
title: string;
slug: string;
}) {
const filePath = `${process.cwd()}/.vitepress/sidebar.blog.json`;
const currentSidebar = await fs.readFile(filePath, "utf8");
const sidebar = JSON.parse(currentSidebar) as Record<
"text" | "link",
string
>[];

sidebar.unshift({
text: `${date}<br>${title}`,
link: getBlogPostUrl({ date, title, slug }),
});

await fs.writeFile(filePath, JSON.stringify(sidebar, null, 2));
}

function getBlogPostUrl({
date,
title,
slug,
}: {
date: string;
title: string;
slug: string;
}) {
return `/blog/${date}-${(slug || title)
.toLocaleLowerCase()
.replace(/\s+/g, "-")}`;
}
17 changes: 17 additions & 0 deletions cli/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineCommand, runMain } from "citty";
import type { CommandDef } from "citty";

const _rDefault = (r: any) => (r.default || r) as Promise<CommandDef>;

const main = defineCommand({
meta: {
name: "pnpm run cli",
description: "CLI for OXC Documentation",
},
subCommands: {
"create-blog-post": () =>
import("./commands/create-blog-post.mjs").then(_rDefault),
},
});

runMain(main);
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "vitepress dev",
"build": "vitepress build",
"preview": "vitepress preview",
"create-blog-post": "tsx ./cli/index.mts create-blog-post",
"typecheck": "tsc",
"prettier": "prettier"
},
Expand All @@ -15,9 +16,13 @@
"vue": "^3.3.8"
},
"devDependencies": {
"@types/node": "^20.10.0",
"citty": "^0.1.5",
"consola": "^3.2.3",
"husky": "^8.0.3",
"lint-staged": "15.0.2",
"prettier": "^3.0.3",
"tsx": "^4.5.0",
"typescript": "~5.2.2"
},
"lint-staged": {
Expand Down
Loading

0 comments on commit a9abe71

Please sign in to comment.