forked from foolip/mdn-bcd-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-new-bcd.js
117 lines (95 loc) · 3.25 KB
/
add-new-bcd.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
'use strict';
const fs = require('fs-extra');
const path = require('path');
const {getMissing} = require('./find-missing');
const {main: updateBcd} = require('./update-bcd');
const BCD_DIR = process.env.BCD_DIR || `../browser-compat-data`;
const compareFeatures = require(path.join(BCD_DIR, 'scripts', 'compare-features'));
const template = {
__compat: {
support: {
chrome: {version_added: null},
chrome_android: {version_added: null},
edge: {version_added: null},
firefox: {version_added: null},
firefox_android: {version_added: null},
ie: {version_added: null},
opera: {version_added: null},
opera_android: {version_added: null},
safari: {version_added: null},
safari_ios: {version_added: null},
samsunginternet_android: {version_added: null},
webview_android: {version_added: null}
},
status: {experimental: false, standard_track: true, deprecated: false}
}
};
const recursiveAdd = (ident, i, data, obj) => {
const part = ident[i];
data[part] = i + 1 < ident.length ?
recursiveAdd(ident, i + 1, part in data ? data[part] : {}, obj) :
Object.assign({}, obj);
return data;
};
const orderFeatures = (key, value) => {
if (value instanceof Object && '__compat' in value) {
value = Object.keys(value)
.sort(compareFeatures)
.reduce((result, key) => {
result[key] = value[key];
return result;
}, {});
}
return value;
};
const writeFile = async (ident, obj) => {
const filepath = path.resolve(path.join(BCD_DIR, ident[0], `${ident[1]}.json`));
let data = {api: {}};
if (await fs.pathExists(filepath)) {
data = await fs.readJSON(filepath);
}
await fs.writeJSON(filepath, recursiveAdd(ident.concat(['__compat']), 0, data, obj.__compat), {spaces: 2, replacer: orderFeatures});
};
const traverseFeatures = async (obj, identifier) => {
for (const i in obj) {
if (!!obj[i] &&
typeof obj[i] == 'object' &&
i !== '__compat') {
const thisIdent = identifier.concat([i]);
if (obj[i].__compat) {
for (const support of Object.values(obj[i].__compat.support)) {
if (!([false, null].includes(support.version_added))) {
await writeFile(thisIdent, obj[i]);
break;
}
}
}
await traverseFeatures(obj[i], thisIdent);
}
}
};
const collectMissing = async (filepath) => {
const missing = {api: {}};
for (const entry of getMissing('bcd-from-collector', ['api']).missingEntries) {
recursiveAdd(entry.split('.'), 0, missing, template);
}
const json = JSON.stringify(missing, null, ' ') + '\n';
await fs.ensureDir(path.dirname(filepath));
await fs.writeFile(filepath, json);
};
const main = async () => {
const filepath = path.resolve(path.join(BCD_DIR, '__missing', '__missing.json'));
console.log('Generating missing BCD...');
await collectMissing(filepath);
await updateBcd(['../mdn-bcd-results/'], {category: ['__missing']});
console.log('Injecting BCD...');
const data = await fs.readJSON(filepath);
await traverseFeatures(data.api, ['api']);
console.log('Cleaning up...');
await fs.remove(filepath);
console.log('Done!');
};
main().catch((e) => {
console.error(e);
process.exit(1);
});