Skip to content

Uses unpkg to compare files before deploying #1120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2021
Merged
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
44 changes: 26 additions & 18 deletions deploy/deployChangedPackages.js
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 { basename } from "path";
import { spawnSync } from "child_process";
import { Octokit } from "@octokit/rest";
import printDiff from "print-diff";
import { gitShowFile, generateChangelogFrom } from "../lib/changelog.js";
import { generateChangelogFrom } from "../lib/changelog.js";
import { packages } from "./createTypesPackages.js";
import { fileURLToPath } from "node:url";
import fetch from "node-fetch";

verify();

Expand All @@ -22,12 +22,18 @@ const uploaded = [];
// .d.ts files from the version available on npm.
const generatedDir = new URL("generated/", import.meta.url);
for (const dirName of fs.readdirSync(generatedDir)) {
console.log(`Looking at ${dirName}`);
const packageDir = new URL(`${dirName}/`, generatedDir);
const localPackageJSONPath = new URL("package.json", packageDir);
const newTSConfig = fs.readFileSync(localPackageJSONPath, "utf-8");
const pkgJSON = JSON.parse(newTSConfig);

// 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}`;

console.log(`\nLooking at ${dirName} vs ${olderVersion}`);

// 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);
Expand All @@ -49,23 +55,17 @@ for (const dirName of fs.readdirSync(generatedDir)) {
if (!filemap) {
throw new Error(`Couldn't find ${file} from ${pkgJSON.name}`);
}
const originalFilename = basename(filemap.from);

const generatedDTSPath = new URL(file, packageDir);
const generatedDTSContent = fs.readFileSync(generatedDTSPath, "utf8");

// 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 oldFile = gitShowFile(
`${pkgJSON.name}@${olderVersion}`,
`baselines/${originalFilename}`
const oldFile = await getFileFromUnpkg(
`${pkgJSON.name}@${olderVersion}/${filemap.to}`
);
console.log(`Comparing ${file} from ${olderVersion}, to now:`);
printDiff(oldFile, generatedDTSContent);
console.log(` - ${file}`);
if (oldFile !== generatedDTSContent)
printDiff(oldFile, generatedDTSContent);

const title = `\n## \`${file}\`\n\n`;
const notes = generateChangelogFrom(oldFile, generatedDTSContent);
Expand All @@ -78,6 +78,7 @@ for (const dirName of fs.readdirSync(generatedDir)) {
console.log(`
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.`);
console.error(error);
upload = true;
}
}
Expand Down Expand Up @@ -107,11 +108,13 @@ Assuming that this means we need to upload this package.`);
}

uploaded.push(dirName);
}

console.log("\n# Release notes:");
console.log(releaseNotes, "\n\n");
console.log("\n# Release notes:");
console.log(releaseNotes, "\n\n");
}
}
console.log("");
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this for? 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To force a newline between the 'looking at' section and the 'results' (in the above example that's before 'did a dry run')


// Warn if we did a dry run.
if (!process.env.NODE_AUTH_TOKEN) {
console.log("Did a dry run because process.env.NODE_AUTH_TOKEN is not set.");
Expand All @@ -120,7 +123,7 @@ if (!process.env.NODE_AUTH_TOKEN) {
if (uploaded.length) {
console.log("Uploaded: ", uploaded.join(", "));
} else {
console.log("No uploads");
console.log("Nothing to upload");
}

/**
Expand Down Expand Up @@ -154,3 +157,8 @@ function verify() {
"There isn't an ENV var set up for creating a GitHub release, expected GITHUB_TOKEN."
);
}

/** @param {string} filepath */
function getFileFromUnpkg(filepath) {
return fetch(`https://unpkg.com/${filepath}`).then((r) => r.text());
}