-
Notifications
You must be signed in to change notification settings - Fork 28
/
pull-and-build.js
52 lines (47 loc) · 1.45 KB
/
pull-and-build.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
const path = require('path');
const exec = require('child_process').exec;
const fs = require('fs');
const command = 'git pull origin master';
const projectPath = __dirname;
const errorLogPath = path.join(__dirname, '/logs/error.log');
const mainLogPath = path.join(__dirname, '/logs/main.log');
// execute a git pull
const child = exec(command, { cwd: projectPath });
const buildSite = require(path.join(__dirname, '/src/builders/buildSite'));
let successText;
function gitIsUpToDate(data) {
return data.indexOf('Already up-to-date') > -1;
}
function tryToBuild() {
var time = new Date().toString();
var text = time + ' - Build has been initiated!\n';
fs.appendFile(mainLogPath, text, function(err) {
var errorText = time + ' - Could not write to ' + mainLogPath + ' file\n';
if (err) {
fs.appendFileSync(errorLogPath, errorText);
}
});
buildSite();
}
// grab stdout
child.stdout.on('data', function(data) {
successText = data;
});
// log error
child.stderr.on('data', function(data) {
var time = new Date().toString();
var errorText = time + ' - Git stderr: \n ' + data;
fs.appendFileSync(errorLogPath, errorText);
});
// try to build the project on exit
child.on('close', function(code) {
var time = new Date().toString();
var text;
// log exit code
if (gitIsUpToDate(successText)) {
text = time + ' - Git is up to date. Code: ' + code + '\n';
fs.appendFileSync(errorLogPath, text);
} else {
tryToBuild();
}
});