|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +// node deploy/createTypesPackages.mjs |
| 4 | + |
| 5 | +// prettier-ignore |
| 6 | +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 | + ]; |
| 17 | + |
| 18 | +// Note: You can add 'version: "1.0.0"' to a package above |
| 19 | +// to set the major or minor, otherwise it will always bump |
| 20 | +// the patch. |
| 21 | + |
| 22 | +import { join, dirname } from "path"; |
| 23 | +import fs from "fs"; |
| 24 | +import { fileURLToPath } from "url"; |
| 25 | +import semver from "semver"; |
| 26 | +import pkg from "prettier"; |
| 27 | +const { format } = pkg; |
| 28 | +import { execSync } from "child_process"; |
| 29 | + |
| 30 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 31 | +// @ts-ignore |
| 32 | +const __filename = fileURLToPath(import.meta.url); |
| 33 | +const __dirname = dirname(__filename); |
| 34 | + |
| 35 | +const go = async () => { |
| 36 | + const gitSha = execSync("git rev-parse HEAD").toString().trim().slice(0, 7); |
| 37 | + |
| 38 | + const generatedDir = join(__dirname, "generated"); |
| 39 | + const templateDir = join(__dirname, "template"); |
| 40 | + |
| 41 | + for (const pkg of packages) { |
| 42 | + const folderName = pkg.name.replace("@", "").replace("/", "-"); |
| 43 | + const packagePath = join(generatedDir, folderName); |
| 44 | + |
| 45 | + if (fs.existsSync(packagePath)) fs.rmSync(packagePath, { recursive: true }); |
| 46 | + fs.mkdirSync(packagePath, { recursive: true }); |
| 47 | + |
| 48 | + // Migrate in the template files |
| 49 | + for (const templateFile of fs.readdirSync(templateDir)) { |
| 50 | + if (templateFile.startsWith(".")) continue; |
| 51 | + |
| 52 | + const templatedFile = join(templateDir, templateFile); |
| 53 | + fs.copyFileSync(templatedFile, join(packagePath, templateFile)); |
| 54 | + } |
| 55 | + |
| 56 | + // Add the reference files in the config above |
| 57 | + pkg.files.forEach((fileRef) => { |
| 58 | + fs.copyFileSync( |
| 59 | + join(__filename, "..", fileRef.from), |
| 60 | + join(packagePath, fileRef.to) |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + // Setup the files in the repo |
| 65 | + const newPkgJSON = await updatePackageJSON(packagePath, pkg, gitSha); |
| 66 | + copyREADME(pkg, newPkgJSON, join(packagePath, "README.md")); |
| 67 | + |
| 68 | + // Done |
| 69 | + console.log("Built:", pkg.name); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +go(); |
| 74 | + |
| 75 | +async function updatePackageJSON(packagePath, pkg, gitSha) { |
| 76 | + const pkgJSONPath = join(packagePath, "package.json"); |
| 77 | + const packageText = fs.readFileSync(pkgJSONPath, "utf8"); |
| 78 | + const packageJSON = JSON.parse(packageText); |
| 79 | + packageJSON.name = pkg.name; |
| 80 | + packageJSON.description = pkg.description; |
| 81 | + |
| 82 | + // Bump the last version of the number from npm, |
| 83 | + // or use the _version in tsconfig if it's higher, |
| 84 | + // or default to 0.0.1 |
| 85 | + let version = pkg.version || "0.0.1"; |
| 86 | + try { |
| 87 | + const npmResponse = await fetch( |
| 88 | + `https://registry.npmjs.org/${packageJSON.name}` |
| 89 | + ); |
| 90 | + const npmPackage = await npmResponse.json(); |
| 91 | + |
| 92 | + const semverMarkers = npmPackage["dist-tags"].latest.split("."); |
| 93 | + const bumpedVersion = `${semverMarkers[0]}.${semverMarkers[1]}.${ |
| 94 | + Number(semverMarkers[2]) + 1 |
| 95 | + }`; |
| 96 | + |
| 97 | + if (semver.gt(version, bumpedVersion)) { |
| 98 | + version = bumpedVersion; |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + // NOOP, this is for the first deploy, which will set it to 0.0.1 |
| 102 | + } |
| 103 | + |
| 104 | + packageJSON.version = version; |
| 105 | + packageJSON.domLibGeneratorSha = gitSha; |
| 106 | + |
| 107 | + fs.writeFileSync( |
| 108 | + pkgJSONPath, |
| 109 | + format(JSON.stringify(packageJSON), { filepath: pkgJSONPath }) |
| 110 | + ); |
| 111 | + |
| 112 | + return packageJSON; |
| 113 | +} |
| 114 | + |
| 115 | +// Copies the README and adds some rudimentary templating to the file. |
| 116 | +function copyREADME(pkg, pkgJSON, writePath) { |
| 117 | + let readme = fs.readFileSync(join(__filename, "..", pkg.readme), "utf-8"); |
| 118 | + |
| 119 | + const htmlEncodedTag = |
| 120 | + encodeURIComponent(pkgJSON.name) + "%40" + pkgJSON.version; |
| 121 | + |
| 122 | + readme = readme |
| 123 | + .replace("{{version}}", pkgJSON.version) |
| 124 | + .replace( |
| 125 | + "{{release_href}}", |
| 126 | + `https://github.com/microsoft/TypeScript-DOM-lib-generator/releases/tag/${htmlEncodedTag}` |
| 127 | + ); |
| 128 | + |
| 129 | + fs.writeFileSync(writePath, readme); |
| 130 | +} |
0 commit comments