diff --git a/dist/npm/index.js b/dist/npm/index.js index b8bf4b0fca..960ce3f09d 100644 --- a/dist/npm/index.js +++ b/dist/npm/index.js @@ -1,172 +1,244 @@ #!/usr/bin/env node -const fs = require('fs'); -const path = require('path'); -const exec = require('child_process').exec; -const request = require('request'); -const Spinner = require('cli-spinner').Spinner; - -const downloadPathTemplate = "https://github.com/devspace-cloud/devspace/releases/download/v{{version}}/devspace-{{platform}}-{{arch}}"; +const fs = require("fs"); +const path = require("path"); +const exec = require("child_process").exec; +const request = require("request"); +const Spinner = require("cli-spinner").Spinner; + +const downloadPathTemplate = + "https://github.com/devspace-cloud/devspace/releases/download/v{{version}}/devspace-{{platform}}-{{arch}}"; const ARCH_MAPPING = { - "ia32": "386", - "x64": "amd64", - "arm": "arm" + ia32: "386", + x64: "amd64", + arm: "arm" }; const PLATFORM_MAPPING = { - "darwin": "darwin", - "linux": "linux", - "win32": "windows", - "freebsd": "freebsd" + darwin: "darwin", + linux: "linux", + win32: "windows", + freebsd: "freebsd" }; -if (!(process.platform in PLATFORM_MAPPING) || !(process.arch in ARCH_MAPPING)) { - console.error("Installation is not supported for this platform (" + process.platform + ") or architecture (" + process.arch + ")"); - return; +if ( + !(process.platform in PLATFORM_MAPPING) || + !(process.arch in ARCH_MAPPING) +) { + console.error( + "Installation is not supported for this platform (" + + process.platform + + ") or architecture (" + + process.arch + + ")" + ); + return; } let action; if (process.argv && process.argv.length > 2) { - action = process.argv[2]; + action = process.argv[2]; } else { - console.error("Please specify a version to publish!"); - return; + console.error("Please specify a version to publish!"); + return; } if (action == "noop") { - console.log("Successfully ran noop command"); - process.exit(0); + console.log("Successfully ran noop command"); + process.exit(0); } const packageJsonPath = path.join(__dirname, "package.json"); if (!fs.existsSync(packageJsonPath)) { - console.error("Unable to find package.json"); - return; + console.error("Unable to find package.json"); + return; } const requestHeaders = { - "User-Agent": "devspace-npm-script" + "User-Agent": "devspace-npm-script" }; let packageJson = JSON.parse(fs.readFileSync(packageJsonPath)); -if (action == "update-version") { - const releasesURL = "https://github.com/devspace-cloud/devspace/releases"; - - request({uri: releasesURL, headers: requestHeaders}, function(err, res, releasePage) { - if (res.statusCode !== 200) { - console.error("Error requesting URL " + releasesURL + " (Status Code: " + res.statusCode + ")"); - console.error(err); - process.exit(1); - } - const latestVersion = releasePage.replace(/^.*?\/devspace-cloud\/devspace\/releases\/download\/v([^\/]*)\/devspace-.*$/s, "$1"); - - if (releasePage != latestVersion && latestVersion) { - packageJson.version = latestVersion; +const getLatestVersion = function(callback) { + const releasesURL = "https://github.com/devspace-cloud/devspace/releases"; + + request({ uri: releasesURL, headers: requestHeaders }, function( + err, + res, + releasePage + ) { + if (res.statusCode != 200) { + console.error( + "Error requesting URL " + + releasesURL + + " (Status Code: " + + res.statusCode + + ")" + ); + console.error(err); + process.exit(1); + } + const latestVersion = releasePage.replace( + /^.*?\/devspace-cloud\/devspace\/releases\/download\/v([^\/]*)\/devspace-.*$/s, + "$1" + ); - fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 4)); + callback(latestVersion); + }); +}; - process.exit(0); - } else { - console.error("Unable to identify latest devspace version") - process.exit(1); - } - }); - return; +if (action == "update-version") { + getLatestVersion(function(latestVersion) { + if (releasePage != latestVersion && latestVersion) { + packageJson.version = latestVersion; + + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 4)); + process.exit(0); + } else { + console.error("Unable to identify latest devspace version"); + process.exit(1); + } + }); + return; } let version = packageJson.version; let platform = PLATFORM_MAPPING[process.platform]; let arch = ARCH_MAPPING[process.arch]; let binaryName = "devspace"; -let downloadPath = downloadPathTemplate.replace("{{version}}", version).replace("{{platform}}", platform).replace("{{arch}}", arch); +let downloadPath = downloadPathTemplate + .replace("{{version}}", version) + .replace("{{platform}}", platform) + .replace("{{arch}}", arch); if (platform == "windows") { - downloadPath += ".exe"; - binaryName += ".exe"; + downloadPath += ".exe"; + binaryName += ".exe"; } exec("npm bin", function(err, stdout, stderr) { - let dir = null; - if (err || stderr || !stdout || stdout.length === 0) { - let env = process.env; - if (env && env.npm_config_prefix) { - dir = path.join(env.npm_config_prefix, "bin"); - } - } else { - dir = stdout.trim(); + let dir = null; + if (err || stderr || !stdout || stdout.length === 0) { + let env = process.env; + if (env && env.npm_config_prefix) { + dir = path.join(env.npm_config_prefix, "bin"); } + } else { + dir = stdout.trim(); + } - if (dir == null) callback("Error finding binary installation directory"); + if (dir == null) callback("Error finding binary installation directory"); - let binaryPath = path.join(dir, binaryName); + let binaryPath = path.join(dir, binaryName); - if (process.argv.length > 3) { - binaryPath = process.argv[3]; - } + if (process.argv.length > 3) { + binaryPath = process.argv[3]; + } - if (platform != "windows" && action == "install") { - process.exit(0); - } + if (platform != "windows" && action == "install") { + process.exit(0); + } + try { + fs.unlinkSync(binaryPath); + } catch (e) {} + + if (platform == "windows") { try { - fs.unlinkSync(binaryPath); - } catch(e) {} - - if (platform == "windows") { - try { - fs.unlinkSync(binaryPath.replace(/\.exe$/i, "")); - } catch(e) {} - - try { - fs.unlinkSync(binaryPath.replace(/\.exe$/i, ".cmd")); - } catch(e) {} - } + fs.unlinkSync(binaryPath.replace(/\.exe$/i, "")); + } catch (e) {} - if (action == "install" || action == "force-install") { - console.log("Download DevSpace CLI release: " + downloadPath + "\n"); - - const spinner = new Spinner('%s Downloading DevSpace CLI... (this may take a minute)'); - spinner.setSpinnerString('|/-\\'); - spinner.start(); - - const showRootError = function() { - spinner.stop(true); - console.error("\n############################################"); - console.error("Failed to download DevSpace CLI due to permission issues!\n"); - console.error("There are two options to fix this:"); - console.error("1. Do not run 'npm install' as root (recommended)"); - console.error("2. Run this command: npm install --unsafe-perm=true -g devspace"); - console.error(" You may need to run this command using sudo."); - console.error("############################################\n"); - process.exit(1); - }; - let writeStream = fs.createWriteStream(binaryPath) - .on('error', function(err) { - showRootError(); + try { + fs.unlinkSync(binaryPath.replace(/\.exe$/i, ".cmd")); + } catch (e) {} + } + + if (action == "install" || action == "force-install") { + console.log("Download DevSpace CLI release: " + downloadPath + "\n"); + + const spinner = new Spinner( + "%s Downloading DevSpace CLI... (this may take a minute)" + ); + spinner.setSpinnerString("|/-\\"); + spinner.start(); + + const showRootError = function() { + spinner.stop(true); + console.error("\n############################################"); + console.error( + "Failed to download DevSpace CLI due to permission issues!\n" + ); + console.error("There are two options to fix this:"); + console.error("1. Do not run 'npm install' as root (recommended)"); + console.error( + "2. Run this command: npm install --unsafe-perm=true -g devspace" + ); + console.error(" You may need to run this command using sudo."); + console.error("############################################\n"); + process.exit(1); + }; + let writeStream = fs + .createWriteStream(binaryPath) + .on("error", function(err) { + showRootError(); + }); + + const downloadRelease = function(downloadPath) { + request({ uri: downloadPath, headers: requestHeaders, encoding: null }) + .on("error", function() { + spinner.stop(true); + console.error("Error requesting URL: " + downloadPath); + process.exit(1); + }) + .on("response", function(res) { + if (res.statusCode == 400) { + console.error("Release version " + version + " not found."); + + getLatestVersion(function(latestVersion) { + if (latestVersion != version) { + console.log( + "Downloading latest stable release instead. Latest version is: " + + latestVersion + ); + + let latestReleaseDownloadPath = downloadPathTemplate + .replace("{{version}}", latestVersion) + .replace("{{platform}}", platform) + .replace("{{arch}}", arch); + downloadRelease(latestReleaseDownloadPath); + } }); + return; + } else if (res.statusCode != 200) { + console.error( + "Error requesting URL " + + downloadPath + + " (Status Code: " + + res.statusCode + + ")" + ); + console.error(err); + process.exit(1); + } else { + try { + res.pipe(writeStream); + } catch (e) { + showRootError(); + } + } + }) + .on("end", function() { + writeStream.end(); + spinner.stop(true); + + try { + fs.chmodSync(binaryPath, 0755); + } catch (e) { + showRootError(); + } + process.exit(0); + }); + }; - request({uri: downloadPath, headers: requestHeaders, encoding: null}) - .on('error', function() { - spinner.stop(true); - console.error("Error requesting URL: " + downloadPath); - process.exit(1); - }) - .on('response', function(res) { - try { - res.pipe(writeStream); - } catch(e) { - showRootError(); - } - }) - .on('end', function() { - writeStream.end(); - spinner.stop(true); - - try { - fs.chmodSync(binaryPath, 0755); - } catch(e) { - showRootError(); - } - process.exit(0); - }); - } + downloadRelease(downloadPath); + } }); diff --git a/dist/npm/package.json b/dist/npm/package.json index 2c20252493..a3acddd579 100644 --- a/dist/npm/package.json +++ b/dist/npm/package.json @@ -1,25 +1,25 @@ { - "name": "devspace", - "version": "3.3.0", - "description": "DevSpace CLI - the swiss-army knife for Kubernetes. DevSpace CLI accelerates developing, deploying and debugging applications with Docker and Kubernetes.", - "bin": { - "devspace": "bin/devspace" - }, - "main": "index.js", - "repository": { - "type": "git", - "url": "https://github.com/devspace-cloud/devspace" - }, - "homepage": "https://github.com/devspace-cloud/devspace", - "scripts": { - "postinstall": "node index.js install", - "preuninstall": "node index.js uninstall", - "prepare": "node index.js update-version", - "test": "node index.js noop" - }, - "dependencies": { - "cli-spinner": "^0.2.10", - "request": "^2.81.0" - }, - "license": "Apache-2.0" + "name": "devspace", + "version": "3.5.13", + "description": "DevSpace CLI - the swiss-army knife for Kubernetes. DevSpace CLI accelerates developing, deploying and debugging applications with Docker and Kubernetes.", + "bin": { + "devspace": "bin/devspace" + }, + "main": "index.js", + "repository": { + "type": "git", + "url": "https://github.com/devspace-cloud/devspace" + }, + "homepage": "https://github.com/devspace-cloud/devspace", + "scripts": { + "postinstall": "node index.js install", + "preuninstall": "node index.js uninstall", + "prepare": "node index.js update-version", + "test": "node index.js noop" + }, + "dependencies": { + "cli-spinner": "^0.2.10", + "request": "^2.81.0" + }, + "license": "Apache-2.0" }