-
Notifications
You must be signed in to change notification settings - Fork 519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(esbuild): Script to update esbuild to the latest available version #2492
Merged
mattem
merged 15 commits into
bazel-contrib:stable
from
JesseTatasciore:feat/esbuild-version-updater
Apr 7, 2021
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
585c540
Add a script that will update esbuild versions in packages/esbuild/es…
JesseTatasciore 8d6649b
Build up lines of the file in an array and only call console.log once
JesseTatasciore f922bb0
Refactor function to have it return json instead of a string as it is…
JesseTatasciore 47257e1
Ensure that the windows rules exports an exe at the right path
JesseTatasciore 33a5886
Replace version and sha256 in the readme and in the example workspace…
JesseTatasciore 9f15cd7
Merge branch 'stable' into feat/esbuild-version-updater
JesseTatasciore 1ffad68
Update bundle golden file
JesseTatasciore 1337f2f
Add back accidentally removed comment
JesseTatasciore 28ccdc7
Update to latest esbuild version
JesseTatasciore fb61461
Remove unwanted changes from merge
JesseTatasciore 4cc3d1c
Switch error-limit flag to log-limit as specified in the esbuild rele…
JesseTatasciore ec925e6
Do not update esbuild to a version that has breaking changes
JesseTatasciore 40a8e0a
Allow user to specify a specific version
JesseTatasciore 9b6e267
Merge branch 'stable' into feat/esbuild-version-updater
JesseTatasciore 7a14e51
Fix sha256 from merge
JesseTatasciore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this always update to latest, or is there a way to force it to a version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to allow for a version to be specified when running the script. Can be run as:
node ./scripts/update-esbuild-versions.js 0.8.57