forked from mdn/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenumerate-features.ts
70 lines (59 loc) · 1.72 KB
/
enumerate-features.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
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */
import fs from 'node:fs';
import path from 'node:path';
import esMain from 'es-main';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { lowLevelWalk } from '../utils/walk.js';
/**
* Enumerate features and write to a destination file
* @param argv Arguments
* @param argv.dest Destination file name
* @param argv.dataFrom Where the data is (leave blank for repository folder)
*/
const main = async (argv: {
dest: string;
dataFrom?: string;
}): Promise<void> => {
const { dest, dataFrom } = argv;
fs.writeFileSync(dest, JSON.stringify(await enumerateFeatures(dataFrom)));
};
/**
* Enumerate compat data features
* @param dataFrom Where to get the data from (leave blank for repository folder)
* @returns A list of features
*/
const enumerateFeatures = async (dataFrom?: string): Promise<string[]> => {
const feats: string[] = [];
const walker = lowLevelWalk(
dataFrom
? (await import(path.join(process.cwd(), dataFrom, 'index.js'))).default
: undefined,
);
for (const feat of walker) {
if (feat.compat || feat.browser) {
feats.push(feat.path);
}
}
return feats;
};
if (esMain(import.meta)) {
const { argv } = yargs(hideBin(process.argv)).command(
'$0 [dest]',
'Write a JSON-formatted list of feature paths',
(yargs) => {
yargs
.positional('dest', {
default: '.features.json',
description: 'File destination',
})
.option('data-from', {
nargs: 1,
description: 'Require compat data from an alternate path',
});
},
);
await main(argv as any);
}
export default enumerateFeatures;