Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to cleanup redundant BCD overrides #2333

Merged
merged 21 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"test:types": "npm run --workspaces test:types && tsc",
"test": "npm run test:caniuse -- --quiet && npm run test:schema && npm run test:specs && npm run test:format && npm run test:dist && npm run test --workspaces && npm run test:lint",
"update-drafts": "tsx scripts/update-drafts.ts",
"migrate-to-bcd": "tsx scripts/migrate-to-bcd.ts"
"migrate-to-bcd": "tsx scripts/migrate-to-bcd.ts",
"cleanup-bcd-overrides": "tsx scripts/cleanup-bcd-overrides.ts"
},
"devDependencies": {
"@eslint/js": "^9.15.0",
Expand Down
105 changes: 105 additions & 0 deletions scripts/cleanup-bcd-overrides.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to suggest renaming this. There are a few possible overrides in a .yml file: compat_features, compute_from, and complete status block and all of them involve BCD to some extent. And "clean" implies that overrides are necessarily "dirty" (some are not).

My naming ideas are:

  • dedupe-compat-tags
  • tidy-compat-features
  • remove-tagged-compat-features

Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { setLogger } from "compute-baseline";
import { Compat, Feature } from "compute-baseline/browser-compat-data";
import { fdir } from "fdir";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { isDeepStrictEqual } from "node:util";
import winston from "winston";
import YAML from "yaml";
import yargs from "yargs";
import {checkForStaleCompat} from "./dist.ts";

const compat = new Compat();

const argv = yargs(process.argv.slice(2))
.scriptName("cleanup-bcd-overrides")
.usage(
"$0 [paths..]",
"Remove `compat_features` from `.yml` files that have an equivalently tagged set of features in @mdn/browser-compat-data",
(yargs) =>
yargs.positional("paths", {
describe: "Directories or files to check/update.",
default: ["features"],
}),
).argv;

const logger = winston.createLogger({
level: argv.verbose > 0 ? "debug" : "warn",
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple(),
),
transports: new winston.transports.Console(),
});

let exitStatus = 0;

setLogger(logger);

const tagsToFeatures: Map<string, Feature[]> = (() => {
// TODO: Use Map.groupBy() instead, when it's available
const map = new Map();
for (const feature of compat.walk()) {
for (const tag of feature.tags) {
let features = map.get(tag);
if (!features) {
features = [];
map.set(tag, features);
}
features.push(feature);
}
}
return map;
})();

function cleanup(sourcePath: string): void {
const source = YAML.parseDocument(
fs.readFileSync(sourcePath, { encoding: "utf-8" }),
);
const { name: id } = path.parse(sourcePath);

// Collect tagged compat features. A `compat_features` list in the source
// takes precedence, but can be removed if it matches the tagged features.
const taggedCompatFeatures = (tagsToFeatures.get(`web-features:${id}`) ?? [])
.map((f) => `${f.id}`)
.sort();

const data = source.contents.get("compat_features");

if (data) {
const features = data.items.map((item) => item.value).sort();
if (isDeepStrictEqual(features, taggedCompatFeatures)) {
const comment = data.comment || data.commentBefore;
if (comment) {
source.comment = (source.comment || "") + comment;
}
source.contents.delete("compat_features");
fs.writeFileSync(sourcePath, source.toString({ lineWidth: 0 }));
logger.info(`${id}: removed compat_features in favor of tag`);
}
}
}

function main() {
const filePaths: string[] = argv.paths.flatMap((fileOrDirectory) => {
if (fs.statSync(fileOrDirectory).isDirectory()) {
return new fdir()
.withBasePath()
.filter((path) => path.endsWith(".yml"))
.crawl(fileOrDirectory)
.sync();
}
return fileOrDirectory.endsWith(".yml") ? fileOrDirectory : [];
});

for (const sourcePath of filePaths) {
cleanup(sourcePath);
}
}

if (process.argv[1] === fileURLToPath(import.meta.url)) {
checkForStaleCompat();
main();
process.exit(exitStatus);
}
2 changes: 1 addition & 1 deletion scripts/dist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ setLogger(logger);
* one pinned in `package.json`. BCD updates frequently, leading to surprising
* error messages if you haven't run `npm install` recently.
*/
function checkForStaleCompat(): void {
export function checkForStaleCompat(): void {
const packageBCDVersionSpecifier: string = (() => {
const packageJSON: unknown = JSON.parse(
fs.readFileSync(process.env.npm_package_json, {
Expand Down
Loading