From 472ed62d255e6a1c0123cb9e165f01d9757c4582 Mon Sep 17 00:00:00 2001 From: Jesse Tatasciore Date: Tue, 6 Apr 2021 20:31:33 -0400 Subject: [PATCH] feat(esbuild): Script to update esbuild to the latest available version (#2492) --- package.json | 1 + packages/esbuild/esbuild_repo.bzl | 10 +-- scripts/update-esbuild-versions.js | 117 +++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 scripts/update-esbuild-versions.js diff --git a/package.json b/package.json index 320ba25a0a..88ed37c8ec 100644 --- a/package.json +++ b/package.json @@ -128,6 +128,7 @@ "bazel:update-deleted-packages": "sed -i.bak \"/--deleted_packages/s#=.*#=$(find {examples,e2e}/*/* \\( -name BUILD -or -name BUILD.bazel \\) | xargs -n 1 dirname | paste -sd, -)#\" .bazelrc && rm .bazelrc.bak", "update-codeowners": "./scripts/update_codeowners.sh", "update-nodejs-versions": "node ./scripts/update-nodejs-versions.js > internal/node/node_versions.bzl", + "update-esbuild-versions": "node ./scripts/update-esbuild-versions.js > packages/esbuild/esbuild_repo.bzl", "format": "git-clang-format", "format-all": "clang-format --glob='{internal/**/,examples/**/}*.{js,ts}' -i", "stardoc": "bazel build --noincompatible_strict_action_env //docs && cp -f dist/bin/docs/*.md ./docs && cp -f dist/bin/docs/docs-out/*.html ./docs && cp -f dist/bin/docs/docs-out/css/*.css ./docs/css", diff --git a/packages/esbuild/esbuild_repo.bzl b/packages/esbuild/esbuild_repo.bzl index c89f6bf001..1bad45a7dc 100644 --- a/packages/esbuild/esbuild_repo.bzl +++ b/packages/esbuild/esbuild_repo.bzl @@ -1,12 +1,12 @@ -""" +""" Generated code; do not edit +Update by running yarn update-esbuild-versions + Helper macro for fetching esbuild versions for internal tests and examples in rules_nodejs """ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -# IMPORTANT: Keep this file in sync with the documentation in _README.md - -_VERSION = "0.11.5" # reminder: update SHAs below when changing this version +_VERSION = "0.11.5" def esbuild_dependencies(): """Helper to install required dependencies for the esbuild rules""" @@ -22,7 +22,6 @@ def esbuild_dependencies(): build_file_content = """exports_files(["bin/esbuild"])""", sha256 = "98436890727bdb0d4beddd9c9e07d0aeff0e8dfe0169f85e568eca0dd43f665e", ) - http_archive( name = "esbuild_windows", urls = [ @@ -32,7 +31,6 @@ def esbuild_dependencies(): build_file_content = """exports_files(["esbuild.exe"])""", sha256 = "589c8ff97210bd41de106e6317ce88f9e88d2cacfd8178ae1217f2b857ff6c3a", ) - http_archive( name = "esbuild_linux", urls = [ diff --git a/scripts/update-esbuild-versions.js b/scripts/update-esbuild-versions.js new file mode 100644 index 0000000000..2652fe4cec --- /dev/null +++ b/scripts/update-esbuild-versions.js @@ -0,0 +1,117 @@ +const https = require("https"); +const { exec } = require('shelljs'); +const { mkdirSync, rmdirSync, createWriteStream, readFileSync, writeFileSync } = require('fs'); +const { join } = require('path'); +const { tmpdir } = require('os'); + +const PLATFORMS = { + "esbuild_darwin": "esbuild-darwin-64", + "esbuild_windows": "esbuild-windows-64", + "esbuild_linux": "esbuild-linux-64" +} + +function replaceFileContent(filepath, replacements) { + let fileContent = readFileSync(filepath, 'utf8'); + + replacements.forEach(replacement => { + const match = replacement[0].exec(fileContent); + + if(match.length > 1) { + fileContent = fileContent.replace(match[1], replacement[1]); + } + }); + + writeFileSync(filepath, fileContent); +} + +function fetch(url) { + return new Promise((resolve, reject) => { + https.get(url, (res) => { + if (res.statusCode !== 200) { + console.error(res); + return reject(); + } + + let body = ''; + res.on("data", (chunk) => body += chunk); + res.on("end", () => resolve(JSON.parse(String(body)))); + }); + }); +} + +function downloadFile(url, dest) { + return new Promise((resolve, reject) => { + const file = createWriteStream(dest); + + const request = https.get(url, (response) => { + response.pipe(file); + }); + + file.on('finish', () => { + file.end(); + resolve(); + }); + }); +}; + +async function main() { + const content = []; + const fileReplacements = []; + + content.push('""" Generated code; do not edit\nUpdate by running yarn update-esbuild-versions\n\nHelper macro for fetching esbuild versions for internal tests and examples in rules_nodejs\n"""\n'); + content.push('load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")\n'); + + if(process.argv.length !== 2 && process.argv.length !== 3) { + console.log("Expected number of arguments is 0 or 1"); + process.exit(1); + } + + let version; + if(process.argv.length === 3) { + version = process.argv[2]; + } else { + version = (await fetch('https://registry.npmjs.org/esbuild/latest')).version; + } + + content.push(`_VERSION = "${version}"\n`); + fileReplacements.push([/_ESBUILD_VERSION = "(.+?)"/, version]); + + content.push('def esbuild_dependencies():'); + content.push(' """Helper to install required dependencies for the esbuild rules"""\n'); + content.push(' version = _VERSION\n'); + + const tmpDir = tmpdir(); + mkdirSync(tmpDir, {recursive: true}); + + for(const platform of Object.keys(PLATFORMS)) { + const downloadUrl = `https://registry.npmjs.org/${PLATFORMS[platform]}/-/${PLATFORMS[platform]}-${version}.tgz`; + + const downloadPath = join(tmpDir, PLATFORMS[platform]); + await downloadFile(downloadUrl, downloadPath); + const shasumOutput = exec(`shasum -a 256 ${downloadPath}`, { silent: true }).stdout; + const shasum = shasumOutput.split(' ')[0]; + + fileReplacements.push([new RegExp(`"${platform}",.+?sha256 = "(.+?)"`, 's'), shasum]); + + content.push(' http_archive('); + content.push(` name = "${platform}",`); + content.push(' urls = ['); + content.push(` "https://registry.npmjs.org/${PLATFORMS[platform]}/-/${PLATFORMS[platform]}-%s.tgz" % version,`); + content.push(' ],'); + content.push(' strip_prefix = "package",'); + content.push(` build_file_content = """exports_files(["${platform === 'esbuild_windows' ? 'esbuild.exe' : 'bin/esbuild'}"])""",`); + content.push(` sha256 = "${shasum}",`); + content.push(' )'); + } + + + rmdirSync(tmpDir, {recursive: true}); + + console.log(content.join('\n')); + + // replace shasums in some manually edited files + replaceFileContent('examples/esbuild/WORKSPACE', fileReplacements); + replaceFileContent('packages/esbuild/_README.md', fileReplacements); +} + +main(); \ No newline at end of file