Skip to content

Commit 3336cb6

Browse files
author
Orta Therox
authored
Merge pull request #1065 from saschanaz/fs-url
Use new standard URL objects for paths
2 parents bc78f9c + e4fc81b commit 3336cb6

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

deploy/createTypesPackages.mjs

+26-28
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@
44

55
// prettier-ignore
66
export const packages = [
7-
{
8-
name: "@types/web",
9-
description: "Types for the DOM, and other web technologies in browsers",
10-
readme: "./readmes/web.md",
11-
files: [
12-
{ from: "../generated/dom.generated.d.ts", to: "index.d.ts" },
13-
{ from: "../generated/dom.iterable.generated.d.ts", to: "index.iterable.d.ts" }
14-
],
15-
},
16-
];
7+
{
8+
name: "@types/web",
9+
description: "Types for the DOM, and other web technologies in browsers",
10+
readme: "./readmes/web.md",
11+
files: [
12+
{ from: "../generated/dom.generated.d.ts", to: "index.d.ts" },
13+
{ from: "../generated/dom.iterable.generated.d.ts", to: "index.iterable.d.ts" }
14+
],
15+
},
16+
];
1717

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

22-
import { join, dirname } from "path";
2322
import fs from "fs";
2423
import fetch from "node-fetch";
2524
import { fileURLToPath } from "url";
@@ -28,51 +27,48 @@ import pkg from "prettier";
2827
const { format } = pkg;
2928
import { execSync } from "child_process";
3029

31-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
32-
// @ts-ignore
33-
const __filename = fileURLToPath(import.meta.url);
34-
const __dirname = dirname(__filename);
35-
3630
const go = async () => {
3731
const gitSha = execSync("git rev-parse HEAD").toString().trim().slice(0, 7);
3832

39-
const generatedDir = join(__dirname, "generated");
40-
const templateDir = join(__dirname, "template");
33+
const generatedDir = new URL("generated/", import.meta.url);
34+
const templateDir = new URL("template/", import.meta.url);
4135

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

46-
if (fs.existsSync(packagePath)) fs.rmSync(packagePath, { recursive: true });
40+
if (fs.existsSync(packagePath)) {
41+
await fs.promises.rm(packagePath, { recursive: true });
42+
}
4743
fs.mkdirSync(packagePath, { recursive: true });
4844

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

53-
const templatedFile = join(templateDir, templateFile);
54-
fs.copyFileSync(templatedFile, join(packagePath, templateFile));
49+
const templatedFile = new URL(templateFile, templateDir);
50+
fs.copyFileSync(templatedFile, new URL(templateFile, packagePath));
5551
}
5652

5753
// Add the reference files in the config above
5854
pkg.files.forEach((fileRef) => {
5955
fs.copyFileSync(
60-
join(__filename, "..", fileRef.from),
61-
join(packagePath, fileRef.to)
56+
new URL(fileRef.from, import.meta.url),
57+
new URL(fileRef.to, packagePath)
6258
);
6359
});
6460

6561
// Setup the files in the repo
6662
const newPkgJSON = await updatePackageJSON(packagePath, pkg, gitSha);
67-
copyREADME(pkg, newPkgJSON, join(packagePath, "README.md"));
63+
copyREADME(pkg, newPkgJSON, new URL("README.md", packagePath));
6864

6965
// Done
7066
console.log("Built:", pkg.name);
7167
}
7268
};
7369

7470
async function updatePackageJSON(packagePath, pkg, gitSha) {
75-
const pkgJSONPath = join(packagePath, "package.json");
71+
const pkgJSONPath = new URL("package.json", packagePath);
7672
const packageText = fs.readFileSync(pkgJSONPath, "utf8");
7773
const packageJSON = JSON.parse(packageText);
7874
packageJSON.name = pkg.name;
@@ -107,15 +103,17 @@ async function updatePackageJSON(packagePath, pkg, gitSha) {
107103

108104
fs.writeFileSync(
109105
pkgJSONPath,
110-
format(JSON.stringify(packageJSON), { filepath: pkgJSONPath })
106+
format(JSON.stringify(packageJSON), {
107+
filepath: fileURLToPath(pkgJSONPath),
108+
})
111109
);
112110

113111
return packageJSON;
114112
}
115113

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

120118
const htmlEncodedTag =
121119
encodeURIComponent(pkgJSON.name) + "%40" + pkgJSON.version;

deploy/deployChangedPackages.mjs

+9-13
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,25 @@
66
// ones which have changed.
77

88
import * as fs from "fs";
9-
import { join, dirname, basename } from "path";
10-
import { fileURLToPath } from "url";
9+
import { basename } from "path";
1110
import { spawnSync, execSync } from "child_process";
1211
import { Octokit } from "@octokit/core";
1312
import printDiff from "print-diff";
1413
import { generateChangelogFrom } from "../lib/changelog.js";
1514
import { packages } from "./createTypesPackages.mjs";
16-
17-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
18-
// @ts-ignore
19-
const __filename = fileURLToPath(import.meta.url);
20-
const __dirname = dirname(__filename);
15+
import { fileURLToPath } from "node:url";
2116

2217
verify();
2318

2419
const uploaded = [];
2520

2621
// Loop through generated packages, deploying versions for anything which has different
2722
// .d.ts files from the version available on npm.
28-
const generatedDir = join(__dirname, "generated");
23+
const generatedDir = new URL("generated/", import.meta.url);
2924
for (const dirName of fs.readdirSync(generatedDir)) {
3025
console.log(`Looking at ${dirName}`);
31-
const localPackageJSONPath = join(generatedDir, dirName, "package.json");
26+
const packageDir = new URL(`${dirName}/`, generatedDir);
27+
const localPackageJSONPath = new URL("package.json", packageDir);
3228
const newTSConfig = fs.readFileSync(localPackageJSONPath, "utf-8");
3329
const pkgJSON = JSON.parse(newTSConfig);
3430

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

3935
const dtsFiles = fs
40-
.readdirSync(join(generatedDir, dirName))
36+
.readdirSync(packageDir)
4137
.filter((f) => f.endsWith(".d.ts"));
4238

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

54-
const generatedDTSPath = join(generatedDir, dirName, file);
50+
const generatedDTSPath = new URL(file, packageDir);
5551
const generatedDTSContent = fs.readFileSync(generatedDTSPath, "utf8");
5652

5753
// This assumes we'll only _ever_ ship patches, which may change in the
@@ -86,7 +82,7 @@ Assuming that this means we need to upload this package.`);
8682
if (upload) {
8783
if (process.env.NODE_AUTH_TOKEN) {
8884
const publish = spawnSync("npm", ["publish", "--access", "public"], {
89-
cwd: join(generatedDir, dirName),
85+
cwd: fileURLToPath(packageDir),
9086
stdio: "inherit",
9187
});
9288

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

0 commit comments

Comments
 (0)