Skip to content

Commit 82f1a37

Browse files
committed
Feedback and a good README
1 parent 5128444 commit 82f1a37

File tree

6 files changed

+92
-31
lines changed

6 files changed

+92
-31
lines changed

.github/workflows/deploy.yml

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ jobs:
3030
run: node deploy/deployChangedPackages.mjs
3131
env:
3232
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3334

deploy/createTypesPackages.mjs

+23-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const packages = [
77
{
88
name: "@types/web",
99
description: "Types for the DOM, and other web technologies in browsers",
10+
readme: "./readmes/web.md",
1011
files: [
1112
{ from: "../generated/dom.generated.d.ts", to: "index.d.ts" }
1213
],
@@ -20,6 +21,7 @@ const packages = [
2021
import { join, dirname } from "path";
2122
import fs from "fs";
2223
import { fileURLToPath } from "url";
24+
import semver from "semver";
2325
import pkg from "prettier";
2426
const { format } = pkg;
2527
import { execSync } from "child_process";
@@ -58,9 +60,11 @@ const go = async () => {
5860
);
5961
});
6062

61-
// Setup the files
62-
await updatePackageJSON(packagePath, pkg, gitSha);
63+
// Setup the files in the repo
64+
const newPkgJSON = await updatePackageJSON(packagePath, pkg, gitSha);
65+
copyREADME(pkg, newPkgJSON, join(packagePath, "README.md"));
6366

67+
// Done
6468
console.log("Built:", pkg.name);
6569
}
6670
};
@@ -89,7 +93,7 @@ async function updatePackageJSON(packagePath, pkg, gitSha) {
8993
Number(semverMarkers[2]) + 1
9094
}`;
9195

92-
if (isBumpedVersionHigher(version, bumpedVersion)) {
96+
if (semver.gt(version, bumpedVersion)) {
9397
version = bumpedVersion;
9498
}
9599
} catch (error) {
@@ -103,20 +107,23 @@ async function updatePackageJSON(packagePath, pkg, gitSha) {
103107
pkgJSONPath,
104108
format(JSON.stringify(packageJSON), { filepath: pkgJSONPath })
105109
);
110+
111+
return packageJSON;
106112
}
107113

108-
/**
109-
* @param packageJSONVersion {string}
110-
* @param bumpedVersion {string}
111-
*/
112-
function isBumpedVersionHigher(packageJSONVersion, bumpedVersion) {
113-
const semverMarkersPackageJSON = packageJSONVersion.split(".");
114-
const semverMarkersBumped = bumpedVersion.split(".");
115-
for (let i = 0; i < 3; i++) {
116-
if (Number(semverMarkersPackageJSON[i]) > Number(semverMarkersBumped[i])) {
117-
return false;
118-
}
119-
}
114+
// Copies the README and adds some rudimentary templating to the file.
115+
function copyREADME(pkg, pkgJSON, writePath) {
116+
let readme = fs.readFileSync(join(__filename, "..", pkg.readme), "utf-8");
117+
118+
const htmlEncodedTag =
119+
encodeURIComponent(pkgJSON.name) + "%40" + pkgJSON.version;
120+
121+
readme = readme
122+
.replace("{{version}}", pkgJSON.version)
123+
.replace(
124+
"{{release_href}}",
125+
`https://github.com/microsoft/TypeScript-DOM-lib-generator/releases/tag/${htmlEncodedTag}`
126+
);
120127

121-
return true;
128+
fs.writeFileSync(writePath, readme);
122129
}

deploy/deployChangedPackages.mjs

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22

33
// node deploy/deployChangedPackages.mjs
4+
45
// Builds on the results of createTypesPackages.mjs and deploys the
56
// ones which have changed.
67

@@ -9,13 +10,24 @@ import { join, dirname } from "path";
910
import { fileURLToPath } from "url";
1011
import fetch from "node-fetch";
1112
import { spawnSync } from "child_process";
13+
import { Octokit } from "@octokit/core";
1214

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

