-
Notifications
You must be signed in to change notification settings - Fork 106
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
Elchi3
merged 21 commits into
web-platform-dx:main
from
queengooborg:cleanup-bcd-overrides
Jan 21, 2025
Merged
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5706bda
Add script to cleanup redundant BCD overrides
queengooborg 1ceecc2
Fix formatting
queengooborg 62a1596
Apply suggestions from code review
queengooborg da0651e
Remove unused imports
queengooborg f66e102
De-duplicate code
queengooborg a28194e
Preserve comments before compat_features key
queengooborg ad1acab
Catch additional comments found
queengooborg 674af90
Don't stringify and add "null"
queengooborg d04b1fd
Update scripts/cleanup-bcd-overrides.ts
queengooborg 4b5a035
Update scripts/cleanup-bcd-overrides.ts
queengooborg 8e65cc6
Format and add comments
queengooborg f7bc2da
Remove leftover line from old code
queengooborg 8e9e70c
Mitigate TypeScript issue
queengooborg 4eccbfd
Merge branch 'main' of github.com:web-platform-dx/web-features into c…
jamesnw 39fac43
Get additional comments
queengooborg 944b1f5
Fix destructuring bug
jamesnw ff2622d
Rename
queengooborg 0ba6ea3
Run formatter afterwards
queengooborg b43a336
Merge branch 'main' into cleanup-bcd-overrides
Elchi3 03f65e3
Update scripts/remove-tagged-compat-features.ts
Elchi3 1a257a8
Update scripts/remove-tagged-compat-features.ts
Elchi3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
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"; | ||
|
||
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 as any).get("compat_features"); | ||
queengooborg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (data) { | ||
const features = data.items.map((item) => item.value).sort(); | ||
if (isDeepStrictEqual(features, taggedCompatFeatures)) { | ||
// Preserve comments around the compat_features key | ||
const comments = data.commentBefore ? [data.commentBefore] : []; | ||
data.items.reduce((acc, item) => { | ||
if (item.commentBefore) acc.push(item.commentBefore); | ||
if (item.comment) acc.push(item.comment); | ||
return acc; | ||
}, comments); | ||
if (data.comment) comments.push(data.comment); | ||
if (comments.length) { | ||
source.comment = (source.comment || "") + comments.join("\n"); | ||
} | ||
|
||
// Delete the key | ||
(source.contents as any).delete("compat_features"); | ||
fs.writeFileSync(sourcePath, source.toString({ lineWidth: 0 })); | ||
queengooborg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 completestatus
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: