-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.ts
48 lines (42 loc) · 1.53 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
import { program } from 'commander';
import slugify from 'slugify';
import * as fs from 'fs';
import * as path from 'path';
import type { Meta } from 'components/Post';
function generateNewPost(meta: Meta) {
const slug = slugify(meta.title, { trim: true, lower: true });
const now = new Date();
const content = `
---
title: ${meta.title}
date: ${now.toISOString()}
published: ${meta.published?.toString()}
score: ${meta.score}
tags: ${meta.tags?.map(item => `- ${item}`).join('\n')}
categories: ${meta.categories?.map(item => `- ${item}`).join('\n')}
description: "${meta.description}"
---
Content is in here
`.trim();
fs.writeFileSync(path.resolve(`contents/vi/posts/${slug}.mdx`), content);
fs.writeFileSync(path.resolve(`contents/en/posts/${slug}.mdx`), content);
}
program.name('www')
.description('Set of commands for writing posts')
.version('1.0.0');
program
.command('generate')
.argument<string>('title', 'Title of post', value => value.toString())
.option<string[]>('-t', '--tag', (value, prev) => [...prev, value.toString()], [])
.option<string[]>('-c', '--category', (value, prev) => [...prev, value.toString()], [])
.option<string>('-d', '--description', value => value.toString(), '')
.option<boolean>('-p', '--published', value => ['true', '1', 'on'].includes(value), false)
.option<number>('-s', '--score', value => parseInt(value), 0)
.action((title, options) => {
generateNewPost({
title,
...options,
});
console.info(`Generated ${title}`);
});
program.parse();