Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
318 changes: 195 additions & 123 deletions dist/npm/index.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
Loading