-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook.js
106 lines (85 loc) · 2.76 KB
/
hook.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const { exec } = require("child_process")
const gatsbyProcesses = require("./hook_commands/gatsby")
const gatsbyFtpProcesses = require("./hook_commands/gatsby_ftp")
const FtpDeploy = require("ftp-deploy");
const ftpDeploy = new FtpDeploy();
const dotenv = require('dotenv')
dotenv.config()
let isPublishing = false
let results = ''
const ftpConfig = {
user: process.env.FTP_USERNAME,
password: process.env.FTP_PASSWORD,
host: process.env.FTP_HOST,
port: parseInt(process.env.FTP_PORT),
include: ['*', '**/*'],
localRoot: __dirname + "/hook_commands",
remoteRoot: "/backup/",
deleteRemote: false,
forcePasv: true,
sftp: false
}
let processes = []
const writeResult = (value) => {
results += value + '\n'
}
const response = (status, message) => {
return { status, message }
}
function ejectRecursive(index) {
if (typeof processes[index] === "undefined") {
writeResult(`Completed : ${new Date()} :`)
isPublishing = false
return
}
writeResult(`=====> command is started [ ${processes[index].command} ] : ${new Date()} : <=====`)
//ftp commond check
if (processes[index].ftp) {
ftpDeploy
.deploy(ftpConfig)
.then(res => {
writeResult(`=====> FTP Upload Finished <=====`)
ejectRecursive(index + 1)
})
return
}
exec(processes[index].command, processes[index].option, (error, stdout, stderr) => {
if (error) {
writeResult(`=====> error command: [ ${processes[index].command} ] : ${new Date()} : <=====`)
writeResult(error.toString())
if (!processes[index].callback && processes[index].type !== "remove") {
writeResult(`=====> break <=====`)
writeResult(`=====> break <=====`)
writeResult(`=====> break <===== : ${new Date()} :`)
isPublishing = false
return;
}
}
if (stderr) {
writeResult(`=====> std error command: [ ${processes[index].command} ] : ${new Date()} : <=====`)
writeResult(stderr.toString())
}
writeResult((stdout || '').toString())
writeResult(`=====> command completed: [ ${processes[index].command} ] : ${new Date()} : <=====`)
if (processes[index].callback) {
processes[index].callback((error || stderr) ? "" : stdout, index)
return;
}
ejectRecursive(index + 1)
})
}
module.exports.startBuild = (request, reply) => {
const receivedHash = request.params.hash
if (isPublishing)
return response(false, "build process is already started")
if (receivedHash !== process.env.BUILD_HASH) {
return response(false, "invalid hash")
}
results = ''
processes = gatsbyFtpProcesses;
writeResult(`Process is starting... : ${new Date()} :`)
ejectRecursive(0)
isPublishing = true
return response(true, "build is started")
}
module.exports.results = () => results