Skip to content

Commit 7780216

Browse files
committed
update-drafts script: don't make date-only modifications
This skips writing changes to draft features when the only change is the `draft_date` value. This prevents needlessly large diffs when updating these files and makes it possible to find out which areas we've touched least-recently. Fixes #1716
1 parent 87db5b8 commit 7780216

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

package-lock.json

Lines changed: 20 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@
3939
"@js-temporal/polyfill": "^0.4.4",
4040
"@mdn/browser-compat-data": "^5.5.50",
4141
"@types/caniuse-lite": "^1.0.4",
42+
"@types/diff": "^5.2.2",
4243
"@types/eslint__js": "^8.42.3",
4344
"@types/node": "^18.19.50",
4445
"ajv": "^8.17.1",
4546
"ajv-formats": "^3.0.1",
4647
"caniuse-lite": "^1.0.30001658",
48+
"diff": "^7.0.0",
4749
"eslint-plugin-new-with-error": "^5.0.0",
4850
"fast-json-stable-stringify": "^2.1.0",
4951
"fdir": "^6.3.0",

scripts/update-drafts.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Compat } from "compute-baseline/browser-compat-data";
2-
import fs from "node:fs/promises";
3-
import fsSync from "node:fs";
2+
import * as diff from "diff";
43
import { fdir } from "fdir";
5-
import Path from "path";
4+
import fsSync from "node:fs";
5+
import fs from "node:fs/promises";
66
import { fileURLToPath } from "node:url";
7+
import Path from "path";
78
import webSpecs from "web-specs" assert { type: "json" };
9+
import winston from "winston";
810
import { Document } from "yaml";
911
import yargs from "yargs";
1012

@@ -22,8 +24,24 @@ const argv = yargs(process.argv.slice(2))
2224
.option("paths", {
2325
type: "array",
2426
describe: "Draft feature files to update",
27+
})
28+
.option("verbose", {
29+
alias: "v",
30+
describe: "Show more information about what files are modified",
31+
type: "count",
32+
default: 0,
33+
defaultDescription: "warn",
2534
}).argv;
2635

36+
const logger = winston.createLogger({
37+
level: argv.verbose > 0 ? "debug" : "info",
38+
format: winston.format.combine(
39+
winston.format.colorize(),
40+
winston.format.simple(),
41+
),
42+
transports: new winston.transports.Console(),
43+
});
44+
2745
function* getPages(spec): Generator<string> {
2846
yield spec.url;
2947
if (spec.nightly?.url) {
@@ -178,7 +196,43 @@ async function main() {
178196

179197
feature.comment = usedFeaturesComment.trimEnd();
180198
}
181-
await fs.writeFile(`features/draft/spec/${id}.yml`, feature.toString());
199+
200+
const destination = `features/draft/spec/${id}.yml`;
201+
const proposedFile = feature.toString();
202+
203+
let originalFile: string;
204+
try {
205+
originalFile = await fs.readFile(destination, { encoding: "utf-8" });
206+
} catch (err: unknown) {
207+
// If there's no file for this spec already, write a new one immediately.
208+
if (typeof err === "object" && "code" in err && err.code === "ENOENT") {
209+
await fs.writeFile(destination, proposedFile);
210+
logger.info(`${destination}: new spec file added`);
211+
continue;
212+
}
213+
throw err;
214+
}
215+
216+
// If there's a file for this spec already, write updates only if something
217+
// other than the `draft_date` changed. Because changes can be comments, we
218+
// have to check a diff rather than parsing and comparing values.
219+
const changes = diff.diffLines(originalFile, proposedFile);
220+
const noChanges =
221+
changes.length === 1 && !changes[0].added && !changes[0].removed;
222+
const onlyDateChanges =
223+
!noChanges &&
224+
changes.length === 3 &&
225+
changes
226+
.filter((change) => change.added || change.removed)
227+
.every((change) => change.value.includes("draft_date: "));
228+
229+
if (noChanges || onlyDateChanges) {
230+
logger.debug(`${destination}: no changes, skipped`);
231+
continue;
232+
}
233+
234+
await fs.writeFile(destination, proposedFile);
235+
logger.info(`${destination}: updated`);
182236
}
183237
}
184238

0 commit comments

Comments
 (0)