forked from timrobertson100/ssg-ipt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildcontent.js
67 lines (61 loc) · 1.93 KB
/
buildcontent.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
//This is used in Jenkins. The idea is to only install packages when needed.
// Installing packages is slow and when content creators work it is annoying having to wait 4 minuttes to see a change
//This way we only reinstall dependencies if package.json or npm-shrinkwrap-json is changed.
function installPackages() {
console.log('Not content only. Install from npm');
const exec = require('child_process').exec;
const child = exec('rm -rf node_modules; npm install;', function (error, stdout, stderr){
if (error !== null) {
console.log('exec error: ' + error);
}
process.exit();
});
}
function isContentFile(file) {
return file != 'package.json' && file != 'npm-shrinkwrap.json';
}
function isContentFiles(files) {
for (var i = 0; i < files.length; i++) {
if (!isContentFile(files[i])) {
return false;
}
}
return true;
}
function isContentOnlyCommit(commit) {
var added = isContentFiles(commit.added);
var removed = isContentFiles(commit.removed);
var modified = isContentFiles(commit.modified);
return added && removed && modified;
}
function isContentOnlyCommits(payload) {
//If
if (!payload.head_commit && !payload.commits) {
return false;
}
var commits = payload.commits || [];
if (payload.head_commit) {
commits.push(payload.head_commit);
}
for (var i = 0; i < commits.length; i++) {
if (!isContentOnlyCommit(commits[i])) {
return false;
}
}
return true;
}
function testCommit(payload) {
//if there is commits that are not content then install packages from npm
if (!isContentOnlyCommits(payload)) {
installPackages();
}
else {
console.log('Content only. Do not reinstall packages. This makes the deployment faster');
}
}
try {
var payload = require('./payload');
testCommit(payload);
} catch (ex) {
installPackages(ex);
}