From 09cb3ec10cf7f897293742eba489b8b36f3cda57 Mon Sep 17 00:00:00 2001 From: James Stuckey Weber Date: Tue, 27 Aug 2024 10:55:24 -0400 Subject: [PATCH 1/5] Add filter argument to update-drafts script --- scripts/update-drafts.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/scripts/update-drafts.ts b/scripts/update-drafts.ts index f0dc55659c9..8a3eb34987c 100644 --- a/scripts/update-drafts.ts +++ b/scripts/update-drafts.ts @@ -1,3 +1,7 @@ +// Updates files in features/draft/spec/* with BCD keys mentioned in specs +// `npm run update-drafts` updates all +// `npm run update-drafts -- [key]` only updates features with a `shortname` that includes `key`. + import { Compat } from "compute-baseline/browser-compat-data"; import fs from "node:fs/promises"; import { fileURLToPath } from "node:url"; @@ -41,7 +45,7 @@ function formatIdentifier(s: string): string { .join("-"); } -async function main() { +async function main(specFilter: string = undefined) { const compat = new Compat(); // Build a map of used BCD keys to feature. @@ -56,7 +60,12 @@ async function main() { // Build a map from URLs to spec. const pageToSpec = new Map(); - for (const spec of webSpecs) { + + const selectedSpecs = specFilter + ? webSpecs.filter((ws) => ws.shortname.includes(specFilter)) + : webSpecs; + + for (const spec of selectedSpecs) { for (const page of getPages(spec)) { pageToSpec.set(normalize(page), spec); } @@ -85,7 +94,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(!specFilter) console.warn(`${url} not matched to any spec`); continue; } const keys = specToCompatFeatures.get(spec); @@ -141,5 +150,5 @@ async function main() { } if (process.argv[1] === fileURLToPath(import.meta.url)) { - await main(); + await main(process.argv[2]); } From 928b89654146599e23db70ec764d957b9293e6e1 Mon Sep 17 00:00:00 2001 From: James Stuckey Weber Date: Tue, 27 Aug 2024 11:01:16 -0400 Subject: [PATCH 2/5] Format --- scripts/update-drafts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/update-drafts.ts b/scripts/update-drafts.ts index 8a3eb34987c..ba3c10736fc 100644 --- a/scripts/update-drafts.ts +++ b/scripts/update-drafts.ts @@ -94,7 +94,7 @@ async function main(specFilter: string = undefined) { for (const url of feature.spec_url) { const spec = pageToSpec.get(normalize(url)); if (!spec) { - if(!specFilter) console.warn(`${url} not matched to any spec`); + if (!specFilter) console.warn(`${url} not matched to any spec`); continue; } const keys = specToCompatFeatures.get(spec); From 13ff87182df10213aaf64a34f25a020149f3f7f2 Mon Sep 17 00:00:00 2001 From: James Stuckey Weber Date: Wed, 28 Aug 2024 14:27:46 -0400 Subject: [PATCH 3/5] Add keys and paths filter to update-drafts --- scripts/update-drafts.ts | 53 ++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/scripts/update-drafts.ts b/scripts/update-drafts.ts index ba3c10736fc..ad39ed74d0f 100644 --- a/scripts/update-drafts.ts +++ b/scripts/update-drafts.ts @@ -1,17 +1,29 @@ -// Updates files in features/draft/spec/* with BCD keys mentioned in specs -// `npm run update-drafts` updates all -// `npm run update-drafts -- [key]` only updates features with a `shortname` that includes `key`. - 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; + function* getPages(spec): Generator { yield spec.url; if (spec.nightly?.url) { @@ -45,7 +57,9 @@ function formatIdentifier(s: string): string { .join("-"); } -async function main(specFilter: string = undefined) { +async function main() { + const { keys: filterKeys, paths: filterPaths } = argv; + const compat = new Compat(); // Build a map of used BCD keys to feature. @@ -61,9 +75,28 @@ async function main(specFilter: string = undefined) { // Build a map from URLs to spec. const pageToSpec = new Map(); - const selectedSpecs = specFilter - ? webSpecs.filter((ws) => ws.shortname.includes(specFilter)) - : 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 = 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)) { @@ -94,7 +127,7 @@ async function main(specFilter: string = undefined) { for (const url of feature.spec_url) { const spec = pageToSpec.get(normalize(url)); if (!spec) { - if (!specFilter) console.warn(`${url} not matched to any spec`); + if (!selectedKeys) console.warn(`${url} not matched to any spec`); continue; } const keys = specToCompatFeatures.get(spec); @@ -150,5 +183,5 @@ async function main(specFilter: string = undefined) { } if (process.argv[1] === fileURLToPath(import.meta.url)) { - await main(process.argv[2]); + await main(); } From 5d253b5bd099ef7b266a291b2bfda4b9e09350c8 Mon Sep 17 00:00:00 2001 From: James Stuckey Weber Date: Thu, 29 Aug 2024 23:04:53 -0400 Subject: [PATCH 4/5] Allow paths AND keys --- scripts/update-drafts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/update-drafts.ts b/scripts/update-drafts.ts index ad39ed74d0f..cbfe4cb242e 100644 --- a/scripts/update-drafts.ts +++ b/scripts/update-drafts.ts @@ -89,7 +89,7 @@ async function main() { } return fileOrDirectory; }); - selectedKeys = filePaths.map((fp) => Path.parse(fp).name); + selectedKeys.push(...filePaths.map((fp) => Path.parse(fp).name)); } if (selectedKeys?.length) { From 9dcb567c717abd22985f443837d34e80d53e7269 Mon Sep 17 00:00:00 2001 From: James Stuckey Weber Date: Sat, 7 Sep 2024 13:37:37 -0400 Subject: [PATCH 5/5] Fix bug --- scripts/update-drafts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/update-drafts.ts b/scripts/update-drafts.ts index cbfe4cb242e..80741900e31 100644 --- a/scripts/update-drafts.ts +++ b/scripts/update-drafts.ts @@ -76,7 +76,7 @@ async function main() { const pageToSpec = new Map(); let selectedSpecs = webSpecs; - let selectedKeys = filterKeys; + let selectedKeys = filterKeys ?? []; if (filterPaths?.length) { const filePaths = filterPaths.flatMap((fileOrDirectory) => {