Skip to content

Commit

Permalink
feat(esbuild): Script to update esbuild to the latest available versi…
Browse files Browse the repository at this point in the history
…on (#2492)
  • Loading branch information
JesseTatasciore authored Apr 7, 2021
1 parent e9e8fe7 commit 472ed62
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 4 additions & 6 deletions packages/esbuild/esbuild_repo.bzl
Original file line number Diff line number Diff line change
@@ -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"""
Expand All @@ -22,7 +22,6 @@ def esbuild_dependencies():
build_file_content = """exports_files(["bin/esbuild"])""",
sha256 = "98436890727bdb0d4beddd9c9e07d0aeff0e8dfe0169f85e568eca0dd43f665e",
)

http_archive(
name = "esbuild_windows",
urls = [
Expand All @@ -32,7 +31,6 @@ def esbuild_dependencies():
build_file_content = """exports_files(["esbuild.exe"])""",
sha256 = "589c8ff97210bd41de106e6317ce88f9e88d2cacfd8178ae1217f2b857ff6c3a",
)

http_archive(
name = "esbuild_linux",
urls = [
Expand Down
117 changes: 117 additions & 0 deletions scripts/update-esbuild-versions.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 472ed62

Please sign in to comment.