Skip to content

Commit 8abf34e

Browse files
committed
strict
1 parent 5e74d70 commit 8abf34e

File tree

5 files changed

+51
-31
lines changed

5 files changed

+51
-31
lines changed

deploy/createTypesPackages.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
// node deploy/createTypesPackages.js
44

5+
/**
6+
* @template T
7+
* @typedef {T extends (infer U)[] ? U : T} ArrayInner
8+
*/
9+
/**
10+
* @typedef {ArrayInner<typeof packages>} Package
11+
*/
512
// prettier-ignore
613
export const packages = [
714
{
@@ -52,11 +59,8 @@ import { fileURLToPath } from "url";
5259
import semver from "semver";
5360
import pkg from "prettier";
5461
const { format } = pkg;
55-
import { execSync } from "child_process";
5662

5763
const go = async () => {
58-
const gitSha = execSync("git rev-parse HEAD").toString().trim().slice(0, 7);
59-
6064
const generatedDir = new URL("generated/", import.meta.url);
6165
const templateDir = new URL("template/", import.meta.url);
6266

@@ -88,25 +92,30 @@ const go = async () => {
8892
prependAutoImports(pkg, packagePath);
8993

9094
// Setup the files in the repo
91-
const newPkgJSON = await updatePackageJSON(pkg, packagePath, gitSha);
95+
const newPkgJSON = await updatePackageJSON(pkg, packagePath);
9296
copyREADME(pkg, newPkgJSON, new URL("README.md", packagePath));
9397

9498
// Done
9599
console.log("Built:", pkg.name);
96100
}
97101
};
98102

99-
async function updatePackageJSON(pkg, packagePath, gitSha) {
103+
/**
104+
* @param {Package} pkg
105+
* @param {URL} packagePath
106+
*/
107+
async function updatePackageJSON(pkg, packagePath) {
100108
const pkgJSONPath = new URL("package.json", packagePath);
101109
const packageText = fs.readFileSync(pkgJSONPath, "utf8");
110+
/** @type {import("./template/package.json")} */
102111
const packageJSON = JSON.parse(packageText);
103112
packageJSON.name = pkg.name;
104113
packageJSON.description = pkg.description;
105114

106115
// Bump the last version of the number from npm,
107116
// or use the _version in tsconfig if it's higher,
108117
// or default to 0.0.1
109-
let version = pkg.version || "0.0.1";
118+
let version = "0.0.1";
110119
try {
111120
const npmResponse = await fetch(
112121
`https://registry.npmjs.org/${packageJSON.name}`
@@ -128,7 +137,6 @@ async function updatePackageJSON(pkg, packagePath, gitSha) {
128137
}
129138

130139
packageJSON.version = version;
131-
packageJSON.domLibGeneratorSha = gitSha;
132140

133141
fs.writeFileSync(
134142
pkgJSONPath,
@@ -140,7 +148,12 @@ async function updatePackageJSON(pkg, packagePath, gitSha) {
140148
return packageJSON;
141149
}
142150

143-
// Copies the README and adds some rudimentary templating to the file.
151+
/**
152+
* Copies the README and adds some rudimentary templating to the file.
153+
* @param {Package} pkg
154+
* @param {import("./template/package.json")} pkgJSON
155+
* @param {URL} writePath
156+
*/
144157
function copyREADME(pkg, pkgJSON, writePath) {
145158
let readme = fs.readFileSync(new URL(pkg.readme, import.meta.url), "utf-8");
146159

@@ -157,7 +170,11 @@ function copyREADME(pkg, pkgJSON, writePath) {
157170
fs.writeFileSync(writePath, readme);
158171
}
159172

160-
// Appends any files marked as autoImport in the metadata.
173+
/**
174+
* Appends any files marked as autoImport in the metadata.
175+
* @param {Package} pkg
176+
* @param {URL} packagePath
177+
*/
161178
function prependAutoImports(pkg, packagePath) {
162179
const index = new URL("index.d.ts", packagePath);
163180
if (!fs.existsSync(index)) return;

deploy/deployChangedPackages.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
import * as fs from "fs";
99
import { basename } from "path";
10-
import { spawnSync, execSync } from "child_process";
10+
import { spawnSync } from "child_process";
1111
import { Octokit } from "@octokit/core";
1212
import printDiff from "print-diff";
13-
import { generateChangelogFrom } from "../lib/changelog.js";
13+
import { gitShowFile, generateChangelogFrom } from "../lib/changelog.js";
1414
import { packages } from "./createTypesPackages.js";
1515
import { fileURLToPath } from "node:url";
1616

@@ -31,21 +31,26 @@ for (const dirName of fs.readdirSync(generatedDir)) {
3131
// We'll need to map back from the filename in the npm package to the
3232
// generated file in baselines inside the git tag
3333
const thisPackageMeta = packages.find((p) => p.name === pkgJSON.name);
34+
if (!thisPackageMeta) {
35+
throw new Error(`Couldn't find ${pkgJSON.name}`);
36+
}
3437

3538
const dtsFiles = fs
3639
.readdirSync(packageDir)
3740
.filter((f) => f.endsWith(".d.ts"));
3841

39-
/** @type {string[]} */
40-
let releaseNotes = [];
42+
/** @type {string} */
43+
let releaseNotes = "";
4144

4245
// Look through each .d.ts file included in a package to
4346
// determine if anything has changed
4447
let upload = false;
4548
for (const file of dtsFiles) {
46-
const originalFilename = basename(
47-
thisPackageMeta.files.find((f) => f.to === file).from
48-
);
49+
const filemap = thisPackageMeta.files.find((f) => f.to === file);
50+
if (!filemap) {
51+
throw new Error(`Couldn't find ${file} from ${pkgJSON.name}`);
52+
}
53+
const originalFilename = basename(filemap.from);
4954

5055
const generatedDTSPath = new URL(file, packageDir);
5156
const generatedDTSContent = fs.readFileSync(generatedDTSPath, "utf8");
@@ -63,10 +68,10 @@ for (const dirName of fs.readdirSync(generatedDir)) {
6368
console.log(`Comparing ${file} from ${olderVersion}, to now:`);
6469
printDiff(oldFile, generatedDTSContent);
6570

66-
const title = `\n## \`${file}\`\n`;
71+
const title = `\n## \`${file}\`\n\n`;
6772
const notes = generateChangelogFrom(oldFile, generatedDTSContent);
68-
releaseNotes.push(title);
69-
releaseNotes.push(notes.trim() === "" ? "No changes" : notes);
73+
releaseNotes = title;
74+
releaseNotes += notes.trim() === "" ? "No changes" : notes;
7075

7176
upload = upload || oldFile !== generatedDTSContent;
7277
} catch (error) {
@@ -106,7 +111,7 @@ Assuming that this means we need to upload this package.`);
106111
}
107112

108113
console.log("\n# Release notes:");
109-
console.log(releaseNotes.join("\n"), "\n\n");
114+
console.log(releaseNotes, "\n\n");
110115
}
111116
// Warn if we did a dry run.
112117
if (!process.env.NODE_AUTH_TOKEN) {
@@ -119,6 +124,10 @@ if (uploaded.length) {
119124
console.log("No uploads");
120125
}
121126

127+
/**
128+
* @param {string} tag
129+
* @param {string} body
130+
*/
122131
async function createRelease(tag, body) {
123132
const authToken = process.env.GITHUB_TOKEN || process.env.GITHUB_API_TOKEN;
124133
const octokit = new Octokit({ auth: authToken });
@@ -146,7 +155,3 @@ function verify() {
146155
"There isn't an ENV var set up for creating a GitHub release, expected GITHUB_TOKEN."
147156
);
148157
}
149-
150-
function gitShowFile(commitish, path) {
151-
return execSync(`git show "${commitish}":${path}`, { encoding: "utf-8" });
152-
}

deploy/jsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"compilerOptions": {
33
"target": "es2020",
44
"module": "esnext", // es2022
5-
"moduleResolution": "node"
5+
"moduleResolution": "node",
6+
"strict": true,
7+
"resolveJsonModule": true
68
}
79
}

deploy/versionChangelog.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
// npm run ts-changelog @types/web 0.0.1 0.0.3
44

5-
import { generateChangelogFrom } from "../lib/changelog.js";
5+
import { gitShowFile, generateChangelogFrom } from "../lib/changelog.js";
66
import { packages } from "./createTypesPackages.js";
7-
import { execSync } from "child_process";
87
import { basename } from "path";
98

109
const [name, before, to] = process.argv.slice(2);
@@ -33,7 +32,4 @@ const go = () => {
3332
}
3433
};
3534

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

src/changelog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { execSync } from "child_process";
22
import ts from "typescript";
33
import { fileURLToPath } from "url";
44

5-
function gitShowFile(commit: string, path: string) {
5+
export function gitShowFile(commit: string, path: string): string {
66
return execSync(`git show ${commit}:${path}`, { encoding: "utf-8" });
77
}
88

0 commit comments

Comments
 (0)