|
| 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 | + files: [ |
| 11 | + { from: "../generated/dom.generated.d.ts", to: "index.d.ts" } |
| 12 | + ], |
| 13 | + }, |
| 14 | + ]; |
| 15 | + |
| 16 | +// Note: You can add 'version: "1.0.0"' to a package above |
| 17 | +// to set the major or minor, otherwise it will always bump |
| 18 | +// the patch. |
| 19 | + |
| 20 | +import { join, dirname } from "path"; |
| 21 | +import fs from "fs"; |
| 22 | +import { fileURLToPath } from "url"; |
| 23 | +import pkg from "prettier"; |
| 24 | +const { format } = pkg; |
| 25 | +import { execSync } from "child_process"; |
| 26 | + |
| 27 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 28 | +// @ts-ignore |
| 29 | +const __filename = fileURLToPath(import.meta.url); |
| 30 | +const __dirname = dirname(__filename); |
| 31 | + |
| 32 | +const go = async () => { |
| 33 | + const gitSha = execSync("git rev-parse HEAD").toString().trim().slice(0, 7); |
| 34 | + |
| 35 | + const generatedDir = join(__dirname, "generated"); |
| 36 | + const templateDir = join(__dirname, "template"); |
| 37 | + |
| 38 | + for (const pkg of packages) { |
| 39 | + const folderName = pkg.name.replace("@", "").replace("/", "-"); |
| 40 | + const packagePath = join(generatedDir, folderName); |
| 41 | + |
| 42 | + if (fs.existsSync(packagePath)) fs.rmSync(packagePath, { recursive: true }); |
| 43 | + fs.mkdirSync(packagePath, { recursive: true }); |
| 44 | + |
| 45 | + // Migrate in the template files |
| 46 | + for (const templateFile of fs.readdirSync(templateDir)) { |
| 47 | + if (templateFile.startsWith(".")) continue; |
| 48 | + |
| 49 | + const templatedFile = join(templateDir, templateFile); |
| 50 | + fs.copyFileSync(templatedFile, join(packagePath, templateFile)); |
| 51 | + } |
| 52 | + |
| 53 | + // Add the reference files in the config above |
| 54 | + pkg.files.forEach((fileRef) => { |
| 55 | + fs.copyFileSync( |
| 56 | + join(__filename, "..", fileRef.from), |
| 57 | + join(packagePath, fileRef.to) |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + // Setup the files |
| 62 | + await updatePackageJSON(packagePath, pkg, gitSha); |
| 63 | + |
| 64 | + console.log("Built:", pkg.name); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +go(); |
| 69 | + |
| 70 | +async function updatePackageJSON(packagePath, pkg, gitSha) { |
| 71 | + const pkgJSONPath = join(packagePath, "package.json"); |
| 72 | + const packageText = fs.readFileSync(pkgJSONPath, "utf8"); |
| 73 | + const packageJSON = JSON.parse(packageText); |
| 74 | + packageJSON.name = pkg.name; |
| 75 | + packageJSON.description = pkg.description; |
| 76 | + |
| 77 | + // Bump the last version of the number from npm, |
| 78 | + // or use the _version in tsconfig if it's higher, |
| 79 | + // or default to 0.0.1 |
| 80 | + let version = pkg.version || "0.0.1"; |
| 81 | + try { |
| 82 | + const npmResponse = await fetch( |
| 83 | + `https://registry.npmjs.org/${packageJSON.name}` |
| 84 | + ); |
| 85 | + const npmPackage = await npmResponse.json(); |
| 86 | + |
| 87 | + const semverMarkers = npmPackage["dist-tags"].latest.split("."); |
| 88 | + const bumpedVersion = `${semverMarkers[0]}.${semverMarkers[1]}.${ |
| 89 | + Number(semverMarkers[2]) + 1 |
| 90 | + }`; |
| 91 | + |
| 92 | + if (isBumpedVersionHigher(version, bumpedVersion)) { |
| 93 | + version = bumpedVersion; |
| 94 | + } |
| 95 | + } catch (error) { |
| 96 | + // NOOP, this is for the first deploy, which will set it to 0.0.1 |
| 97 | + } |
| 98 | + |
| 99 | + packageJSON.version = version; |
| 100 | + packageJSON.domLibGeneratorSha = gitSha; |
| 101 | + |
| 102 | + fs.writeFileSync( |
| 103 | + pkgJSONPath, |
| 104 | + format(JSON.stringify(packageJSON), { filepath: pkgJSONPath }) |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 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 | + } |
| 120 | + |
| 121 | + return true; |
| 122 | +} |
0 commit comments