-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathdeploy-helper.js
More file actions
132 lines (101 loc) · 4.13 KB
/
deploy-helper.js
File metadata and controls
132 lines (101 loc) · 4.13 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const yaml = require("js-yaml")
const moment = require("moment")
const fs = require("fs")
const path = require("path")
function getCompilationQuarterValueForAudioVideo() {
try {
const baseDir = "src/en"
// Get all subdirectories and sort alphabetically, take last 2
const folders = fs.readdirSync(baseDir)
.filter(f => /\d{4}-\d{2}$/.test(f) && fs.statSync(path.join(baseDir, f)).isDirectory())
.sort()
.slice(-2)
const today = moment()
for (const folder of folders) {
const infoPath = path.join(baseDir, folder, "info.yml")
if (!fs.existsSync(infoPath)) continue
const info = yaml.load(fs.readFileSync(infoPath, "utf8"))
const startDate = moment(info.start_date, "DD/MM/YYYY")
const endDate = moment(info.end_date, "DD/MM/YYYY")
if (today.isBetween(startDate, endDate, "day", "[]")) {
return folder
}
}
return getCompilationQuarterValue(null, true)
} catch (e) {
return getCompilationQuarterValue(null, true)
}
}
let getCompilationQuarterValue = function (d, strict, includePrevious) {
d = d || new Date();
let quarterIndex = (Math.ceil((d.getMonth() + 1) / 3)),
nextQuarter = (quarterIndex <= 3) ? d.getFullYear() + "-0" + (quarterIndex + 1) : (d.getFullYear() + 1) + "-01";
let ret = `+(${includePrevious ? getPreviousQuarter() + "|" : ''}${d.getFullYear()}-0${quarterIndex}|${nextQuarter})`;
if (!strict) {
ret = `${ret}*`
}
return ret
};
let getCurrentQuarter = function () {
let d = new Date();
let quarterIndex = (Math.ceil((d.getMonth() + 1) / 3));
return `${d.getFullYear()}-0${quarterIndex}`;
};
let getCurrentQuarterWithOffset = function (offset) {
let d = new Date();
d.setDate(d.getDate() + offset);
let quarterIndex = (Math.ceil((d.getMonth() + 1) / 3));
return `${d.getFullYear()}-0${quarterIndex}`;
};
let getNextQuarter = function () {
let d = new Date();
let quarterIndex = (Math.ceil((d.getMonth() + 1) / 3));
let nextQuarter = (quarterIndex <= 3) ? d.getFullYear() + "-0" + (quarterIndex + 1) : (d.getFullYear() + 1) + "-01";
return `${nextQuarter}`;
};
let getPreviousQuarter = function () {
let d = new Date();
let quarterIndex = (Math.ceil((d.getMonth() + 1) / 3));
let prevQuarter = (quarterIndex === 1) ? (d.getFullYear() - 1) + "-04" : (d.getFullYear()) + `-0${(quarterIndex - 1)}`;
return `${prevQuarter}`;
};
let getInfoFromPath = function (path) {
let infoRegExp = /src\/([a-z]{2,3})?\/?([a-z0-9-]{6,})?\/?([0-9a-z\-]{2,})?\/?([a-z0-9-]{2,}(\.md)?)?\/?/g,
matches = infoRegExp.exec(path),
info = {};
info.language = matches[1] || null;
info.quarterly = matches[2] || null;
info.lesson = matches[3] || null;
info.day = (matches[4]) ? matches[4].replace(".md", "") : null;
return info;
};
const WILDCARD_BATCH_SIZE = 15
let generateInvalidations = function (paths) {
if (paths.length > 1000) {
return {
"invalidation.json": buildInvalidationJson(["/*"]),
}
}
const nonWildcard = paths.filter((p) => !p.includes("*"))
const wildcard = paths.filter((p) => p.includes("*"))
const files = {}
if (nonWildcard.length > 0) {
files["invalidation.json"] = buildInvalidationJson(nonWildcard)
}
for (let i = 0; i < wildcard.length; i += WILDCARD_BATCH_SIZE) {
const batch = wildcard.slice(i, i + WILDCARD_BATCH_SIZE)
const fileIndex = Math.floor(i / WILDCARD_BATCH_SIZE) + 1
files[`invalidation-${fileIndex}.json`] = buildInvalidationJson(batch)
}
return files
}
let buildInvalidationJson = function (items) {
return {
Paths: {
Quantity: items.length,
Items: items,
},
CallerReference: `deploy.js (${Date.now()}-${Math.random().toString(36).slice(2, 7)})`,
}
}
module.exports = { getCompilationQuarterValueForAudioVideo, getCompilationQuarterValue, getCurrentQuarter, getNextQuarter, getPreviousQuarter, getInfoFromPath, getCurrentQuarterWithOffset, generateInvalidations }