npmci/dist/npmci.bash.js
2017-03-11 14:07:36 +01:00

110 lines
8.4 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./npmci.plugins");
const paths = require("./npmci.paths");
const smartq = require("smartq");
/**
* wether nvm is available or not
*/
exports.nvmAvailable = smartq.defer();
exports.yarnAvailable = smartq.defer();
/**
* the smartshell instance for npmci
*/
let npmciSmartshell = new plugins.smartshell.Smartshell({
executor: 'bash',
sourceFilePaths: []
});
/**
* check for tools.
*/
let checkToolsAvailable = () => __awaiter(this, void 0, void 0, function* () {
// check for nvm
if ((yield plugins.smartshell.execSilent(`bash -c "source /usr/local/nvm/nvm.sh"`)).exitCode === 0) {
npmciSmartshell.addSourceFiles([`/usr/local/nvm/nvm.sh`]);
exports.nvmAvailable.resolve(true);
}
else if ((yield plugins.smartshell.execSilent(`bash -c "source ~/.nvm/nvm.sh"`)).exitCode === 0) {
npmciSmartshell.addSourceFiles([`~/.nvm/nvm.sh`]);
exports.nvmAvailable.resolve(true);
}
else {
exports.nvmAvailable.resolve(false);
}
;
// check for yarn
yield plugins.smartshell.which('yarn').then(() => {
plugins.smartshell.exec(`yarn config set cache-folder ${plugins.path.join(paths.cwd, '.yarn')}`);
exports.yarnAvailable.resolve(true);
}, () => { exports.yarnAvailable.resolve(false); });
});
checkToolsAvailable();
/**
* bash() allows using bash with nvm in path
* @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
*/
exports.bash = (commandArg, retryArg = 2, bareArg = false) => __awaiter(this, void 0, void 0, function* () {
yield exports.nvmAvailable.promise; // make sure nvm check has run
let execResult;
// determine if we fail
let failOnError = true;
if (retryArg === -1) {
failOnError = false;
retryArg = 0;
}
if (!process.env.NPMTS_TEST) {
for (let i = 0; i <= retryArg; i++) {
if (!bareArg) {
execResult = yield npmciSmartshell.exec(commandArg);
}
else {
execResult = yield plugins.smartshell.exec(commandArg);
}
// determine how bash reacts to error and success
if (execResult.exitCode !== 0 && i === retryArg) {
if (failOnError) {
plugins.beautylog.error('something went wrong and retries are exhausted');
process.exit(1);
}
}
else if (execResult.exitCode === 0) {
i = retryArg + 1; // retry +1 breaks for loop, if everything works out ok retrials are not wanted
}
else {
plugins.beautylog.warn('Something went wrong! Exit Code: ' + execResult.exitCode.toString());
plugins.beautylog.info('Retry ' + (i + 1).toString() + ' of ' + retryArg.toString());
}
}
}
else {
plugins.beautylog.log('ShellExec would be: ' + commandArg);
yield plugins.smartdelay.delayFor(100);
execResult = {
exitCode: 0,
stdout: 'testOutput'
};
}
return execResult.stdout;
});
/**
* bashBare allows usage of bash without sourcing any files like nvm
*/
exports.bashBare = (commandArg, retryArg = 2) => __awaiter(this, void 0, void 0, function* () {
return yield exports.bash(commandArg, retryArg, true);
});
/**
* bashNoError allows executing stuff without throwing an error
*/
exports.bashNoError = (commandArg) => __awaiter(this, void 0, void 0, function* () {
return yield exports.bash(commandArg, -1);
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtY2kuYmFzaC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWNpLmJhc2gudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLDJDQUEwQztBQUMxQyx1Q0FBc0M7QUFFdEMsaUNBQWdDO0FBRWhDOztHQUVHO0FBQ1EsUUFBQSxZQUFZLEdBQUcsTUFBTSxDQUFDLEtBQUssRUFBVyxDQUFBO0FBQ3RDLFFB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