Skip to content

Commit

Permalink
Check for stale BCD before updating dist files (#1641)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddbeck authored Sep 5, 2024
1 parent 79b6761 commit fbfb7a8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/compute-baseline/src/browser-compat-data/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import bcd, {
CompatData,
} from "@mdn/browser-compat-data" with { type: "json" };
import { Browser, Feature, browser, feature, query, walk } from "./index.js";
import { isIndexable, isMetaBlock } from "./typeUtils.js";

export class Compat {
data: unknown;
Expand All @@ -14,6 +15,17 @@ export class Compat {
this.features = new Map();
}

/**
* Get the version string from @mdn/browser-compat-data's `__meta` object (or
* `"unknown"` if unset).
*/
get version(): string {
if (isIndexable(this.data) && isMetaBlock(this.data.__meta)) {
return this.data.__meta.version;
}
return "unknown";
}

query(path: string) {
return query(path, this.data);
}
Expand Down
34 changes: 34 additions & 0 deletions scripts/dist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,39 @@ let exitStatus = 0;

setLogger(logger);

/**
* Check that the installed @mdn/browser-compat-data (BCD) package matches the
* one pinned in `package.json`. BCD updates frequently, leading to surprising
* error messages if you haven't run `npm install` recently.
*/
function checkForStaleCompat(): void {
const packageBCDVersionSpecifier: string = (() => {
const packageJSON: unknown = JSON.parse(
fs.readFileSync(process.env.npm_package_json, {
encoding: "utf-8",
}),
);
if (typeof packageJSON === "object" && "devDependencies" in packageJSON) {
const bcd = packageJSON.devDependencies["@mdn/browser-compat-data"];
if (typeof bcd === "string") {
return bcd;
}
throw new Error(
"@mdn/browser-compat-data version not found in package.json",
);
}
})();
const installedBCDVersion = compat.version;

if (!packageBCDVersionSpecifier.includes(installedBCDVersion)) {
logger.error(
`Installed @mdn/browser-compat-data (${installedBCDVersion}) does not match package.json version (${packageBCDVersionSpecifier})`,
);
logger.error("Run `npm install` and try again.");
process.exit(1);
}
}

/**
* Update (or create) a dist YAML file from a feature definition YAML file.
*
Expand Down Expand Up @@ -379,6 +412,7 @@ function main() {
}

if (process.argv[1] === fileURLToPath(import.meta.url)) {
checkForStaleCompat();
main();
process.exit(exitStatus);
}

0 comments on commit fbfb7a8

Please sign in to comment.