-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathcreateTypesPackages.mjs
130 lines (104 loc) · 3.84 KB
/
createTypesPackages.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// @ts-check
// node deploy/createTypesPackages.mjs
// prettier-ignore
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" }
],
},
];
// 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 { fileURLToPath } from "url";
import semver from "semver";
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");
for (const pkg of packages) {
const folderName = pkg.name.replace("@", "").replace("/", "-");
const packagePath = join(generatedDir, folderName);
if (fs.existsSync(packagePath)) fs.rmSync(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));
}
// Add the reference files in the config above
pkg.files.forEach((fileRef) => {
fs.copyFileSync(
join(__filename, "..", fileRef.from),
join(packagePath, fileRef.to)
);
});
// Setup the files in the repo
const newPkgJSON = await updatePackageJSON(packagePath, pkg, gitSha);
copyREADME(pkg, newPkgJSON, join(packagePath, "README.md"));
// Done
console.log("Built:", pkg.name);
}
};
go();
async function updatePackageJSON(packagePath, pkg, gitSha) {
const pkgJSONPath = join(packagePath, "package.json");
const packageText = fs.readFileSync(pkgJSONPath, "utf8");
const packageJSON = JSON.parse(packageText);
packageJSON.name = pkg.name;
packageJSON.description = pkg.description;
// Bump the last version of the number from npm,
// or use the _version in tsconfig if it's higher,
// or default to 0.0.1
let version = pkg.version || "0.0.1";
try {
const npmResponse = await fetch(
`https://registry.npmjs.org/${packageJSON.name}`
);
const npmPackage = await npmResponse.json();
const semverMarkers = npmPackage["dist-tags"].latest.split(".");
const bumpedVersion = `${semverMarkers[0]}.${semverMarkers[1]}.${
Number(semverMarkers[2]) + 1
}`;
if (semver.gt(version, bumpedVersion)) {
version = bumpedVersion;
}
} catch (error) {
// NOOP, this is for the first deploy, which will set it to 0.0.1
}
packageJSON.version = version;
packageJSON.domLibGeneratorSha = gitSha;
fs.writeFileSync(
pkgJSONPath,
format(JSON.stringify(packageJSON), { filepath: 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");
const htmlEncodedTag =
encodeURIComponent(pkgJSON.name) + "%40" + pkgJSON.version;
readme = readme
.replace("{{version}}", pkgJSON.version)
.replace(
"{{release_href}}",
`https://github.com/microsoft/TypeScript-DOM-lib-generator/releases/tag/${htmlEncodedTag}`
);
fs.writeFileSync(writePath, readme);
}