Download a GitHub Repository using Node.JS
Published: |
Updated:
| by Julian Knight Reading time ~2 min.
📖 Kb
| 📎
Development
| 🔖
node.js, nodejs, JavaScript, ECMAscript
Downloading a repository from GitHub using Node.JS should be really easy. But it isn’t! This KB post reminds us of the tricks required.
We have to be careful with the source URL names when working with GitHub as the URL’s generally exposed on the web interface are not the actual URL’s. The Node.JS http/https get() functions don’t handle redirections well.
#!/usr/bin/env node
'use strict'
/**
* Install the Node-RED alternate installation template
*
* Author: Julian Knight (Totally Information), Jan. 2019
* License: MIT
*/
const fs = require('fs')
const https = require('https')
function download(url, dest, cb) {
const file = fs.createWriteStream(dest);
const request = https.get(url, function (response) {
response.pipe(file);
file.on('finish', function () {
file.close(cb); // close() is async, call cb after close completes.
});
}).on('error', function (err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message);
});
};
// Download latest archive from GitHub to temp folder
const dest = './alternate-node-red-installer.zip'
const url = 'https://codeload.github.com/TotallyInformation/alternate-node-red-installer/zip/master'
download(url, dest, function(){
console.log('Done')
})
Obviously, it would be better to add proper error traps and messaging.
GitHub URL’s 🔗︎
As mentioned, most of the exposed GitHub URL’s actually redirect which upsets Node’s built-in get functions. The downside of using the direct URL’s is that they may change format from time-to-time. If you can, use a 3rd-party library such as request which handles http vs https and redirects without additional programming.
The format for the actual repository archives (in zipped form) is:
https://codeload.github.com/<userOrOrgName>/<repoName>/zip/<branch>
Where: <branch>
is either ‘master’ or a git tag ID (e.g. ‘v1.2.3’) or other git branch ID.
For an individual file from the repository, use:
raw.githubusercontent.com/<userOrOrgName>/<repository>/<branch>/<filename>
https://raw.githubusercontent.com/TotallyInformation/alternate-node-red-installer/