-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhooks.js
96 lines (87 loc) · 3.28 KB
/
webhooks.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
// Deps //
const { exec } = require("child_process");
const express = require("express");
const gatsbyWebhookHelper = express();
gatsbyWebhookHelper.use( express.json() );
// Config //
const WEBHOOK_PORT = 9000;
var ongoing_process = false;
// Gatsby Update/Re-build sequences //
const cleanRebuildSequence = [
"npm run clean",
"gatsby build"
// Add custom commands here
];
const quickRebuildSequence = [
"gatsby build"
// Add custom commands here
];
// You can also create your custom callback terminal command sequences...
// Routing with callback terminal command sequences //
// Default delay is 120 and 30s respectively = build will trigger only after inactivity for 2 minutes, to not to repeat the same thing multiple times.
createGatsbyWebhook('/clean_gatsby_rebuild/', cleanRebuildSequence, 120000)
createGatsbyWebhook('/quick_gatsby_rebuild/', quickRebuildSequence, 30000)
// You can also create custom webhook callbacks with your custom sequences...
// FUNCTIONS //
/*
FUNCTION: Create Gatsby webhook route.
Accepts:
endpoint(string) "/test/" - endpoint path
sequence(array) [string, ...] - callback commands, will be executed synchronously in order, same as "commands" in runSequence()
delay(int) - delay in ms. The action will be fired only after inactivity of receiving the same request within delay period.
Returns:
HTTP 200 OK || console.log
*/
function createGatsbyWebhook(endpoint, sequence, delay = 15000){
gatsbyWebhookHelper.post( endpoint, ( req, res ) => {
if(ongoing_process === false) {
console.log("Initializing a Webhook sequence.");
ongoing_process = true;
console.log("Countdown of "+delay+"ms started...");
setTimeout(()=>{runSequence(sequence)}, delay);
res.sendStatus( 200 );
} else {
console.log ('Sorry, already running a sequence. Try again in few minutes.');
}
});
}
/*
FUNCTION: Run Terminal Sequence.
Accepts:
commands(array || false) [string, ...] - callback commands, will be executed synchronously in order.
Returns:
status(boolean) - success is true, failure is false
*/
function runSequence (commands=false){
if(commands === false) return false;
var commandArr = [...commands];
let newCommand = commandArr[0];
commandArr.shift();
console.log('Running "'+newCommand+'"');
// Execute terminal command
if (!newCommand || newCommand.trim() === "") return false;
exec(String(newCommand), (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
}
if (stderr) {
console.log(`stderr: ${stderr}`);
}
console.log(`stdout: ${stdout}`);
// Recursive continuation
if (commandArr.length>0) {
runSequence(commandArr);
} else {
// declare finished process after completed sequence
ongoing_process = false;
console.log("Webhook callback sequence Completed.");
}
});
return true;
}
// /FUNCTIONS //
// Start //
function start_gatsby_webhook_listener(){
gatsbyWebhookHelper.listen( WEBHOOK_PORT, () => console.log( 'Node Webhook helper started on port '+String(WEBHOOK_PORT) ) );
}
start_gatsby_webhook_listener();