-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (73 loc) · 1.91 KB
/
index.js
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
#!/usr/bin/env node
const log = require("./lib/log")
const { Spinner } = require("clui")
const clear = require("clear")
const path = require("path")
const files = require("./lib/files")
const inquirer = require("./lib/inquirer")
const github = require("./lib/github")
const BLOG_POSTS_PATH = path.join("content", "blog")
async function handleBlogPostUserInput() {
const { title, description, date, tags } = await inquirer.askBlogPostDetails()
const urlFormatTitle = title
.toLowerCase()
.split(" ")
.join("-")
files.createDirectory(path.join(BLOG_POSTS_PATH, urlFormatTitle))
return Promise.resolve({
title,
urlFormatTitle,
description,
date,
tags,
})
}
async function main() {
clear()
log.print("Create Blog Post", { color: "yellow", isTitle: true })
log.print(
`A simple CLI tool to create and schedule a blog post based on Gatsby blog starter.
Make sure to run this from the root of your project.`,
{ color: "blue" }
)
if (!files.isDirectoryGitRepo()) {
log.errorAndExit(
"This is not your root directory or it's not a valid git repo."
)
}
try {
await github.authenticateUser()
const {
title,
urlFormatTitle,
description,
date,
tags,
} = await handleBlogPostUserInput()
const newBlogPostFilePath = path.join(
BLOG_POSTS_PATH,
urlFormatTitle,
`index.md`
)
files.createPostTemplate(
title,
newBlogPostFilePath,
description,
date,
tags
)
const status = new Spinner(
`Committing your changes to branch ${urlFormatTitle}...`
)
status.start()
await github.checkoutNewBranch(urlFormatTitle)
await github.add(newBlogPostFilePath)
await github.commit()
await github.push(urlFormatTitle)
status.stop()
await github.submitPr(urlFormatTitle, date.split(" ")[0])
} catch (error) {
log.error(error.message)
}
}
main()