Skip to content

Commit

Permalink
Switch to using git tags to compare the dts files
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Jul 9, 2021
1 parent 292e4a2 commit 4b2e311
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
8 changes: 5 additions & 3 deletions deploy/createTypesPackages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// node deploy/createTypesPackages.mjs

// prettier-ignore
const packages = [
export const packages = [
{
name: "@types/web",
description: "Types for the DOM, and other web technologies in browsers",
Expand Down Expand Up @@ -71,8 +71,6 @@ const go = async () => {
}
};

go();

async function updatePackageJSON(packagePath, pkg, gitSha) {
const pkgJSONPath = join(packagePath, "package.json");
const packageText = fs.readFileSync(pkgJSONPath, "utf8");
Expand Down Expand Up @@ -131,3 +129,7 @@ function copyREADME(pkg, pkgJSON, writePath) {

fs.writeFileSync(writePath, readme);
}

if (process.argv[1] === fileURLToPath(import.meta.url)) {
go();
}
40 changes: 29 additions & 11 deletions deploy/deployChangedPackages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
// ones which have changed.

import * as fs from "fs";
import { join, dirname } from "path";
import { join, dirname, basename } from "path";
import { fileURLToPath } from "url";
import fetch from "node-fetch";
import { spawnSync } from "child_process";
import { spawnSync, execSync } from "child_process";
import { Octokit } from "@octokit/core";
import printDiff from "print-diff";
import { generateChangelogFrom } from "../lib/changelog.js";
import { packages } from "./createTypesPackages.mjs";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand All @@ -27,6 +27,9 @@ const verify = () => {
);
};

const gitShowFile = (commitish, path) =>
execSync(`git show "${commitish}":${path}`, { encoding: "utf-8" });

const go = async () => {
verify();

Expand All @@ -41,6 +44,10 @@ const go = async () => {
const newTSConfig = fs.readFileSync(localPackageJSONPath, "utf-8");
const pkgJSON = JSON.parse(newTSConfig);

// We'll need to map back from the filename in the npm package to the
// generated file in baselines inside the git tag
const thisPackageMeta = packages.find((p) => p.name === pkgJSON.name);

const dtsFiles = fs
.readdirSync(join(generatedDir, dirName))
.filter((f) => f.endsWith(".d.ts"));
Expand All @@ -52,25 +59,36 @@ const go = async () => {
// determine if anything has changed
let upload = false;
for (const file of dtsFiles) {
const originalFilename = basename(
thisPackageMeta.files.find((f) => f.to === file).from
);

const generatedDTSPath = join(generatedDir, dirName, file);
const generatedDTSContent = fs.readFileSync(generatedDTSPath, "utf8");
const unpkgURL = `https://unpkg.com/${pkgJSON.name}/${file}`;

// This assumes we'll only _ever_ ship patches, which may change in the
// future someday.
const [maj, min, patch] = pkgJSON.version.split(".");
const olderVersion = `${maj}.${min}.${patch - 1}`;

try {
const npmDTSReq = await fetch(unpkgURL);
const npmDTSText = await npmDTSReq.text();
console.log(`Comparing ${file} from unpkg, to generated version:`);
printDiff(npmDTSText, generatedDTSContent);
const oldFile = gitShowFile(
`${pkgJSON.name}@${olderVersion}`,
`baselines/${originalFilename}`
);
console.log(`Comparing ${file} from ${olderVersion}, to now:`);
printDiff(oldFile, generatedDTSContent);

const title = `\n## \`${file}\`\n`;
const notes = generateChangelogFrom(npmDTSText, generatedDTSContent);
const notes = generateChangelogFrom(oldFile, generatedDTSContent);
releaseNotes.push(title);
releaseNotes.push(notes.trim() === "" ? "No changes" : notes);

upload = upload || npmDTSText !== generatedDTSContent;
upload = upload || oldFile !== generatedDTSContent;
} catch (error) {
// Could not find a previous build
console.log(`
Could not get the file ${file} inside the npm package ${pkgJSON.name} from unpkg at ${unpkgURL}
Could not get the file ${file} inside the npm package ${pkgJSON.name} from tag ${olderVersion}.
Assuming that this means we need to upload this package.`);
upload = true;
}
Expand Down
6 changes: 5 additions & 1 deletion src/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ export function generateDefaultFromRecentTag(): string {
const [base = gitLatestTag(), head = "HEAD"] = process.argv.slice(2);
const previous = gitShowFile(base, dom);
const current = gitShowFile(head, dom);
return generateChangelogFrom(previous, current);
const changelog = generateChangelogFrom(previous, current);
if (!changelog.length) {
throw new Error(`No change reported between ${base} and ${head}.`);
}
return changelog;
}

export function generateChangelogFrom(
Expand Down

0 comments on commit 4b2e311

Please sign in to comment.