forked from zhipengyan/auto-publish-hexo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
85 lines (74 loc) · 2.32 KB
/
index.coffee
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
http = require "http"
shelljs=require "shelljs"
crypto = require "crypto"
bl = require 'bl'
config = require './config'
moment = require 'moment-timezone'
key = config.webhook_secret
currentDir = ''+shelljs.pwd()
hexoSourceDir = "#{currentDir}/#{config.path.hexo_source_path}"
hexoDir = "#{currentDir}/#{config.path.hexo_path}"
listenPort = config.listen_port
timezone = config.time_zone
getTime = ()->
#datetime = moment().format 'MMMM Do YYYY, h:mm:ss a'
datetime = moment().tz(timezone).format 'MMMM Do YYYY, h:mm:ss a'
return datetime
http.createServer (request, response)->
request.pipe bl (err, blob)->
signBlob = (key) ->
return 'sha1=' + crypto.createHmac 'sha1', key
.update blob
.digest 'hex'
sig = request.headers['x-hub-signature']
event = request.headers['x-github-event']
id = request.headers['x-github-delivery']
statusCode = 400
result =
success:false
errMsg: ''
if not (sig and id and event and signBlob(key) is sig+'')
statusCode = 401
result = {
success:false
errMsg: 'vertify failed'
}
else
#pull posts
shelljs.cd hexoSourceDir
pullCmd = shelljs.exec "ls & git pull origin master "
if pullCmd.code is 0
#pull successed!
console.log "pull successed!"
if shelljs.which 'node'
shelljs.cd hexoDir
hexoCmd = shelljs.exec "hexo clean & hexo generate"
if hexoCmd.code isnt 0
console.log "hexo generate failed! at #{getTime()}"
statusCode = 500
result =
success: false
errMsg: "hexo generage failed:"+hexoCmd.output
else
console.log "hexo generate successed! at #{getTime()}"
statusCode = 200
result =
success: true
errMsg: ''
msg: hexoCmd.output
else
result =
success: false
errMsg: "can't use node, check it!"
else
console.log "pull posts failed"
statusCode = 500
result =
success: false
errMsg: "pull posts failed:"+pullCmd.output
shelljs.cd currentDir
response.writeHead statusCode, {"Content-Type": "application/json"}
response.end JSON.stringify result
return
return
.listen listenPort