Skip to content

Commit

Permalink
Fix npm download to use HTML page instead of API
Browse files Browse the repository at this point in the history
Fixes: #740

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Dec 19, 2019
1 parent 73004c2 commit e5f498a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
3 changes: 3 additions & 0 deletions npm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.bin
/node_modules/**
faas-cli
10 changes: 6 additions & 4 deletions npm/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ module.exports.install = async () => {
del.sync(dest, { force: true });

try {
let releases = await lib.getReleases();
let release = releases.find(release => release.name === name);
let url = release && release.browser_download_url;

let releaseURL = await lib.getRelease()
let downloadURL = releaseURL.replace("tag", "download") +"/"+ binaryName;

let url = downloadURL;

console.log(`Downloading package ${url} to ${dest}`);
await lib.download(url, dest);
Expand All @@ -41,4 +43,4 @@ module.exports.init = () => {
console.error(err.message);
process.exit(1);
});
}
}
17 changes: 13 additions & 4 deletions npm/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ module.exports = {
let extname = suffix === '.exe' ? '.exe' : '';
return `faas-cli${extname}`;
},
getRelease() {
return new Promise(async (resolve, reject)=> {
let latestURL = "https://github.com/openfaas/faas-cli/releases/latest"
let location = ""
try {
await axios({url:latestURL, maxRedirects:0});
}catch (e){
location = e.response.headers.location;
}

return resolve(location);
})
},
download(url, dest) {
return new Promise(async (resolve, reject) => {
let ws = fs.createWriteStream(dest);
Expand All @@ -47,10 +60,6 @@ module.exports = {
});
});
},
getReleases() {
return axios.get(`https://api.github.com/repos/openfaas/faas-cli/releases/tags/${pkg.version}`)
.then(res => res.data.assets);
},
async cmd(...args) {
let { stdout, stderr } = await exec(...args);
if (stdout) console.log(stdout);
Expand Down
2 changes: 1 addition & 1 deletion npm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfaas/faas-cli",
"version": "0.9.3",
"version": "0.11.3-2",
"description": "OpenFaaS CLI",
"main": "index.js",
"bin": {
Expand Down

0 comments on commit e5f498a

Please sign in to comment.