-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.js
More file actions
28 lines (24 loc) · 770 Bytes
/
git.js
File metadata and controls
28 lines (24 loc) · 770 Bytes
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
const simpleGit = require('simple-git')
const { DateTime } = require("luxon")
const sanitize = require("sanitize-filename")
const fs = require('fs').promises
const path = require('path')
module.exports = async function (text) {
const {
BLOG_PATH,
COMMIT_MESSAGE,
FILE_EXTENSION,
GIT_BRANCH_NAME,
GIT_REMOTE,
} = process.env
const DATE_MASK = DateTime.now().toString()
const FILE_NAME = sanitize(`${DATE_MASK}${FILE_EXTENSION}`)
const FILE_PATH = path.normalize(`${BLOG_PATH}${path.sep}${FILE_NAME}`)
await fs.stat(BLOG_PATH)
const git = simpleGit(BLOG_PATH)
await fs.writeFile(FILE_PATH, text, 'utf8')
await git.pull()
await git.add(FILE_NAME)
await git.commit(COMMIT_MESSAGE)
await git.push(GIT_REMOTE, GIT_BRANCH_NAME)
}