Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
"@js-temporal/polyfill": "^0.4.4",
"@mdn/browser-compat-data": "^5.5.51",
"@types/caniuse-lite": "^1.0.4",
"@types/diff": "^5.2.2",
"@types/node": "^18.19.50",
"ajv": "^8.17.1",
"ajv-formats": "^3.0.1",
"caniuse-lite": "^1.0.30001660",
"diff": "^7.0.0",
"eslint-plugin-new-with-error": "^5.0.0",
"fast-json-stable-stringify": "^2.1.0",
"fdir": "^6.3.0",
Expand Down
68 changes: 64 additions & 4 deletions scripts/update-drafts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Compat } from "compute-baseline/browser-compat-data";
import fs from "node:fs/promises";
import fsSync from "node:fs";
import * as diff from "diff";
import { fdir } from "fdir";
import Path from "path";
import fsSync from "node:fs";
import fs from "node:fs/promises";
import { fileURLToPath } from "node:url";
import Path from "path";
import webSpecs from "web-specs" assert { type: "json" };
import winston from "winston";
import { Document } from "yaml";
import yargs from "yargs";

Expand All @@ -22,8 +24,24 @@ const argv = yargs(process.argv.slice(2))
.option("paths", {
type: "array",
describe: "Draft feature files to update",
})
.option("verbose", {
alias: "v",
describe: "Show more information about what files are modified",
type: "count",
default: 0,
defaultDescription: "warn",
}).argv;

const logger = winston.createLogger({
level: argv.verbose > 0 ? "debug" : "info",
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple(),
),
transports: new winston.transports.Console(),
});

function* getPages(spec): Generator<string> {
yield spec.url;
if (spec.nightly?.url) {
Expand Down Expand Up @@ -178,7 +196,49 @@ async function main() {

feature.comment = usedFeaturesComment.trimEnd();
}
await fs.writeFile(`features/draft/spec/${id}.yml`, feature.toString());

const destination = `features/draft/spec/${id}.yml`;
const proposedFile = feature.toString();

let originalFile: string;
try {
originalFile = await fs.readFile(destination, { encoding: "utf-8" });
} catch (err: unknown) {
// If there's no file for this spec already, write a new one immediately.
if (typeof err === "object" && "code" in err && err.code === "ENOENT") {
await fs.writeFile(destination, proposedFile);
logger.info(`${destination}: new spec file added`);
continue;
}
throw err;
}

// If there's a file for this spec already, write updates only if something
// other than the `draft_date` changed. Because changes can be comments, we
// have to check a diff rather than parsing and comparing values.
const changes = diff.diffLines(originalFile, proposedFile);

// When there are no changes, diffLines returns 1 "change" with nothing
// added or removed.
const noChanges =
changes.length === 1 && !changes[0].added && !changes[0].removed;

// When there are date-only changes, diffLines returns 3 "changes" (one added, one
// removed, the rest with nothing added or removed).
const onlyDateChanges =
!noChanges &&
changes.length === 3 &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document the magic 1 and 3 numbers above

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I won't even remember that in a week. 😅 Fixed with 13e94db.

changes
.filter((change) => change.added || change.removed)
.every((change) => change.value.includes("draft_date: "));

if (noChanges || onlyDateChanges) {
logger.debug(`${destination}: no changes, skipped`);
continue;
}

await fs.writeFile(destination, proposedFile);
logger.info(`${destination}: updated`);
}
}

Expand Down