-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathupdate-statistics.js
More file actions
152 lines (132 loc) · 4.49 KB
/
Copy pathupdate-statistics.js
File metadata and controls
152 lines (132 loc) · 4.49 KB
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
// @ts-check
import bcd from "@mdn/browser-compat-data" with { type: "json" };
import caniuse from "caniuse-lite";
import fs from "node:fs/promises";
import path from "node:path";
import { features } from "web-features";
import { getAllBCDKeys } from "./utils.js";
const OUTPUT_STATISTICS = path.join(
import.meta.dirname,
"site",
"assets",
"statistics.json",
);
const TODAY = new Date().toISOString().slice(0, 10);
const HISTORICAL_SOURCE_URL =
"https://github.com/web-platform-dx/web-features/issues/788";
async function main() {
const existingStatistics = await readExistingStatistics();
const currentSnapshot = getCurrentSnapshot(TODAY);
const snapshots = mergeSnapshots(
existingStatistics.snapshots ?? [],
currentSnapshot,
);
const statistics = {
generatedAt: new Date().toISOString(),
versions: await getVersions(),
latest: snapshots.at(-1),
snapshots,
notes: [
`Historical rows before this page was added were seeded from weekly report comments in ${HISTORICAL_SOURCE_URL}.`,
"Rows generated after seeding are computed from the installed web-features, @mdn/browser-compat-data, and caniuse-lite packages.",
],
};
await fs.writeFile(
OUTPUT_STATISTICS,
`${JSON.stringify(statistics, null, 2)}\n`,
"utf8",
);
}
async function readExistingStatistics() {
try {
return JSON.parse(await fs.readFile(OUTPUT_STATISTICS, "utf8"));
} catch (error) {
if (error.code === "ENOENT") {
return { snapshots: [] };
}
throw error;
}
}
function getCurrentSnapshot(date) {
const ordinaryFeatures = Object.values(features).filter(
(feature) => feature.kind === "feature",
);
const bcdKeys = getAllBCDKeys(bcd);
const mappedBcdKeys = new Set(
ordinaryFeatures.flatMap((feature) => feature.compat_features ?? []),
);
const standardNonDeprecatedBcdKeys = bcdKeys.filter(({ status }) => {
return status?.standard_track === true && status?.deprecated !== true;
});
const visibleCaniuseIds = Object.entries(caniuse.features)
.filter(([, feature]) => caniuse.feature(feature).shown)
.map(([id]) => id);
const visibleCaniuseIdSet = new Set(visibleCaniuseIds);
const mappedCaniuseIds = new Set(
ordinaryFeatures
.flatMap((feature) => {
if (!feature.caniuse) {
return [];
}
return Array.isArray(feature.caniuse) ? feature.caniuse : [feature.caniuse];
})
.filter((id) => visibleCaniuseIdSet.has(id)),
);
const bcdKeysMapped = countMappedKeys(bcdKeys, mappedBcdKeys);
const standardNonDeprecatedBcdKeysMapped = countMappedKeys(
standardNonDeprecatedBcdKeys,
mappedBcdKeys,
);
return {
date,
features: ordinaryFeatures.length,
bcdKeysMapped,
bcdKeysTotal: bcdKeys.length,
bcdKeysUnmapped: bcdKeys.length - bcdKeysMapped,
bcdCoveragePercent: toPercent(bcdKeysMapped, bcdKeys.length),
standardNonDeprecatedBcdKeysMapped,
standardNonDeprecatedBcdKeysTotal: standardNonDeprecatedBcdKeys.length,
standardNonDeprecatedBcdKeysUnmapped:
standardNonDeprecatedBcdKeys.length - standardNonDeprecatedBcdKeysMapped,
standardNonDeprecatedBcdCoveragePercent: toPercent(
standardNonDeprecatedBcdKeysMapped,
standardNonDeprecatedBcdKeys.length,
),
caniuseIdsMapped: mappedCaniuseIds.size,
caniuseIdsTotal: visibleCaniuseIds.length,
caniuseIdsUnmapped: visibleCaniuseIds.length - mappedCaniuseIds.size,
caniuseCoveragePercent: toPercent(
mappedCaniuseIds.size,
visibleCaniuseIds.length,
),
};
}
function countMappedKeys(keys, mappedKeys) {
return keys.filter(({ key }) => mappedKeys.has(key)).length;
}
function mergeSnapshots(existingSnapshots, currentSnapshot) {
const snapshotsByDate = new Map(
existingSnapshots.map((snapshot) => [snapshot.date, snapshot]),
);
snapshotsByDate.set(currentSnapshot.date, currentSnapshot);
return [...snapshotsByDate.values()].sort((a, b) =>
a.date.localeCompare(b.date),
);
}
async function getVersions() {
const webFeaturesPackage = JSON.parse(
await fs.readFile(new URL("./node_modules/web-features/package.json", import.meta.url), "utf8"),
);
const caniuseLitePackage = JSON.parse(
await fs.readFile(new URL("./node_modules/caniuse-lite/package.json", import.meta.url), "utf8"),
);
return {
webFeatures: webFeaturesPackage.version,
bcd: bcd.__meta.version,
caniuseLite: caniuseLitePackage.version,
};
}
function toPercent(value, total) {
return total ? Number(((value / total) * 100).toFixed(2)) : 0;
}
main();