summaryrefslogtreecommitdiffhomepage
path: root/services/blog/nuldoc-src/commands/new.ts
blob: 651c59e62fb25f7cdb039f320d1eb59684312415 (plain)
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
import { dirname, join } from "@std/path";
import { ensureDir } from "@std/fs";
import { parseArgs } from "@std/cli";
import { Config } from "../config.ts";

export async function runNewCommand(config: Config) {
  const parsedArgs = parseArgs(Deno.args, {
    string: ["date"],
  });

  const type = parsedArgs._[1];
  if (type !== "post" && type !== "slide") {
    console.log(`Usage: nuldoc new <type>

<type> must be either "post" or "slide".

OPTIONS:
  --date <DATE>
`);
    Deno.exit(1);
  }

  const ymd = (() => {
    if (parsedArgs.date) {
      return parsedArgs.date;
    }

    const now = new Date();
    const y = now.getFullYear();
    const d = (now.getMonth() + 1).toString().padStart(2, "0");
    const m = now.getDate().toString().padStart(2, "0");
    return `${y}-${d}-${m}`;
  })();

  const destFilePath = join(
    Deno.cwd(),
    config.locations.contentDir,
    getDirPath(type),
    ymd,
    getFilename(type),
  );

  await ensureDir(dirname(destFilePath));
  await Deno.writeTextFile(destFilePath, getTemplate(type, ymd));
  console.log(
    `New file ${
      destFilePath.replace(Deno.cwd(), "")
    } was successfully created.`,
  );
}

function getFilename(type: "post" | "slide"): string {
  return type === "post" ? "TODO.dj" : "TODO.toml";
}

function getDirPath(type: "post" | "slide"): string {
  return type === "post" ? "posts" : "slides";
}

function getTemplate(type: "post" | "slide", date: string): string {
  const uuid = crypto.randomUUID();
  if (type === "post") {
    return `---
[article]
uuid = "${uuid}"
title = "TODO"
description = "TODO"
tags = [
  "TODO",
]

[[article.revisions]]
date = "${date}"
remark = "公開"
---
{#TODO}
# TODO

TODO
`;
  } else {
    return `[slide]
uuid = "${uuid}"
title = "TODO"
event = "TODO"
talkType = "TODO"
link = "TODO"
tags = [
  "TODO",
]

[[slide.revisions]]
date = "${date}"
remark = "登壇"
`;
  }
}