20+
const verify = () => {
21+
const authToken = process.env.GITHUB_TOKEN || process.env.GITHUB_API_TOKEN;
22+
if (!authToken)
23+
throw new Error(
24+
"There isn't an ENV var set up for creating a GitHub release, expected GITHUB_TOKEN."
25+
);
26+
};
27+
1828
const go = async () => {
29+
verify();
30+
1931
const uploaded = [];
2032

2133
// Loop through generated packages, deploying versions for anything which has different
@@ -36,14 +48,18 @@ const go = async () => {
3648
for (const file of dtsFiles) {
3749
const generatedDTSPath = join(generatedDir, dirName, file);
3850
const generatedDTSContent = fs.readFileSync(generatedDTSPath, "utf8");
51+
const unpkgURL = `https://unpkg.com/${pkgJSON.name}/${file}`;
3952
try {
40-
const unpkgURL = `https://unpkg.com/${pkgJSON.name}/${file}`;
4153
const npmDTSReq = await fetch(unpkgURL);
4254
const npmDTSText = await npmDTSReq.text();
4355
upload = upload || npmDTSText !== generatedDTSContent;
4456
} catch (error) {
45-
// Not here, definitely needs to be uploaded
46-
upload = true;
57+
// Could not find a previous build
58+
console.log(
59+
`Could not get the file ${file} inside the npm package ${pkgJSON.name} from unpkg at ${unpkgURL}`
60+
);
61+
process.exitCode = 1;
62+
upload = false;
4763
}
4864
}
4965

@@ -60,6 +76,8 @@ const go = async () => {
6076
process.exit(publish.status);
6177
} else {
6278
console.log(publish.stdout?.toString());
79+
80+
await createRelease(`${pkgJSON.name}@${pkgJSON.version}`);
6381
}
6482
}
6583

@@ -81,4 +99,16 @@ const go = async () => {
8199
}
82100
};
83101

102+
async function createRelease(tag) {
103+
const authToken = process.env.GITHUB_TOKEN || process.env.GITHUB_API_TOKEN;
104+
const octokit = new Octokit({ auth: authToken });
105+
106+
await octokit.request("POST /repos/{owner}/{repo}/releases", {
107+
owner: "microsoft",
108+
repo: "TypeScript-DOM-lib-generator",
109+
tag_name: tag,
110+
target_commitish: process.env.GITHUB_SHA,
111+
});
112+
}
113+
84114
go();

deploy/readmes/web.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
### `@types/web` - Types for the DOM and most web-related APIs
2+
3+
This module contains the DOM types for the majority of the web APIs used in a web browser.
4+
5+
The APIs inside `@types/web` are generated from the specifications for CSS, HTML and JavaScript. Given the size and state of constant change in web browsers, `@types/web` only has APIs which have passed a certain level of standardization and are available in at least two different browser engines.
6+
7+
`@types/web` is also included inside TypeScript, available as `dom` in the [`lib`](https://www.typescriptlang.org/tsconfig#lib) section and included in projects by default. By using `@types/web` you can lock your the web APIs used in your projects, easing the process of updating TypeScript and offering more control in your environment.
8+
9+
## Installation
10+
11+
To use `@types/web` you need to do two things:
12+
13+
1. Install the dependency: `npm install @types/web --save-dev`, `yarn add @types/web --dev` or `pnpm add @types/web --dev`.
14+
15+
1. Update your [`tsconfig.json`](https://www.typescriptlang.org/tsconfig). There are two cases to consider depending on if you have `lib` defined in your `tsconfig.json` or not.
16+
17+
1. **Without "lib"** - You will need to add `"lib": []`. The value you want to add inside your lib should correlate to your [`"target"`](https://www.typescriptlang.org/tsconfig#target). For example if you had `"target": "es2017"`, then you would add `"lib": ["es2017"]`
18+
1. **With "lib"** - You should remove `"dom"`.
19+
20+
That's all.
21+
22+
## SemVer
23+
24+
This project does not respect semantic versioning as almost every change could potentially break a project, though we try to minimize removing types.
25+
`@types/web` follow the specifications, so when they mark a function/object/API/type as deprecated or removed - that is respected.
26+
27+
## TypeScript Version Support
28+
29+
Prior to `@types/web` the web APIs were deployed with a version of TypeScript, and backwards compatibility has not been a concern. Now the web APIs and TypeScript can be de-coupled, then we expect to eventually hit a point where we take backwards compatibility in mind. For now, `@types/web` officially supports TypeScript 4.4 and above. It very likely will work with TypeScript versions much earlier that that however.
30+
31+
## Deploy Metadata
32+
33+
You can read what changed in version {{version}} at {{release_href}}.

deploy/template/README.md

-12
This file was deleted.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"dependencies": {
1919
"@mdn/browser-compat-data": "2.0.7",
20+
"@octokit/core": "^3.5.1",
2021
"@types/jsdom": "^16.2.10",
2122
"@types/node": "^15.6.1",
2223
"@types/node-fetch": "^2.5.10",
@@ -33,6 +34,7 @@
3334
"parse-diff": "^0.8.1",
3435
"prettier": "^2.3.0",
3536
"print-diff": "^1.0.0",
37+
"semver": "^7.3.5",
3638
"styleless-innertext": "^1.1.3",
3739
"typescript": "^4.3.0-dev.20210327",
3840
"webidl2": "^24.1.1"

0 commit comments

Comments
 (0)