forked from foolip/mdn-bcd-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-new-bcd.ts
168 lines (143 loc) · 4.23 KB
/
add-new-bcd.ts
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
//
// mdn-bcd-collector: add-new-bcd.ts
// Adds missing entries to BCD that have support in some browser version
//
// © Gooborg Studios, Google LLC
// See LICENSE.txt for copyright details
//
import {Identifier} from '@mdn/browser-compat-data/types';
import path from 'node:path';
import fs from 'fs-extra';
import esMain from 'es-main';
import {getMissing} from './find-missing-features.js';
import {main as updateBcd} from './update-bcd.js';
const tests = await fs.readJson(new URL('./tests.json', import.meta.url));
const overrides = await fs.readJson(
new URL('./overrides.json', import.meta.url)
);
const BCD_DIR = process.env.BCD_DIR || `../browser-compat-data`;
const {default: bcd} = await import(`${BCD_DIR}/index.js`);
const {default: compareFeatures} = await import(
`${BCD_DIR}/scripts/compare-features.js`
);
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}
}
};
export const recursiveAdd = (
ident: string[],
i: number,
data: Identifier,
obj: any
): Identifier => {
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;
};
export const orderFeatures = (key: string, value: Identifier): Identifier => {
if (value instanceof Object && '__compat' in value) {
value = Object.keys(value)
.sort(compareFeatures)
.reduce((result, key) => {
result[key] = value[key];
return result;
}, {});
}
return value;
};
/* c8 ignore start */
const writeFile = async (ident: string[], obj: any): Promise<void> => {
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}
);
};
/* c8 ignore stop */
export const traverseFeatures = async (
obj: Identifier,
identifier: string[]
): Promise<any> => {
for (const i in obj) {
if (!!obj[i] && typeof obj[i] == 'object' && i !== '__compat') {
const thisIdent = identifier.concat([i]);
const support = obj[i]?.__compat?.support;
if (support) {
for (const statements of Object.values(support)) {
const supported = (
Array.isArray(statements) ? statements : [statements]
).some((s) => s.version_added);
if (supported) {
await writeFile(thisIdent, obj[i]);
break;
}
}
}
await traverseFeatures(obj[i], thisIdent);
}
}
};
export const collectMissing = async (filepath: string): Promise<void> => {
const missing = {api: {}};
for (const entry of getMissing(
bcd,
tests,
'bcd-from-collector',
['api'],
false
).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);
};
/* c8 ignore start */
const main = async (): Promise<void> => {
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']},
bcd.browsers,
overrides
);
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!');
};
if (esMain(import.meta)) {
await main();
}
/* c8 ignore stop */