npmci/dist/npmci.bash.js

73 lines
5.5 KiB
JavaScript
Raw Normal View History

2016-05-30 01:40:07 +00:00
"use strict";
2016-07-18 14:56:53 +00:00
const plugins = require("./npmci.plugins");
2016-11-24 22:21:40 +00:00
let nvmSourceString = '';
exports.nvmAvailable = false;
let checkNvm = () => {
2016-08-02 21:11:42 +00:00
if (plugins.shelljs.exec(`bash -c "source /usr/local/nvm/nvm.sh"`, { silent: true }).code === 0) {
nvmSourceString = `source /usr/local/nvm/nvm.sh && `;
exports.nvmAvailable = true;
}
2016-08-02 21:11:42 +00:00
else if (plugins.shelljs.exec(`bash -c "source ~/.nvm/nvm.sh"`, { silent: true }).code === 0) {
nvmSourceString = `source ~/.nvm/nvm.sh && `;
exports.nvmAvailable = true;
}
;
};
checkNvm();
2016-12-10 21:43:14 +00:00
/**
* bash() allows using bash with nvm in path
2016-12-14 22:10:54 +00:00
* @param commandArg - The command to execute
* @param retryArg - The retryArg: 0 to any positive number will retry, -1 will always succeed, -2 will return undefined
2016-12-10 21:43:14 +00:00
*/
2016-07-18 14:56:53 +00:00
exports.bash = (commandArg, retryArg = 2, bareArg = false) => {
let exitCode;
let stdOut;
let execResult;
2016-12-14 22:10:54 +00:00
let failOnError = true;
if (retryArg === -1) {
failOnError = false;
retryArg = 0;
}
2016-06-05 12:55:08 +00:00
if (!process.env.NPMTS_TEST) {
2016-07-18 14:56:53 +00:00
for (let i = 0; i <= retryArg; i++) {
2016-06-07 01:59:47 +00:00
if (!bareArg) {
execResult = plugins.shelljs.exec(`bash -c "${nvmSourceString} ${commandArg}"`);
}
2016-06-05 19:11:30 +00:00
else {
2016-06-07 03:37:02 +00:00
execResult = plugins.shelljs.exec(commandArg);
2016-06-05 19:11:30 +00:00
}
2016-06-07 01:59:47 +00:00
exitCode = execResult.code;
stdOut = execResult.stdout;
2016-12-10 21:43:14 +00:00
// determine how bash reacts to error and success
2016-11-24 22:21:40 +00:00
if (exitCode !== 0 && i === retryArg) {
2016-12-14 22:10:54 +00:00
if (failOnError) {
process.exit(1);
}
}
2016-12-14 22:10:54 +00:00
else if (exitCode === 0) {
2016-12-10 21:43:14 +00:00
i = retryArg + 1; // retry +1 breaks for loop, if everything works out ok retrials are not wanted
}
2016-06-05 19:11:30 +00:00
else {
2016-11-24 22:21:40 +00:00
plugins.beautylog.warn('Something went wrong! Exit Code: ' + exitCode.toString());
plugins.beautylog.info('Retry ' + (i + 1).toString() + ' of ' + retryArg.toString());
2016-06-05 19:11:30 +00:00
}
2016-06-05 12:55:08 +00:00
}
}
else {
2016-11-24 22:21:40 +00:00
plugins.beautylog.log('ShellExec would be: ' + commandArg);
2016-05-30 01:54:14 +00:00
}
2016-06-07 01:59:47 +00:00
return stdOut;
};
2016-12-10 21:43:14 +00:00
/**
* bashBare allows usage of bash without sourcing any files like nvm
*/
2016-07-18 14:56:53 +00:00
exports.bashBare = (commandArg, retryArg = 2) => {
2016-06-07 01:59:47 +00:00
return exports.bash(commandArg, retryArg, true);
2016-05-30 01:40:07 +00:00
};
2016-12-10 21:43:14 +00:00
/**
* bashNoError allows executing stuff without throwing an error
*/
exports.bashNoError = (commandArg) => {
return exports.bash(commandArg, -1);
};
2016-12-14 22:10:54 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtY2kuYmFzaC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWNpLmJhc2gudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDJDQUEwQztBQUUxQyxJQUFJLGVBQWUsR0FBVyxFQUFFLENBQUE7QUFDckIsUUFBQSxZQUFZLEdBQVksS0FBSyxDQUFBO0FBQ3hDLElBQUksUUFBUSxHQUFHO0lBQ1gsRUFBRSxDQUFDLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsd0NBQXdDLEVBQUMsRUFBQyxNQUFNLEVBQUUsSUFBSSxFQUFDLENBQUMsQ0FB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