forked from mdn/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
69 lines (57 loc) · 1.7 KB
/
index.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
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { fdir } from 'fdir';
import { CompatData } from './types/types.js';
import extend from './scripts/lib/extend.js';
import { normalizePath, walk } from './utils/index.js';
const dirname = fileURLToPath(new URL('.', import.meta.url));
export const dataFolders = [
'api',
'browsers',
'css',
'html',
'http',
'javascript',
'mathml',
'svg',
'webassembly',
'webdriver',
'webextensions',
];
/**
* Recursively load one or more directories passed as arguments.
* @param dirs The directories to load
* @returns All of the browser compatibility data
*/
const load = async (...dirs: string[]): Promise<CompatData> => {
const result = {};
for (const dir of dirs) {
const paths = new fdir()
.withBasePath()
.filter((fp) => fp.endsWith('.json'))
.crawl(path.join(dirname, dir))
.sync() as string[];
for (const fp of paths) {
try {
const rawcontents = await fs.readFile(fp);
const contents: CompatData = JSON.parse(rawcontents.toString('utf8'));
// Add source_file props
const walker = walk(undefined, contents);
for (const { compat } of walker) {
compat.source_file = normalizePath(path.relative(dirname, fp));
}
extend(result, contents);
/* c8 ignore start */
} catch (e) {
// Skip invalid JSON. Tests will flag the problem separately.
continue;
}
/* c8 ignore stop */
}
}
return result as CompatData;
};
export default await load(...dataFolders);