-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.js
83 lines (67 loc) · 2.37 KB
/
git.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
const {
computeUntilToday, DEAD_CELL, Rule,
translateToBinary, translateToChars
} = require('./cells.js');
const DateTime = require('luxon').DateTime;
const execSync = require('child_process').execSync;
const fs = require('fs');
const path = require('path');
const readLastLines = require('read-last-lines');
const pjson = require('./package.json');
const LOCAL_CLONE_PATH = path.join(process.cwd(), 'tmp');
const CELLS_FILENAME = 'cells.txt';
const CELLS_FILE_PATH = path.join(LOCAL_CLONE_PATH, CELLS_FILENAME);
const REPO_URL = pjson.custom.targetRepository.url;
let updateFile = () => {
const lastUpdateTimestamp = execSync('git log -1 --format="%at"', {
'cwd': LOCAL_CLONE_PATH
})
.toString()
.trim();
const lastUpdate = DateTime
.fromSeconds(+lastUpdateTimestamp)
.startOf('day');
return readLastLines.read(CELLS_FILE_PATH, 2)
.then(lines => {
let lastLines = lines.trimStart().split('\n');
let parents, startDay;
if(lastLines.length == 1){
parents = translateToBinary(lastLines[0]);
startDay = 0;
} else if(lastLines[1].length >= 7){
parents = translateToBinary(lastLines[1]);
startDay = 0;
} else {
parents = translateToBinary(lastLines[0]);
startDay = lastLines[1].length;
}
let rule110 = Rule.fromNumber(110);
let result = computeUntilToday(
rule110, parents, lastUpdate, startDay
);
let output = result.map(translateToChars).join('\n');
if(output[output.length - 1] === DEAD_CELL){
return false;
} else if(startDay === 0){
output = '\n' + output;
}
fs.appendFileSync(CELLS_FILE_PATH, output, 'utf8');
return true;
});
};
let cloneRepository = () => {
return execSync(`bash clone.bash ${REPO_URL}`);
};
let pushChanges = () => {
let credentials = `cell-invaders:${process.env.GITHUB_TOKEN}`;
let [scheme, host] = REPO_URL.split('://');
let pushUrl = scheme + '://' + credentials + '@' + host;
execSync(`bash push.bash ${pushUrl}`);
};
cloneRepository();
let updateResult = updateFile();
updateResult.then(hasUpdated => {
if(hasUpdated){
pushChanges();
}
});