Skip to content
Merged
Changes from all 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
46 changes: 44 additions & 2 deletions scripts/update-drafts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { Compat } from "compute-baseline/browser-compat-data";
import fs from "node:fs/promises";
import fsSync from "node:fs";
import { fdir } from "fdir";
import Path from "path";
import { fileURLToPath } from "node:url";
import webSpecs from "web-specs" assert { type: "json" };
import { Document } from "yaml";
import yargs from "yargs";

import { features } from "../index.js";

type WebSpecsSpec = (typeof webSpecs)[number];

const argv = yargs(process.argv.slice(2))
.scriptName("update-drafts")
.usage("$0", "Update draft features with BCD keys mentioned in specs.")
.option("keys", {
type: "array",
describe: "Keys to match",
})
.option("paths", {
type: "array",
describe: "Draft feature files to update",
}).argv;

Comment on lines +15 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

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

So CLIs are always more complicated than I imagine. 😅

This behaves very strangely if both options are set. I think it's applying both filters, such that nothing gets to match? I'm not sure.

I think the right thing to do here is to make sure the options don't layer on top of each other (or set some kind of flag like --matches=id, --matches=path, and then all positional arguments must be one type). But this is starting to get complicated and holding up useful work.

How about this: would you like to revert the changes after my review, add a # TODO: linking back here and one of us can take that up in a follow up PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was a simple change to make it an AND- for instance, npx tsx scripts/update-drafts.ts --keys css --paths features/draft/spec/ba* will update all CSS specs AND badging.yml and battery-status.yml.

If this doesn't seem correct, I'll revert and TODO.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nearly there! See my comment below.

function* getPages(spec): Generator<string> {
yield spec.url;
if (spec.nightly?.url) {
Expand Down Expand Up @@ -42,6 +58,8 @@ function formatIdentifier(s: string): string {
}

async function main() {
const { keys: filterKeys, paths: filterPaths } = argv;

const compat = new Compat();

// Build a map of used BCD keys to feature.
Expand All @@ -56,7 +74,31 @@ async function main() {

// Build a map from URLs to spec.
const pageToSpec = new Map<string, WebSpecsSpec>();
for (const spec of webSpecs) {

let selectedSpecs = webSpecs;
let selectedKeys = filterKeys ?? [];

if (filterPaths?.length) {
const filePaths = filterPaths.flatMap((fileOrDirectory) => {
if (fsSync.statSync(fileOrDirectory).isDirectory()) {
return new fdir()
.withBasePath()
.filter((fp) => fp.endsWith(".yml"))
.crawl(fileOrDirectory)
.sync();
}
return fileOrDirectory;
});
selectedKeys.push(...filePaths.map((fp) => Path.parse(fp).name));
}

if (selectedKeys?.length) {
selectedSpecs = selectedSpecs.filter((ws) =>
selectedKeys.some((selectedKey) => ws.shortname.includes(selectedKey)),
);
}

for (const spec of selectedSpecs) {
for (const page of getPages(spec)) {
pageToSpec.set(normalize(page), spec);
}
Expand Down Expand Up @@ -85,7 +127,7 @@ async function main() {
for (const url of feature.spec_url) {
const spec = pageToSpec.get(normalize(url));
if (!spec) {
console.warn(`${url} not matched to any spec`);
if (!selectedKeys) console.warn(`${url} not matched to any spec`);
continue;
}
const keys = specToCompatFeatures.get(spec);
Expand Down