Skip to content

Commit

Permalink
first pass at generating feature-list json
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisC committed Dec 13, 2023
1 parent 2be5064 commit 993f111
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions scripts/update-bcd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ interface UpdateLog {
reason: Reason;
}

interface FeatureListLog {
browser: BrowserName;
path: string;
statements: SimpleSupportStatement[];
}

/** Values available in operations. */
interface UpdateState extends UpdateLog {
shared: UpdateShared;
Expand Down Expand Up @@ -1008,6 +1014,18 @@ const pickLog = <T extends UpdateLog>({
};
};

const pickFeatureList = <T extends UpdateLog>({
browser,
path,
statements,
}: T): FeatureListLog => {
return {
browser,
path,
statements,
};
};

/**
* Generates a sequence of key-value pairs representing the entries in an object tree.
* The keys are generated by concatenating the prefix with each nested key.
Expand Down Expand Up @@ -1041,8 +1059,8 @@ export const update = (
bcd: Identifier,
supportMatrix: SupportMatrix,
options: any,
): boolean => {
const changes: UpdateLog[] = [];
): FeatureListLog[] => {
const results: UpdateLog[] = [];
for (const state of compose(
expand("entry", function* () {
for (const [path, entry] of walkEntries("", bcd)) {
Expand Down Expand Up @@ -1136,7 +1154,7 @@ export const update = (
}
}),
)()) {
changes.push(pickLog(state));
results.push(pickLog(state));
if (state.statements) {
state.shared.support[state.browser] =
state.statements.length === 1 ? state.statements[0] : state.statements;
Expand All @@ -1145,8 +1163,11 @@ export const update = (
logger.warn(state.reason.message);
}
}
// TODO: Serialize changes to a file
return changes.some(({statements}) => Boolean(statements));

const updates = results
.filter(({statements}) => Boolean(statements))
.map((result) => pickFeatureList(result));
return updates;
};

/* c8 ignore start */
Expand Down Expand Up @@ -1232,18 +1253,25 @@ export const main = async (
browsers,
overrides.filter(Array.isArray as (item: unknown) => item is OverrideTuple),
);
let featureList: FeatureListLog[] = [];

// Should match https://github.com/mdn/browser-compat-data/blob/f10bf2cc7d1b001a390e70b7854cab9435ffb443/test/linter/test-style.js#L63
// TODO: https://github.com/mdn/browser-compat-data/issues/3617
for (const [file, data] of Object.entries(bcdFiles)) {
const modified = update(data, supportMatrix, filter);
if (!modified) {
const updates = update(data, supportMatrix, filter);
if (!updates.length) {
continue;
}
featureList = featureList.concat(updates);
logger.info(`Updating ${path.relative(BCD_DIR, file)}`);
const json = JSON.stringify(data, null, " ") + "\n";
await fs.writeFile(file, json);
}

if (Boolean(featureList.length)) {
const featureListJSON = JSON.stringify(featureList, null, " ") + "\n";
await fs.writeFile("feature-list.json", featureListJSON);
}
};

if (esMain(import.meta)) {
Expand Down

0 comments on commit 993f111

Please sign in to comment.