forked from accessibilitysupported/a11ysupport.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
375 lines (311 loc) · 10.3 KB
/
build.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
let fs = require('fs');
let rimraf = require('rimraf');
let glob = require('glob');
let helper = require(__dirname+'/src/feature-helper');
let tech = require(__dirname+"/data/tech.json");
let ATBrowsers = require(__dirname+"/data/ATBrowsers.json");
let testMap = {};
let allTests = [];
let featureMap = {};
let allFeatures = [];
/**
* Generic array sorting
*
* @param property
* @returns {Function}
*/
let sortByProperty = function (property) {
return function (x, y) {
if (typeof x[property] === 'string') {
return ((x[property].toLowerCase() === y[property].toLowerCase()) ? 0 : ((x[property].toLowerCase() > y[property].toLowerCase()) ? 1 : -1));
}
return ((x[property] === y[property]) ? 0 : ((x[property] > y[property]) ? 1 : -1));
};
};
let sortByKeys = function(unordered) {
let ordered = {};
Object.keys(unordered).sort().forEach(function(key) {
ordered[key] = unordered[key];
});
return ordered;
};
let initalizeFeatures = function(techId, buildDir) {
let dir_feature = __dirname + '/data/tech/'+techId;
if (!fs.existsSync(dir_feature)) {
// Directory doesn't exist, so there are not features yet
return;
}
let files = fs.readdirSync(dir_feature);
let techDir = buildDir+'/tech/'+techId;
if (!fs.existsSync(techDir)) {
fs.mkdirSync(techDir);
}
files.forEach(function(file) {
let feature = require(dir_feature + '/' + file);
let id = file.slice(0, -5);
// Initialize the feature object to add missing data points and generate support strings
helper.initalizeFeatureObject(feature, techId, techId + '/' + id);
// Save initialized JSON in the build dir
fs.writeFileSync(techDir + '/' + file, JSON.stringify(feature, null, 2));
});
};
let getFeatures = function(techId, buildDir) {
let dir_feature = __dirname + '/build/tech/'+techId;
if (!fs.existsSync(dir_feature)) {
// Directory doesn't exist, so there are not features yet
return;
}
let files = fs.readdirSync(dir_feature);
let features = [];
let techDir = buildDir+'/tech/'+techId;
files.forEach(function(file) {
let feature = require(dir_feature+'/'+file);
let id = file.slice(0, -5);
let failingTests = [];
// search the test map for tests that include this id.
feature.tests = [];
if (featureMap[techId+'/'+id]) {
feature.tests = featureMap[techId+'/'+id];
}
// Initialize the feature object to add missing data points and generate support strings
helper.bubbleFeatureSupport(feature);
// Save initialized JSON in the build dir
fs.writeFileSync(techDir+'/'+file, JSON.stringify(feature, null, 2));
// Map associated tests
for (let testIndex = 0; testIndex < feature.tests.length; testIndex++) {
if (!testMap[feature.tests[testIndex].id]) {
testMap[feature.tests[testIndex].id] = [];
}
// Slow, but it works. Room for optimization
testMap[feature.tests[testIndex].id].forEach(function(data, index) {
if (data.featureId !== feature.id) {
return;
}
testMap[feature.tests[testIndex].id][index].techId = techId;
testMap[feature.tests[testIndex].id][index].title = feature.title;
});
//populate failing tests
let found = false;
["sr", "vc"].forEach(type => {
if (found) {
return;
}
if (feature.tests[testIndex].core_support[type].includes('n')) {
failingTests.push({
title: feature.tests[testIndex].title,
id: feature.tests[testIndex].id
});
found = true;
}
});
}
//Include AT and browser version combinations in feature objects
let simplifiedTests = [];
if (feature.tests.length > 0) {
simplifiedTests = feature.tests.map(function (test) {
for (at in test.versions) {
test.versions[at].title = ATBrowsers.at[at].title;
test.versions[at].type = ATBrowsers.at[at].type;
for (browser in test.versions[at].browsers) {
test.versions[at].browsers[browser].title = ATBrowsers.browsers[browser].title;
}
}
return {
title: test.title,
id: test.id,
versions: test.versions
}
});
}
let simplifiedFeature = {
id: id,
techId: feature.techId,
title: feature.title,
keywords_string: feature.keywords_string,
core_support: {
sr: feature.core_support.sr,
vc: feature.core_support.vc
},
core_support_string: {
sr: feature.core_support_string.sr,
vc: feature.core_support_string.vc
},
core_support_by_at: feature.core_support_by_at,
core_support_by_at_browser: feature.core_support_by_at_browser,
failing_tests: failingTests,
total_test_count: feature.tests.length,
allTests: simplifiedTests,
all_dates: feature.all_dates,
failing_dates: feature.failing_dates,
assertions: [],
supports_at: feature.supports_at,
core_must_support_string: feature.core_must_support_string,
core_should_support_string: feature.core_should_support_string,
core_may_support_string: feature.core_may_support_string
};
feature.assertions.forEach(function(assertion, assertion_key) {
simplifiedFeature.assertions[assertion_key] = {
id: assertion.id,
title: assertion.title,
core_support: {
sr: assertion.core_support ? assertion.core_support.sr : [],
vc: assertion.core_support ? assertion.core_support.vc : []
},
core_support_string: {
sr: assertion.core_support_string ? assertion.core_support_string.sr : 'unknown',
vc: assertion.core_support_string ? assertion.core_support_string.vc : 'unknown'
},
core_support_by_at: assertion.core_support_by_at
}
});
// Push to the list of features
features.push(simplifiedFeature);
});
return features;
};
let buildDir = __dirname+'/build';
let dataDir = __dirname+'/data';
// Update Windows directory strings to use forward slash
buildDir = buildDir.replace(/\\/g, '/');
dataDir = dataDir.replace(/\\/g, '/');
if (fs.existsSync(buildDir)) {
rimraf.sync(buildDir);
}
fs.mkdirSync(buildDir);
fs.mkdirSync(buildDir+'/tech');
fs.mkdirSync(buildDir+'/tests');
//let testFiles = fs.readdirSync(dataDir+'/tests');
let supportPoints = [];
for (let techId in tech) {
initalizeFeatures(techId, buildDir);
}
let testFiles = glob.sync(dataDir+'/tests/**/*.json');
testFiles.forEach(function(file) {
if (!file.endsWith('.json')) {
return;
}
// make the file name relative to the build dir
file = file.replace(dataDir+'/tests/', '');
// load the test
let test = require(dataDir+'/tests/'+file);
// Set the test ID to the file name minus the extension
test.id = file.slice(0, -5);
if (!test.html_file) {
// html_file property isn't set. Map it to it's ID by default.
test.html_file = test.id+'.html';
}
// Set up the test case
helper.initalizeTestCase(test);
test.assertions.forEach(assertion => {
if (!testMap[test.id]) {
testMap[test.id] = [];
}
let found = testMap[test.id].find(o => o.featureId === assertion.feature_id);
if (!found) {
testMap[test.id].push({
// link tests with features here
featureId: assertion.feature_id,
});
}
// populate the featuremap
if (!featureMap[assertion.feature_id]) {
featureMap[assertion.feature_id] = [];
}
if (-1 === featureMap[assertion.feature_id].indexOf(test.id)) {
featureMap[assertion.feature_id].push(test.id);
}
});
test.assertions.forEach(assertion => {
for(let at in ATBrowsers.at) {
let validBrowsers = ATBrowsers.at[at].core_browsers.concat(ATBrowsers.at[at].extended_browsers);
validBrowsers.forEach(function(browser) {
supportPoints.push(assertion.results[at].browsers[browser]);
});
}
});
// ensure the path exists
let path = buildDir+'/tests/'+file;
path = path.substring(0, path.lastIndexOf("/"));
if (!fs.existsSync(path)) {
fs.mkdirSync(path, { recursive: true });
}
// now write the file
fs.writeFileSync(buildDir+'/tests/'+file, JSON.stringify(test, null, 2));
});
for (let techId in tech) {
tech[techId].features = getFeatures(techId, buildDir);
if (tech[techId].features) {
allFeatures = allFeatures.concat(tech[techId].features);
}
}
let builtTestFiles = glob.sync(buildDir+'/tests/**/*.json');
testFiles.forEach(function(file) {
if (!file.endsWith('.json')) {
return;
}
let test = require(file);
let simplifiedTest = {
id: test.id,
title: test.title,
keywords_string: test.title,
core_support: {
sr: test.core_support.sr,
vc: test.core_support.vc
},
core_support_string: {
sr: test.core_support_string.sr,
vc: test.core_support_string.vc
},
last_update: test.history.pop(),
assertions: []
};
var feature_titles = [];
test.assertions.forEach(function(assertion, assertion_key) {
let tech_id = assertion.feature_id.split('/')[0];
let feature_id = assertion.feature_id.split('/')[1];
let ref_feature = allFeatures.find(o => o.techId === tech_id && o.id === feature_id);
let ref_assertion = ref_feature.assertions.find(o => o.id === assertion.feature_assertion_id);
simplifiedTest.assertions[assertion_key] = {
feature_id: ref_feature.techId+'/'+ref_feature.id,
feature_assertion_id: assertion.feature_assertion_id,
feature_title: ref_feature.title,
assertion_title: ref_assertion.title,
core_support: {
sr: assertion.core_support.sr,
vc: assertion.core_support.vc
},
core_support_string: {
sr: assertion.core_support_string.sr,
vc: assertion.core_support_string.vc,
},
};
feature_titles.push(ref_feature.title);
});
simplifiedTest.keywords_string = simplifiedTest.keywords_string + " " + feature_titles;
allTests.push(simplifiedTest);
});
allTests.forEach(function(test, test_key) {
});
let commands = {};
commands.sr = {};
commands.vc = {};
for(let at in ATBrowsers.at) {
let type = ATBrowsers.at[at].type;
for(let key in ATBrowsers.at[at].commands) {
if (!commands[type][key]) {
commands[type][key] = [];
}
commands[type][key].push(at);
}
}
commands.sr = sortByKeys(commands.sr);
commands.vc = sortByKeys(commands.vc);
allTests.sort(sortByProperty('title'));
allFeatures.sort(sortByProperty('title'));
supportPoints.sort(sortByProperty('priority'));
fs.writeFileSync(buildDir+'/tech.json', JSON.stringify(tech, null, 2));
fs.writeFileSync(buildDir+'/test_map.json', JSON.stringify(testMap, null, 2));
fs.writeFileSync(buildDir+'/features.json', JSON.stringify(allFeatures, null, 2));
fs.writeFileSync(buildDir+'/tests.json', JSON.stringify(allTests, null, 2));
fs.writeFileSync(buildDir+'/support_points.json', JSON.stringify(supportPoints, null, 2));
fs.writeFileSync(buildDir+'/command_matrix.json', JSON.stringify(commands, null, 2));