-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
204 lines (175 loc) · 5.32 KB
/
index.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
Exports: run(oldFilePath, latestFilePath)
Takes file paths for two bcd json files,
does some processing, produces a flattened delta
and outputs out/index.html
*/
let fs = require('fs'),
Handlebars = require('handlebars')
store_path = './store',
output_path = './out'
flatten = require('./flatten.js').flatten,
delta = require('./delta.js').delta,
formatter = require('./format'),
RSS = require('./feed-creator.js'),
entriesStore = require('./entriesStore.js'),
utils = require('./utils.js')
Handlebars.registerHelper('stripFileExtension', utils.stripFileExtension)
Handlebars.registerHelper('pluralize', function(number, singular, plural) {
if (number === 1)
return singular;
else
return (typeof plural === 'string' ? plural : singular + 's');
});
function toTopicsFromStrings(list) {
let ret = {}
list.forEach(item => {
let topic = item.match(/bcd ::: (\w)*/)[0].replace("bcd ::: ", "")
ret[topic] = ret[topic] || []
ret[topic].push(item)
})
return ret
}
function lastNFeedEntries(which, blurb, n) {
let baseFilePath = output_path + "/" + which + "/"
let entries = getSortedListOfEntries(baseFilePath)
let x = entries.slice(0, n)
let items = []
return x.map(fileName => {
let out = fs.readFileSync(baseFilePath + fileName).toString()
const re = {
title: /title>(?<title>.*)<\/title>/g
}
let titleMatch = out.matchAll(re.title)
let title = titleMatch.next().value.groups.title
return {
content: out,
name: fileName,
file: which + "/" + fileName,
title: title,
blurb: blurb,
image: "",
pubDate:
title
.split(", ")[1]
.trim()
}
})
}
function toTopicsFromObjects(list) {
let ret = {}
list.forEach(item => {
let topic = item.key.match(/bcd ::: (\w)*/)[0].replace("bcd ::: ", "")
ret[topic] = ret[topic] || []
ret[topic].push(item)
})
return ret
}
// TODO: DO We need this?
function getSortedListOfEntries(path) {
let files = fs.readdirSync(path)
files = files.filter(name => name.endsWith(".html") && name !== "index.html")
files.sort(function(a,b){
return new Date(utils.stripFileExtension(b)) - new Date(utils.stripFileExtension(a));
})
return files
}
function makeHistoricalIndex() {
const compiledTemplate = require("./templates/index.handlebars");
fs.writeFileSync(
output_path + "/index.html",
compiledTemplate({
weekly: getSortedListOfEntries(output_path + "/weekly"),
completed: getSortedListOfEntries(output_path + "/weekly-completed")
})
)
}
function getLastVersions(browserReleases) {
return Object.keys(browserReleases).map(n => {
return parseFloat(n)
}).sort((a, b) => {
if(a > b) { return -1; }
else if (b < a) { return -1; }
else return 0
}).slice(0, 3)
}
function run(o, l, reportName='') {
let inputA = JSON.parse(fs.readFileSync(`${store_path}/${o}`))
let inputB = JSON.parse(fs.readFileSync(`${store_path}/${l}`))
let latestBrowsers = {
chrome: getLastVersions(inputB.browsers.chrome.releases),
firefox: getLastVersions(inputB.browsers.firefox.releases),
safari: getLastVersions(inputB.browsers.safari.releases)
}
let flattenedA = flatten(inputA)
let flattenedB = flatten(inputB)
let reportDate = utils.dateFromISODateString(reportName)
let previousReportDate = utils.findPreviousMonday(reportDate)
let data = delta(flattenedA, flattenedB, latestBrowsers)
let fromDate = new Date(inputA.__meta.timestamp)
let toDate = new Date(inputB.__meta.timestamp)
let name = reportName
data.__meta = [{
generatedOn: name,
older: {
releaseDate: fromDate,
monday: previousReportDate,
version: inputA.__meta.version
},
newer: {
releaseDate: toDate,
monday: reportDate,
version: inputB.__meta.version
}
}]
data.hasNewData = !utils.areSameDate(fromDate, toDate),
data.addedFeatures = toTopicsFromStrings(data.added)
data.removedFeatures = toTopicsFromStrings(data.removed)
data.backfilledImplementations = toTopicsFromObjects(data.backfilledImplementations)
data.addedImplementations = toTopicsFromObjects(data.addedImplementations)
data.permalink = name
let out = formatter.formatSummary(data, flattenedB)
let title = 'BCD Changes Report, ' + reportDate.toDateString();
// current...
fs.writeFileSync(
output_path + '/weekly/index.html',
out,
'utf8'
)
// archived
fs.writeFileSync(
output_path + `/weekly/${name}.html`,
out,
'utf8'
)
RSS({
items: lastNFeedEntries(`weekly`, 'Weekly summary of new changes in BCD data', 5)
},
{
title: `Changes Report (weekly)`,
path: output_path + `/weekly`
}
)
let outComplete = formatter.formatCompleted(data, flattenedB)
title = 'BCD New Baselines Report, ' + reportDate.toDateString();
fs.writeFileSync(
output_path + `/weekly-completed/${name}.html`,
outComplete,
'utf8'
)
fs.writeFileSync(
output_path + '/weekly-completed/index.html',
outComplete,
'utf8'
)
RSS({
items: lastNFeedEntries(`weekly-completed`, 'Weekly summary of new Baseline items in BCD data', 5)
},
{
title: `New Baselines Report (weekly)`,
path: output_path + `/weekly-completed`
}
)
makeHistoricalIndex()
}
exports.run = run