forked from emacs-themes/emacs-themes-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.js
53 lines (47 loc) · 1.45 KB
/
cron.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
'use strict';
var exec = require('child_process').exec;
var fs = require('fs');
var command = 'git pull origin master';
var projectPath = __dirname;
var errorLogPath = __dirname + '/logs/error.log';
var mainLogPath = __dirname + '/logs/main.log';
// execute a git pull
var child = exec(command, {cwd: projectPath});
var buildSite = require(__dirname + '/src/builders/buildSite');
var 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();
}
});