-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsyncBlog.mjs
35 lines (31 loc) · 1.07 KB
/
syncBlog.mjs
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
import fs from 'fs'
import { spawn } from 'node:child_process'
const REPO_URL = 'https://github.com/chanshiyucx/blog.git'
const CONTENT_PATH = './public/blog'
const runBashCommand = (command) =>
new Promise((resolve, reject) => {
console.log(`Run bash command: ${command}`)
const child = spawn(command, [], { shell: true })
child.stdout.setEncoding('utf8')
child.stdout.on('data', (data) => process.stdout.write(data))
child.stderr.setEncoding('utf8')
child.stderr.on('data', (data) => process.stderr.write(data))
child.on('close', function (code) {
if (code === 0) {
resolve(void 0)
} else {
reject(new Error(`Command failed with exit code ${code}`))
}
})
})
const syncContentFromGit = async (repoUrl, contentPath) => {
console.log('Syncing content files from git')
if (fs.existsSync(contentPath)) {
await runBashCommand(`cd ${contentPath} && git pull`)
} else {
await runBashCommand(
`git clone --depth 1 --single-branch ${repoUrl} ${contentPath}`,
)
}
}
syncContentFromGit(REPO_URL, CONTENT_PATH)