Skip to content

Commit

Permalink
Outputs a log of updated features (#947)
Browse files Browse the repository at this point in the history
* first pass at generating feature-list json

* adds parameter to specify feature-list output

* updates docstrings

* gitignores default feature-list path

* fixes formatting

* fixes call to update-bcd in add-new-bcd script
  • Loading branch information
ChrisC committed Dec 19, 2023
1 parent a8547aa commit b3d0512
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ secrets.json
tests.json
tests.old.json
.testtmp
feature-list.json
1 change: 1 addition & 0 deletions scripts/add-new-bcd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ const main = async (): Promise<void> => {
{addNewFeatures: true},
bcd.browsers,
overrides,
"",
);

console.log("Injecting BCD...");
Expand Down
64 changes: 55 additions & 9 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 @@ -1055,6 +1061,27 @@ const pickLog = <T extends UpdateLog>({
};
};

/**
* Picks specific properties from an object of type T and returns a new object of type FeatureListLog.
* @template T - The type of the input object.
* @param obj - The input object.
* @param obj.browser - The browser property of the input object.
* @param obj.path - The path property of the input object.
* @param obj.statements - The statements property of the input object.
* @returns - The new object of type FeatureListLog.
*/
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 @@ -1082,14 +1109,14 @@ export const walkEntries = function* (
* @param bcd - The BCD identifier.
* @param supportMatrix - The support matrix.
* @param options - Additional options for the update.
* @returns A boolean indicating whether any changes were made during the update.
* @returns An array of objects representing BCD paths that have been updated.
*/
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 @@ -1207,7 +1234,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 @@ -1216,8 +1243,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 @@ -1262,13 +1292,15 @@ export const loadJsonFiles = async (
* @param filter - An object containing filter options.
* @param browsers - An object representing the browsers to include in the update.
* @param overrides - An object containing override options.
* @param outputPath - An string specifying filename and path for feature list output. Defaults to `feature-list.json`.
* @returns A Promise that resolves when the update is complete.
*/
export const main = async (
reportPaths: string[],
filter: any,
browsers: Browsers,
overrides: Overrides,
outputPath: string,
): Promise<void> => {
// Replace filter.path with a minimatch object.
if (filter.path && filter.path.includes("*")) {
Expand Down Expand Up @@ -1303,18 +1335,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 (featureList.length) {
const featureListJSON = JSON.stringify(featureList, null, " ") + "\n";
await fs.writeFile(outputPath, featureListJSON);
}
};

if (esMain(import.meta)) {
Expand Down Expand Up @@ -1363,10 +1402,17 @@ if (esMain(import.meta)) {
'Only update when versions are a specific number (or "false"), disallowing ranges',
type: "boolean",
default: false,
})
.option("output", {
alias: "o",
describe:
'Specify filename and output path for a json list of updated features. Defaults to "feature-list.json"',
type: "string",
default: "feature-list.json",
});
},
);

await main(argv.reports, argv, browsers, overrides);
await main(argv.reports, argv, browsers, overrides, argv.output);
}
/* c8 ignore stop */

0 comments on commit b3d0512

Please sign in to comment.