Skip to content

Use new standard URL objects for paths #1065

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
Jul 10, 2021
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
54 changes: 26 additions & 28 deletions deploy/createTypesPackages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@

// prettier-ignore
export const packages = [
{
name: "@types/web",
description: "Types for the DOM, and other web technologies in browsers",
readme: "./readmes/web.md",
files: [
{ from: "../generated/dom.generated.d.ts", to: "index.d.ts" },
{ from: "../generated/dom.iterable.generated.d.ts", to: "index.iterable.d.ts" }
],
},
];
{
name: "@types/web",
description: "Types for the DOM, and other web technologies in browsers",
readme: "./readmes/web.md",
files: [
{ from: "../generated/dom.generated.d.ts", to: "index.d.ts" },
{ from: "../generated/dom.iterable.generated.d.ts", to: "index.iterable.d.ts" }
],
},
];

// Note: You can add 'version: "1.0.0"' to a package above
// to set the major or minor, otherwise it will always bump
// the patch.

import { join, dirname } from "path";
import fs from "fs";
import fetch from "node-fetch";
import { fileURLToPath } from "url";
Expand All @@ -28,51 +27,48 @@ import pkg from "prettier";
const { format } = pkg;
import { execSync } from "child_process";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const go = async () => {
const gitSha = execSync("git rev-parse HEAD").toString().trim().slice(0, 7);

const generatedDir = join(__dirname, "generated");
const templateDir = join(__dirname, "template");
const generatedDir = new URL("generated/", import.meta.url);
const templateDir = new URL("template/", import.meta.url);

for (const pkg of packages) {
const folderName = pkg.name.replace("@", "").replace("/", "-");
const packagePath = join(generatedDir, folderName);
const packagePath = new URL(`${folderName}/`, generatedDir);

if (fs.existsSync(packagePath)) fs.rmSync(packagePath, { recursive: true });
if (fs.existsSync(packagePath)) {
await fs.promises.rm(packagePath, { recursive: true });
}
fs.mkdirSync(packagePath, { recursive: true });

// Migrate in the template files
for (const templateFile of fs.readdirSync(templateDir)) {
if (templateFile.startsWith(".")) continue;

const templatedFile = join(templateDir, templateFile);
fs.copyFileSync(templatedFile, join(packagePath, templateFile));
const templatedFile = new URL(templateFile, templateDir);
fs.copyFileSync(templatedFile, new URL(templateFile, packagePath));
}

// Add the reference files in the config above
pkg.files.forEach((fileRef) => {
fs.copyFileSync(
join(__filename, "..", fileRef.from),
join(packagePath, fileRef.to)
new URL(fileRef.from, import.meta.url),
new URL(fileRef.to, packagePath)
);
});

// Setup the files in the repo
const newPkgJSON = await updatePackageJSON(packagePath, pkg, gitSha);
copyREADME(pkg, newPkgJSON, join(packagePath, "README.md"));
copyREADME(pkg, newPkgJSON, new URL("README.md", packagePath));

// Done
console.log("Built:", pkg.name);
}
};

async function updatePackageJSON(packagePath, pkg, gitSha) {
const pkgJSONPath = join(packagePath, "package.json");
const pkgJSONPath = new URL("package.json", packagePath);
const packageText = fs.readFileSync(pkgJSONPath, "utf8");
const packageJSON = JSON.parse(packageText);
packageJSON.name = pkg.name;
Expand Down Expand Up @@ -107,15 +103,17 @@ async function updatePackageJSON(packagePath, pkg, gitSha) {

fs.writeFileSync(
pkgJSONPath,
format(JSON.stringify(packageJSON), { filepath: pkgJSONPath })
format(JSON.stringify(packageJSON), {
filepath: fileURLToPath(pkgJSONPath),
})
);

return packageJSON;
}

// Copies the README and adds some rudimentary templating to the file.
function copyREADME(pkg, pkgJSON, writePath) {
let readme = fs.readFileSync(join(__filename, "..", pkg.readme), "utf-8");
let readme = fs.readFileSync(new URL(pkg.readme, import.meta.url), "utf-8");

const htmlEncodedTag =
encodeURIComponent(pkgJSON.name) + "%40" + pkgJSON.version;
Expand Down
22 changes: 9 additions & 13 deletions deploy/deployChangedPackages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,25 @@
// ones which have changed.

import * as fs from "fs";
import { join, dirname, basename } from "path";
import { fileURLToPath } from "url";
import { basename } from "path";
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
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import { fileURLToPath } from "node:url";

verify();

const uploaded = [];

// Loop through generated packages, deploying versions for anything which has different
// .d.ts files from the version available on npm.
const generatedDir = join(__dirname, "generated");
const generatedDir = new URL("generated/", import.meta.url);
for (const dirName of fs.readdirSync(generatedDir)) {
console.log(`Looking at ${dirName}`);
const localPackageJSONPath = join(generatedDir, dirName, "package.json");
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);

Expand All @@ -37,7 +33,7 @@ for (const dirName of fs.readdirSync(generatedDir)) {
const thisPackageMeta = packages.find((p) => p.name === pkgJSON.name);

const dtsFiles = fs
.readdirSync(join(generatedDir, dirName))
.readdirSync(packageDir)
.filter((f) => f.endsWith(".d.ts"));

/** @type {string[]} */
Expand All @@ -51,7 +47,7 @@ for (const dirName of fs.readdirSync(generatedDir)) {
thisPackageMeta.files.find((f) => f.to === file).from
);

const generatedDTSPath = join(generatedDir, dirName, file);
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
Expand Down Expand Up @@ -86,7 +82,7 @@ Assuming that this means we need to upload this package.`);
if (upload) {
if (process.env.NODE_AUTH_TOKEN) {
const publish = spawnSync("npm", ["publish", "--access", "public"], {
cwd: join(generatedDir, dirName),
cwd: fileURLToPath(packageDir),
stdio: "inherit",
});

Expand All @@ -102,7 +98,7 @@ Assuming that this means we need to upload this package.`);
} else {
console.log(
"Wanting to run: 'npm publish --access public' in " +
join(generatedDir, dirName)
fileURLToPath(packageDir)
);
}

Expand Down